| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							| 
									
										
										
										
											2016-02-02 17:25:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  |   cfg = config.boot.initrd.network; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 19:03:13 +01:00
										 |  |  |   udhcpcScript = pkgs.writeScript "udhcp-script" | 
					
						
							|  |  |  |     ''
 | 
					
						
							|  |  |  |       #! /bin/sh | 
					
						
							|  |  |  |       if [ "$1" = bound ]; then | 
					
						
							|  |  |  |         ip address add "$ip/$mask" dev "$interface" | 
					
						
							|  |  |  |         if [ -n "$router" ]; then | 
					
						
							|  |  |  |           ip route add default via "$router" dev "$interface" | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |         if [ -n "$dns" ]; then | 
					
						
							|  |  |  |           rm -f /etc/resolv.conf | 
					
						
							|  |  |  |           for i in $dns; do | 
					
						
							|  |  |  |             echo "nameserver $dns" >> /etc/resolv.conf | 
					
						
							|  |  |  |           done | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  | in | 
					
						
							| 
									
										
										
										
											2016-02-02 17:25:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     boot.initrd.network.enable = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							| 
									
										
										
										
											2016-02-02 19:03:13 +01:00
										 |  |  |         Add network connectivity support to initrd. The network may be | 
					
						
							|  |  |  |         configured using the <literal>ip</literal> kernel parameter, | 
					
						
							|  |  |  |         as described in <link | 
					
						
							|  |  |  |         xlink:href="https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt">the | 
					
						
							|  |  |  |         kernel documentation</link>.  Otherwise, if | 
					
						
							|  |  |  |         <option>networking.useDHCP</option> is enabled, an IP address | 
					
						
							|  |  |  |         is acquired using DHCP. | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:25:18 +01:00
										 |  |  |     boot.initrd.network.postCommands = mkOption { | 
					
						
							|  |  |  |       default = ""; | 
					
						
							|  |  |  |       type = types.lines; | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  |       description = ''
 | 
					
						
							| 
									
										
										
										
											2016-02-02 17:25:18 +01:00
										 |  |  |         Shell commands to be executed after stage 1 of the | 
					
						
							|  |  |  |         boot has initialised the network. | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     boot.initrd.kernelModules = [ "af_packet" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     boot.initrd.extraUtilsCommands = ''
 | 
					
						
							|  |  |  |       copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig | 
					
						
							|  |  |  |     '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-03 15:51:28 +03:00
										 |  |  |     boot.initrd.preLVMCommands = mkBefore ( | 
					
						
							| 
									
										
										
										
											2016-02-02 19:03:13 +01:00
										 |  |  |       # Search for interface definitions in command line. | 
					
						
							| 
									
										
										
										
											2016-02-03 15:51:28 +03:00
										 |  |  |       ''
 | 
					
						
							| 
									
										
										
										
											2016-02-02 19:03:13 +01:00
										 |  |  |         for o in $(cat /proc/cmdline); do | 
					
						
							|  |  |  |           case $o in | 
					
						
							|  |  |  |             ip=*) | 
					
						
							|  |  |  |               ipconfig $o && hasNetwork=1 | 
					
						
							|  |  |  |               ;; | 
					
						
							|  |  |  |           esac | 
					
						
							|  |  |  |         done | 
					
						
							|  |  |  |       ''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Otherwise, use DHCP. | 
					
						
							|  |  |  |       + optionalString config.networking.useDHCP ''
 | 
					
						
							|  |  |  |         if [ -z "$hasNetwork" ]; then | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # Bring up all interfaces. | 
					
						
							|  |  |  |           for iface in $(cd /sys/class/net && ls); do | 
					
						
							|  |  |  |             echo "bringing up network interface $iface..." | 
					
						
							|  |  |  |             ip link set "$iface" up | 
					
						
							|  |  |  |           done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # Acquire a DHCP lease. | 
					
						
							|  |  |  |           echo "acquiring IP address via DHCP..." | 
					
						
							| 
									
										
										
										
											2016-02-03 15:51:28 +03:00
										 |  |  |           udhcpc --quit --now --script ${udhcpcScript} && hasNetwork=1 | 
					
						
							| 
									
										
										
										
											2016-02-02 19:03:13 +01:00
										 |  |  |         fi | 
					
						
							|  |  |  |       ''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-03 15:51:28 +03:00
										 |  |  |       + ''
 | 
					
						
							|  |  |  |         if [ -n "$hasNetwork" ]; then | 
					
						
							|  |  |  |           echo "networking is up!" | 
					
						
							|  |  |  |           ${cfg.postCommands} | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |       '');
 | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-02-02 17:25:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 20:35:18 +03:00
										 |  |  | } |