diff --git a/nixos/modules/system/boot/initrd-network.nix b/nixos/modules/system/boot/initrd-network.nix index 384ae909b7011..9fe870742b95a 100644 --- a/nixos/modules/system/boot/initrd-network.nix +++ b/nixos/modules/system/boot/initrd-network.nix @@ -76,6 +76,11 @@ in boot.initrd.kernelModules = [ "af_packet" ]; + boot.initrd.extraUdevRulesCommands = mkIf config.networking.usePredictableInterfaceNames '' + cp -v ${pkgs.systemd}/lib/udev/rules.d/75-net-description.rules $out/ + cp -v ${pkgs.path}/nixos/modules/services/hardware/80-net-setup-link.rules $out/ + ''; + boot.initrd.extraUtilsCommands = '' copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig ''; @@ -103,8 +108,12 @@ in done # Acquire a DHCP lease. + iface="${optionalString config.networking.usePredictableInterfaceNames + "--interface $(cd /sys/class/net; printf en*)" + }" echo "acquiring IP address via DHCP..." - udhcpc --quit --now --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1 + # The interface can be overriden in `udhcpcArgs` + udhcpc --quit --now $iface --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1 fi '' diff --git a/nixos/release-combined.nix b/nixos/release-combined.nix index 66b253c230f1d..becdc2d5cac43 100644 --- a/nixos/release-combined.nix +++ b/nixos/release-combined.nix @@ -121,6 +121,8 @@ in rec { (all nixos.tests.predictable-interface-names.unpredictable) (all nixos.tests.predictable-interface-names.predictableNetworkd) (all nixos.tests.predictable-interface-names.unpredictableNetworkd) + (all nixos.tests.predictable-interface-names.predictableInitrdNetwork) + (all nixos.tests.predictable-interface-names.unpredictableInitrdNetwork) (all nixos.tests.printing) (all nixos.tests.proxy) (all nixos.tests.sddm.default) diff --git a/nixos/tests/predictable-interface-names.nix b/nixos/tests/predictable-interface-names.nix index 0d73436c1c3f5..3038119003779 100644 --- a/nixos/tests/predictable-interface-names.nix +++ b/nixos/tests/predictable-interface-names.nix @@ -2,17 +2,29 @@ let inherit (import ../lib/testing.nix { inherit system; }) makeTest pkgs; -in pkgs.lib.listToAttrs (pkgs.lib.crossLists (predictable: withNetworkd: { - name = pkgs.lib.optionalString (!predictable) "un" + "predictable" - + pkgs.lib.optionalString withNetworkd "Networkd"; + inherit (pkgs) lib; +in lib.listToAttrs (map ({ predictable, withNetworkd ? false, withInitrdNetwork ? false }: rec { + name = lib.optionalString (!predictable) "un" + "predictable" + + lib.optionalString withNetworkd "Networkd" + + lib.optionalString withInitrdNetwork "InitrdNetwork"; + value = makeTest { - name = "${if predictable then "" else "un"}predictableInterfaceNames${if withNetworkd then "-with-networkd" else ""}"; + name = builtins.replaceStrings ["predictable"] ["predictableInterfaceNames"] name; meta = {}; machine = { lib, ... }: { + imports = [ ../modules/profiles/minimal.nix ]; networking.usePredictableInterfaceNames = lib.mkForce predictable; networking.useNetworkd = withNetworkd; networking.dhcpcd.enable = !withNetworkd; + boot.initrd.network.enable = withInitrdNetwork; + + # Ensure initrd DHCP works with predictable names. + # Use the same method as in tests/initrd-network.nix + boot.initrd.network.postCommands = lib.mkIf (predictable && !withNetworkd && withInitrdNetwork) '' + ip addr | grep 10.0.2.15 || exit 1 + ping -c1 10.0.2.2 || exit 1 + ''; }; testScript = '' @@ -21,4 +33,13 @@ in pkgs.lib.listToAttrs (pkgs.lib.crossLists (predictable: withNetworkd: { $machine->fail("ip link show ${if predictable then "eth0" else "ens3"}"); ''; }; -}) [[true false] [true false]]) +}) [ + { predictable = true; } + { predictable = false; } + + { withNetworkd = true; predictable = true; } + { withNetworkd = true; predictable = false; } + + { withInitrdNetwork = true; predictable = true; } + { withInitrdNetwork = true; predictable = false; } +])