summaryrefslogtreecommitdiffstats
path: root/modules/dns.nix
blob: 276e9f81f9b75975886dd0c02b7e6c2f6f58d200 (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
{ config, lib, pkgs, ... }: {
  options.hrm.dns.zones = lib.mkOption {
    default = {};
    description = ''
      DNS zones, written as zone files for an authoritative server.
    '';
    type = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: {
      options = {
        ttl = lib.mkOption {
          type = lib.types.ints.unsigned;
          default = 60;
        };
        soa = {
          nameserver = lib.mkOption {
            type = lib.types.str;
          };
          email = lib.mkOption {
            type = lib.types.str;
          };
          serial = lib.mkOption {
            type = lib.types.ints.unsigned;
          };
          refresh = lib.mkOption {
            type = lib.types.ints.between 3600 86400;
            default = 3600;
          };
          retry = lib.mkOption {
            type = lib.types.ints.unsigned;
            default = 900;
          };
          expire = lib.mkOption {
            type = lib.types.ints.between 604800 3600000;
            default = 604800;
          };
          minimum = lib.mkOption {
            type = lib.types.ints.between 180 86400;
            default = 180;
          };
        };
        records = lib.mkOption {
          type = lib.types.attrsOf (lib.types.listOf lib.types.str);
          default = {};
          description = ''
            Records by owner name, each written after its name in the zone
            file, e.g. "MX 10 ne" or "3600 NS ns2".
          '';
        };
        file = lib.mkOption {
          type = lib.types.package;
          readOnly = true;
        };
      };
      config.file = pkgs.runCommand "${name}.zone" {
        nativeBuildInputs = [ pkgs.knot-dns ];
        preferLocalBuild = true;
        passAsFile = [ "text" ];
        text = lib.concatLines ([
          "$TTL ${toString config.ttl}"
          "@ SOA ${lib.concatStringsSep " " [
            config.soa.nameserver
            (lib.replaceStrings [ "@" ] [ "." ] config.soa.email + ".")
            (toString config.soa.serial)
            (toString config.soa.refresh)
            (toString config.soa.retry)
            (toString config.soa.expire)
            (toString config.soa.minimum)
          ]}"
        ] ++ lib.concatLists (lib.mapAttrsToList
          (owner: map (record: "${owner} ${record}"))
          config.records));
      } ''
        kzonecheck --origin ${lib.escapeShellArg name} "$textPath"
        cp "$textPath" "$out"
      '';
    }));
  };
}