at 17.09-beta 2.5 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 7 cfg = config.boot.initrd.network; 8 9 udhcpcScript = pkgs.writeScript "udhcp-script" 10 '' 11 #! /bin/sh 12 if [ "$1" = bound ]; then 13 ip address add "$ip/$mask" dev "$interface" 14 if [ -n "$router" ]; then 15 ip route add default via "$router" dev "$interface" 16 fi 17 if [ -n "$dns" ]; then 18 rm -f /etc/resolv.conf 19 for i in $dns; do 20 echo "nameserver $dns" >> /etc/resolv.conf 21 done 22 fi 23 fi 24 ''; 25 26in 27 28{ 29 30 options = { 31 32 boot.initrd.network.enable = mkOption { 33 type = types.bool; 34 default = false; 35 description = '' 36 Add network connectivity support to initrd. The network may be 37 configured using the <literal>ip</literal> kernel parameter, 38 as described in <link 39 xlink:href="https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt">the 40 kernel documentation</link>. Otherwise, if 41 <option>networking.useDHCP</option> is enabled, an IP address 42 is acquired using DHCP. 43 ''; 44 }; 45 46 boot.initrd.network.postCommands = mkOption { 47 default = ""; 48 type = types.lines; 49 description = '' 50 Shell commands to be executed after stage 1 of the 51 boot has initialised the network. 52 ''; 53 }; 54 55 56 }; 57 58 config = mkIf cfg.enable { 59 60 boot.initrd.kernelModules = [ "af_packet" ]; 61 62 boot.initrd.extraUtilsCommands = '' 63 copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig 64 ''; 65 66 boot.initrd.preLVMCommands = mkBefore ( 67 # Search for interface definitions in command line. 68 '' 69 for o in $(cat /proc/cmdline); do 70 case $o in 71 ip=*) 72 ipconfig $o && hasNetwork=1 73 ;; 74 esac 75 done 76 '' 77 78 # Otherwise, use DHCP. 79 + optionalString config.networking.useDHCP '' 80 if [ -z "$hasNetwork" ]; then 81 82 # Bring up all interfaces. 83 for iface in $(cd /sys/class/net && ls); do 84 echo "bringing up network interface $iface..." 85 ip link set "$iface" up 86 done 87 88 # Acquire a DHCP lease. 89 echo "acquiring IP address via DHCP..." 90 udhcpc --quit --now --script ${udhcpcScript} && hasNetwork=1 91 fi 92 '' 93 94 + '' 95 if [ -n "$hasNetwork" ]; then 96 echo "networking is up!" 97 ${cfg.postCommands} 98 fi 99 ''); 100 101 }; 102 103}