dns: abstracted away much of the dns logic into teh dns config file

This commit is contained in:
silver 2023-07-15 15:54:42 +01:00
parent 6412a53070
commit d683598060
4 changed files with 94 additions and 178 deletions

View file

@ -1,4 +1,4 @@
{ lib, pkgs, config, ... }:
{ lib, pkgs, config, nodes, ... }:
let
cfg = config.skynet_dns;
@ -9,7 +9,7 @@ let
get_config_file = (domain:
''$TTL 60 ; 1 minute
; hostmaster@${domain} is an email address that recieves stuff related to dns
@ IN SOA ${cfg.own.nameserver}.${domain}. hostmaster.${domain}. (
@ IN SOA ${nameserver}.${domain}. hostmaster.${domain}. (
; Serial (YYYYMMDDCC) this has to be updated for each time the record is updated
${current_date}
600 ; Refresh (10 minutes)
@ -31,14 +31,7 @@ let
; ------------------------------------------
; Server Names
; ------------------------------------------
; External addresses
; ------------------------------------------
${lib.strings.concatMapStrings (x: x + "\n") cfg.records.external}
; this is fixed for now
wintermute A 193.1.101.148
${lib.strings.concatMapStrings (x: x + "\n") records.external}
; internal addresses
@ -49,7 +42,7 @@ wintermute A 193.1.101.148
; cname's
; ------------------------------------------
${lib.strings.concatMapStrings (x: x + "\n") cfg.records.cname}
${lib.strings.concatMapStrings (x: x + "\n") records.cname}
''
);
@ -61,7 +54,7 @@ ${lib.strings.concatMapStrings (x: x + "\n") cfg.records.cname}
''$ORIGIN 99.1.193.in-addr.arpa.
$TTL 60 ; 1 minute
; hostmaster@skynet.ie is an email address that recieves stuff related to dns
@ IN SOA ${cfg.own.nameserver}.skynet.ie. hostmaster.skynet.ie. (
@ IN SOA ${nameserver}.skynet.ie. hostmaster.skynet.ie. (
; Serial (YYYYMMDDCC) this has to be updated for each time the record is updated
${current_date}
600 ; Refresh (10 minutes)
@ -73,7 +66,7 @@ $TTL 60 ; 1 minute
@ NS ns1.skynet.ie.
@ NS ns2.skynet.ie.
${lib.strings.concatMapStrings (x: x + "\n") cfg.records.reverse}
${lib.strings.concatMapStrings (x: x + "\n") records.reverse}
''
);
@ -81,7 +74,7 @@ ${lib.strings.concatMapStrings (x: x + "\n") cfg.records.reverse}
get_config_file_old_domains = (domain:
''$TTL 60 ; 1 minute
; hostmaster@skynet.ie is an email address that recieves stuff related to dns
@ IN SOA ${cfg.own.nameserver}.skynet.ie. hostmaster.skynet.ie. (
@ IN SOA ${nameserver}.skynet.ie. hostmaster.skynet.ie. (
; Serial (YYYYMMDDCC) this has to be updated for each time the record is updated
${current_date}
600 ; Refresh (10 minutes)
@ -100,18 +93,18 @@ ${lib.strings.concatMapStrings (x: x + "\n") cfg.records.reverse}
tmp1 = ["193.1.99.109"];
tmp2 = ["193.1.99.120"];
primaries = (if cfg.primary then
primaries = (if cfg.server.primary then
# primary servers have no primaries (ones they listen to)
[]
else
if builtins.elem cfg.own.ip tmp1 then
if builtins.elem cfg.server.ip tmp1 then
tmp2
else
tmp1
);
secondaries = (if cfg.primary then
if builtins.elem cfg.own.ip tmp1 then
secondaries = (if cfg.server.primary then
if builtins.elem cfg.server.ip tmp1 then
tmp2
else
tmp1
@ -158,7 +151,7 @@ ${extraConfig}
// ${current_date}
'';
# really wish teh nixos config didnt use master/slave
master = cfg.primary;
master = cfg.server.primary;
masters = primaries;
slaves = secondaries;
# need to write this to a file
@ -176,7 +169,7 @@ ${extraConfig}
extraConfig = {
owned =
if cfg.primary then
if cfg.server.primary then
''
allow-update { key rfc2136key.skynet.ie.; };
@ -192,6 +185,53 @@ inline-signing yes;
old = "";
};
records = {
# using the same logic as the firewall, comments there
external = builtins.concatLists (
lib.attrsets.mapAttrsToList (key: value:
let
details_server = value.config.skynet_dns.server;
details_records = value.config.skynet_dns.records;
in
if builtins.hasAttr "skynet_dns" value.config
then (
if details_server.enable
then (
if details_server.primary
then details_records.external ++ ["ns1 A ${details_server.ip}"]
else details_records.external ++ ["ns2 A ${details_server.ip}"]
)
else details_records.external
)
else []
) nodes
);
cname = builtins.concatLists (
lib.attrsets.mapAttrsToList (key: value:
let
details_records = value.config.skynet_dns.records;
in
if builtins.hasAttr "skynet_dns" value.config
then details_records.cname
else []
) nodes
);
reverse = builtins.concatLists (
lib.attrsets.mapAttrsToList (key: value:
let
details_records = value.config.skynet_dns.records;
in
if builtins.hasAttr "skynet_dns" value.config
then details_records.reverse
else []
) nodes
);
};
nameserver = if cfg.server.primary then "ns1" else "ns2";
in {
imports = [
@ -200,60 +240,26 @@ in {
options = {
skynet_dns = {
enable = lib.mkEnableOption {
default = false;
example = true;
description = "Skynet DNS";
type = lib.types.bool;
};
server = {
enable = lib.mkEnableOption {
default = false;
description = "Skynet DNS server";
type = lib.types.bool;
};
primary = lib.mkOption {
type = lib.types.bool;
default = false;
};
primary = lib.mkOption {
type = lib.types.bool;
default = false;
};
own = {
ip = lib.mkOption {
type = lib.types.str;
description = ''
ip of this server
'';
};
nameserver = lib.mkOption {
default = "ns1";
type = lib.types.str;
description = ''
the hostname of this nameserver, eg ns1, ns2
'';
};
external = lib.mkOption {
default = [ ];
type = lib.types.listOf lib.types.str;
description = ''
External records like: agentjones A 193.1.99.72
'';
};
cname = lib.mkOption {
default = [ ];
type = lib.types.listOf lib.types.str;
description = ''
External records like: ns1 CNAME ns1
'';
};
reverse = lib.mkOption {
default = [ ];
type = lib.types.listOf lib.types.str;
description = ''
External records like: 20 IN PTR vigil
'';
};
};
records = {
external = lib.mkOption {
default = [ ];
@ -283,12 +289,12 @@ in {
};
};
config = lib.mkIf cfg.enable {
config = lib.mkIf cfg.server.enable {
# open the firewall for this
skynet_firewall.forward = [
"ip daddr ${cfg.own.ip} tcp dport 53 counter packets 0 bytes 0 accept"
"ip daddr ${cfg.own.ip} udp dport 53 counter packets 0 bytes 0 accept"
"ip daddr ${cfg.server.ip} tcp dport 53 counter packets 0 bytes 0 accept"
"ip daddr ${cfg.server.ip} udp dport 53 counter packets 0 bytes 0 accept"
];
services.bind.zones =