summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/hosts.nix
blob: a95557b3d5fb587ce1bc4b7f1e332556b90938ca (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
with import <stockholm/lib>;
{ config, ... }: let
  # TODO dedup functions with ./retiolum-hosts.nix
  check = hostname: any (domain: hasSuffix ".${domain}" hostname) domains;
  domains = attrNames (filterAttrs (_: eq "hosts") config.krebs.dns.providers);
in {

  options = {
    krebs.hosts = mkOption {
      default = {};
      type = types.attrsOf types.host;
    };
  };

  config = {
    networking.extraHosts =
      concatStringsSep
        "\n"
        (flatten
          (mapAttrsToList
            (hostname: host:
              mapAttrsToList
                (netname: net: let
                  aliases = longs ++ shorts;
                  longs = filter check net.aliases;
                  shorts = let s = ".${config.krebs.dns.search-domain}"; in
                    map (removeSuffix s) (filter (hasSuffix s) longs);
                in
                  optionals
                    (aliases != [])
                    (map (addr: "${addr} ${toString aliases}") net.addrs))
                (filterAttrs (name: host: host.aliases != []) host.nets))
            config.krebs.hosts));
  };

}