zaphyra's git: nixfiles

zaphyra's nixfiles

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 
{
  nixosConfigurations,
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib) types;

in
{

  options.dns = {
    enable = lib.mkEnableOption "dns zones via nix";

    # contains dns entries defined on the local host
    zones = lib.mkOption {
      type = types.attrsOf pkgs.dnsNix.types.subzone;
      default = { };
    };

    # contains dns entries defined on the local host and on remote hosts, merged together
    allZones = lib.mkOption {
      type = types.attrsOf pkgs.dnsNix.types.zone;
      default = { };
    };

    zoneFiles = lib.mkOption {
      type = types.attrsOf types.path;
      readOnly = true;
      default = lib.mapAttrs (
        name: zone:
        toString (
          pkgs.writeTextFile {
            name = "${name}.zone";
            text = pkgs.dnsNix.types.zoneToString name (pkgs.dnsNix.evalZone name zone);
          }
        )
      ) config.dns.allZones;
    };
  };

  config = lib.mkIf config.dns.enable {
    # serve records defined in all host configs
    dns.allZones = lib.mkMerge (
      lib.flip lib.mapAttrsToList nixosConfigurations (
        _hostName: machineConfig: machineConfig.config.dns.zones
      )
    );
  };

}