summaryrefslogtreecommitdiff
path: root/config/networking/services/dhcp.nix
blob: d27bbcef1fdc9fce0906ac91eb83fcb31a90f071 (plain)
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
{
  config,
  lib,
  ...
}: let
  nets = config.personal.networking.networks;
  netdevServices =
    builtins.map (subnet: "${subnet.interface}-netdev.service")
    (with nets; [wan iot]);
in {
  services.kea.dhcp4 = {
    enable = true;
    settings = let
      subnets = with nets; [wan iot eth0];
    in {
      interfaces-config = {
        interfaces = builtins.map (network: network.interface) subnets;
        service-sockets-max-retries = 20;
        service-sockets-retry-wait-time = 5000;
      };
      lease-database = {
        name = "/var/lib/kea/dhcp4.leases";
        persist = true;
        type = "memfile";
      };
      valid-lifetime = 600;
      max-valid-lifetime = 7200;
      option-data = [
        {
          name = "domain-name-servers";
          data = "${nets.lan.subnet}.1, 9.9.9.9";
        }
        {
          name = "subnet-mask";
          data = "255.255.255.0";
        }
      ];
      subnet4 =
        builtins.map (network: {
          subnet = "${network.subnet}.0/24";
          option-data = [
            {
              name = "broadcast-address";
              data = "${network.subnet}.255";
            }
            {
              name = "routers";
              data = network.machines.self.ip;
            }
          ];
          inherit (network) interface;
          pools = [{pool = "${network.subnet}.10 - ${network.subnet}.99";}];
          reservations = let
            machines = builtins.attrValues (lib.filterAttrs (name: {mac, ...}: name != "self" && mac != null) network.machines);
          in
            builtins.map ({
              ip,
              mac,
            }: {
              hw-address = mac;
              ip-address = ip;
            })
            machines;
        })
        subnets;
    };
  };

  systemd.services.kea-dhcp4-server.after = netdevServices;
  systemd.services.kea-dhcp4-server.bindsTo = netdevServices;
}