summaryrefslogtreecommitdiff
path: root/hosts/vidhar/network/dhcp/default.nix
blob: 098d30610a3041b454b4413debba0d48042760b4 (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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
{ flake, config, pkgs, lib, sources, ... }:

with lib;

let
  nfsrootBaseUrl = "http://nfsroot.vidhar.yggdrasil";
  tftpIp = "10.141.0.1";
  nfsIp = tftpIp;
  ipxe = pkgs.ipxe.override {
    additionalTargets = {
      "bin-i386-efi/ipxe.efi" = "i386-ipxe.efi";
    };
    additionalOptions = [
      "NSLOOKUP_CMD"
      "PING_CMD"
      "CONSOLE_CMD"
    ];
    embedScript = pkgs.writeText "yggdrasil.ipxe" ''
      #!ipxe

      cpair --background 9 1
      cpair --background 9 3
      cpair --background 9 6

      set user-class iPXE-yggdrasil

      autoboot
    '';
  };
in {
  config = {
    services.kea = {
      dhcp4 = {
        enable = true;
        settings = {
          valid-lifetime = 4000;
          rebind-timer = 2000;
          renew-timer = 1000;

          interfaces-config = {
            interfaces = [ "lan" "mgmt" ];
          };

          lease-database = {
            name = "/var/lib/kea/dhcp4.leases";
            persist = true;
            type = "memfile";
          };

          client-classes = [
            { name = "ipxe-eostre";
              test = "hexstring(pkt4.mac, ':') == '00:d8:61:79:c5:40' and option[77].hex == 'iPXE-yggdrasil'";
              next-server = tftpIp;
              boot-file-name = "${nfsrootBaseUrl}/eostre.menu.ipxe";
              only-if-required = true;
            }
            { name = "ipxe-yggdrasil";
              test = "option[77].hex == 'iPXE-yggdrasil'";
              next-server = tftpIp;
              boot-file-name = "${nfsrootBaseUrl}/installer-x86_64-linux.menu.ipxe";
              only-if-required = true;
            }

            { name = "uefi-http";
              test = "option[client-system].hex == 0x0010";
              option-data = [
                { name = "vendor-class-identifier"; data = "HTTPClient"; }
              ];
              boot-file-name = "${nfsrootBaseUrl}/ipxe.efi";
              only-if-required = true;
            }

            { name = "ipxe-uefi-64";
              test = "option[77].hex == 'iPXE' and (substring(option[60].hex,0,20) == 'PXEClient:Arch:00007' or substring(option[60].hex,0,20) == 'PXEClient:Arch:00008' or substring(option[60].hex,0,20) == 'PXEClient:Arch:00009')";
              boot-file-name = "${nfsrootBaseUrl}/ipxe.efi";
              only-if-required = true;
            }
            { name = "ipxe-uefi-32";
              test = "option[77].hex == 'iPXE' and (substring(option[60].hex,0,20) == 'PXEClient:Arch:00002' or substring(option[60].hex,0,20) == 'PXEClient:Arch:00006')";
              boot-file-name = "${nfsrootBaseUrl}/i386-ipxe.efi";
              only-if-required = true;
            }
            { name = "ipxe-legacy";
              test = "option[77].hex == 'iPXE' and substring(option[60].hex,0,20) == 'PXEClient:Arch:00000'";
              boot-file-name = "${nfsrootBaseUrl}/ipxe.lkrn";
              only-if-required = true;
            }

            { name = "uefi-64";
              test = "substring(option[60].hex,0,20) == 'PXEClient:Arch:00007' or substring(option[60].hex,0,20) == 'PXEClient:Arch:00008' or substring(option[60].hex,0,20) == 'PXEClient:Arch:00009'";
              option-data = [
                { name = "tftp-server-name"; data = tftpIp; }
              ];
              boot-file-name = "ipxe.efi";
              only-if-required = true;
            }
            { name = "uefi-32";
              test = "substring(option[60].hex,0,20) == 'PXEClient:Arch:00002' or substring(option[60].hex,0,20) == 'PXEClient:Arch:00006'";
              option-data = [
                { name = "tftp-server-name"; data = tftpIp; }
              ];
              boot-file-name = "i386-ipxe.efi";
              only-if-required = true;
            }
            { name = "legacy";
              test = "substring(option[60].hex,0,20) == 'PXEClient:Arch:00000'";
              option-data = [
                { name = "tftp-server-name"; data = tftpIp; }
              ];
              boot-file-name = "ipxe.lkrn";
              only-if-required = true;
            }
          ];

          dhcp-ddns.enable-updates = true;
          ddns-send-updates = false;
          ddns-override-client-update = true;
          ddns-override-no-update = true;
          ddns-replace-client-name = "when-not-present";
          ddns-generated-prefix = "noname";
          ddns-update-on-renew = true;

          subnet4 = [
            { id = 3;
              subnet = "10.141.0.0/24";
              option-data = [
                { name = "domain-name-servers";
                  data = "10.141.0.1";
                }
                { name = "broadcast-address";
                  data = "10.141.0.255";
                }
                { name = "routers";
                  data = "10.141.0.1";
                }
                { name = "domain-name";
                  data = "yggdrasil";
                }
                { name = "domain-search";
                  data = "lan.yggdrasil, yggdrasil";
                }
              ];
              ddns-send-updates = true;
              ddns-qualifying-suffix = "lan.yggdrasil";
              pools = [ { pool = "10.141.0.128 - 10.141.0.254"; } ];
              require-client-classes = map (cc: cc.name) config.services.kea.dhcp4.settings.client-classes;
              reservations = [
                { hostname = "sif";
                  hw-address = "3c:e1:a1:52:24:35";
                }
                { hostname = "sif";
                  hw-address = "ee:32:68:76:83:ac";
                }
                { hostname = "sif";
                  hw-address = "48:2a:e3:64:62:97";
                }
                { hostname = "eos";
                  hw-address = "00:d8:61:79:c5:40";
                }
                { hostname = "geri";
                  hw-address = "0e:e6:43:5e:37:7b";
                }
              ];
            }
            { id = 2;
              subnet = "10.141.1.0/24";
              option-data = [
                { name = "domain-name-servers";
                  data = "10.141.1.1";
                }
                { name = "broadcast-address";
                  data = "10.141.1.255";
                }
                { name = "ntp-servers";
                  data = "10.141.1.1";
                }
                { name = "domain-name";
                  data = "yggdrasil";
                }
                { name = "domain-search";
                  data = "mgmt.yggdrasil, yggdrasil";
                }
              ];
              ddns-send-updates = true;
              ddns-qualifying-suffix = "mgmt.yggdrasil";
              pools = [ { pool = "10.141.1.128 - 10.141.1.254"; } ];
              reservations = [
                { hostname = "switch01";
                  hw-address = "60:a4:b7:53:94:b5";
                  ip-address = "10.141.1.2";
                }
                { hostname = "ap01";
                  hw-address = "74:ac:b9:29:ad:9a";
                  ip-address = "10.141.1.4";
                }
              ];
            }
            { id = 4;
              subnet = "10.141.2.0/24";
              option-data = [
                { name = "domain-name-servers";
                  data = "10.141.2.1";
                }
                { name = "broadcast-address";
                  data = "10.141.2.255";
                }
                { name = "routers";
                  data = "10.141.2.1";
                }
              ];
              ddns-send-updates = false;
              pools = [ { pool = "10.141.2.128 - 10.141.2.254"; } ];
              reservations = [];
            }
          ];
        };
      };
      # dhcp6 = {
      #   enable = true;
      #   settings = {
      #     interfaces-config = {
      #       interfaces = [ "lan" ];
      #     };

      #     lease-database = {
      #       name = "/var/lib/kea/dhcp6.leases";
      #       persist = true;
      #       type = "memfile";
      #     };
      #   };
      # };
      dhcp-ddns = {
        enable = true;
        settings = {
          forward-ddns = {
            ddns-domains = [
              { name = "yggdrasil.";
                dns-servers = [
                  { ip-address = "127.0.0.1";
                    port = 5353;
                    key-name = "local_key";
                  }
                ];
              }
            ];
          };
          reverse-ddns = {
            ddns-domains = [
              { name = "141.10.in-addr.arpa.";
                dns-servers = [
                  { ip-address = "127.0.0.1";
                    port = 5353;
                    key-name = "local_key";
                  }
                ];
              }
            ];
          };
        };
      };
    };

    systemd.services.kea-dhcp-ddns-server = {
      preStart = let
        configLines = [
          "<?include \"\${CREDENTIALS_DIRECTORY}/knot-tsig.json.frag\"?>"
        ] ++ mapAttrsToList (k: v:
          "\"${k}\": ${builtins.toJSON v}"
        ) config.services.kea.dhcp-ddns.settings;

        config-template = pkgs.writeText "dhcp-ddns.conf" ''
          {"DhcpDdns": {
            ${concatStringsSep ",\n  " configLines}
          }}
        '';
      in ''
        ${pkgs.envsubst}/bin/envsubst -i "${config-template}" -o "''${RUNTIME_DIRECTORY}/dhcp-ddns.conf"
      '';

      serviceConfig = {
        ExecStart = mkForce ''
          ${pkgs.kea}/bin/kea-dhcp-ddns -c "''${RUNTIME_DIRECTORY}/dhcp-ddns.conf" ${escapeShellArgs config.services.kea.dhcp-ddns.extraArgs}
        '';
        LoadCredential = [
          "knot-tsig.json.frag:${config.sops.secrets."kea-knot-tsig.json.frag".path}"
        ];
      };
    };

    sops.secrets."kea-knot-tsig.json.frag" = {
      format = "binary";
      sopsFile = ./knot-tsig.json.frag;
    };

    services.nginx.virtualHosts."nfsroot.vidhar.yggdrasil" = {
      addSSL = false;
      forceSSL = false;
      locations."/" = {
        extraConfig = ''
          autoindex on;
        '';
        root = pkgs.symlinkJoin {
          name = "nfsroot.vidhar.yggdrasil";
          paths =
            (map (system:
              pkgs.symlinkJoin {
                name = "installer-${system}";
                paths = [
                  (let
                    installerBuild = (flake.nixosConfigurations.${"installer-${system}-nfsroot"}.extendModules {
                      modules = [
                        ({ ... }: {
                          config.nfsroot.storeDevice = "${nfsIp}:nix-store";
                          config.nfsroot.registrationUrl = "${nfsrootBaseUrl}/installer-${system}/registration";
                        })
                      ];
                    }).config.system.build;
                  in builtins.toPath (pkgs.runCommandLocal "install-${system}" {} ''
                    mkdir -p $out/installer-${system}
                    install -m 0444 -t $out/installer-${system} \
                      ${installerBuild.initialRamdisk}/initrd \
                      ${installerBuild.kernel}/bzImage \
                      ${installerBuild.netbootIpxeScript}/netboot.ipxe \
                      ${pkgs.closureInfo { rootPaths = installerBuild.storeContents; }}/registration
                  ''))
                  (pkgs.writeTextFile {
                    name = "installer-${system}.menu.ipxe";
                    destination = "/installer-${system}.menu.ipxe";
                    text = ''
                      #!ipxe

                      :start
                      menu iPXE boot menu for installer-${system}
                      item installer installer-${system}
                      item memtest memtest86plus
                      item netboot netboot.xyz
                      item shell iPXE shell
                      choose --timeout 0 --default installer selected || goto shell
                      goto ''${selected}

                      :shell
                      shell
                      goto start

                      :installer
                      chain installer-${system}/netboot.ipxe
                      goto start

                      :netboot
                      iseq ''${platform} efi && chain --autofree netboot.xyz.efi || chain --autofree netboot.xyz.lkrn
                      goto start

                      :memtest
                      iseq ''${platform} efi && chain --autofree memtest.efi || chain --autofree memtest.bin
                      goto start
                    '';
                  })
                ];
              }) ["x86_64-linux"]
            ) ++ [
              (pkgs.runCommandLocal "utils" {} ''
                mkdir $out
                install -m 0444 -t $out \
                  ${ipxe}/{ipxe.efi,i386-ipxe.efi,ipxe.lkrn} \
                  ${pkgs.memtest86plus}/{memtest.efi,memtest.bin}
                install -m 0444 ${sources.netbootxyz-efi.src} $out/netboot.xyz.efi
                install -m 0444 ${sources.netbootxyz-lkrn.src} $out/netboot.xyz.lkrn
              '')
              (let
                 eostreBuild = (flake.nixosConfigurations.eostre.extendModules {
                   modules = [
                     ({ ... }: {
                       config.nfsroot.storeDevice = "${nfsIp}:nix-store";
                       config.nfsroot.registrationUrl = "${nfsrootBaseUrl}/eostre/registration";
                     })
                   ];
                 }).config.system.build;
               in builtins.toPath (pkgs.runCommandLocal "eostre" {} ''
                    mkdir -p $out/eostre
                    install -m 0444 -t $out/eostre \
                      ${eostreBuild.initialRamdisk}/initrd \
                      ${eostreBuild.kernel}/bzImage \
                      ${eostreBuild.netbootIpxeScript}/netboot.ipxe \
                      ${pkgs.closureInfo { rootPaths = eostreBuild.storeContents; }}/registration
                  ''))
              (pkgs.writeTextFile {
                name = "eostre.menu.ipxe";
                destination = "/eostre.menu.ipxe";
                text = ''
                  #!ipxe

                  set menu-timeout 5000

                  :start
                  menu iPXE boot menu for eostre
                  item eostre eostre
                  item memtest memtest86plus
                  item netboot netboot.xyz
                  item shell iPXE shell
                  choose --timeout ''${menu-timeout} --default eostre selected || goto shell
                  set menu-timeout 0
                  goto ''${selected}

                  :shell
                  set menu-timeout 0
                  shell
                  goto start

                  :eostre
                  chain eostre/netboot.ipxe
                  goto start

                  :netboot
                  iseq ''${platform} efi && chain --autofree netboot.xyz.efi || chain --autofree netboot.xyz.lkrn
                  goto start

                  :memtest
                  iseq ''${platform} efi && chain --autofree memtest.efi || chain --autofree memtest.bin
                  goto start
                '';
              })
            ];
        };
      };
    };

    systemd.services."pxe-atftpd" = {
      description = "TFTP Server for PXE Booting";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig.ExecStart = let
        tftpRoot = pkgs.runCommandLocal "netboot" {} ''
          mkdir -p $out
          install -m 0444 -t $out \
            ${ipxe}/{ipxe.efi,i386-ipxe.efi,ipxe.lkrn}
        '';
      in "${pkgs.atftp}/sbin/atftpd --daemon --no-fork --bind-address=${tftpIp} ${tftpRoot}";
    };
  };
}