summary refs log tree commit diff
path: root/nixos/modules/services/networking/dhcpd.nix
blob: 3c4c0069dfd00661ca56f349f919ac9ad4fec25e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
{ config, lib, pkgs, ... }:

with lib;

let

  cfg4 = config.services.dhcpd4;
  cfg6 = config.services.dhcpd6;

  writeConfig = cfg: pkgs.writeText "dhcpd.conf"
    ''
      default-lease-time 600;
      max-lease-time 7200;
      ${optionalString (!cfg.authoritative) "not "}authoritative;
      ddns-update-style interim;
      log-facility local1; # see dhcpd.nix

      ${cfg.extraConfig}

      ${lib.concatMapStrings
          (machine: ''
            host ${machine.hostName} {
              hardware ethernet ${machine.ethernetAddress};
              fixed-address ${machine.ipAddress};
            }
          '')
          cfg.machines
      }
    '';

  dhcpdService = postfix: cfg:
    let
      configFile =
        if cfg.configFile != null
          then cfg.configFile
          else writeConfig cfg;
      leaseFile = "/var/lib/dhcpd${postfix}/dhcpd.leases";
      args = [
        "@${pkgs.dhcp}/sbin/dhcpd" "dhcpd${postfix}" "-${postfix}"
        "-pf" "/run/dhcpd${postfix}/dhcpd.pid"
        "-cf" configFile
        "-lf" leaseFile
      ] ++ cfg.extraFlags
        ++ cfg.interfaces;
    in
      optionalAttrs cfg.enable {
        "dhcpd${postfix}" = {
          description = "DHCPv${postfix} server";
          wantedBy = [ "multi-user.target" ];
          after = [ "network.target" ];

          preStart = "touch ${leaseFile}";
          serviceConfig = {
            ExecStart = concatMapStringsSep " " escapeShellArg args;
            Type = "forking";
            Restart = "always";
            DynamicUser = true;
            User = "dhcpd";
            Group = "dhcpd";
            AmbientCapabilities = [
              "CAP_NET_RAW"          # to send ICMP messages
              "CAP_NET_BIND_SERVICE" # to bind on DHCP port (67)
            ];
            StateDirectory   = "dhcpd${postfix}";
            RuntimeDirectory = "dhcpd${postfix}";
            PIDFile = "/run/dhcpd${postfix}/dhcpd.pid";
          };
        };
      };

  machineOpts = { ... }: {

    options = {

      hostName = mkOption {
        type = types.str;
        example = "foo";
        description = ''
          Hostname which is assigned statically to the machine.
        '';
      };

      ethernetAddress = mkOption {
        type = types.str;
        example = "00:16:76:9a:32:1d";
        description = ''
          MAC address of the machine.
        '';
      };

      ipAddress = mkOption {
        type = types.str;
        example = "192.168.1.10";
        description = ''
          IP address of the machine.
        '';
      };

    };
  };

  dhcpConfig = postfix: {

    enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to enable the DHCPv${postfix} server.
      '';
    };

    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.
      '';
    };

    extraFlags = mkOption {
      type = types.listOf types.str;
      default = [];
      description = ''
        Additional command line flags to be passed to the dhcpd daemon.
      '';
    };

    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.
      '';
    };

    interfaces = mkOption {
      type = types.listOf types.str;
      default = ["eth0"];
      description = ''
        The interfaces on which the DHCP server should listen.
      '';
    };

    machines = mkOption {
      type = with types; listOf (submodule machineOpts);
      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.
      '';
    };

    authoritative = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Whether the DHCP server shall send DHCPNAK messages to misconfigured
        clients. If this is not done, clients may be unable to get a correct
        IP address after changing subnets until their old lease has expired.
      '';
    };

  };

in

{

  imports = [
    (mkRenamedOptionModule [ "services" "dhcpd" ] [ "services" "dhcpd4" ])
  ] ++ flip map [ "4" "6" ] (postfix:
    mkRemovedOptionModule [ "services" "dhcpd${postfix}" "stateDir" ] ''
      The DHCP server state directory is now managed with the systemd's DynamicUser mechanism.
      This means the directory is named after the service (dhcpd${postfix}), created under
      /var/lib/private/ and symlinked to /var/lib/.
    ''
  );

  ###### interface

  options = {

    services.dhcpd4 = dhcpConfig "4";
    services.dhcpd6 = dhcpConfig "6";

  };


  ###### implementation

  config = mkIf (cfg4.enable || cfg6.enable) {

    systemd.services = dhcpdService "4" cfg4 // dhcpdService "6" cfg6;

  };

}