From fb44ecd129c8ee49fb599f82f2ff8041ebd34682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janne=20He=C3=9F?= Date: Tue, 5 Apr 2022 14:45:50 +0100 Subject: [PATCH 1/2] nixos/udev: Add systemd stage 1 support --- nixos/modules/services/hardware/udev.nix | 84 ++++++++++++++++++-- nixos/modules/system/boot/systemd/initrd.nix | 14 ++-- 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/nixos/modules/services/hardware/udev.nix b/nixos/modules/services/hardware/udev.nix index 61448af2d33b..b0cdfd551e49 100644 --- a/nixos/modules/services/hardware/udev.nix +++ b/nixos/modules/services/hardware/udev.nix @@ -30,10 +30,10 @@ let ''; # Perform substitutions in all udev rules files. - udevRules = pkgs.runCommand "udev-rules" + udevRulesFor = { name, udevPackages, udevPath, udev, systemd, binPackages, initrdBin ? null }: pkgs.runCommand name { preferLocalBuild = true; allowSubstitutes = false; - packages = unique (map toString cfg.packages); + packages = unique (map toString udevPackages); } '' mkdir -p $out @@ -61,6 +61,9 @@ let --replace \"/bin/mount \"${pkgs.util-linux}/bin/mount \ --replace /usr/bin/readlink ${pkgs.coreutils}/bin/readlink \ --replace /usr/bin/basename ${pkgs.coreutils}/bin/basename + ${optionalString (initrdBin != null) '' + substituteInPlace $i --replace '/run/current-system/systemd' "${removeSuffix "/bin" initrdBin}" + ''} done echo -n "Checking that all programs called by relative paths in udev rules exist in ${udev}/lib/udev... " @@ -85,8 +88,9 @@ let for i in $import_progs $run_progs; do # if the path refers to /run/current-system/systemd, replace with config.systemd.package if [[ $i == /run/current-system/systemd* ]]; then - i="${config.systemd.package}/''${i#/run/current-system/systemd/}" + i="${systemd}/''${i#/run/current-system/systemd/}" fi + if [[ ! -x $i ]]; then echo "FAIL" echo "$i is called in udev rules but is not executable or does not exist" @@ -103,7 +107,7 @@ let echo "Consider fixing the following udev rules:" echo "$filesToFixup" | while read localFile; do remoteFile="origin unknown" - for i in ${toString cfg.packages}; do + for i in ${toString binPackages}; do for j in "$i"/*/udev/rules.d/*; do [ -e "$out/$(basename "$j")" ] || continue [ "$(basename "$j")" = "$(basename "$localFile")" ] || continue @@ -126,7 +130,7 @@ let ${optionalString (!config.boot.hardwareScan) '' ln -s /dev/null $out/80-drivers.rules ''} - ''; # */ + ''; hwdbBin = pkgs.runCommand "hwdb.bin" { preferLocalBuild = true; @@ -193,6 +197,34 @@ in apply = map getBin; }; + initrdPackages = mkOption { + type = types.listOf types.path; + default = []; + visible = false; + description = '' + This will only be used when systemd is used in stage 1. + + List of packages containing udev rules that will be copied to stage 1. + All files found in + pkg/etc/udev/rules.d and + pkg/lib/udev/rules.d + will be included. + ''; + }; + + initrdBinPackages = mkOption { + type = types.listOf types.path; + default = []; + visible = false; + description = '' + This will only be used when systemd is used in stage 1. + + Packages to search for binaries that are referenced by the udev rules in stage 1. + This list always contains /bin of the initrd. + ''; + apply = map getBin; + }; + path = mkOption { type = types.listOf types.path; default = []; @@ -298,16 +330,54 @@ in boot.kernelParams = mkIf (!config.networking.usePredictableInterfaceNames) [ "net.ifnames=0" ]; - boot.initrd.extraUdevRulesCommands = optionalString (cfg.initrdRules != "") + boot.initrd.extraUdevRulesCommands = optionalString (!config.boot.initrd.systemd.enable && cfg.initrdRules != "") '' cat <<'EOF' > $out/99-local.rules ${cfg.initrdRules} EOF ''; + boot.initrd.systemd.additionalUpstreamUnits = [ + # TODO: "initrd-udevadm-cleanup-db.service" is commented out because of https://github.com/systemd/systemd/issues/12953 + "systemd-udevd-control.socket" + "systemd-udevd-kernel.socket" + "systemd-udevd.service" + "systemd-udev-settle.service" + "systemd-udev-trigger.service" + ]; + boot.initrd.systemd.storePaths = [ + "${config.boot.initrd.systemd.package}/lib/systemd/systemd-udevd" + "${config.boot.initrd.systemd.package}/lib/udev" + ] ++ map toString cfg.initrdBinPackages; + + # Generate the udev rules for the initrd + boot.initrd.systemd.contents = { + "/etc/udev/rules.d".source = udevRulesFor { + name = "initrd-udev-rules"; + initrdBin = config.boot.initrd.systemd.contents."/bin".source; + udevPackages = cfg.initrdPackages; + udevPath = config.boot.initrd.systemd.contents."/bin".source; + udev = config.boot.initrd.systemd.package; + systemd = config.boot.initrd.systemd.package; + binPackages = cfg.initrdBinPackages ++ [ config.boot.initrd.systemd.contents."/bin".source ]; + }; + }; + # Insert custom rules + services.udev.initrdPackages = mkIf (cfg.initrdRules != "") (pkgs.writeTextFile { + name = "initrd-udev-rules"; + destination = "/etc/udev/rules.d/99-local.rules"; + text = cfg.initrdRules; + }); + environment.etc = { - "udev/rules.d".source = udevRules; + "udev/rules.d".source = udevRulesFor { + name = "udev-rules"; + udevPackages = cfg.packages; + systemd = config.systemd.package; + binPackages = cfg.packages; + inherit udevPath udev; + }; "udev/hwdb.bin".source = hwdbBin; }; diff --git a/nixos/modules/system/boot/systemd/initrd.nix b/nixos/modules/system/boot/systemd/initrd.nix index c383486bb0bc..6f884d41d63c 100644 --- a/nixos/modules/system/boot/systemd/initrd.nix +++ b/nixos/modules/system/boot/systemd/initrd.nix @@ -34,7 +34,6 @@ let "initrd-switch-root.service" "initrd-switch-root.target" "initrd.target" - "initrd-udevadm-cleanup-db.service" "kexec.target" "kmod-static-nodes.service" "local-fs-pre.target" @@ -71,12 +70,6 @@ let "systemd-sysctl.service" "systemd-tmpfiles-setup-dev.service" "systemd-tmpfiles-setup.service" - "systemd-udevd-control.socket" - "systemd-udevd-kernel.socket" - "systemd-udevd.service" - "systemd-udev-settle.service" - "systemd-udev-trigger.service" - "systemd-vconsole-setup.service" "timers.target" "umount.target" @@ -384,6 +377,11 @@ in { "/etc/sysctl.d/nixos.conf".text = "kernel.modprobe = /sbin/modprobe"; "/etc/modprobe.d/systemd.conf".source = "${cfg.package}/lib/modprobe.d/systemd.conf"; + "/etc/modprobe.d/ubuntu.conf".source = pkgs.runCommand "initrd-kmod-blacklist-ubuntu" { } '' + ${pkgs.buildPackages.perl}/bin/perl -0pe 's/## file: iwlwifi.conf(.+?)##/##/s;' $src > $out + ''; + "/etc/modprobe.d/debian.conf".source = pkgs.kmod-debian-aliases; + }; storePaths = [ @@ -397,12 +395,10 @@ in { "${cfg.package}/lib/systemd/systemd-remount-fs" "${cfg.package}/lib/systemd/systemd-sulogin-shell" "${cfg.package}/lib/systemd/systemd-sysctl" - "${cfg.package}/lib/systemd/systemd-udevd" "${cfg.package}/lib/systemd/systemd-vconsole-setup" # additional systemd directories "${cfg.package}/lib/systemd/system-generators" - "${cfg.package}/lib/udev" # utilities needed by systemd "${cfg.package.util-linux}/bin/mount" From 7024b4e5e369f3d7d47c01dd21c7390657fee4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janne=20He=C3=9F?= Date: Sun, 10 Apr 2022 18:49:49 +0100 Subject: [PATCH 2/2] nixos/udev: Put all initrd options into a namespace --- nixos/modules/services/hardware/udev.nix | 106 ++++++++++++----------- nixos/tests/networking.nix | 2 +- 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/nixos/modules/services/hardware/udev.nix b/nixos/modules/services/hardware/udev.nix index b0cdfd551e49..f5b77ce24da2 100644 --- a/nixos/modules/services/hardware/udev.nix +++ b/nixos/modules/services/hardware/udev.nix @@ -197,34 +197,6 @@ in apply = map getBin; }; - initrdPackages = mkOption { - type = types.listOf types.path; - default = []; - visible = false; - description = '' - This will only be used when systemd is used in stage 1. - - List of packages containing udev rules that will be copied to stage 1. - All files found in - pkg/etc/udev/rules.d and - pkg/lib/udev/rules.d - will be included. - ''; - }; - - initrdBinPackages = mkOption { - type = types.listOf types.path; - default = []; - visible = false; - description = '' - This will only be used when systemd is used in stage 1. - - Packages to search for binaries that are referenced by the udev rules in stage 1. - This list always contains /bin of the initrd. - ''; - apply = map getBin; - }; - path = mkOption { type = types.listOf types.path; default = []; @@ -234,20 +206,6 @@ in ''; }; - initrdRules = mkOption { - default = ""; - example = '' - SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:1D:60:B9:6D:4F", KERNEL=="eth*", NAME="my_fast_network_card" - ''; - type = types.lines; - description = '' - udev rules to include in the initrd - only. They'll be written into file - 99-local.rules. Thus they are read and applied - after the essential initrd rules. - ''; - }; - extraRules = mkOption { default = ""; example = '' @@ -315,6 +273,52 @@ in ''; }; + boot.initrd.services.udev = { + + packages = mkOption { + type = types.listOf types.path; + default = []; + visible = false; + description = '' + This will only be used when systemd is used in stage 1. + + List of packages containing udev rules that will be copied to stage 1. + All files found in + pkg/etc/udev/rules.d and + pkg/lib/udev/rules.d + will be included. + ''; + }; + + binPackages = mkOption { + type = types.listOf types.path; + default = []; + visible = false; + description = '' + This will only be used when systemd is used in stage 1. + + Packages to search for binaries that are referenced by the udev rules in stage 1. + This list always contains /bin of the initrd. + ''; + apply = map getBin; + }; + + rules = mkOption { + default = ""; + example = '' + SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:1D:60:B9:6D:4F", KERNEL=="eth*", NAME="my_fast_network_card" + ''; + type = types.lines; + description = '' + udev rules to include in the initrd + only. They'll be written into file + 99-local.rules. Thus they are read and applied + after the essential initrd rules. + ''; + }; + + }; + }; @@ -330,10 +334,10 @@ in boot.kernelParams = mkIf (!config.networking.usePredictableInterfaceNames) [ "net.ifnames=0" ]; - boot.initrd.extraUdevRulesCommands = optionalString (!config.boot.initrd.systemd.enable && cfg.initrdRules != "") + boot.initrd.extraUdevRulesCommands = optionalString (!config.boot.initrd.systemd.enable && config.boot.initrd.services.udev.rules != "") '' cat <<'EOF' > $out/99-local.rules - ${cfg.initrdRules} + ${config.boot.initrd.services.udev.rules} EOF ''; @@ -348,25 +352,25 @@ in boot.initrd.systemd.storePaths = [ "${config.boot.initrd.systemd.package}/lib/systemd/systemd-udevd" "${config.boot.initrd.systemd.package}/lib/udev" - ] ++ map toString cfg.initrdBinPackages; + ] ++ map (x: "${x}/bin") config.boot.initrd.services.udev.binPackages; # Generate the udev rules for the initrd boot.initrd.systemd.contents = { "/etc/udev/rules.d".source = udevRulesFor { name = "initrd-udev-rules"; initrdBin = config.boot.initrd.systemd.contents."/bin".source; - udevPackages = cfg.initrdPackages; + udevPackages = config.boot.initrd.services.udev.packages; udevPath = config.boot.initrd.systemd.contents."/bin".source; udev = config.boot.initrd.systemd.package; systemd = config.boot.initrd.systemd.package; - binPackages = cfg.initrdBinPackages ++ [ config.boot.initrd.systemd.contents."/bin".source ]; + binPackages = config.boot.initrd.services.udev.binPackages ++ [ config.boot.initrd.systemd.contents."/bin".source ]; }; }; # Insert custom rules - services.udev.initrdPackages = mkIf (cfg.initrdRules != "") (pkgs.writeTextFile { + boot.initrd.services.udev.packages = mkIf (config.boot.initrd.services.udev.rules != "") (pkgs.writeTextFile { name = "initrd-udev-rules"; destination = "/etc/udev/rules.d/99-local.rules"; - text = cfg.initrdRules; + text = config.boot.initrd.services.udev.rules; }); environment.etc = @@ -408,4 +412,8 @@ in }; }; + + imports = [ + (mkRenamedOptionModule [ "services" "udev" "initrdRules" ] [ "boot" "initrd" "services" "udev" "rules" ]) + ]; } diff --git a/nixos/tests/networking.nix b/nixos/tests/networking.nix index bd517093eb3d..a1150097a091 100644 --- a/nixos/tests/networking.nix +++ b/nixos/tests/networking.nix @@ -878,7 +878,7 @@ let linkConfig.Name = "custom_name"; }; } - else { services.udev.initrdRules = '' + else { boot.initrd.services.udev.rules = '' SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:12:01:01", KERNEL=="eth*", NAME="custom_name" ''; });