2014-11-11 19:12:28 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.dnscrypt-proxy;
|
2017-03-09 16:54:29 +00:00
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
stateDirectory = "/var/lib/dnscrypt-proxy";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
# The minisign public key used to sign the upstream resolver list.
|
|
|
|
# This is somewhat more flexible than preloading the key as an
|
|
|
|
# embedded string.
|
|
|
|
upstreamResolverListPubKey = pkgs.fetchurl {
|
|
|
|
url = https://raw.githubusercontent.com/jedisct1/dnscrypt-proxy/master/minisign.pub;
|
|
|
|
sha256 = "18lnp8qr6ghfc2sd46nn1rhcpr324fqlvgsp4zaigw396cd7vnnh";
|
|
|
|
};
|
|
|
|
|
2017-03-10 14:36:13 +00:00
|
|
|
# Internal flag indicating whether the upstream resolver list is used.
|
2017-03-10 13:20:30 +00:00
|
|
|
useUpstreamResolverList = cfg.customResolver == null;
|
2016-10-23 23:34:03 +01:00
|
|
|
|
2017-03-10 14:36:13 +00:00
|
|
|
# The final local address.
|
|
|
|
localAddress = "${cfg.localAddress}:${toString cfg.localPort}";
|
|
|
|
|
|
|
|
# The final resolvers list path.
|
2017-03-10 13:20:30 +00:00
|
|
|
resolverList = "${stateDirectory}/dnscrypt-resolvers.csv";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2017-03-10 14:36:13 +00:00
|
|
|
# Build daemon command line
|
|
|
|
|
|
|
|
resolverArgs =
|
|
|
|
if (cfg.customResolver == null)
|
|
|
|
then
|
|
|
|
[ "-L ${resolverList}"
|
|
|
|
"-R ${cfg.resolverName}"
|
|
|
|
]
|
|
|
|
else with cfg.customResolver;
|
|
|
|
[ "-N ${name}"
|
|
|
|
"-k ${key}"
|
|
|
|
"-r ${address}:${toString port}"
|
|
|
|
];
|
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
daemonArgs =
|
2017-03-10 14:36:13 +00:00
|
|
|
[ "-a ${localAddress}" ]
|
|
|
|
++ resolverArgs
|
|
|
|
++ cfg.extraArgs;
|
2014-11-11 19:12:28 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2016-08-29 17:10:00 +01:00
|
|
|
meta = {
|
|
|
|
maintainers = with maintainers; [ joachifm ];
|
|
|
|
doc = ./dnscrypt-proxy.xml;
|
|
|
|
};
|
|
|
|
|
2014-11-11 19:12:28 +00:00
|
|
|
options = {
|
2017-03-10 14:36:13 +00:00
|
|
|
# Before adding another option, consider whether it could
|
|
|
|
# equally well be passed via extraArgs.
|
|
|
|
|
2014-11-11 19:12:28 +00:00
|
|
|
services.dnscrypt-proxy = {
|
2016-10-23 22:26:21 +01:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "Whether to enable the DNSCrypt client proxy";
|
|
|
|
};
|
2016-03-17 13:14:46 +00:00
|
|
|
|
2014-11-11 19:12:28 +00:00
|
|
|
localAddress = mkOption {
|
|
|
|
default = "127.0.0.1";
|
2016-08-01 11:55:42 +01:00
|
|
|
type = types.str;
|
2014-11-11 19:12:28 +00:00
|
|
|
description = ''
|
2016-03-17 13:14:46 +00:00
|
|
|
Listen for DNS queries to relay on this address. The only reason to
|
|
|
|
change this from its default value is to proxy queries on behalf
|
|
|
|
of other machines (typically on the local network).
|
2014-11-11 19:12:28 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2015-04-14 22:11:28 +01:00
|
|
|
localPort = mkOption {
|
2014-11-11 19:12:28 +00:00
|
|
|
default = 53;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
2016-03-17 13:14:46 +00:00
|
|
|
Listen for DNS queries to relay on this port. The default value
|
|
|
|
assumes that the DNSCrypt proxy should relay DNS queries directly.
|
|
|
|
When running as a forwarder for another DNS client, set this option
|
|
|
|
to a different value; otherwise leave the default.
|
2014-11-11 19:12:28 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2014-11-11 19:12:28 +00:00
|
|
|
resolverName = mkOption {
|
2016-03-24 14:14:10 +00:00
|
|
|
default = "dnscrypt.eu-nl";
|
2016-08-01 11:55:42 +01:00
|
|
|
type = types.nullOr types.str;
|
2014-11-11 19:12:28 +00:00
|
|
|
description = ''
|
2017-03-10 13:20:30 +00:00
|
|
|
The name of the DNSCrypt resolver to use, taken from
|
|
|
|
<filename>${resolverList}</filename>. The default
|
|
|
|
resolver is located in Holland, supports DNS security
|
|
|
|
extensions, and <emphasis>claims</emphasis> to not
|
|
|
|
keep logs.
|
2016-05-10 06:04:20 +01:00
|
|
|
'';
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2015-04-14 22:11:28 +01:00
|
|
|
customResolver = mkOption {
|
|
|
|
default = null;
|
|
|
|
description = ''
|
2016-03-17 13:14:46 +00:00
|
|
|
Use an unlisted resolver (e.g., a private DNSCrypt provider). For
|
|
|
|
advanced users only. If specified, this option takes precedence.
|
2015-04-14 22:11:28 +01:00
|
|
|
'';
|
|
|
|
type = types.nullOr (types.submodule ({ ... }: { options = {
|
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
2016-08-29 17:19:18 +01:00
|
|
|
description = "IP address";
|
2015-04-14 22:11:28 +01:00
|
|
|
example = "208.67.220.220";
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2015-04-14 22:11:28 +01:00
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
2016-08-29 17:19:18 +01:00
|
|
|
description = "Port";
|
2015-04-14 22:11:28 +01:00
|
|
|
default = 443;
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2015-04-14 22:11:28 +01:00
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
2016-08-29 17:19:18 +01:00
|
|
|
description = "Fully qualified domain name";
|
2017-03-10 13:59:59 +00:00
|
|
|
example = "2.dnscrypt-cert.example.com";
|
2016-03-17 13:14:46 +00:00
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2016-03-17 13:14:46 +00:00
|
|
|
key = mkOption {
|
|
|
|
type = types.str;
|
2016-08-29 17:19:18 +01:00
|
|
|
description = "Public key";
|
2016-03-17 13:14:46 +00:00
|
|
|
example = "B735:1140:206F:225D:3E2B:D822:D7FD:691E:A1C3:3CC8:D666:8D0C:BE04:BFAB:CA43:FB79";
|
|
|
|
};
|
|
|
|
}; }));
|
2015-04-14 22:11:28 +01:00
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2017-03-10 14:36:13 +00:00
|
|
|
extraArgs = mkOption {
|
|
|
|
default = [];
|
|
|
|
type = types.listOf types.str;
|
2016-03-17 13:34:56 +00:00
|
|
|
description = ''
|
2017-03-10 14:36:13 +00:00
|
|
|
Additional command-line arguments passed verbatim to the daemon.
|
|
|
|
See <citerefentry><refentrytitle>dnscrypt-proxy</refentrytitle>
|
|
|
|
<manvolnum>8</manvolnum></citerefentry> for details.
|
2014-11-11 19:12:28 +00:00
|
|
|
'';
|
2017-03-10 17:45:11 +00:00
|
|
|
example = [ "-X libdcplugin_example_cache.so,--min-ttl=60" ];
|
2014-11-11 19:12:28 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-07 22:40:31 +00:00
|
|
|
config = mkIf cfg.enable (mkMerge [{
|
2015-04-14 22:11:28 +01:00
|
|
|
assertions = [
|
|
|
|
{ assertion = (cfg.customResolver != null) || (cfg.resolverName != null);
|
|
|
|
message = "please configure upstream DNSCrypt resolver";
|
|
|
|
}
|
|
|
|
];
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2017-03-07 22:40:31 +00:00
|
|
|
users.users.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy daemon user";
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "dnscrypt-proxy";
|
|
|
|
};
|
|
|
|
users.groups.dnscrypt-proxy = {};
|
|
|
|
|
|
|
|
systemd.sockets.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy listening socket";
|
|
|
|
documentation = [ "man:dnscrypt-proxy(8)" ];
|
|
|
|
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
|
|
|
|
socketConfig = {
|
|
|
|
ListenStream = localAddress;
|
|
|
|
ListenDatagram = localAddress;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy daemon";
|
|
|
|
documentation = [ "man:dnscrypt-proxy(8)" ];
|
|
|
|
|
|
|
|
before = [ "nss-lookup.target" ];
|
2017-03-12 15:20:10 +00:00
|
|
|
after = [ "network.target" ];
|
|
|
|
requires = [ "dnscrypt-proxy.socket "];
|
2017-03-07 22:40:31 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
NonBlocking = "true";
|
2017-03-09 16:54:29 +00:00
|
|
|
ExecStart = "${pkgs.dnscrypt-proxy}/bin/dnscrypt-proxy ${toString daemonArgs}";
|
2017-03-13 19:04:42 +00:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2017-03-07 22:40:31 +00:00
|
|
|
|
|
|
|
User = "dnscrypt-proxy";
|
|
|
|
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:20:10 +00:00
|
|
|
(mkIf config.security.apparmor.enable {
|
|
|
|
systemd.services.dnscrypt-proxy.after = [ "apparmor.service" ];
|
|
|
|
|
2017-03-07 22:40:31 +00:00
|
|
|
security.apparmor.profiles = singleton (pkgs.writeText "apparmor-dnscrypt-proxy" ''
|
2017-03-09 16:54:29 +00:00
|
|
|
${pkgs.dnscrypt-proxy}/bin/dnscrypt-proxy {
|
2015-03-15 21:22:45 +00:00
|
|
|
/dev/null rw,
|
|
|
|
/dev/urandom r,
|
2015-01-09 12:57:04 +00:00
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
/etc/passwd r,
|
|
|
|
/etc/group r,
|
|
|
|
${config.environment.etc."nsswitch.conf".source} r,
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2016-05-15 10:31:53 +01:00
|
|
|
${getLib pkgs.glibc}/lib/*.so mr,
|
2015-03-15 21:22:45 +00:00
|
|
|
${pkgs.tzdata}/share/zoneinfo/** r,
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
network inet stream,
|
|
|
|
network inet6 stream,
|
|
|
|
network inet dgram,
|
|
|
|
network inet6 dgram,
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2017-03-10 14:08:59 +00:00
|
|
|
${getLib pkgs.dnscrypt-proxy}/lib/dnscrypt-proxy/libdcplugin*.so mr,
|
|
|
|
|
2016-05-15 10:31:53 +01:00
|
|
|
${getLib pkgs.gcc.cc}/lib/libssp.so.* mr,
|
|
|
|
${getLib pkgs.libsodium}/lib/libsodium.so.* mr,
|
|
|
|
${getLib pkgs.systemd}/lib/libsystemd.so.* mr,
|
|
|
|
${getLib pkgs.xz}/lib/liblzma.so.* mr,
|
|
|
|
${getLib pkgs.libgcrypt}/lib/libgcrypt.so.* mr,
|
|
|
|
${getLib pkgs.libgpgerror}/lib/libgpg-error.so.* mr,
|
|
|
|
${getLib pkgs.libcap}/lib/libcap.so.* mr,
|
|
|
|
${getLib pkgs.lz4}/lib/liblz4.so.* mr,
|
2017-03-07 22:40:31 +00:00
|
|
|
${getLib pkgs.attr}/lib/libattr.so.* mr, # */
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
${resolverList} r,
|
2017-03-24 13:36:54 +00:00
|
|
|
|
|
|
|
/run/systemd/notify rw,
|
2015-03-15 21:22:45 +00:00
|
|
|
}
|
2016-10-23 23:34:03 +01:00
|
|
|
'');
|
2017-03-07 22:40:31 +00:00
|
|
|
})
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2017-03-07 22:40:31 +00:00
|
|
|
(mkIf useUpstreamResolverList {
|
|
|
|
systemd.services.init-dnscrypt-proxy-statedir = {
|
2016-10-23 23:34:03 +01:00
|
|
|
description = "Initialize dnscrypt-proxy state directory";
|
2017-03-07 22:40:31 +00:00
|
|
|
|
|
|
|
wantedBy = [ "dnscrypt-proxy.service" ];
|
|
|
|
before = [ "dnscrypt-proxy.service" ];
|
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
script = ''
|
|
|
|
mkdir -pv ${stateDirectory}
|
|
|
|
chown -c dnscrypt-proxy:dnscrypt-proxy ${stateDirectory}
|
2017-03-07 22:40:31 +00:00
|
|
|
cp -uv \
|
2016-10-23 23:34:03 +01:00
|
|
|
${pkgs.dnscrypt-proxy}/share/dnscrypt-proxy/dnscrypt-resolvers.csv \
|
|
|
|
${stateDirectory}
|
|
|
|
'';
|
2017-03-07 22:40:31 +00:00
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-07 22:40:31 +00:00
|
|
|
systemd.services.update-dnscrypt-resolvers = {
|
2016-10-23 23:34:03 +01:00
|
|
|
description = "Update list of DNSCrypt resolvers";
|
|
|
|
|
|
|
|
requires = [ "init-dnscrypt-proxy-statedir.service" ];
|
|
|
|
after = [ "init-dnscrypt-proxy-statedir.service" ];
|
|
|
|
|
2017-03-08 02:09:22 +00:00
|
|
|
path = with pkgs; [ curl diffutils dnscrypt-proxy minisign ];
|
2016-10-23 23:34:03 +01:00
|
|
|
script = ''
|
|
|
|
cd ${stateDirectory}
|
2017-03-14 23:29:49 +00:00
|
|
|
domain=raw.githubusercontent.com
|
2017-03-07 22:17:01 +00:00
|
|
|
get="curl -fSs --resolve $domain:443:$(hostip -r 8.8.8.8 $domain | head -1)"
|
|
|
|
$get -o dnscrypt-resolvers.csv.tmp \
|
2017-03-14 23:29:49 +00:00
|
|
|
https://$domain/jedisct1/dnscrypt-proxy/master/dnscrypt-resolvers.csv
|
2017-03-07 22:17:01 +00:00
|
|
|
$get -o dnscrypt-resolvers.csv.minisig.tmp \
|
2017-03-14 23:29:49 +00:00
|
|
|
https://$domain/jedisct1/dnscrypt-proxy/master/dnscrypt-resolvers.csv.minisig
|
2016-10-23 23:34:03 +01:00
|
|
|
mv dnscrypt-resolvers.csv.minisig{.tmp,}
|
2017-03-14 23:52:20 +00:00
|
|
|
if ! minisign -q -V -p ${upstreamResolverListPubKey} \
|
|
|
|
-m dnscrypt-resolvers.csv.tmp -x dnscrypt-resolvers.csv.minisig ; then
|
|
|
|
echo "failed to verify resolver list!" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2017-03-08 02:09:22 +00:00
|
|
|
[[ -f dnscrypt-resolvers.csv ]] && mv dnscrypt-resolvers.csv{,.old}
|
2016-10-23 23:34:03 +01:00
|
|
|
mv dnscrypt-resolvers.csv{.tmp,}
|
2017-03-08 02:09:22 +00:00
|
|
|
if cmp dnscrypt-resolvers.csv{,.old} ; then
|
|
|
|
echo "no change"
|
|
|
|
else
|
|
|
|
echo "resolver list updated"
|
|
|
|
fi
|
2016-10-23 23:34:03 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHome = true;
|
2017-03-07 23:42:20 +00:00
|
|
|
ProtectSystem = "strict";
|
|
|
|
ReadWritePaths = "${dirOf stateDirectory} ${stateDirectory}";
|
|
|
|
SystemCallFilter = "~@mount";
|
2016-10-23 23:34:03 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-07 22:40:31 +00:00
|
|
|
systemd.timers.update-dnscrypt-resolvers = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
2016-10-23 23:34:03 +01:00
|
|
|
timerConfig = {
|
|
|
|
OnBootSec = "5min";
|
|
|
|
OnUnitActiveSec = "6h";
|
|
|
|
};
|
|
|
|
};
|
2017-03-07 22:40:31 +00:00
|
|
|
})
|
|
|
|
]);
|
2017-03-09 16:55:45 +00:00
|
|
|
|
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "dnscrypt-proxy" "port" ] [ "services" "dnscrypt-proxy" "localPort" ])
|
2017-03-10 14:36:13 +00:00
|
|
|
|
|
|
|
(mkChangedOptionModule
|
|
|
|
[ "services" "dnscrypt-proxy" "tcpOnly" ]
|
|
|
|
[ "services" "dnscrypt-proxy" "extraArgs" ]
|
|
|
|
(config:
|
|
|
|
let val = getAttrFromPath [ "services" "dnscrypt-proxy" "tcpOnly" ] config; in
|
|
|
|
optional val "-T"))
|
|
|
|
|
|
|
|
(mkChangedOptionModule
|
|
|
|
[ "services" "dnscrypt-proxy" "ephemeralKeys" ]
|
|
|
|
[ "services" "dnscrypt-proxy" "extraArgs" ]
|
|
|
|
(config:
|
|
|
|
let val = getAttrFromPath [ "services" "dnscrypt-proxy" "ephemeralKeys" ] config; in
|
|
|
|
optional val "-E"))
|
2017-03-10 13:20:30 +00:00
|
|
|
|
|
|
|
(mkRemovedOptionModule [ "services" "dnscrypt-proxy" "resolverList" ] ''
|
|
|
|
The current resolver listing from upstream is always used
|
|
|
|
unless a custom resolver is specified.
|
|
|
|
'')
|
2017-03-09 16:55:45 +00:00
|
|
|
];
|
2014-11-11 19:12:28 +00:00
|
|
|
}
|