refactor(dns): simplify DNS config and associate architect domain to every interface

This commit is contained in:
Giulio De Pasquale 2024-10-07 12:09:12 +01:00
parent e10d9a4d5a
commit 74efccbdb6

View File

@ -1,11 +1,12 @@
{ config, lib, ... }: { config, lib, ... }:
with lib;
let let
# Function to generate CoreDNS config for a single vhost generateCoreDNSConfig = domains:
generateCoreDNSConfigForVhost = domain: conf:
let let
# Retrieve architect's IP on each interface generateForDomain = domain: conf:
interfaceConfigs = builtins.map concatMapStrings
(iface: (iface:
let let
architectIP = config.architect.networks.${iface}.devices.architect.address; architectIP = config.architect.networks.${iface}.devices.architect.address;
@ -20,9 +21,11 @@ let
template IN A ${domain} { template IN A ${domain} {
answer "${domain}. 60 IN A ${architectIP}" answer "${domain}. 60 IN A ${architectIP}"
} }
template IN HTTPS ${domain} { template IN HTTPS ${domain} {
answer "${domain}. 60 IN HTTPS 1 . ipv4hint=\"${architectIP}\"" answer "${domain}. 60 IN HTTPS 1 . ipv4hint=\"${architectIP}\""
} }
cache cache
log log
} }
@ -30,14 +33,19 @@ let
) )
conf.dnsInterfaces; conf.dnsInterfaces;
in in
lib.concatStringsSep "\n" interfaceConfigs; concatStrings (mapAttrsToList generateForDomain domains);
# Combine vhosts and the single domain
allDomains = config.architect.vhost // {
"architect.devs.giugl.io" = { dnsInterfaces = [ "lan" "tailscale" ]; };
};
in in
{ {
services = { services = {
coredns = { coredns = {
enable = true; enable = true;
config = '' config = ''
${lib.concatStringsSep "\n" (lib.mapAttrsToList generateCoreDNSConfigForVhost config.architect.vhost)} ${generateCoreDNSConfig allDomains}
. { . {
cache cache