Skip to content

Commit 4c277e0

Browse files
committed
host nameserver on NixOS
1 parent 6669853 commit 4c277e0

3 files changed

Lines changed: 99 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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)