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;
|
2015-03-15 21:19:48 +00:00
|
|
|
resolverListFile = "${dnscrypt-proxy}/share/dnscrypt-proxy/dnscrypt-resolvers.csv";
|
2015-04-14 22:11:28 +01:00
|
|
|
localAddress = "${cfg.localAddress}:${toString cfg.localPort}";
|
2014-11-21 14:42:11 +00:00
|
|
|
daemonArgs =
|
2015-04-14 22:11:28 +01:00
|
|
|
[ "--local-address=${localAddress}"
|
2014-11-21 14:42:11 +00:00
|
|
|
(optionalString cfg.tcpOnly "--tcp-only")
|
2015-04-14 22:11:28 +01:00
|
|
|
]
|
|
|
|
++ resolverArgs;
|
|
|
|
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
|
|
|
|
[ "--resolvers-list=${resolverListFile}"
|
|
|
|
"--resolver-name=${toString cfg.resolverName}"
|
|
|
|
];
|
2014-11-11 19:12:28 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.dnscrypt-proxy = {
|
2016-03-17 13:14:46 +00:00
|
|
|
enable = mkEnableOption "dnscrypt-proxy" // { description = ''
|
|
|
|
Whether to enable the DNSCrypt client proxy. The proxy relays
|
|
|
|
DNS queries to a DNSCrypt enabled upstream resolver. The traffic
|
|
|
|
between the client and the upstream resolver is encrypted and
|
|
|
|
authenticated, mitigating the risk of MITM attacks and third-party
|
2015-03-23 19:44:55 +00:00
|
|
|
snooping (assuming the upstream is trustworthy).
|
2016-03-17 13:14:46 +00:00
|
|
|
|
|
|
|
Enabling this option does not alter the system nameserver; to relay
|
|
|
|
local queries, prepend <literal>127.0.0.1</literal> to
|
|
|
|
<option>networking.nameservers</option>.
|
|
|
|
|
|
|
|
The recommended configuration is to run DNSCrypt proxy as a forwarder
|
|
|
|
for a caching DNS client, as in
|
|
|
|
<programlisting>
|
|
|
|
{
|
|
|
|
services.dnscrypt-proxy.enable = true;
|
|
|
|
services.dnscrypt-proxy.localPort = 43;
|
|
|
|
services.dnsmasq.enable = true;
|
|
|
|
services.dnsmasq.servers = [ "127.0.0.1#43" ];
|
|
|
|
services.dnsmasq.resolveLocalQueries = true; # this is the default
|
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
''; };
|
2014-11-11 19:12:28 +00:00
|
|
|
localAddress = mkOption {
|
|
|
|
default = "127.0.0.1";
|
|
|
|
type = types.string;
|
|
|
|
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
|
|
|
'';
|
|
|
|
};
|
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
|
|
|
'';
|
|
|
|
};
|
|
|
|
resolverName = mkOption {
|
2016-03-09 01:51:46 +00:00
|
|
|
default = "cisco";
|
2015-04-14 22:11:28 +01:00
|
|
|
type = types.nullOr types.string;
|
2014-11-11 19:12:28 +00:00
|
|
|
description = ''
|
2016-01-13 10:48:11 +00:00
|
|
|
The name of the upstream DNSCrypt resolver to use. See
|
2016-03-17 13:14:46 +00:00
|
|
|
<filename>${resolverListFile}</filename> for alternative resolvers
|
2016-01-13 10:48:11 +00:00
|
|
|
(e.g., if you are concerned about logging and/or server
|
|
|
|
location).
|
2014-11-11 19:12:28 +00: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;
|
|
|
|
description = "Resolver IP address";
|
|
|
|
example = "208.67.220.220";
|
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
description = "Resolver port";
|
|
|
|
default = 443;
|
|
|
|
};
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Provider fully qualified domain name";
|
|
|
|
example = "2.dnscrypt-cert.opendns.com";
|
2016-03-17 13:14:46 +00:00
|
|
|
};
|
|
|
|
key = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Provider public key";
|
|
|
|
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
|
|
|
};
|
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
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
security.apparmor.profiles = mkIf apparmorEnabled (singleton (pkgs.writeText "apparmor-dnscrypt-proxy" ''
|
|
|
|
${dnscrypt-proxy}/bin/dnscrypt-proxy {
|
|
|
|
/dev/null rw,
|
|
|
|
/dev/urandom r,
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
/etc/passwd r,
|
|
|
|
/etc/group r,
|
|
|
|
${config.environment.etc."nsswitch.conf".source} r,
|
2015-02-23 17:02:12 +00:00
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
${pkgs.glibc}/lib/*.so mr,
|
|
|
|
${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,
|
2015-03-15 21:19:48 +00:00
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
${pkgs.gcc.cc}/lib/libssp.so.* mr,
|
|
|
|
${pkgs.libsodium}/lib/libsodium.so.* mr,
|
|
|
|
${pkgs.systemd}/lib/libsystemd.so.* mr,
|
|
|
|
${pkgs.xz}/lib/liblzma.so.* mr,
|
|
|
|
${pkgs.libgcrypt}/lib/libgcrypt.so.* mr,
|
|
|
|
${pkgs.libgpgerror}/lib/libgpg-error.so.* mr,
|
2016-03-09 03:10:19 +00:00
|
|
|
${pkgs.libcap}/lib/libcap.so.* mr,
|
|
|
|
${pkgs.lz4}/lib/liblz4.so.* mr,
|
|
|
|
${pkgs.attr}/lib/libattr.so.* mr,
|
2015-03-15 21:19:48 +00:00
|
|
|
|
2015-03-15 21:22:45 +00:00
|
|
|
${resolverListFile} r,
|
|
|
|
}
|
|
|
|
''));
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2015-03-15 21:19:48 +00:00
|
|
|
users.extraUsers.dnscrypt-proxy = {
|
|
|
|
uid = config.ids.uids.dnscrypt-proxy;
|
2014-11-11 19:12:28 +00:00
|
|
|
description = "dnscrypt-proxy daemon user";
|
|
|
|
};
|
2015-03-15 21:19:48 +00:00
|
|
|
users.extraGroups.dnscrypt-proxy.gid = config.ids.gids.dnscrypt-proxy;
|
2014-11-11 19:12:28 +00:00
|
|
|
|
2015-03-07 18:13:12 +00:00
|
|
|
systemd.sockets.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy listening socket";
|
|
|
|
socketConfig = {
|
2015-04-14 22:11:28 +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";
|
|
|
|
after = [ "network.target" ] ++ optional apparmorEnabled "apparmor.service";
|
2015-03-07 18:13:12 +00:00
|
|
|
requires = [ "dnscrypt-proxy.socket "] ++ optional apparmorEnabled "apparmor.service";
|
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}";
|
2015-03-15 21:19:48 +00:00
|
|
|
User = "dnscrypt-proxy";
|
|
|
|
Group = "dnscrypt-proxy";
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
2014-11-11 19:12:28 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|