Skip to content

Commit cc0964e

Browse files
committed
host nameserver on NixOS
1 parent 6669853 commit cc0964e

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

flake.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
ref = "master";
7878
};
7979

80+
ocf-dns = {
81+
url = "github:ocf/dns";
82+
flake = false;
83+
};
84+
8085
ocf-sync-etc = {
8186
type = "github";
8287
owner = "ocf";
@@ -143,6 +148,7 @@
143148
disko,
144149
nix-index-database,
145150
ocflib,
151+
ocf-dns,
146152
ocf-sync-etc,
147153
ocf-pam-trimspaces,
148154
ocf-utils,

modules/nameserver.nix

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
config,
3+
inputs,
4+
lib,
5+
pkgs,
6+
...
7+
}:
8+
9+
let
10+
cfg = config.ocf.nameserver;
11+
12+
build-zones = pkgs.stdenv.mkDerivation {
13+
name = "build-zones";
14+
src = inputs.ocf-dns;
15+
buildInputs = [
16+
(config.ocf.python.package.withPackages (ps: [
17+
ps.ldap3
18+
ps.ocflib
19+
]))
20+
];
21+
installPhase = ''
22+
install -Dt "$out/bin" build-zones check-zones
23+
'';
24+
meta.mainProgram = "build-zones";
25+
};
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+
tmp="$(mktemp -d)"
49+
cp -r ${inputs.ocf-dns} "$tmp"
50+
cd "$tmp"
51+
build-zones
52+
check-zones
53+
cp -r etc /run/dns/
54+
'';
55+
serviceConfig = {
56+
DynamicUser = true;
57+
ExecStartPost = [
58+
# + runs the command as root
59+
"+${lib.getExe' pkgs.coreutils "mkdir"} -p /srv/dns/"
60+
# cannot use atomic `exch` for the next two operations because /run/dns is on a different fs (bind mount)
61+
"+${lib.getExe' pkgs.coreutils "rm"} -rf /srv/dns/etc"
62+
"+${lib.getExe' pkgs.coreutils "mv"} /run/dns/etc /srv/dns/"
63+
"+${lib.getExe' pkgs.systemd "systemctl"} reload bind.service"
64+
];
65+
RuntimeDirectory = "dns";
66+
Type = "oneshot";
67+
};
68+
wantedBy = [ "multi-user.target" ];
69+
wants = [ "network-online.target" ];
70+
};
71+
72+
systemd.timers.rebuild-dns-from-ldap = {
73+
timerConfig.OnCalendar = "hourly";
74+
wantedBy = [ "timers.target" ];
75+
};
76+
};
77+
}

0 commit comments

Comments
 (0)