summary refs log tree commit diff
path: root/nixos/modules/system/boot/initrd-network.nix
diff options
context:
space:
mode:
authorvolth <volth@webmaster.ms>2018-09-10 02:10:47 +0000
committerGitHub <noreply@github.com>2018-09-10 02:10:47 +0000
commit502b37ae6391e52b3a319c4ebf49e3358c50a440 (patch)
tree96379eb1dfe843d4f5f8c65f56fe103663119de1 /nixos/modules/system/boot/initrd-network.nix
parent857bf71655823b1e5140e11d1046afa89eca1634 (diff)
downloadnixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar
nixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.gz
nixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.bz2
nixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.lz
nixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.xz
nixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.zst
nixpkgs-502b37ae6391e52b3a319c4ebf49e3358c50a440.zip
nixos/initrd-network: multiple fixes
 * acquire DHCP on the interfaces with networking.interface.$name.useDHCP == true or on all interfaces if networking.useDHCP == true (was only only "eth0")
 * respect "mtu" if it was in DHCP answer (it happens in the wild)
 * acquire and set up staticroutes (unlike others clients, udhcpc does not do the query by default); this supersedes https://github.com/NixOS/nixpkgs/pull/41829
Diffstat (limited to 'nixos/modules/system/boot/initrd-network.nix')
-rw-r--r--nixos/modules/system/boot/initrd-network.nix30
1 files changed, 24 insertions, 6 deletions
diff --git a/nixos/modules/system/boot/initrd-network.nix b/nixos/modules/system/boot/initrd-network.nix
index 33862b0965c..1019b8db6da 100644
--- a/nixos/modules/system/boot/initrd-network.nix
+++ b/nixos/modules/system/boot/initrd-network.nix
@@ -6,11 +6,23 @@ let
 
   cfg = config.boot.initrd.network;
 
+  dhcpinterfaces = lib.attrNames (lib.filterAttrs (iface: v: v.useDHCP == true) (config.networking.interfaces or {}));
+
   udhcpcScript = pkgs.writeScript "udhcp-script"
     ''
       #! /bin/sh
       if [ "$1" = bound ]; then
-        ip address add "$ip/$mask" dev "$interface"
+        if [ -n "$mtu" ]; then
+          ip address add "$ip/$mask" dev "$interface" mtu "$mtu"
+        else
+          ip address add "$ip/$mask" dev "$interface"
+        fi
+        if [ -n "$staticroutes" ]; then
+          echo "$staticroutes" \
+            | sed -r "s@(\S+) (\S+)@ ip route add \"\1\" via \"\2\" dev \"$interface\" ; @g" \
+            | sed -r "s@ via \"0\.0\.0\.0\"@@g" \
+            | /bin/sh
+        fi
         if [ -n "$router" ]; then
           ip route add default via "$router" dev "$interface"
         fi
@@ -92,18 +104,24 @@ in
       ''
 
       # Otherwise, use DHCP.
-      + optionalString config.networking.useDHCP ''
+      + optionalString (config.networking.useDHCP || dhcpinterfaces != []) ''
         if [ -z "$hasNetwork" ]; then
 
           # Bring up all interfaces.
-          for iface in $(cd /sys/class/net && ls); do
+          for iface in $(ls /sys/class/net/); do
             echo "bringing up network interface $iface..."
             ip link set "$iface" up
           done
 
-          # Acquire a DHCP lease.
-          echo "acquiring IP address via DHCP..."
-          udhcpc --quit --now --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1
+          # Acquire DHCP leases.
+          for iface in ${ if config.networking.useDHCP then
+                            "$(ls /sys/class/net/ | grep -v ^lo$)"
+                          else
+                            lib.concatMapStringsSep " " lib.escapeShellArg dhcpinterfaces
+                        }; do
+            echo "acquiring IP address via DHCP on $iface..."
+            udhcpc --quit --now -i $iface -O staticroutes --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1
+          done
         fi
       ''