@@ -3,12 +3,34 @@
33with lib;
44
55let
6−
76 cfg = config.services.kresd;
8− configFile = pkgs.writeText "kresd.conf" ''
9− ${optionalString (cfg.listenDoH != []) "modules.load('http')"}
10− ${cfg.extraConfig};
11− '';
7+
8+ # Convert systemd-style address specification to kresd config line(s).
9+ # On Nix level we don't attempt to precisely validate the address specifications.
10+ mkListen = kind: addr: let
11+ al_v4 = builtins.match "([0-9.]\+):([0-9]\+)" addr;
12+ al_v6 = builtins.match "\\[(.\+)]:([0-9]\+)" addr;
13+ al_portOnly = builtins.match "()([0-9]\+)" addr;
14+ al = findFirst (a: a != null)
15+ (throw "services.kresd.*: incorrect address specification '${addr}'")
16+ [ al_v4 al_v6 al_portOnly ];
17+ port = last al;
18+ addrSpec = if al_portOnly == null then "'${head al}'" else "{'::', '127.0.0.1'}";
19+ in # freebind is set for compatibility with earlier kresd services;
20+ # it could be configurable, for example.
21+ ''
22+ net.listen(${addrSpec}, ${port}, { kind = '${kind}', freebind = true })
23+ '';
24+
25+ configFile = pkgs.writeText "kresd.conf" (
26+ optionalString (cfg.listenDoH != []) ''
27+ modules.load('http')
28+ ''
29+ + concatMapStrings (mkListen "dns") cfg.listenPlain
30+ + concatMapStrings (mkListen "tls") cfg.listenTLS
31+ + concatMapStrings (mkListen "doh") cfg.listenDoH
32+ + cfg.extraConfig
33+ );
1234
1335 package = pkgs.knot-resolver.override {
1436 extraFeatures = cfg.listenDoH != [];
@@ -25,6 +47,7 @@ in {
2547 value
2648 )
2749 )
50+ (mkRemovedOptionModule [ "services" "kresd" "cacheDir" ] "Please use (bind-)mounting instead.")
2851 ];
2952
3053 ###### interface
@@ -35,8 +58,8 @@ in {
3558 description = ''
3659 Whether to enable knot-resolver domain name server.
3760 DNSSEC validation is turned on by default.
38− You can run <literal>sudo nc -U /run/kresd/control</literal>
39− and give commands interactively to kresd.
61+ You can run <literal>sudo nc -U /run/knot-resolver/control/1</literal>
62+ and give commands interactively to kresd@1.service.
4063 '';
4164 };
4265 extraConfig = mkOption {
@@ -46,16 +69,10 @@ in {
4669 Extra lines to be added verbatim to the generated configuration file.
4770 '';
4871 };
49− cacheDir = mkOption {
50− type = types.path;
51− default = "/var/cache/kresd";
52− description = ''
53− Directory for caches. They are intended to survive reboots.
54− '';
55− };
5672 listenPlain = mkOption {
5773 type = with types; listOf str;
5874 default = [ "[::1]:53" "127.0.0.1:53" ];
75+ example = [ "53" ];
5976 description = ''
6077 What addresses and ports the server should listen on.
6178 For detailed syntax see ListenStream in man systemd.socket.
@@ -75,91 +92,54 @@ in {
7592 default = [];
7693 example = [ "198.51.100.1:443" "[2001:db8::1]:443" "443" ];
7794 description = ''
78− Addresses and ports on which kresd should provide DNS over HTTPS (see RFC 7858).
95+ Addresses and ports on which kresd should provide DNS over HTTPS (see RFC 8484).
7996 For detailed syntax see ListenStream in man systemd.socket.
8097 '';
8198 };
99+ instances = mkOption {
100+ type = types.ints.unsigned;
101+ default = 1;
102+ description = ''
103+ The number of instances to start. They will be called kresd@{1,2,...}.service.
104+ Knot Resolver uses no threads, so this is the way to scale.
105+ You can dynamically start/stop them at will, so this is just system default.
106+ '';
107+ };
82108 # TODO: perhaps options for more common stuff like cache size or forwarding
83109 };
84110
85111 ###### implementation
86112 config = mkIf cfg.enable {
87− environment.etc."kresd.conf".source = configFile; # not required
113+ environment.etc."knot-resolver/kresd.conf".source = configFile; # not required
88114
89− users.users.kresd =
90− { uid = config.ids.uids.kresd;
91− group = "kresd";
115+ users.users.knot-resolver =
116+ { isSystemUser = true;
117+ group = "knot-resolver";
92118 description = "Knot-resolver daemon user";
93119 };
94− users.groups.kresd.gid = config.ids.gids.kresd;
95−
96− systemd.sockets.kresd = rec {
97− wantedBy = [ "sockets.target" ];
98− before = wantedBy;
99− listenStreams = cfg.listenPlain;
100− socketConfig = {
101− ListenDatagram = listenStreams;
102− FreeBind = true;
103− FileDescriptorName = "dns";
104− };
105− };
120+ users.groups.knot-resolver.gid = null;
106121
107− systemd.sockets.kresd-tls = mkIf (cfg.listenTLS != []) rec {
108− wantedBy = [ "sockets.target" ];
109− before = wantedBy;
110− partOf = [ "kresd.socket" ];
111− listenStreams = cfg.listenTLS;
112− socketConfig = {
113− FileDescriptorName = "tls";
114− FreeBind = true;
115− Service = "kresd.service";
116− };
117− };
122+ systemd.packages = [ package ]; # the units are patched inside the package a bit
118123
119− systemd.sockets.kresd-doh = mkIf (cfg.listenDoH != []) rec {
120− wantedBy = [ "sockets.target" ];
121− before = wantedBy;
122− partOf = [ "kresd.socket" ];
123− listenStreams = cfg.listenDoH;
124− socketConfig = {
125− FileDescriptorName = "doh";
126− FreeBind = true;
127− Service = "kresd.service";
128− };
124+ systemd.targets.kresd = { # configure units started by default
125+ wantedBy = [ "multi-user.target" ];
126+ wants = [ "kres-cache-gc.service" ]
127+ ++ map (i: "kresd@${toString i}.service") (range 1 cfg.instances);
129128 };
130−
131− systemd.sockets.kresd-control = rec {
132− wantedBy = [ "sockets.target" ];
133− before = wantedBy;
134− partOf = [ "kresd.socket" ];
135− listenStreams = [ "/run/kresd/control" ];
136− socketConfig = {
137− FileDescriptorName = "control";
138− Service = "kresd.service";
139− SocketMode = "0660"; # only root user/group may connect and control kresd
140− };
129+ systemd.services."kresd@".serviceConfig = {
130+ ExecStart = "${package}/bin/kresd --noninteractive "
131+ + "-c ${package}/lib/knot-resolver/distro-preconfig.lua -c ${configFile}";
132+ # Ensure correct ownership in case UID or GID changes.
133+ CacheDirectory = "knot-resolver";
134+ CacheDirectoryMode = "0750";
141135 };
142136
143− systemd.tmpfiles.rules = [ "d '${cfg.cacheDir}' 0770 kresd kresd - -" ];
137+ environment.etc."tmpfiles.d/knot-resolver.conf".source =
138+ "${package}/lib/tmpfiles.d/knot-resolver.conf";
144139
145− systemd.services.kresd = {
146− description = "Knot-resolver daemon";
147−
148− serviceConfig = {
149− User = "kresd";
150− Type = "notify";
151− WorkingDirectory = cfg.cacheDir;
152− Restart = "on-failure";
153− Sockets = [ "kresd.socket" "kresd-control.socket" ]
154− ++ optional (cfg.listenTLS != []) "kresd-tls.socket";
155− };
156−
157− # Trust anchor goes from dns-root-data by default.
158− script = ''
159− exec '${package}/bin/kresd' --config '${configFile}' --forks=1
160− '';
161−
162− requires = [ "kresd.socket" ];
163− };
140+ # Try cleaning up the previously default location of cache file.
141+ # Note that /var/cache/* should always be safe to remove.
142+ # TODO: remove later, probably between 20.09 and 21.03
143+ systemd.tmpfiles.rules = [ "R /var/cache/kresd" ];
164144 };
165145}