2017-01-25 18:41:52 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.kresd;
|
2020-01-23 20:19:14 +01:00
|
|
|
configFile = pkgs.writeText "kresd.conf" ''
|
|
|
|
${optionalString (cfg.listenDoH != []) "modules.load('http')"}
|
|
|
|
${cfg.extraConfig};
|
|
|
|
'';
|
2017-01-25 18:41:52 +01:00
|
|
|
|
2020-01-23 20:19:14 +01:00
|
|
|
package = pkgs.knot-resolver.override {
|
|
|
|
extraFeatures = cfg.listenDoH != [];
|
|
|
|
};
|
|
|
|
in {
|
2017-01-25 18:41:52 +01:00
|
|
|
meta.maintainers = [ maintainers.vcunat /* upstream developer */ ];
|
|
|
|
|
2019-12-16 23:06:27 +01:00
|
|
|
imports = [
|
|
|
|
(mkChangedOptionModule [ "services" "kresd" "interfaces" ] [ "services" "kresd" "listenPlain" ]
|
|
|
|
(config:
|
|
|
|
let value = getAttrFromPath [ "services" "kresd" "interfaces" ] config;
|
|
|
|
in map
|
|
|
|
(iface: if elem ":" (stringToCharacters iface) then "[${iface}]:53" else "${iface}:53") # Syntax depends on being IPv6 or IPv4.
|
|
|
|
value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
2017-01-25 18:41:52 +01:00
|
|
|
###### interface
|
|
|
|
options.services.kresd = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable knot-resolver domain name server.
|
|
|
|
DNSSEC validation is turned on by default.
|
|
|
|
You can run <literal>sudo nc -U /run/kresd/control</literal>
|
|
|
|
and give commands interactively to kresd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra lines to be added verbatim to the generated configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
cacheDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/cache/kresd";
|
|
|
|
description = ''
|
|
|
|
Directory for caches. They are intended to survive reboots.
|
|
|
|
'';
|
|
|
|
};
|
2019-12-16 23:06:27 +01:00
|
|
|
listenPlain = mkOption {
|
2017-01-25 18:41:52 +01:00
|
|
|
type = with types; listOf str;
|
2019-12-16 23:06:27 +01:00
|
|
|
default = [ "[::1]:53" "127.0.0.1:53" ];
|
2017-01-25 18:41:52 +01:00
|
|
|
description = ''
|
2019-12-16 23:06:27 +01:00
|
|
|
What addresses and ports the server should listen on.
|
|
|
|
For detailed syntax see ListenStream in man systemd.socket.
|
2017-01-25 18:41:52 +01:00
|
|
|
'';
|
|
|
|
};
|
2018-02-12 20:48:25 +01:00
|
|
|
listenTLS = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "198.51.100.1:853" "[2001:db8::1]:853" "853" ];
|
|
|
|
description = ''
|
2019-12-16 23:06:27 +01:00
|
|
|
Addresses and ports on which kresd should provide DNS over TLS (see RFC 7858).
|
2018-02-12 20:48:25 +01:00
|
|
|
For detailed syntax see ListenStream in man systemd.socket.
|
|
|
|
'';
|
|
|
|
};
|
2020-01-23 20:19:14 +01:00
|
|
|
listenDoH = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "198.51.100.1:443" "[2001:db8::1]:443" "443" ];
|
|
|
|
description = ''
|
2020-01-27 20:50:38 +01:00
|
|
|
Addresses and ports on which kresd should provide DNS over HTTPS (see RFC 8484).
|
2020-01-23 20:19:14 +01:00
|
|
|
For detailed syntax see ListenStream in man systemd.socket.
|
|
|
|
'';
|
|
|
|
};
|
2017-01-25 18:41:52 +01:00
|
|
|
# TODO: perhaps options for more common stuff like cache size or forwarding
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
environment.etc."kresd.conf".source = configFile; # not required
|
|
|
|
|
2019-09-14 19:51:29 +02:00
|
|
|
users.users.kresd =
|
|
|
|
{ uid = config.ids.uids.kresd;
|
2017-01-25 18:41:52 +01:00
|
|
|
group = "kresd";
|
|
|
|
description = "Knot-resolver daemon user";
|
|
|
|
};
|
2019-09-14 19:51:29 +02:00
|
|
|
users.groups.kresd.gid = config.ids.gids.kresd;
|
2017-01-25 18:41:52 +01:00
|
|
|
|
|
|
|
systemd.sockets.kresd = rec {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
before = wantedBy;
|
2019-12-16 23:06:27 +01:00
|
|
|
listenStreams = cfg.listenPlain;
|
2019-05-19 14:05:54 +02:00
|
|
|
socketConfig = {
|
|
|
|
ListenDatagram = listenStreams;
|
|
|
|
FreeBind = true;
|
|
|
|
FileDescriptorName = "dns";
|
|
|
|
};
|
2017-01-25 18:41:52 +01:00
|
|
|
};
|
|
|
|
|
2018-02-12 20:48:25 +01:00
|
|
|
systemd.sockets.kresd-tls = mkIf (cfg.listenTLS != []) rec {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
before = wantedBy;
|
|
|
|
partOf = [ "kresd.socket" ];
|
|
|
|
listenStreams = cfg.listenTLS;
|
|
|
|
socketConfig = {
|
|
|
|
FileDescriptorName = "tls";
|
|
|
|
FreeBind = true;
|
|
|
|
Service = "kresd.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-01-23 20:19:14 +01:00
|
|
|
systemd.sockets.kresd-doh = mkIf (cfg.listenDoH != []) rec {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
before = wantedBy;
|
|
|
|
partOf = [ "kresd.socket" ];
|
|
|
|
listenStreams = cfg.listenDoH;
|
|
|
|
socketConfig = {
|
|
|
|
FileDescriptorName = "doh";
|
|
|
|
FreeBind = true;
|
|
|
|
Service = "kresd.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-01-25 18:41:52 +01:00
|
|
|
systemd.sockets.kresd-control = rec {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
before = wantedBy;
|
|
|
|
partOf = [ "kresd.socket" ];
|
|
|
|
listenStreams = [ "/run/kresd/control" ];
|
|
|
|
socketConfig = {
|
|
|
|
FileDescriptorName = "control";
|
|
|
|
Service = "kresd.service";
|
2018-01-09 17:19:13 +01:00
|
|
|
SocketMode = "0660"; # only root user/group may connect and control kresd
|
2017-01-25 18:41:52 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-01-09 17:11:36 +01:00
|
|
|
systemd.tmpfiles.rules = [ "d '${cfg.cacheDir}' 0770 kresd kresd - -" ];
|
2017-01-25 18:41:52 +01:00
|
|
|
|
|
|
|
systemd.services.kresd = {
|
|
|
|
description = "Knot-resolver daemon";
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = "kresd";
|
|
|
|
Type = "notify";
|
|
|
|
WorkingDirectory = cfg.cacheDir;
|
2018-01-09 17:19:13 +01:00
|
|
|
Restart = "on-failure";
|
2018-02-12 20:48:25 +01:00
|
|
|
Sockets = [ "kresd.socket" "kresd-control.socket" ]
|
|
|
|
++ optional (cfg.listenTLS != []) "kresd-tls.socket";
|
2017-01-25 18:41:52 +01:00
|
|
|
};
|
|
|
|
|
2018-02-01 17:15:28 +01:00
|
|
|
# Trust anchor goes from dns-root-data by default.
|
2017-01-25 18:41:52 +01:00
|
|
|
script = ''
|
2018-02-01 17:15:28 +01:00
|
|
|
exec '${package}/bin/kresd' --config '${configFile}' --forks=1
|
2017-01-25 18:41:52 +01:00
|
|
|
'';
|
|
|
|
|
2018-01-09 17:11:36 +01:00
|
|
|
requires = [ "kresd.socket" ];
|
2017-01-25 18:41:52 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|