summaryrefslogtreecommitdiff
path: root/sys/ns/ns.nix
blob: e5b30e8762c45eee664057c80702b92019735a48 (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
{
  config,
  lib,
  ...
}:
with lib; let
  inherit (config.networking) domain;
  inherit (config.local.nets) gate-public;
  inherit (config.local.ns.server) tsigName;

  ptrNets = config.local.ns.ptr;
in {
  options.local.ns.zones = mkOption {
    type = with lib.types;
      attrsOf
      (submodule
        ({
          config,
          name,
          ...
        }: let
          inherit (config.soa) primary;

          cfg = config.localNS;
          ptrDomain = cfg.ptrNet.v4 != null || cfg.ptrNet.v6 != null;
        in {
          options.localNS = {
            enable = mkEnableOption "local NS settings";

            acme = mkOption {
              default = {};
              type = attrsOf str;
            };

            ptrNet = {
              v4 = mkOption {
                type = nullOr str;
                default = null;
              };

              v6 = mkOption {
                type = nullOr str;
                default = null;
              };
            };
          };

          config =
            mkIf cfg.enable
            {
              ptrName = let
                name =
                  if cfg.ptrNet.v6 != null
                  then "${cfg.ptrNet.v6}-v6"
                  else "${cfg.ptrNet.v4}-v4";
              in
                mkIf ptrDomain name;

              # https://docs.gandi.net/en/domain_names/advanced_users/secondary_nameserver.html
              nsdConfig = let
                providerSecondary = [
                  "37.205.15.45 ${tsigName}" # ns3.vpsfree.cz
                  "37.205.11.85 ${tsigName}" # ns4.vpsfree.cz
                  "2a03:3b40:fe:2be::1 ${tsigName}" # ns3.vpsfree.cz
                  "2a03:3b40:101:4::1 ${tsigName}" # ns4.vpsfree.cz
                ];
              in {
                notify = providerSecondary;
                provideXFR = providerSecondary;
              };

              ns = [
                {
                  name = "@";
                  host = primary;
                }
                {
                  name = "@";
                  host = "ns3.vpsfree.cz.";
                }
                {
                  name = "@";
                  host = "ns4.vpsfree.cz.";
                }
              ];

              a =
                optional (!ptrDomain)
                {
                  name = primary;
                  ipv4 = gate-public.hosts.gate.v4.address;
                  ptr = null;
                };

              aaaa =
                optional (!ptrDomain)
                {
                  name = primary;
                  ipv6 = gate-public.hosts.gate.v6.address;
                  ptr = null;
                };

              ptr = let
                ptrsToRecords = mapAttrsToList (suffix: target: {
                  name = suffix;
                  inherit target;
                });

                v4Net = cfg.ptrNet.v4;
                v6Net = cfg.ptrNet.v6;

                v4Records = optionals (v4Net != null) (ptrsToRecords ptrNets.${v4Net}.v4.targets);
                v6Records = optionals (v6Net != null) (ptrsToRecords ptrNets.${v6Net}.v6.targets);
              in
                v4Records ++ v6Records;

              soa = mkIf ptrDomain {
                authorityZone = mkDefault "${domain}.";
              };

              cname =
                mapAttrsToList
                (name: id: {
                  name = "_acme-challenge" + optionalString (name != "@") ".${name}";
                  target = "${id}.acme-challenge.${domain}.";
                })
                cfg.acme;
            };
        }));
  };

  config = {
    assertions =
      mapAttrsToList
      (name: zone: {
        assertion = zone.localNS.ptrNet.v4 != null -> zone.localNS.ptrNet.v6 == null;
        message = "zone '${name}' defined as both a v4 and v6 PTR zone";
      })
      config.local.ns.zones;

    local.ns.ptr = let
      zonePtrNets = name: zone:
        optionalAttrs (zone.localNS.ptrNet.v4 != null)
        {
          ${zone.localNS.ptrNet.v4}.v4.zone = name;
        }
        // optionalAttrs (zone.localNS.ptrNet.v6 != null) {
          ${zone.localNS.ptrNet.v6}.v6.zone = name;
        };
    in
      mkMerge (flatten (mapAttrsToList zonePtrNets (filterAttrs (_: zone: zone.localNS.enable) config.local.ns.zones)));
  };
}