Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initrd: Opt-in bare bones systemd-based initrd #164943

Merged
merged 20 commits into from Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
52c98fc
nixos: systemd: Split unit types into separate module
ElvishJerricco Mar 19, 2022
d193ef8
make-initrd-ng: init
ElvishJerricco Nov 21, 2021
2511374
nixos: systemd-lib: Make generateUnits general with default args
ElvishJerricco Mar 20, 2022
2d4ebf1
initrd: Optional systemd-based initrd
ElvishJerricco Mar 20, 2022
be10e86
systemd-initrd: Partially fix qemu-vm
ElvishJerricco Mar 20, 2022
1abf154
systemd-initrd: Add PATH to everything
ElvishJerricco Mar 20, 2022
213de9b
systemd-initrd: autoFormat and autoResize in initrd
ElvishJerricco Mar 20, 2022
3365666
systemd-initrd: Basic test case
ElvishJerricco Mar 20, 2022
9828446
systemd-initrd: Fix Environment= and PATH
ElvishJerricco Mar 21, 2022
2431347
systemd-initrd: Test autoResize
ElvishJerricco Mar 21, 2022
5bfe213
Clarify suppressed units description
ElvishJerricco Mar 22, 2022
76d05df
fakeNss: move to toplevel
flokli Mar 24, 2022
74bae06
systemd-initrd: use pkgs.fakeNss, document why we need libnss_files.so
flokli Mar 24, 2022
e3083de
systemd-initrd, systemd-lib: drop initrdServiceToUnit
flokli Mar 24, 2022
fc91cdb
nixos/lib/systemd-lib.nix: move comment back down to packages
flokli Mar 24, 2022
1e5261f
nixos/systemd-lib: Use module composition
dasJ Apr 1, 2022
b7c62b8
nixos/systemd-initrd: Remove unit options that don't work
dasJ Apr 1, 2022
c465c8d
nixos/systemd-initrd: Make emergency access more flexible
dasJ Apr 1, 2022
5653209
nixos/systemd-initrd: Redo object specifications
dasJ Apr 1, 2022
7ebb4eb
nixos/systemd-stage-1: Append (Initrd) to /etc/initrd-release
dasJ Apr 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 28 additions & 18 deletions nixos/lib/systemd-lib.nix
Expand Up @@ -120,18 +120,23 @@ in rec {
(if isList value then value else [value]))
as));

generateUnits = generateUnits' true;

generateUnits' = allowCollisions: type: units: upstreamUnits: upstreamWants:
pkgs.runCommand "${type}-units"
generateUnits = { allowCollisions ? true, type, units, upstreamUnits, upstreamWants, packages ? cfg.packages, package ? cfg.package }:
let
typeDir = ({
system = "system";
initrd = "system";
user = "user";
nspawn = "nspawn";
}).${type};
in pkgs.runCommand "${type}-units"
{ preferLocalBuild = true;
allowSubstitutes = false;
} ''
mkdir -p $out

# Copy the upstream systemd units we're interested in.
for i in ${toString upstreamUnits}; do
fn=${cfg.package}/example/systemd/${type}/$i
fn=${package}/example/systemd/${typeDir}/$i
if ! [ -e $fn ]; then echo "missing $fn"; false; fi
if [ -L $fn ]; then
target="$(readlink "$fn")"
Expand All @@ -148,7 +153,7 @@ in rec {
# Copy .wants links, but only those that point to units that
# we're interested in.
for i in ${toString upstreamWants}; do
fn=${cfg.package}/example/systemd/${type}/$i
fn=${package}/example/systemd/${typeDir}/$i
if ! [ -e $fn ]; then echo "missing $fn"; false; fi
x=$out/$(basename $fn)
mkdir $x
Expand All @@ -160,14 +165,14 @@ in rec {
done

# Symlink all units provided listed in systemd.packages.
packages="${toString cfg.packages}"
packages="${toString packages}"

# Filter duplicate directories
declare -A unique_packages
for k in $packages ; do unique_packages[$k]=1 ; done

for i in ''${!unique_packages[@]}; do
for fn in $i/etc/systemd/${type}/* $i/lib/systemd/${type}/*; do
for fn in $i/etc/systemd/${typeDir}/* $i/lib/systemd/${typeDir}/*; do
if ! [[ "$fn" =~ .wants$ ]]; then
if [[ -d "$fn" ]]; then
targetDir="$out/$(basename "$fn")"
Expand Down Expand Up @@ -286,17 +291,11 @@ in rec {
};
};

serviceConfig = { name, config, ... }: {
mkServiceConfig = path: { name, config, ... }: {
dasJ marked this conversation as resolved.
Show resolved Hide resolved
config = mkMerge
[ { # Default path for systemd services. Should be quite minimal.
path = mkAfter
[ pkgs.coreutils
pkgs.findutils
pkgs.gnugrep
pkgs.gnused
systemd
];
environment.PATH = "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}";
[ {
path = mkAfter path;
dasJ marked this conversation as resolved.
Show resolved Hide resolved
environment.PATH = mkIf (config.path != []) "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}";
}
(mkIf (config.preStart != "")
{ serviceConfig.ExecStartPre =
Expand Down Expand Up @@ -325,6 +324,17 @@ in rec {
];
};

# Default path for systemd services. Should be quite minimal.
serviceConfig = mkServiceConfig [
pkgs.coreutils
pkgs.findutils
pkgs.gnugrep
pkgs.gnused
systemd
];
dasJ marked this conversation as resolved.
Show resolved Hide resolved

initrdServiceConfig = mkServiceConfig [];

mountConfig = { config, ... }: {
config = {
mountConfig =
Expand Down
30 changes: 30 additions & 0 deletions nixos/lib/systemd-types.nix
@@ -0,0 +1,30 @@
{ lib, systemdUtils }:

with systemdUtils.lib;
with systemdUtils.unitOptions;
with lib;

rec {
units = with types;
attrsOf (submodule ({ name, config, ... }: {
options = concreteUnitOptions;
config = { unit = mkDefault (systemdUtils.lib.makeUnit name config); };
}));

services = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig serviceConfig ]);
initrdServices = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig initrdServiceConfig ]);
dasJ marked this conversation as resolved.
Show resolved Hide resolved

targets = with types; attrsOf (submodule [ { options = targetOptions; } unitConfig ]);

sockets = with types; attrsOf (submodule [ { options = socketOptions; } unitConfig ]);

timers = with types; attrsOf (submodule [ { options = timerOptions; } unitConfig ]);

paths = with types; attrsOf (submodule [ { options = pathOptions; } unitConfig ]);

slices = with types; attrsOf (submodule [ { options = sliceOptions; } unitConfig ]);

mounts = with types; listOf (submodule [ { options = mountOptions; } unitConfig mountConfig ]);

automounts = with types; listOf (submodule [ { options = automountOptions; } unitConfig automountConfig ]);
}
1 change: 1 addition & 0 deletions nixos/lib/utils.nix
Expand Up @@ -197,5 +197,6 @@ rec {
systemdUtils = {
lib = import ./systemd-lib.nix { inherit lib config pkgs; };
unitOptions = import ./systemd-unit-options.nix { inherit lib systemdUtils; };
types = import ./systemd-types.nix { inherit lib systemdUtils; };
Comment on lines 198 to +200
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lib = import ./systemd-lib.nix { inherit lib config pkgs; };
unitOptions = import ./systemd-unit-options.nix { inherit lib systemdUtils; };
types = import ./systemd-types.nix { inherit lib systemdUtils; };
lib = import ./systemd/lib.nix { inherit lib config pkgs; };
unitOptions = import ./systemd/unit-options.nix { inherit lib systemdUtils; };
types = import ./systemd/types.nix { inherit lib systemdUtils; };

Maybe? I am not sure. Just an idea.

};
}
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Expand Up @@ -1172,6 +1172,7 @@
./system/boot/systemd/nspawn.nix
./system/boot/systemd/tmpfiles.nix
./system/boot/systemd/user.nix
./system/boot/systemd/initrd.nix
./system/boot/timesyncd.nix
./system/boot/tmp.nix
./system/etc/etc-activation.nix
Expand Down
8 changes: 6 additions & 2 deletions nixos/modules/system/boot/stage-1.nix
Expand Up @@ -706,8 +706,12 @@ in
}
];

system.build =
{ inherit bootStage1 initialRamdisk initialRamdiskSecretAppender extraUtils; };
system.build = mkMerge [
{ inherit bootStage1 initialRamdiskSecretAppender extraUtils; }

# generated in nixos/modules/system/boot/systemd/initrd.nix
(mkIf (!config.boot.initrd.systemd.enable) { inherit initialRamdisk; })
ElvishJerricco marked this conversation as resolved.
Show resolved Hide resolved
];

system.requiredKernelConfig = with config.lib.kernelConfig; [
(isYes "TMPFS")
Expand Down
43 changes: 18 additions & 25 deletions nixos/modules/system/boot/systemd.nix
Expand Up @@ -11,14 +11,7 @@ let
systemd = cfg.package;

inherit (systemdUtils.lib)
makeUnit
generateUnits
makeJobScript
unitConfig
serviceConfig
mountConfig
automountConfig
commonUnitText
targetToUnit
serviceToUnit
socketToUnit
Expand Down Expand Up @@ -185,13 +178,7 @@ in
systemd.units = mkOption {
description = "Definition of systemd units.";
default = {};
type = with types; attrsOf (submodule (
{ name, config, ... }:
{ options = concreteUnitOptions;
config = {
unit = mkDefault (makeUnit name config);
};
}));
type = systemdUtils.types.units;
};

systemd.packages = mkOption {
Expand All @@ -203,37 +190,37 @@ in

systemd.targets = mkOption {
default = {};
type = with types; attrsOf (submodule [ { options = targetOptions; } unitConfig] );
type = systemdUtils.types.targets;
description = "Definition of systemd target units.";
};

systemd.services = mkOption {
default = {};
type = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig serviceConfig ]);
type = systemdUtils.types.services;
description = "Definition of systemd service units.";
};

systemd.sockets = mkOption {
default = {};
type = with types; attrsOf (submodule [ { options = socketOptions; } unitConfig ]);
type = systemdUtils.types.sockets;
description = "Definition of systemd socket units.";
};

systemd.timers = mkOption {
default = {};
type = with types; attrsOf (submodule [ { options = timerOptions; } unitConfig ]);
type = systemdUtils.types.timers;
description = "Definition of systemd timer units.";
};

systemd.paths = mkOption {
default = {};
type = with types; attrsOf (submodule [ { options = pathOptions; } unitConfig ]);
type = systemdUtils.types.paths;
description = "Definition of systemd path units.";
};

systemd.mounts = mkOption {
default = [];
type = with types; listOf (submodule [ { options = mountOptions; } unitConfig mountConfig ]);
type = systemdUtils.types.mounts;
description = ''
Definition of systemd mount units.
This is a list instead of an attrSet, because systemd mandates the names to be derived from
Expand All @@ -243,7 +230,7 @@ in

systemd.automounts = mkOption {
default = [];
type = with types; listOf (submodule [ { options = automountOptions; } unitConfig automountConfig ]);
type = systemdUtils.types.automounts;
description = ''
Definition of systemd automount units.
This is a list instead of an attrSet, because systemd mandates the names to be derived from
Expand All @@ -253,7 +240,7 @@ in

systemd.slices = mkOption {
default = {};
type = with types; attrsOf (submodule [ { options = sliceOptions; } unitConfig] );
type = systemdUtils.types.slices;
description = "Definition of slice configurations.";
};

Expand Down Expand Up @@ -352,10 +339,11 @@ in
type = types.listOf types.str;
example = [ "systemd-backlight@.service" ];
description = ''
A list of units to suppress when generating system systemd configuration directory. This has
A list of units to skip when generating system systemd configuration directory. This has
priority over upstream units, <option>systemd.units</option>, and
<option>systemd.additionalUpstreamSystemUnits</option>. The main purpose of this is to
suppress a upstream systemd unit with any modifications made to it by other NixOS modules.
prevent a upstream systemd unit from being added to the initrd with any modifications made to it
by other NixOS modules.
'';
};

Expand Down Expand Up @@ -471,7 +459,12 @@ in
enabledUpstreamSystemUnits = filter (n: ! elem n cfg.suppressedSystemUnits) upstreamSystemUnits;
enabledUnits = filterAttrs (n: v: ! elem n cfg.suppressedSystemUnits) cfg.units;
in ({
"systemd/system".source = generateUnits "system" enabledUnits enabledUpstreamSystemUnits upstreamSystemWants;
"systemd/system".source = generateUnits {
type = "system";
units = enabledUnits;
upstreamUnits = enabledUpstreamSystemUnits;
upstreamWants = upstreamSystemWants;
};

"systemd/system.conf".text = ''
[Manager]
Expand Down