@@ -15,26 +15,19 @@ let
1515in
1616
1717{
18− ##### interface
19−
2018 options = {
21−
2219 services.dnscrypt-proxy = {
23−
2420 enable = mkOption {
2521 default = false;
2622 type = types.bool;
2723 description = ''
28− Enable dnscrypt-proxy.
29− The proxy relays regular DNS queries to a DNSCrypt enabled
30− upstream resolver.
31− The traffic between the client and the upstream resolver is
32− encrypted and authenticated, which may mitigate the risk of MITM
33− attacks and third-party snooping (assuming the upstream is
34− trustworthy).
24+ Enable dnscrypt-proxy. The proxy relays regular DNS queries to a
25+ DNSCrypt enabled upstream resolver. The traffic between the
26+ client and the upstream resolver is encrypted and authenticated,
27+ which may mitigate the risk of MITM attacks and third-party
28+ snooping (assuming the upstream is trustworthy).
3529 '';
3630 };
37−
3831 localAddress = mkOption {
3932 default = "127.0.0.1";
4033 type = types.string;
@@ -42,7 +35,6 @@ in
4235 Listen for DNS queries on this address.
4336 '';
4437 };
45−
4638 port = mkOption {
4739 default = 53;
4840 type = types.int;
@@ -50,7 +42,6 @@ in
5042 Listen on this port.
5143 '';
5244 };
53−
5445 resolverName = mkOption {
5546 default = "opendns";
5647 type = types.string;
@@ -61,59 +52,47 @@ in
6152 location).
6253 '';
6354 };
64−
6555 tcpOnly = mkOption {
6656 default = false;
6757 type = types.bool;
6858 description = ''
6959 Force sending encrypted DNS queries to the upstream resolver
70− over TCP instead of UDP (on port 443).
71− Enabling this option may help circumvent filtering, but should
72− not be used otherwise.
60+ over TCP instead of UDP (on port 443). Enabling this option may
61+ help circumvent filtering, but should not be used otherwise.
7362 '';
7463 };
75−
7664 };
77−
7865 };
7966
80− ##### implementation
81−
8267 config = mkIf cfg.enable {
8368
84− ### AppArmor profile
85−
86− security.apparmor.profiles = mkIf apparmorEnabled [
87− (pkgs.writeText "apparmor-dnscrypt-proxy" ''
88−
89− ${dnscrypt-proxy}/bin/dnscrypt-proxy {
90−
91− /dev/null rw,
92− /dev/urandom r,
69+ security.apparmor.profiles = mkIf apparmorEnabled (singleton (pkgs.writeText "apparmor-dnscrypt-proxy" ''
70+ ${dnscrypt-proxy}/bin/dnscrypt-proxy {
71+ /dev/null rw,
72+ /dev/urandom r,
9373
94− /etc/passwd r,
95− /etc/group r,
96− ${config.environment.etc."nsswitch.conf".source} r,
74+ /etc/passwd r,
75+ /etc/group r,
76+ ${config.environment.etc."nsswitch.conf".source} r,
9777
98− ${pkgs.glibc}/lib/*.so mr,
99− ${pkgs.tzdata}/share/zoneinfo/** r,
78+ ${pkgs.glibc}/lib/*.so mr,
79+ ${pkgs.tzdata}/share/zoneinfo/** r,
10080
101− network inet stream,
102− network inet6 stream,
103− network inet dgram,
104− network inet6 dgram,
81+ network inet stream,
82+ network inet6 stream,
83+ network inet dgram,
84+ network inet6 dgram,
10585
106− ${pkgs.gcc.cc}/lib/libssp.so.* mr,
107− ${pkgs.libsodium}/lib/libsodium.so.* mr,
108− ${pkgs.systemd}/lib/libsystemd.so.* mr,
109− ${pkgs.xz}/lib/liblzma.so.* mr,
110− ${pkgs.libgcrypt}/lib/libgcrypt.so.* mr,
111− ${pkgs.libgpgerror}/lib/libgpg-error.so.* mr,
86+ ${pkgs.gcc.cc}/lib/libssp.so.* mr,
87+ ${pkgs.libsodium}/lib/libsodium.so.* mr,
88+ ${pkgs.systemd}/lib/libsystemd.so.* mr,
89+ ${pkgs.xz}/lib/liblzma.so.* mr,
90+ ${pkgs.libgcrypt}/lib/libgcrypt.so.* mr,
91+ ${pkgs.libgpgerror}/lib/libgpg-error.so.* mr,
11292
113− ${resolverListFile} r,
114− }
115− '')
116− ];
93+ ${resolverListFile} r,
94+ }
95+ ''));
11796
11897 users.extraUsers.dnscrypt-proxy = {
11998 uid = config.ids.uids.dnscrypt-proxy;
@@ -121,26 +100,21 @@ in
121100 };
122101 users.extraGroups.dnscrypt-proxy.gid = config.ids.gids.dnscrypt-proxy;
123102
124− ## derived from upstream dnscrypt-proxy.socket
125103 systemd.sockets.dnscrypt-proxy = {
126104 description = "dnscrypt-proxy listening socket";
127−
128105 socketConfig = {
129106 ListenStream = "${cfg.localAddress}:${toString cfg.port}";
130107 ListenDatagram = "${cfg.localAddress}:${toString cfg.port}";
131108 };
132−
133109 wantedBy = [ "sockets.target" ];
134110 };
135111
136− # derived from upstream dnscrypt-proxy.service
137112 systemd.services.dnscrypt-proxy = {
138113 description = "dnscrypt-proxy daemon";
139114 after = [ "network.target" ] ++ optional apparmorEnabled "apparmor.service";
140115 requires = [ "dnscrypt-proxy.socket "] ++ optional apparmorEnabled "apparmor.service";
141116 serviceConfig = {
142117 Type = "simple";
143− ## note: NonBlocking is required for socket activation to work
144118 NonBlocking = "true";
145119 ExecStart = "${dnscrypt-proxy}/bin/dnscrypt-proxy ${toString daemonArgs}";
146120 User = "dnscrypt-proxy";
@@ -149,6 +123,5 @@ in
149123 PrivateDevices = true;
150124 };
151125 };
152−
153126 };
154127}