at 25.11-pre 5.3 kB view raw
1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7 8with lib; 9 10let 11 12 cfg = config.boot.initrd.network; 13 14 dhcpInterfaces = lib.attrNames ( 15 lib.filterAttrs (iface: v: v.useDHCP == true) (config.networking.interfaces or { }) 16 ); 17 doDhcp = cfg.udhcpc.enable || dhcpInterfaces != [ ]; 18 dhcpIfShellExpr = 19 if config.networking.useDHCP || cfg.udhcpc.enable then 20 "$(ls /sys/class/net/ | grep -v ^lo$)" 21 else 22 lib.concatMapStringsSep " " lib.escapeShellArg dhcpInterfaces; 23 24 udhcpcScript = pkgs.writeScript "udhcp-script" '' 25 #! /bin/sh 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" 30 fi 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" \ 35 | /bin/sh 36 fi 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" 40 fi 41 if [ -n "$dns" ]; then 42 rm -f /etc/resolv.conf 43 for server in $dns; do 44 echo "nameserver $server" >> /etc/resolv.conf 45 done 46 fi 47 fi 48 ''; 49 50 udhcpcArgs = toString cfg.udhcpc.extraArgs; 51 52in 53 54{ 55 56 options = { 57 58 boot.initrd.network.enable = mkOption { 59 type = types.bool; 60 default = false; 61 description = '' 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). 65 Otherwise, if 66 {option}`networking.useDHCP` is enabled, an IP address 67 is acquired using DHCP. 68 69 You should add the module(s) required for your network card to 70 boot.initrd.availableKernelModules. 71 `lspci -v | grep -iA8 'network\|ethernet'` 72 will tell you which. 73 ''; 74 }; 75 76 boot.initrd.network.flushBeforeStage2 = mkOption { 77 type = types.bool; 78 default = !config.boot.initrd.systemd.enable; 79 defaultText = "!config.boot.initrd.systemd.enable"; 80 description = '' 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. 84 85 The default is false when systemd is enabled in initrd, 86 because the systemd-networkd documentation suggests it. 87 ''; 88 }; 89 90 boot.initrd.network.udhcpc.enable = mkOption { 91 default = config.networking.useDHCP && !config.boot.initrd.systemd.enable; 92 defaultText = "networking.useDHCP"; 93 type = types.bool; 94 description = '' 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. 98 ''; 99 }; 100 101 boot.initrd.network.udhcpc.extraArgs = mkOption { 102 default = [ ]; 103 type = types.listOf types.str; 104 description = '' 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. 108 ''; 109 }; 110 111 boot.initrd.network.postCommands = mkOption { 112 default = ""; 113 type = types.lines; 114 description = '' 115 Shell commands to be executed after stage 1 of the 116 boot has initialised the network. 117 ''; 118 }; 119 120 }; 121 122 config = mkIf cfg.enable { 123 124 boot.initrd.kernelModules = [ "af_packet" ]; 125 126 boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) '' 127 copy_bin_and_libs ${pkgs.klibc}/lib/klibc/bin.static/ipconfig 128 ''; 129 130 boot.initrd.preLVMCommands = mkIf (!config.boot.initrd.systemd.enable) ( 131 mkBefore ( 132 # Search for interface definitions in command line. 133 '' 134 ifaces="" 135 for o in $(cat /proc/cmdline); do 136 case $o in 137 ip=*) 138 ipconfig $o && ifaces="$ifaces $(echo $o | cut -d: -f6)" 139 ;; 140 esac 141 done 142 '' 143 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" 150 done 151 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} 156 done 157 '' 158 159 + cfg.postCommands 160 ) 161 ); 162 163 boot.initrd.postMountCommands = 164 mkIf (cfg.flushBeforeStage2 && !config.boot.initrd.systemd.enable) 165 '' 166 for iface in $ifaces; do 167 ip address flush dev "$iface" 168 ip link set dev "$iface" down 169 done 170 ''; 171 172 }; 173 174}