12 cfg = config.boot.initrd.network;
14 dhcpInterfaces = lib.attrNames (
15 lib.filterAttrs (iface: v: v.useDHCP == true) (config.networking.interfaces or { })
17 doDhcp = cfg.udhcpc.enable || dhcpInterfaces != [ ];
19 if config.networking.useDHCP || cfg.udhcpc.enable then
20 "$(ls /sys/class/net/ | grep -v ^lo$)"
22 lib.concatMapStringsSep " " lib.escapeShellArg dhcpInterfaces;
24 udhcpcScript = pkgs.writeScript "udhcp-script" ''
26 if [ "$1" = bound ]; then
27 ip address add "$ip/$mask" dev "$interface"
28 if [ -n "$mtu" ]; then
29 ip link set mtu "$mtu" dev "$interface"
31 if [ -n "$staticroutes" ]; then
32 echo "$staticroutes" \
33 | sed -r "s@(\S+) (\S+)@ ip route add \"\1\" via \"\2\" dev \"$interface\" ; @g" \
34 | sed -r "s@ via \"0\.0\.0\.0\"@@g" \
37 if [ -n "$router" ]; then
38 ip route add "$router" dev "$interface" # just in case if "$router" is not within "$ip/$mask" (e.g. Hetzner Cloud)
39 ip route add default via "$router" dev "$interface"
41 if [ -n "$dns" ]; then
42 rm -f /etc/resolv.conf
43 for server in $dns; do
44 echo "nameserver $server" >> /etc/resolv.conf
50 udhcpcArgs = toString cfg.udhcpc.extraArgs;
58 boot.initrd.network.enable = mkOption {
62 Add network connectivity support to initrd. The network may be
63 configured using the `ip` kernel parameter,
64 as described in [the kernel documentation](https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt).
66 {option}`networking.useDHCP` is enabled, an IP address
67 is acquired using DHCP.
69 You should add the module(s) required for your network card to
70 boot.initrd.availableKernelModules.
71 `lspci -v | grep -iA8 'network\|ethernet'`
76 boot.initrd.network.flushBeforeStage2 = mkOption {
78 default = !config.boot.initrd.systemd.enable;
79 defaultText = "!config.boot.initrd.systemd.enable";
81 Whether to clear the configuration of the interfaces that were set up in
82 the initrd right before stage 2 takes over. Stage 2 will do the regular network
83 configuration based on the NixOS networking options.
85 The default is false when systemd is enabled in initrd,
86 because the systemd-networkd documentation suggests it.
90 boot.initrd.network.udhcpc.enable = mkOption {
91 default = config.networking.useDHCP && !config.boot.initrd.systemd.enable;
92 defaultText = "networking.useDHCP";
95 Enables the udhcpc service during stage 1 of the boot process. This
96 defaults to {option}`networking.useDHCP`. Therefore, this useful if
97 useDHCP is off but the initramfs should do dhcp.
101 boot.initrd.network.udhcpc.extraArgs = mkOption {
103 type = types.listOf types.str;
105 Additional command-line arguments passed verbatim to
106 udhcpc if {option}`boot.initrd.network.enable` and
107 {option}`boot.initrd.network.udhcpc.enable` are enabled.
111 boot.initrd.network.postCommands = mkOption {
115 Shell commands to be executed after stage 1 of the
116 boot has initialised the network.
122 config = mkIf cfg.enable {
124 boot.initrd.kernelModules = [ "af_packet" ];
126 boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
127 copy_bin_and_libs ${pkgs.klibc}/lib/klibc/bin.static/ipconfig
130 boot.initrd.preLVMCommands = mkIf (!config.boot.initrd.systemd.enable) (
132 # Search for interface definitions in command line.
135 for o in $(cat /proc/cmdline); do
138 ipconfig $o && ifaces="$ifaces $(echo $o | cut -d: -f6)"
144 # Otherwise, use DHCP.
145 + optionalString doDhcp ''
146 # Bring up all interfaces.
147 for iface in ${dhcpIfShellExpr}; do
148 echo "bringing up network interface $iface..."
149 ip link set dev "$iface" up && ifaces="$ifaces $iface"
152 # Acquire DHCP leases.
153 for iface in ${dhcpIfShellExpr}; do
154 echo "acquiring IP address via DHCP on $iface..."
155 udhcpc --quit --now -i $iface -O staticroutes --script ${udhcpcScript} ${udhcpcArgs}
163 boot.initrd.postMountCommands =
164 mkIf (cfg.flushBeforeStage2 && !config.boot.initrd.systemd.enable)
166 for iface in $ifaces; do
167 ip address flush dev "$iface"
168 ip link set dev "$iface" down