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
26 udhcpcArgs = toString cfg.udhcpc.extraArgs;
27
28in
29
30{
31
32 options = {
33
34 boot.initrd.network.enable = mkOption {
35 type = types.bool;
36 default = false;
37 description = ''
38 Add network connectivity support to initrd. The network may be
39 configured using the <literal>ip</literal> kernel parameter,
40 as described in <link
41 xlink:href="https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt">the
42 kernel documentation</link>. Otherwise, if
43 <option>networking.useDHCP</option> is enabled, an IP address
44 is acquired using DHCP.
45
46 You should add the module(s) required for your network card to
47 boot.initrd.availableKernelModules. lspci -v -s <ethernet controller>
48 will tell you which.
49 '';
50 };
51
52 boot.initrd.network.udhcpc.extraArgs = mkOption {
53 default = [];
54 type = types.listOf types.str;
55 description = ''
56 Additional command-line arguments passed verbatim to udhcpc if
57 <option>boot.initrd.network.enable</option> and <option>networking.useDHCP</option>
58 are enabled.
59 '';
60 };
61
62 boot.initrd.network.postCommands = mkOption {
63 default = "";
64 type = types.lines;
65 description = ''
66 Shell commands to be executed after stage 1 of the
67 boot has initialised the network.
68 '';
69 };
70
71
72 };
73
74 config = mkIf cfg.enable {
75
76 boot.initrd.kernelModules = [ "af_packet" ];
77
78 boot.initrd.extraUtilsCommands = ''
79 copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig
80 '';
81
82 boot.initrd.preLVMCommands = mkBefore (
83 # Search for interface definitions in command line.
84 ''
85 for o in $(cat /proc/cmdline); do
86 case $o in
87 ip=*)
88 ipconfig $o && hasNetwork=1
89 ;;
90 esac
91 done
92 ''
93
94 # Otherwise, use DHCP.
95 + optionalString config.networking.useDHCP ''
96 if [ -z "$hasNetwork" ]; then
97
98 # Bring up all interfaces.
99 for iface in $(cd /sys/class/net && ls); do
100 echo "bringing up network interface $iface..."
101 ip link set "$iface" up
102 done
103
104 # Acquire a DHCP lease.
105 echo "acquiring IP address via DHCP..."
106 udhcpc --quit --now --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1
107 fi
108 ''
109
110 + ''
111 if [ -n "$hasNetwork" ]; then
112 echo "networking is up!"
113 ${cfg.postCommands}
114 fi
115 '');
116
117 };
118
119}