1 # This module automatically discovers zones in BIND and NSD NixOS
2 # configurations and creates zones for all definitions of networking.extraHosts
3 # (except those that point to 127.0.0.1 or ::1) within the current test network
4 # and delegates these zones using a fake root zone served by a BIND recursive
6 { config, nodes, pkgs, lib, ... }:
9 options.test-support.resolver.enable = lib.mkOption {
10 type = lib.types.bool;
14 Whether to enable the resolver that automatically discovers zone in the
17 This option is `true` by default, because the module
18 defining this option needs to be explicitly imported.
20 The reason this option exists is for the
21 {file}`nixos/tests/common/acme/server` module, which
22 needs that option to disable the resolver once the user has set its own
27 config = lib.mkIf config.test-support.resolver.enable {
28 networking.firewall.enable = false;
29 services.bind.enable = true;
30 services.bind.cacheNetworks = lib.mkForce [ "any" ];
31 services.bind.forwarders = lib.mkForce [];
32 services.bind.zones = lib.singleton {
36 addDot = zone: zone + lib.optionalString (!lib.hasSuffix "." zone) ".";
37 mkNsdZoneNames = zones: map addDot (lib.attrNames zones);
38 mkBindZoneNames = zones: map addDot (lib.attrNames zones);
39 getZones = cfg: mkNsdZoneNames cfg.services.nsd.zones
40 ++ mkBindZoneNames cfg.services.bind.zones;
42 getZonesForNode = attrs: {
43 ip = attrs.config.networking.primaryIPAddress;
44 zones = lib.filter (zone: zone != ".") (getZones attrs.config);
47 zoneInfo = lib.mapAttrsToList (lib.const getZonesForNode) nodes;
49 # A and AAAA resource records for all the definitions of
50 # networking.extraHosts except those for 127.0.0.1 or ::1.
52 # The result is an attribute set with keys being the host name and the
53 # values are either { ipv4 = ADDR; } or { ipv6 = ADDR; } where ADDR is
54 # the IP address for the corresponding key.
55 recordsFromExtraHosts = let
56 getHostsForNode = lib.const (n: n.config.networking.extraHosts);
57 allHostsList = lib.mapAttrsToList getHostsForNode nodes;
58 allHosts = lib.concatStringsSep "\n" allHostsList;
60 reIp = "[a-fA-F0-9.:]+";
61 reHost = "[a-zA-Z0-9.-]+";
63 matchAliases = str: let
64 matched = builtins.match "[ \t]+(${reHost})(.*)" str;
65 continue = lib.singleton (lib.head matched)
66 ++ matchAliases (lib.last matched);
67 in lib.optional (matched != null) continue;
70 result = builtins.match "[ \t]*(${reIp})[ \t]+(${reHost})(.*)" str;
71 in if result == null then null else {
72 ipAddr = lib.head result;
73 hosts = lib.singleton (lib.elemAt result 1)
74 ++ matchAliases (lib.last result);
78 rest = builtins.match "[^\n]*\n(.*)" str;
79 in if rest == null then "" else lib.head rest;
81 getEntries = str: acc: let
82 result = matchLine str;
83 next = getEntries (skipLine str);
84 newEntry = acc ++ lib.singleton result;
85 continue = if result == null then next acc else next newEntry;
86 in if str == "" then acc else continue;
88 isIPv6 = str: builtins.match ".*:.*" str != null;
89 loopbackIps = [ "127.0.0.1" "::1" ];
90 filterLoopback = lib.filter (e: !lib.elem e.ipAddr loopbackIps);
92 allEntries = lib.concatMap (entry: map (host: {
94 ${if isIPv6 entry.ipAddr then "ipv6" else "ipv4"} = entry.ipAddr;
95 }) entry.hosts) (filterLoopback (getEntries (allHosts + "\n") []));
97 mkRecords = entry: let
98 records = lib.optional (entry ? ipv6) "AAAA ${entry.ipv6}"
99 ++ lib.optional (entry ? ipv4) "A ${entry.ipv4}";
100 mkRecord = typeAndData: "${entry.host}. IN ${typeAndData}";
101 in lib.concatMapStringsSep "\n" mkRecord records;
103 in lib.concatMapStringsSep "\n" mkRecords allEntries;
105 # All of the zones that are subdomains of existing zones.
106 # For example if there is only "example.com" the following zones would
112 # While the following would *not* be 'subZones':
118 allZones = lib.concatMap (zi: zi.zones) zoneInfo;
119 isSubZoneOf = z1: z2: lib.hasSuffix z2 z1 && z1 != z2;
120 in lib.filter (z: lib.any (isSubZoneOf z) allZones) allZones;
122 # All the zones without 'subZones'.
123 filteredZoneInfo = map (zi: zi // {
124 zones = lib.filter (x: !lib.elem x subZones) zi.zones;
127 in pkgs.writeText "fake-root.zone" ''
129 . IN SOA ns.fakedns. admin.fakedns. ( 1 3h 1h 1w 1d )
130 ns.fakedns. IN A ${config.networking.primaryIPAddress}
132 ${lib.concatImapStrings (num: { ip, zones }: ''
133 ns${toString num}.fakedns. IN A ${ip}
134 ${lib.concatMapStrings (zone: ''
135 ${zone} IN NS ns${toString num}.fakedns.
137 '') (lib.filter (zi: zi.zones != []) filteredZoneInfo)}
138 ${recordsFromExtraHosts}