2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-03-06 12:26:05 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
cfg = config.services.dhcpd;
|
|
|
|
|
|
|
|
stateDir = "/var/lib/dhcp"; # Don't use /var/state/dhcp; not FHS-compliant.
|
|
|
|
|
|
|
|
configFile = if cfg.configFile != null then cfg.configFile else pkgs.writeText "dhcpd.conf"
|
|
|
|
''
|
|
|
|
default-lease-time 600;
|
|
|
|
max-lease-time 7200;
|
|
|
|
authoritative;
|
2014-06-24 23:23:16 +01:00
|
|
|
ddns-update-style interim;
|
2009-10-12 17:36:19 +01:00
|
|
|
log-facility local1; # see dhcpd.nix
|
2013-10-29 16:29:37 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
${cfg.extraConfig}
|
|
|
|
|
2014-05-05 19:58:51 +01:00
|
|
|
${lib.concatMapStrings
|
2009-10-12 17:36:19 +01:00
|
|
|
(machine: ''
|
|
|
|
host ${machine.hostName} {
|
|
|
|
hardware ethernet ${machine.ethernetAddress};
|
|
|
|
fixed-address ${machine.ipAddress};
|
2009-03-06 12:26:05 +00:00
|
|
|
}
|
2009-10-12 17:36:19 +01:00
|
|
|
'')
|
|
|
|
cfg.machines
|
|
|
|
}
|
|
|
|
'';
|
2009-03-06 12:26:05 +00:00
|
|
|
|
|
|
|
in
|
2013-10-29 16:29:37 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
{
|
2009-03-06 12:26:05 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
###### interface
|
2007-03-16 16:41:38 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
options = {
|
2013-10-29 16:29:37 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
services.dhcpd = {
|
2007-03-20 11:04:45 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether to enable the DHCP server.
|
|
|
|
";
|
|
|
|
};
|
2007-12-04 14:53:37 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
2013-10-29 16:29:37 +00:00
|
|
|
example = ''
|
2009-10-12 17:36:19 +01:00
|
|
|
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;
|
2013-10-29 16:29:37 +00:00
|
|
|
option domain-name "example.org";
|
2009-10-12 17:36:19 +01:00
|
|
|
subnet 192.168.1.0 netmask 255.255.255.0 {
|
|
|
|
range 192.168.1.100 192.168.1.200;
|
|
|
|
}
|
2013-10-29 16:29:37 +00:00
|
|
|
'';
|
2009-10-12 17:36:19 +01:00
|
|
|
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.
|
|
|
|
";
|
|
|
|
};
|
2007-03-20 11:04:45 +00:00
|
|
|
|
2016-07-27 00:16:38 +01:00
|
|
|
extraFlags = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "-6";
|
|
|
|
description = "
|
|
|
|
Additional command line flags to be passed to the dhcpd daemon.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
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.
|
|
|
|
";
|
|
|
|
};
|
2007-12-04 14:53:37 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
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.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2013-10-29 16:29:37 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2013-10-29 16:29:37 +00:00
|
|
|
|
2007-03-16 16:41:38 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
###### implementation
|
2007-03-16 16:41:38 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
config = mkIf config.services.dhcpd.enable {
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2014-06-25 09:28:53 +01:00
|
|
|
users = {
|
|
|
|
extraUsers.dhcpd = {
|
|
|
|
uid = config.ids.uids.dhcpd;
|
|
|
|
description = "DHCP daemon user";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-06-27 08:02:48 +01:00
|
|
|
systemd.services.dhcpd =
|
2009-10-12 17:36:19 +01:00
|
|
|
{ description = "DHCP server";
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2014-06-27 08:02:48 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2007-03-20 12:34:52 +00:00
|
|
|
|
2014-08-06 21:38:02 +01:00
|
|
|
after = [ "network.target" ];
|
|
|
|
|
2014-06-27 08:02:48 +01:00
|
|
|
path = [ pkgs.dhcp ];
|
|
|
|
|
|
|
|
preStart =
|
2009-10-12 17:36:19 +01: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
|
|
|
|
|
2014-06-27 08:02:48 +01:00
|
|
|
mkdir -m 755 -p /run/dhcpd
|
|
|
|
chown dhcpd /run/dhcpd
|
2009-10-12 17:36:19 +01:00
|
|
|
'';
|
2014-06-27 08:02:48 +01:00
|
|
|
|
|
|
|
serviceConfig =
|
|
|
|
{ ExecStart = "@${pkgs.dhcp}/sbin/dhcpd dhcpd"
|
|
|
|
+ " -pf /run/dhcpd/dhcpd.pid -cf ${configFile}"
|
|
|
|
+ " -lf ${stateDir}/dhcpd.leases -user dhcpd -group nogroup"
|
2016-07-27 00:16:38 +01:00
|
|
|
+ " ${cfg.extraFlags}"
|
2014-06-27 08:02:48 +01:00
|
|
|
+ " ${toString cfg.interfaces}";
|
|
|
|
Restart = "always";
|
|
|
|
Type = "forking";
|
|
|
|
PIDFile = "/run/dhcpd/dhcpd.pid";
|
|
|
|
};
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2009-03-06 12:26:05 +00:00
|
|
|
|
|
|
|
};
|
2013-10-29 16:29:37 +00:00
|
|
|
|
2007-03-16 16:41:38 +00:00
|
|
|
}
|