| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | with lib; | 
					
						
							| 
									
										
										
										
											2009-03-06 12:26:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   cfg4 = config.services.dhcpd4; | 
					
						
							|  |  |  |   cfg6 = config.services.dhcpd6; | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   writeConfig = cfg: pkgs.writeText "dhcpd.conf" | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |     ''
 | 
					
						
							|  |  |  |       default-lease-time 600; | 
					
						
							|  |  |  |       max-lease-time 7200; | 
					
						
							|  |  |  |       authoritative; | 
					
						
							| 
									
										
										
										
											2014-06-24 17:23:16 -05:00
										 |  |  |       ddns-update-style interim; | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |       log-facility local1; # see dhcpd.nix | 
					
						
							| 
									
										
										
										
											2013-10-29 17:29:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |       ${cfg.extraConfig} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-05 14:58:51 -04:00
										 |  |  |       ${lib.concatMapStrings | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |           (machine: ''
 | 
					
						
							|  |  |  |             host ${machine.hostName} { | 
					
						
							|  |  |  |               hardware ethernet ${machine.ethernetAddress}; | 
					
						
							|  |  |  |               fixed-address ${machine.ipAddress}; | 
					
						
							| 
									
										
										
										
											2009-03-06 12:26:05 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |           '')
 | 
					
						
							|  |  |  |           cfg.machines | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     '';
 | 
					
						
							| 
									
										
										
										
											2009-03-06 12:26:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   dhcpdService = postfix: cfg: optionalAttrs cfg.enable { | 
					
						
							|  |  |  |     "dhcpd${postfix}" = { | 
					
						
							|  |  |  |       description = "DHCPv${postfix} server"; | 
					
						
							|  |  |  |       wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |       after = [ "network.target" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       preStart = ''
 | 
					
						
							|  |  |  |         mkdir -m 755 -p ${cfg.stateDir} | 
					
						
							|  |  |  |         touch ${cfg.stateDir}/dhcpd.leases | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       serviceConfig = | 
					
						
							|  |  |  |         let | 
					
						
							|  |  |  |           configFile = if cfg.configFile != null then cfg.configFile else writeConfig cfg; | 
					
						
							|  |  |  |           args = [ "@${pkgs.dhcp}/sbin/dhcpd" "dhcpd${postfix}" "-${postfix}" | 
					
						
							|  |  |  |                    "-pf" "/run/dhcpd${postfix}/dhcpd.pid" | 
					
						
							|  |  |  |                    "-cf" "${configFile}" | 
					
						
							|  |  |  |                    "-lf" "${cfg.stateDir}/dhcpd.leases" | 
					
						
							|  |  |  |                    "-user" "dhcpd" "-group" "nogroup" | 
					
						
							|  |  |  |                  ] ++ cfg.extraFlags | 
					
						
							|  |  |  |                    ++ cfg.interfaces; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         in { | 
					
						
							|  |  |  |           ExecStart = concatMapStringsSep " " escapeShellArg args; | 
					
						
							|  |  |  |           Type = "forking"; | 
					
						
							|  |  |  |           Restart = "always"; | 
					
						
							|  |  |  |           RuntimeDirectory = [ "dhcpd${postfix}" ]; | 
					
						
							|  |  |  |           PIDFile = "/run/dhcpd${postfix}/dhcpd.pid"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:29:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-27 10:47:37 +01:00
										 |  |  |   machineOpts = { ... }: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     options = { | 
					
						
							| 
									
										
										
										
											2007-03-20 11:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |       hostName = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         example = "foo"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Hostname which is assigned statically to the machine. | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2007-12-04 14:53:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |       ethernetAddress = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         example = "00:16:76:9a:32:1d"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           MAC address of the machine. | 
					
						
							| 
									
										
										
										
											2013-10-29 17:29:37 +01:00
										 |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2007-03-20 11:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |       ipAddress = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         example = "192.168.1.10"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           IP address of the machine. | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2016-07-27 01:16:38 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2007-12-04 14:53:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   dhcpConfig = postfix: { | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     enable = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Whether to enable the DHCPv${postfix} server. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     stateDir = mkOption { | 
					
						
							|  |  |  |       type = types.path; | 
					
						
							|  |  |  |       # We use /var/lib/dhcp for DHCPv4 to save backwards compatibility. | 
					
						
							|  |  |  |       default = "/var/lib/dhcp${if postfix == "4" then "" else postfix}"; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         State directory for the DHCP server. | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:29:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     extraConfig = mkOption { | 
					
						
							|  |  |  |       type = types.lines; | 
					
						
							|  |  |  |       default = ""; | 
					
						
							|  |  |  |       example = ''
 | 
					
						
							|  |  |  |         option subnet-mask 255.255.255.0; | 
					
						
							|  |  |  |         option broadcast-address 192.168.1.255; | 
					
						
							|  |  |  |         option routers 192.168.1.5; | 
					
						
							|  |  |  |         option domain-name-servers 130.161.158.4, 130.161.33.17, 130.161.180.1; | 
					
						
							|  |  |  |         option domain-name "example.org"; | 
					
						
							|  |  |  |         subnet 192.168.1.0 netmask 255.255.255.0 { | 
					
						
							|  |  |  |           range 192.168.1.100 192.168.1.200; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Extra text to be appended to the DHCP server configuration | 
					
						
							|  |  |  |         file. Currently, you almost certainly need to specify something | 
					
						
							|  |  |  |         there, such as the options specifying the subnet mask, DNS servers, | 
					
						
							|  |  |  |         etc. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:29:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     extraFlags = mkOption { | 
					
						
							|  |  |  |       type = types.listOf types.str; | 
					
						
							|  |  |  |       default = []; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Additional command line flags to be passed to the dhcpd daemon. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2007-03-16 16:41:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     configFile = mkOption { | 
					
						
							|  |  |  |       type = types.nullOr types.path; | 
					
						
							|  |  |  |       default = null; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         The path of the DHCP server configuration file.  If no file | 
					
						
							|  |  |  |         is specified, a file is generated using the other options. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2007-03-16 16:41:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     interfaces = mkOption { | 
					
						
							|  |  |  |       type = types.listOf types.str; | 
					
						
							|  |  |  |       default = ["eth0"]; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         The interfaces on which the DHCP server should listen. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2007-03-20 12:34:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     machines = mkOption { | 
					
						
							| 
									
										
										
										
											2017-02-27 10:47:37 +01:00
										 |  |  |       type = with types; listOf (submodule machineOpts); | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |       default = []; | 
					
						
							|  |  |  |       example = [ | 
					
						
							|  |  |  |         { hostName = "foo"; | 
					
						
							|  |  |  |           ethernetAddress = "00:16:76:9a:32:1d"; | 
					
						
							|  |  |  |           ipAddress = "192.168.1.10"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { hostName = "bar"; | 
					
						
							|  |  |  |           ethernetAddress = "00:19:d1:1d:c4:9a"; | 
					
						
							|  |  |  |           ipAddress = "192.168.1.11"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         A list mapping Ethernet addresses to IPv${postfix} addresses for the | 
					
						
							|  |  |  |         DHCP server. | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2014-06-25 03:28:53 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### interface | 
					
						
							| 
									
										
										
										
											2007-03-20 12:34:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   options = { | 
					
						
							| 
									
										
										
										
											2007-03-20 12:34:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     services.dhcpd4 = dhcpConfig "4"; | 
					
						
							|  |  |  |     services.dhcpd6 = dhcpConfig "6"; | 
					
						
							| 
									
										
										
										
											2014-08-06 15:38:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2014-06-27 02:02:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-20 12:34:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   ###### implementation | 
					
						
							| 
									
										
										
										
											2009-03-06 12:26:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |   config = mkIf (cfg4.enable || cfg6.enable) { | 
					
						
							| 
									
										
										
										
											2014-06-27 02:02:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     users = { | 
					
						
							|  |  |  |       extraUsers.dhcpd = { | 
					
						
							|  |  |  |         uid = config.ids.uids.dhcpd; | 
					
						
							|  |  |  |         description = "DHCP daemon user"; | 
					
						
							| 
									
										
										
										
											2009-10-12 16:36:19 +00:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2017-01-14 14:36:33 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     systemd.services = dhcpdService "4" cfg4 // dhcpdService "6" cfg6; | 
					
						
							| 
									
										
										
										
											2009-03-06 12:26:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:29:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-16 16:41:38 +00:00
										 |  |  | } |