2009-03-06 12:26:05 +00:00
|
|
|
{pkgs, config, ...}:
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
let
|
|
|
|
inherit (pkgs.lib) mkOption mkIf;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services = {
|
|
|
|
dhcpd = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether to enable the DHCP server.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
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 = "
|
|
|
|
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.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
interfaces = mkOption {
|
|
|
|
default = ["eth0"];
|
|
|
|
description = "
|
|
|
|
The interfaces on which the DHCP server should listen.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
description = "
|
|
|
|
A list mapping ethernet addresses to IP addresses for the
|
|
|
|
DHCP server.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
###### implementation
|
2007-03-16 16:41:38 +00:00
|
|
|
|
2007-03-20 11:04:45 +00:00
|
|
|
let
|
|
|
|
|
2007-12-04 14:53:37 +00:00
|
|
|
cfg = config.services.dhcpd;
|
|
|
|
|
2007-03-20 11:04:45 +00:00
|
|
|
stateDir = "/var/lib/dhcp"; # Don't use /var/state/dhcp; not FHS-compliant.
|
|
|
|
|
2008-05-08 12:27:01 +00:00
|
|
|
machines = pkgs.lib.concatMapStrings (machine: ''
|
2007-12-04 14:53:37 +00:00
|
|
|
host ${machine.hostName} {
|
|
|
|
hardware ethernet ${machine.ethernetAddress};
|
|
|
|
fixed-address ${machine.ipAddress};
|
|
|
|
}
|
2008-05-08 12:27:01 +00:00
|
|
|
'') cfg.machines;
|
2007-12-04 14:53:37 +00:00
|
|
|
|
2008-05-08 12:27:01 +00:00
|
|
|
configFile = if cfg.configFile != null then cfg.configFile else pkgs.writeText "dhcpd.conf" ''
|
2007-12-04 14:53:37 +00:00
|
|
|
default-lease-time 600;
|
|
|
|
max-lease-time 7200;
|
|
|
|
authoritative;
|
|
|
|
ddns-update-style ad-hoc;
|
2008-05-08 12:27:01 +00:00
|
|
|
log-facility local1; # see dhcpd.nix
|
2007-12-04 14:53:37 +00:00
|
|
|
${cfg.extraConfig}
|
|
|
|
${machines}
|
2008-05-08 12:27:01 +00:00
|
|
|
'';
|
2007-12-04 14:53:37 +00:00
|
|
|
|
2007-03-20 11:04:45 +00:00
|
|
|
in
|
|
|
|
|
2007-03-16 16:41:38 +00:00
|
|
|
|
2009-03-06 12:26:05 +00:00
|
|
|
mkIf config.services.dhcpd.enable {
|
|
|
|
require = [
|
|
|
|
options
|
|
|
|
];
|
2007-03-16 16:41:38 +00:00
|
|
|
|
2009-03-06 12:26:05 +00:00
|
|
|
services = {
|
|
|
|
extraJobs = [{
|
|
|
|
name = "dhcpd";
|
|
|
|
|
|
|
|
job = ''
|
|
|
|
description "DHCP server"
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2009-03-06 12:26:05 +00:00
|
|
|
start on network-interfaces/started
|
|
|
|
stop on network-interfaces/stop
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2009-03-06 12:26:05 +00:00
|
|
|
script
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2009-03-06 12:26:05 +00:00
|
|
|
mkdir -m 755 -p ${stateDir}
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2009-03-06 12:26:05 +00:00
|
|
|
touch ${stateDir}/dhcpd.leases
|
|
|
|
|
|
|
|
exec ${pkgs.dhcp}/sbin/dhcpd -f -cf ${configFile} \
|
|
|
|
-lf ${stateDir}/dhcpd.leases \
|
|
|
|
${toString cfg.interfaces}
|
|
|
|
|
|
|
|
end script
|
|
|
|
'';
|
|
|
|
}];
|
|
|
|
};
|
2007-03-16 16:41:38 +00:00
|
|
|
}
|