nixos/services: make dns configuration easier

This commit is contained in:
2026-05-03 19:32:39 +02:00
committed by ErrorNoInternet
parent a0125116cd
commit 1a866719ea
9 changed files with 155 additions and 65 deletions
+81 -39
View File
@@ -1,58 +1,100 @@
{ config, ... }:
{ config, lib, ... }:
let
fqdn = "tuxcord.net";
# fqdn = config.networking.fqdn;
agenixDnsDir = ../../agenix/dns + "/${config.dns.domain}";
agenixKeys = builtins.attrNames (builtins.readDir agenixDnsDir);
zonesub = _: "zonesub";
subdomain = name: "subdomain ${name}";
keys = map (
filename:
let
zonesub = _: "zonesub";
subdomain = name: "subdomain ${name}";
# careful, assumes the fqdn (name) matches the key name content
keys = [
zoneDomain =
if lib.strings.hasSuffix ".key.age" filename then
lib.strings.removeSuffix ".key.age" filename
else
throw "${filename} is not a `.key.age` file";
in
{
name = "tuxcord.net";
path = config.age.secrets.dns-root-key.path;
type = zonesub;
name = zoneDomain;
path = config.age.secrets."dns/${filename}".path;
type = if zoneDomain == config.dns.domain then zonesub else subdomain;
}
];
) agenixKeys;
cfg = config.dns;
inherit (lib)
mkEnableOption
mkOption
mkIf
;
in
{
services.bind = {
enable = true;
options.dns = {
enable = mkEnableOption "" // {
default = true;
};
extraConfig = builtins.concatStringsSep "\n" (map (key: "include \"${key.path}\";") keys);
zones = {
"${fqdn}" = {
# grant "tuxcord.net" zonesub ANY;
extraConfig = ''
update-policy {
${builtins.concatStringsSep "\n" (
map (key: "grant \"${key.name}\" ${key.type key.name} ANY;") keys
)}
};
'';
file = "/var/dns/${fqdn}.zone"; # need to put default stuff
master = true;
};
domain = mkOption {
type = with lib.types; str;
default = config.networking.fqdn;
};
};
environment.persistence."/persist" = {
directories = [
config = mkIf cfg.enable {
age.secrets = builtins.listToAttrs (
map (
filename:
let
path = "${agenixDnsDir}/${filename}";
in
{
name = "dns/${filename}";
value = {
file = path;
group = "named";
owner = "named";
};
}
) agenixKeys
);
services.bind = {
enable = true;
extraConfig = builtins.concatStringsSep "\n" (map (key: "include \"${key.path}\";") keys);
zones = {
"${config.dns.domain}" = {
# grant "tuxcord.net" zonesub ANY;
extraConfig = ''
update-policy {
${builtins.concatStringsSep "\n" (
map (key: "grant \"${key.name}\" ${key.type key.name} ANY;") keys
)}
};
'';
file = "/var/dns/${config.dns.domain}.zone"; # need to put default stuff
master = true;
};
};
};
environment.persistence."/persist".directories = [
{
directory = "/var/dns";
group = "named";
user = "named";
}
];
};
networking.firewall =
let
ports = [ config.services.bind.listenOnPort ];
in
{
allowedTCPPorts = ports;
allowedUDPPorts = ports;
};
networking.firewall =
let
ports = [ config.services.bind.listenOnPort ];
in
{
allowedTCPPorts = ports;
allowedUDPPorts = ports;
};
};
}