diff --git a/system/options.nix b/system/options.nix index dae2763943d..e06b4c2197b 100644 --- a/system/options.nix +++ b/system/options.nix @@ -543,9 +543,31 @@ "; }; - configFile = mkOption { + extraConfig = mkOption { + 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 = " - The path of the DHCP server configuration file. + Extra text to be appended to the DHCP server configuration + file. Currently, you almost certainly need to specify + something here, such as the options specifying the subnet + mask, DNS servers, etc. + "; + }; + + configFile = mkOption { + default = null; + description = " + The path of the DHCP server configuration file. If no file + is specified, a file is generated using the other options. "; }; @@ -556,6 +578,20 @@ "; }; + machines = mkOption { + 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"; + } + ]; + }; + }; diff --git a/upstart-jobs/default.nix b/upstart-jobs/default.nix index 993fb12528a..d0677b66e0c 100644 --- a/upstart-jobs/default.nix +++ b/upstart-jobs/default.nix @@ -121,9 +121,7 @@ let # DHCP server. ++ optional config.services.dhcpd.enable (import ../upstart-jobs/dhcpd.nix { - inherit (pkgs) dhcp; - configFile = config.services.dhcpd.configFile; - interfaces = config.services.dhcpd.interfaces; + inherit pkgs config; }) # SSH daemon. diff --git a/upstart-jobs/dhcpd.nix b/upstart-jobs/dhcpd.nix index 5025f18e978..95040b3a3d4 100644 --- a/upstart-jobs/dhcpd.nix +++ b/upstart-jobs/dhcpd.nix @@ -1,9 +1,27 @@ -{dhcp, configFile, interfaces}: +{pkgs, config}: let + cfg = config.services.dhcpd; + stateDir = "/var/lib/dhcp"; # Don't use /var/state/dhcp; not FHS-compliant. + machines = pkgs.lib.concatStrings (map (machine: " + host ${machine.hostName} { + hardware ethernet ${machine.ethernetAddress}; + fixed-address ${machine.ipAddress}; + } + ") cfg.machines); + + configFile = if cfg.configFile != null then cfg.configFile else pkgs.writeText "dhcpd.conf" " + default-lease-time 600; + max-lease-time 7200; + authoritative; + ddns-update-style ad-hoc; + ${cfg.extraConfig} + ${machines} + "; + in { @@ -21,9 +39,9 @@ script touch ${stateDir}/dhcpd.leases - exec ${dhcp}/sbin/dhcpd -f -cf ${configFile} \\ + exec ${pkgs.dhcp}/sbin/dhcpd -f -cf ${configFile} \\ -lf ${stateDir}/dhcpd.leases \\ - ${toString interfaces} + ${toString cfg.interfaces} end script ";