Self-host your own digital island
1{ pkgs, config, lib, ... }:
2
3let cfg = config.eilean.services.dns;
4in lib.mkIf (cfg.enable && cfg.server == "eon") {
5 services.eon = {
6 enable = true;
7 application = "capd";
8 capnpAddress = lib.mkDefault config.networking.domain;
9 zoneFiles = let
10 mapZonefile = zonename: zone:
11 "${
12 import ./zonefile.nix { inherit pkgs config lib zonename zone; }
13 }/${zonename}";
14 in lib.attrsets.mapAttrsToList mapZonefile cfg.zones;
15 };
16
17 users.users = { eon.extraGroups = [ config.services.opendkim.group ]; };
18
19 ### bind prestart copy zonefiles
20 systemd.services.eon.postStart = let
21 update = ''
22 update() {
23 local file="$1"
24 local domain="$2"
25 local input=$(tr -d '\n' < "$file")
26 local record_name=$(echo "$input" | ${pkgs.gawk}/bin/awk '{print $1}')
27 local record_type=$(echo "$input" | ${pkgs.gawk}/bin/awk '{print $3}')
28 local ttl=3600
29 local record_value=$(echo "$input" | ${pkgs.gnused}/bin/sed -E 's/[^"]*"([^"]*)"[^"]*/\1/g')
30 ${config.services.eon.package}/bin/capc update /var/lib/eon/caps/domain/''${domain}.cap -u "add|''${record_name}.''${domain}|''${record_type}|''${record_value}|''${ttl}" || exit 0
31 }
32 shopt -s nullglob
33 '';
34 ops = let
35 mapZones = zonename: zone: ''
36 for f in ${config.mailserver.dkimKeyDirectory}/${zonename}.*.txt; do
37 update $f ${zonename}
38 done
39 '';
40 in lib.attrsets.mapAttrsToList mapZones cfg.zones;
41 in update + builtins.concatStringsSep "\n" ops;
42}