|
| 1 | +{ |
| 2 | + config, |
| 3 | + inputs, |
| 4 | + lib, |
| 5 | + pkgs, |
| 6 | + ... |
| 7 | +}: |
| 8 | + |
| 9 | +let |
| 10 | + build-zones = pkgs.stdenv.mkDerivation { |
| 11 | + name = "build-zones"; |
| 12 | + src = inputs.ocf-dns; |
| 13 | + buildInputs = [ |
| 14 | + (config.ocf.python.package.withPackages (ps: [ |
| 15 | + ps.ldap3 |
| 16 | + ps.ocflib |
| 17 | + ])) |
| 18 | + ]; |
| 19 | + installPhase = '' |
| 20 | + install -Dt "$out/bin" build-zones check-zones |
| 21 | + ''; |
| 22 | + meta.mainProgram = "build-zones"; |
| 23 | + }; |
| 24 | + |
| 25 | + cfg = config.ocf.nameserver; |
| 26 | +in |
| 27 | +{ |
| 28 | + options.ocf.nameserver = { |
| 29 | + enable = lib.mkEnableOption "name server"; |
| 30 | + }; |
| 31 | + |
| 32 | + config = lib.mkIf cfg.enable { |
| 33 | + services.bind = { |
| 34 | + enable = true; |
| 35 | + configFile = "/srv/dns/etc/named.conf.local"; |
| 36 | + }; |
| 37 | + systemd.services.bind = { |
| 38 | + after = [ "rebuild-dns-from-ldap.service" ]; |
| 39 | + }; |
| 40 | + |
| 41 | + systemd.services.rebuild-dns-from-ldap = { |
| 42 | + after = [ "network-online.target" ]; |
| 43 | + path = [ |
| 44 | + build-zones |
| 45 | + pkgs.bind |
| 46 | + ]; |
| 47 | + script = '' |
| 48 | + cd "$(mktemp -d)" |
| 49 | + cp -r ${inputs.ocf-dns}/. . |
| 50 | + build-zones |
| 51 | + check-zones |
| 52 | + cp -r etc /run/dns/ |
| 53 | + ''; |
| 54 | + serviceConfig = { |
| 55 | + DynamicUser = true; |
| 56 | + ExecStartPost = [ |
| 57 | + # + runs the command as root |
| 58 | + "+${lib.getExe' pkgs.coreutils "mkdir"} -p /srv/dns/" |
| 59 | + # cannot use atomic `exch` for the next two operations because /run/dns is on a different fs (bind mount) |
| 60 | + "+${lib.getExe' pkgs.coreutils "rm"} -rf /srv/dns/etc" |
| 61 | + "+${lib.getExe' pkgs.coreutils "mv"} /run/dns/etc /srv/dns/" |
| 62 | + "+${lib.getExe' pkgs.systemd "systemctl"} reload bind.service" |
| 63 | + ]; |
| 64 | + RuntimeDirectory = "dns"; |
| 65 | + Type = "oneshot"; |
| 66 | + }; |
| 67 | + wantedBy = [ "multi-user.target" ]; |
| 68 | + wants = [ "network-online.target" ]; |
| 69 | + }; |
| 70 | + |
| 71 | + systemd.timers.rebuild-dns-from-ldap = { |
| 72 | + timerConfig.OnCalendar = "hourly"; |
| 73 | + wantedBy = [ "timers.target" ]; |
| 74 | + }; |
| 75 | + }; |
| 76 | +} |
0 commit comments