2014-11-11 19:12:28 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
apparmorEnabled = config.security.apparmor.enable;
|
|
|
|
dnscrypt-proxy = pkgs.dnscrypt-proxy;
|
|
|
|
cfg = config.services.dnscrypt-proxy;
|
2016-10-23 23:34:03 +01:00
|
|
|
stateDirectory = "/var/lib/dnscrypt-proxy";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2015-04-14 22:11:28 +01:00
|
|
|
localAddress = "${cfg.localAddress}:${toString cfg.localPort}";
|
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";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Internal flag indicating whether the upstream resolver list is used
|
|
|
|
useUpstreamResolverList = cfg.resolverList == null && cfg.customResolver == null;
|
|
|
|
|
|
|
|
resolverList =
|
|
|
|
if (cfg.resolverList != null)
|
|
|
|
then cfg.resolverList
|
|
|
|
else "${stateDirectory}/dnscrypt-resolvers.csv";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2015-04-14 22:11:28 +01:00
|
|
|
resolverArgs = if (cfg.customResolver != null)
|
|
|
|
then
|
|
|
|
[ "--resolver-address=${cfg.customResolver.address}:${toString cfg.customResolver.port}"
|
|
|
|
"--provider-name=${cfg.customResolver.name}"
|
|
|
|
"--provider-key=${cfg.customResolver.key}"
|
|
|
|
]
|
|
|
|
else
|
2016-10-23 23:34:03 +01:00
|
|
|
[ "--resolvers-list=${resolverList}"
|
|
|
|
"--resolver-name=${cfg.resolverName}"
|
2015-04-14 22:11:28 +01:00
|
|
|
];
|
2016-10-23 23:34:03 +01:00
|
|
|
|
|
|
|
# The final command line arguments passed to the daemon
|
|
|
|
daemonArgs =
|
|
|
|
[ "--local-address=${localAddress}" ]
|
|
|
|
++ optional cfg.tcpOnly "--tcp-only"
|
|
|
|
++ optional cfg.ephemeralKeys "-E"
|
|
|
|
++ resolverArgs;
|
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 = {
|
|
|
|
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 = ''
|
2016-10-23 23:34:03 +01:00
|
|
|
The name of the upstream 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.
|
2014-11-11 19:12:28 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2016-05-10 06:04:20 +01:00
|
|
|
resolverList = mkOption {
|
2016-10-23 23:34:03 +01:00
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.path;
|
2016-05-10 06:04:20 +01:00
|
|
|
description = ''
|
2016-10-23 23:34:03 +01:00
|
|
|
List of DNSCrypt resolvers. The default is to use the list of
|
|
|
|
public resolvers provided by upstream.
|
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";
|
2015-04-14 22:11:28 +01:00
|
|
|
example = "2.dnscrypt-cert.opendns.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
|
|
|
|
2014-11-11 19:12:28 +00:00
|
|
|
tcpOnly = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
2016-03-17 13:14:46 +00:00
|
|
|
Force sending encrypted DNS queries to the upstream resolver over
|
|
|
|
TCP instead of UDP (on port 443). Use only if the UDP port is blocked.
|
2014-11-11 19:12:28 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-08-29 17:19:18 +01:00
|
|
|
|
2016-03-17 13:34:56 +00:00
|
|
|
ephemeralKeys = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Compute a new key pair for every query. Enabling this option
|
|
|
|
increases CPU usage, but makes it more difficult for the upstream
|
|
|
|
resolver to track your usage of their service across IP addresses.
|
|
|
|
The default is to re-use the public key pair for all queries, making
|
|
|
|
tracking trivial.
|
2014-11-11 19:12:28 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
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
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
security.apparmor.profiles = optional apparmorEnabled (pkgs.writeText "apparmor-dnscrypt-proxy" ''
|
2015-03-15 21:22:45 +00:00
|
|
|
${dnscrypt-proxy}/bin/dnscrypt-proxy {
|
|
|
|
/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
|
|
|
|
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,
|
|
|
|
${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,
|
2015-03-15 21:22:45 +00:00
|
|
|
}
|
2016-10-23 23:34:03 +01:00
|
|
|
'');
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2016-03-23 19:42:36 +00:00
|
|
|
users.users.dnscrypt-proxy = {
|
2014-11-11 19:12:28 +00:00
|
|
|
description = "dnscrypt-proxy daemon user";
|
2016-03-23 19:42:36 +00:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "dnscrypt-proxy";
|
2014-11-11 19:12:28 +00:00
|
|
|
};
|
2016-03-23 19:42:36 +00:00
|
|
|
users.groups.dnscrypt-proxy = {};
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
systemd.services.init-dnscrypt-proxy-statedir = optionalAttrs useUpstreamResolverList {
|
|
|
|
description = "Initialize dnscrypt-proxy state directory";
|
|
|
|
script = ''
|
|
|
|
mkdir -pv ${stateDirectory}
|
|
|
|
chown -c dnscrypt-proxy:dnscrypt-proxy ${stateDirectory}
|
|
|
|
cp --preserve=timestamps -uv \
|
|
|
|
${pkgs.dnscrypt-proxy}/share/dnscrypt-proxy/dnscrypt-resolvers.csv \
|
|
|
|
${stateDirectory}
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.update-dnscrypt-resolvers = optionalAttrs useUpstreamResolverList {
|
|
|
|
description = "Update list of DNSCrypt resolvers";
|
|
|
|
|
|
|
|
requires = [ "init-dnscrypt-proxy-statedir.service" ];
|
|
|
|
after = [ "init-dnscrypt-proxy-statedir.service" ];
|
|
|
|
|
|
|
|
path = with pkgs; [ curl minisign ];
|
|
|
|
script = ''
|
|
|
|
cd ${stateDirectory}
|
|
|
|
curl -fSsL -o dnscrypt-resolvers.csv.tmp \
|
|
|
|
https://download.dnscrypt.org/dnscrypt-proxy/dnscrypt-resolvers.csv
|
|
|
|
curl -fSsL -o dnscrypt-resolvers.csv.minisig.tmp \
|
|
|
|
https://download.dnscrypt.org/dnscrypt-proxy/dnscrypt-resolvers.csv.minisig
|
|
|
|
mv dnscrypt-resolvers.csv.minisig{.tmp,}
|
|
|
|
minisign -q -V -p ${upstreamResolverListPubKey} \
|
|
|
|
-m dnscrypt-resolvers.csv.tmp -x dnscrypt-resolvers.csv.minisig
|
|
|
|
mv dnscrypt-resolvers.csv{.tmp,}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectSystem = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.update-dnscrypt-resolvers = optionalAttrs useUpstreamResolverList {
|
|
|
|
timerConfig = {
|
|
|
|
OnBootSec = "5min";
|
|
|
|
OnUnitActiveSec = "6h";
|
|
|
|
};
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
};
|
|
|
|
|
2015-03-07 18:13:12 +00:00
|
|
|
systemd.sockets.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy listening socket";
|
|
|
|
socketConfig = {
|
2016-10-23 23:34:03 +01:00
|
|
|
ListenStream = localAddress;
|
|
|
|
ListenDatagram = localAddress;
|
2015-03-07 18:13:12 +00:00
|
|
|
};
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
};
|
2014-11-11 19:12:28 +00:00
|
|
|
|
|
|
|
systemd.services.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy daemon";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2016-10-23 23:34:03 +01:00
|
|
|
after = [ "network.target" ]
|
|
|
|
++ optional apparmorEnabled "apparmor.service"
|
|
|
|
++ optional useUpstreamResolverList "init-dnscrypt-proxy-statedir.service";
|
|
|
|
|
|
|
|
requires = [ "dnscrypt-proxy.socket "]
|
|
|
|
++ optional apparmorEnabled "apparmor.service"
|
|
|
|
++ optional useUpstreamResolverList "init-dnscrypt-proxy-statedir.service";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2014-11-11 19:12:28 +00:00
|
|
|
serviceConfig = {
|
2015-03-07 18:13:12 +00:00
|
|
|
Type = "simple";
|
|
|
|
NonBlocking = "true";
|
2015-01-09 12:57:04 +00:00
|
|
|
ExecStart = "${dnscrypt-proxy}/bin/dnscrypt-proxy ${toString daemonArgs}";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2015-03-15 21:19:48 +00:00
|
|
|
User = "dnscrypt-proxy";
|
2016-03-23 19:41:49 +00:00
|
|
|
|
2015-03-15 21:19:48 +00:00
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
2016-03-23 19:42:01 +00:00
|
|
|
ProtectHome = true;
|
2014-11-11 19:12:28 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|