2014-11-11 20:12:28 +01: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 22:19:48 +01:00
|
|
|
resolverListFile = "${dnscrypt-proxy}/share/dnscrypt-proxy/dnscrypt-resolvers.csv";
|
2014-11-21 15:42:11 +01:00
|
|
|
daemonArgs =
|
2015-03-15 22:19:48 +01:00
|
|
|
[ "--local-address=${cfg.localAddress}:${toString cfg.port}"
|
2014-11-21 15:42:11 +01:00
|
|
|
(optionalString cfg.tcpOnly "--tcp-only")
|
2015-03-15 22:19:48 +01:00
|
|
|
"--resolvers-list=${resolverListFile}"
|
2014-11-21 15:42:11 +01:00
|
|
|
"--resolver-name=${cfg.resolverName}"
|
|
|
|
];
|
2014-11-11 20:12:28 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.dnscrypt-proxy = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
2015-03-15 22:22:45 +01:00
|
|
|
Enable dnscrypt-proxy. The proxy relays regular DNS queries to a
|
|
|
|
DNSCrypt enabled upstream resolver. The traffic between the
|
|
|
|
client and the upstream resolver is encrypted and authenticated,
|
|
|
|
which may mitigate the risk of MITM attacks and third-party
|
|
|
|
snooping (assuming the upstream is trustworthy).
|
2014-11-11 20:12:28 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
localAddress = mkOption {
|
|
|
|
default = "127.0.0.1";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
Listen for DNS queries on this address.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
default = 53;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Listen on this port.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
resolverName = mkOption {
|
|
|
|
default = "opendns";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
2015-03-15 22:19:48 +01:00
|
|
|
The name of the upstream DNSCrypt resolver to use. See
|
|
|
|
<literal>${resolverListFile}</literal> for alternative resolvers
|
|
|
|
(e.g., if you are concerned about logging and/or server
|
|
|
|
location).
|
2014-11-11 20:12:28 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
tcpOnly = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Force sending encrypted DNS queries to the upstream resolver
|
2015-03-15 22:22:45 +01:00
|
|
|
over TCP instead of UDP (on port 443). Enabling this option may
|
|
|
|
help circumvent filtering, but should not be used otherwise.
|
2014-11-11 20:12:28 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2015-03-15 22:22:45 +01: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 20:12:28 +01:00
|
|
|
|
2015-03-15 22:22:45 +01:00
|
|
|
/etc/passwd r,
|
|
|
|
/etc/group r,
|
|
|
|
${config.environment.etc."nsswitch.conf".source} r,
|
2015-02-23 18:02:12 +01:00
|
|
|
|
2015-03-15 22:22:45 +01:00
|
|
|
${pkgs.glibc}/lib/*.so mr,
|
|
|
|
${pkgs.tzdata}/share/zoneinfo/** r,
|
2014-11-11 20:12:28 +01:00
|
|
|
|
2015-03-15 22:22:45 +01:00
|
|
|
network inet stream,
|
|
|
|
network inet6 stream,
|
|
|
|
network inet dgram,
|
|
|
|
network inet6 dgram,
|
2015-03-15 22:19:48 +01:00
|
|
|
|
2015-03-15 22:22:45 +01: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,
|
2015-03-15 22:19:48 +01:00
|
|
|
|
2015-03-15 22:22:45 +01:00
|
|
|
${resolverListFile} r,
|
|
|
|
}
|
|
|
|
''));
|
2014-11-11 20:12:28 +01:00
|
|
|
|
2015-03-15 22:19:48 +01:00
|
|
|
users.extraUsers.dnscrypt-proxy = {
|
|
|
|
uid = config.ids.uids.dnscrypt-proxy;
|
2014-11-11 20:12:28 +01:00
|
|
|
description = "dnscrypt-proxy daemon user";
|
|
|
|
};
|
2015-03-15 22:19:48 +01:00
|
|
|
users.extraGroups.dnscrypt-proxy.gid = config.ids.gids.dnscrypt-proxy;
|
2014-11-11 20:12:28 +01:00
|
|
|
|
2015-03-07 19:13:12 +01:00
|
|
|
systemd.sockets.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy listening socket";
|
|
|
|
socketConfig = {
|
|
|
|
ListenStream = "${cfg.localAddress}:${toString cfg.port}";
|
|
|
|
ListenDatagram = "${cfg.localAddress}:${toString cfg.port}";
|
|
|
|
};
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
};
|
|
|
|
|
2014-11-11 20:12:28 +01:00
|
|
|
systemd.services.dnscrypt-proxy = {
|
|
|
|
description = "dnscrypt-proxy daemon";
|
|
|
|
after = [ "network.target" ] ++ optional apparmorEnabled "apparmor.service";
|
2015-03-07 19:13:12 +01:00
|
|
|
requires = [ "dnscrypt-proxy.socket "] ++ optional apparmorEnabled "apparmor.service";
|
2014-11-11 20:12:28 +01:00
|
|
|
serviceConfig = {
|
2015-03-07 19:13:12 +01:00
|
|
|
Type = "simple";
|
|
|
|
NonBlocking = "true";
|
2015-01-09 13:57:04 +01:00
|
|
|
ExecStart = "${dnscrypt-proxy}/bin/dnscrypt-proxy ${toString daemonArgs}";
|
2015-03-15 22:19:48 +01:00
|
|
|
User = "dnscrypt-proxy";
|
|
|
|
Group = "dnscrypt-proxy";
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
2014-11-11 20:12:28 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|