@@ -3,120 +3,116 @@
7− inherit (pkgs) tor privoxy;
9− stateDir = "/var/lib/tor";
10− privoxyDir = stateDir+"/privoxy";
12 cfg = config.services.tor;
6 cfg = config.services.tor;
13−
7+ torDirectory = "/var/lib/tor";
16− opt = name: value: if value != "" then "${name} ${value}" else "";
9+ opt = name: value: optionalString (value != null) "${name} ${value}";
17− optint = name: value: if value != 0 then "${name} ${toString value}" else "";
10+ optint = name: value: optionalString (value != 0) "${name} ${toString value}";
14+ DataDirectory ${torDirectory}
16+ ${optint "ControlPort" cfg.controlPort}
18+ # Client connection config
19+ + optionalString cfg.client.enable ''
20+ SOCKSPort ${cfg.client.socksListenAddress}
21+ ${opt "SocksPolicy" cfg.client.socksPolicy}
24+ + optionalString cfg.relay.enable ''
25+ ORPort ${cfg.relay.portSpec}
26+ ${opt "Nickname" cfg.relay.nickname}
27+ ${opt "ContactInfo" cfg.relay.contactInfo}
29+ ${optint "RelayBandwidthRate" cfg.relay.bandwidthRate}
30+ ${optint "RelayBandwidthBurst" cfg.relay.bandwidthBurst}
31+ ${opt "AccountingMax" cfg.relay.accountingMax}
32+ ${opt "AccountingStart" cfg.relay.accountingStart}
34+ ${if cfg.relay.isExit then
35+ opt "ExitPolicy" cfg.relay.exitPolicy
37+ "ExitPolicy reject *:*"}
39+ ${optionalString cfg.relay.isBridge ''
41+ ServerTransportPlugin obfs2,obfs3 exec ${pkgs.pythonPackages.obfsproxy}/bin/obfsproxy managed
46+ torRcFile = pkgs.writeText "torrc" torRc;
25 options = {
49 options = {
27 services.tor = {
50 services.tor = {
55+ Enable the Tor daemon. By default, the daemon is run without
56+ relay, exit, bridge or client connectivity.
29− config = mkOption {
60+ extraConfig = mkOption {
30 default = "";
62 default = "";
31 description = ''
63 description = ''
32 Extra configuration. Contents will be added verbatim to the
64 Extra configuration. Contents will be added verbatim to the
33− configuration file.
65+ configuration file at the end.
37− client = {
69+ controlPort = mkOption {
74+ If set, Tor will accept connections on the specified port
75+ and allow them to control the tor process.
39 enable = mkOption {
80 enable = mkOption {
40 default = false;
82 default = false;
41 description = ''
83 description = ''
42− Whether to enable Tor daemon to route application connections.
84+ Whether to enable Tor daemon to route application
43− You might want to disable this if you plan running a dedicated Tor relay.
85+ connections. You might want to disable this if you plan
86+ running a dedicated Tor relay.
47 socksListenAddress = mkOption {
90 socksListenAddress = mkOption {
48 default = "127.0.0.1:9050";
92 default = "127.0.0.1:9050";
49 example = "192.168.0.1:9100";
93 example = "192.168.0.1:9100";
50 description = ''
94 description = ''
51− Bind to this address to listen for connections from Socks-speaking
95+ Bind to this address to listen for connections from
52− applications.
96+ Socks-speaking applications.
56− socksListenAddressFaster = mkOption {
57− default = "127.0.0.1:9063";
59− Same as socksListenAddress but uses weaker circuit isolation to provide
60− performance suitable for a web browser.
64 socksPolicy = mkOption {
100 socksPolicy = mkOption {
101+ type = types.nullOr types.str;
65− default = "";
102+ default = null;
66 example = "accept 192.168.0.0/16, reject *";
103 example = "accept 192.168.0.0/16, reject *";
67 description = ''
104 description = ''
68− Entry policies to allow/deny SOCKS requests based on IP address.
105+ Entry policies to allow/deny SOCKS requests based on IP
69− First entry that matches wins. If no SocksPolicy is set, we accept
106+ address. First entry that matches wins. If no SocksPolicy
107+ is set, we accept all (and only) requests from
70− all (and only) requests from SocksListenAddress.
108+ SocksListenAddress.
79− Whether to enable a special instance of privoxy dedicated to Tor.
80− To have anonymity, protocols need to be scrubbed of identifying
82− Most people using Tor want to anonymize their web traffic, so by
83− default we enable an special instance of privoxy specifically for
85− However, if you are only going to use Tor only for other kinds of
86− traffic then you can disable this option.
90− listenAddress = mkOption {
91− default = "127.0.0.1:8118";
93− Address that Tor's instance of privoxy is listening to.
94− *This does not configure the standard NixOS instance of privoxy.*
95− This is for Tor connections only!
96− See services.privoxy.listenAddress to configure the standard NixOS
104− Extra configuration for Tor's instance of privoxy. Contents will be
105− added verbatim to the configuration file.
106− *This does not configure the standard NixOS instance of privoxy.*
107− This is for Tor connections only!
108− See services.privoxy.extraConfig to configure the standard NixOS
117 relay = {
113 relay = {
119 enable = mkOption {
114 enable = mkOption {
120 default = false;
116 default = false;
121 description = ''
117 description = ''
122 Whether to enable relaying TOR traffic for others.
118 Whether to enable relaying TOR traffic for others.
@@ -126,16 +122,19 @@ in
128 isBridge = mkOption {
124 isBridge = mkOption {
129 default = false;
126 default = false;
130 description = ''
127 description = ''
131− Bridge relays (or "bridges" ) are Tor relays that aren't listed in the
128+ Bridge relays (or "bridges") are Tor relays that aren't
132− main directory. Since there is no complete public list of them, even if an
129+ listed in the main directory. Since there is no complete
130+ public list of them, even if an ISP is filtering
133− ISP is filtering connections to all the known Tor relays, they probably
131+ connections to all the known Tor relays, they probably
134 won't be able to block all the bridges.
132 won't be able to block all the bridges.
136 A bridge relay can't be an exit relay.
134 A bridge relay can't be an exit relay.
138− You need to set relay.enable to true for this option to take effect.
136+ You need to set relay.enable to true for this option to
140 The bridge is set up with an obfuscated transport proxy.
139 The bridge is set up with an obfuscated transport proxy.
@@ -144,25 +143,72 @@ in
146 isExit = mkOption {
145 isExit = mkOption {
147 default = false;
147 default = false;
148 description = ''
148 description = ''
149− An exit relay allows Tor users to access regular Internet services.
149+ An exit relay allows Tor users to access regular Internet
151− Unlike running a non-exit relay, running an exit relay may expose
152+ Unlike running a non-exit relay, running an exit relay may
153+ expose you to abuse complaints. See
152− you to abuse complaints. See https://www.torproject.org/faq.html.en#ExitPolicies for more info.
154+ https://www.torproject.org/faq.html.en#ExitPolicies for
154− You can specify which services Tor users may access via your exit relay using exitPolicy option.
157+ You can specify which services Tor users may access via
158+ your exit relay using exitPolicy option.
158 nickname = mkOption {
162 nickname = mkOption {
159 default = "anonymous";
164 default = "anonymous";
160 description = ''
165 description = ''
161 A unique handle for your TOR relay.
166 A unique handle for your TOR relay.
170+ contactInfo = mkOption {
171+ type = types.nullOr types.str;
173+ example = "admin@relay.com";
175+ Contact information for the relay owner (e.g. a mail
176+ address and GPG key ID).
180+ accountingMax = mkOption {
181+ type = types.nullOr types.str;
183+ example = "450 GBytes";
185+ Specify maximum bandwidth allowed during an accounting
186+ period. This allows you to limit overall tor bandwidth
187+ over some time period. See the
188+ <literal>AccountingMax</literal> option by looking at the
189+ tor manual (<literal>man tor</literal>) for more.
191+ Note this limit applies individually to upload and
192+ download; if you specify <literal>"500 GBytes"</literal>
193+ here, then you may transfer up to 1 TBytes of overall
194+ bandwidth (500 GB upload, 500 GB download).
198+ accountingStart = mkOption {
199+ type = types.nullOr types.str;
201+ example = "month 1 1:00";
203+ Specify length of an accounting period. This allows you to
204+ limit overall tor bandwidth over some time period. See the
205+ <literal>AccountingStart</literal> option by looking at
206+ the tor manual (<literal>man tor</literal>) for more.
165 bandwidthRate = mkOption {
210 bandwidthRate = mkOption {
166 default = 0;
212 default = 0;
167 example = 100;
213 example = 100;
168 description = ''
214 description = ''
@@ -172,6 +218,7 @@ in
174 bandwidthBurst = mkOption {
220 bandwidthBurst = mkOption {
175 default = cfg.relay.bandwidthRate;
222 default = cfg.relay.bandwidthRate;
176 example = 200;
223 example = 200;
177 description = ''
224 description = ''
@@ -181,143 +228,99 @@ in
184− port = mkOption {
231+ portSpec = mkOption {
185− default = 9001;
232+ type = types.str;
186 description = ''
234 description = ''
187− What port to advertise for Tor connections.
235+ What port to advertise for Tor connections. This corresponds
188− '';
236+ to the <literal>ORPort</literal> section in the Tor manual; see
189− };
237+ <literal>man tor</literal> for more details.
191− listenAddress = mkOption {
239+ At a minimum, you should just specify the port for the
192− default = "";
240+ relay to listen on; a common one like 143, 22, 80, or 443
193− example = "0.0.0.0:9090";
241+ to help Tor users who may have very restrictive port-based
194− description = ''
242+ firewalls.
195− Set this if you need to listen on a port other than the one advertised
196− in relayPort (e.g. to advertise 443 but bind to 9090). You'll need to do
197− ipchains or other port forwsarding yourself to make this work.
201 exitPolicy = mkOption {
246 exitPolicy = mkOption {
247+ type = types.nullOr types.str;
202− default = "";
248+ default = null;
203 example = "accept *:6660-6667,reject *:*";
249 example = "accept *:6660-6667,reject *:*";
204 description = ''
250 description = ''
205− A comma-separated list of exit policies. They're considered first
251+ A comma-separated list of exit policies. They're
206− to last, and the first match wins. If you want to _replace_
252+ considered first to last, and the first match wins. If you
207− the default exit policy, end this with either a reject *:* or an
253+ want to _replace_ the default exit policy, end this with
254+ either a reject *:* or an accept *:*. Otherwise, you're
208− accept *:*. Otherwise, you're _augmenting_ (prepending to) the
255+ _augmenting_ (prepending to) the default exit
209− default exit policy. Leave commented to just use the default, which is
256+ policy. Leave commented to just use the default, which is
257+ available in the man page or at
210− available in the man page or at https://www.torproject.org/documentation.html
258+ https://www.torproject.org/documentation.html
212 Look at https://www.torproject.org/faq-abuse.html#TypicalAbuses
260 Look at https://www.torproject.org/faq-abuse.html#TypicalAbuses
213 for issues you might encounter if you use the default exit policy.
261 for issues you might encounter if you use the default exit policy.
215− If certain IPs and ports are blocked externally, e.g. by your firewall,
263+ If certain IPs and ports are blocked externally, e.g. by
216− you should update your exit policy to reflect this -- otherwise Tor
264+ your firewall, you should update your exit policy to
265+ reflect this -- otherwise Tor users will be told that
217− users will be told that those destinations are down.
266+ those destinations are down.
228− ###### implementation
230− config = mkIf (cfg.client.enable || cfg.relay.enable) {
273+ config = mkIf cfg.enable {
232 assertions = singleton
274 assertions = singleton
233− { assertion = cfg.relay.enable -> !(cfg.relay.isBridge && cfg.relay.isExit);
234− message = "Can't be both an exit and a bridge relay at the same time";
275+ { message = "Can't be both an exit and a bridge relay at the same time";
277+ cfg.relay.enable -> !(cfg.relay.isBridge && cfg.relay.isExit);
280+ users.extraGroups.tor.gid = config.ids.gids.tor;
237− users.extraUsers = singleton
281+ users.extraUsers.tor =
239− uid = config.ids.uids.tor;
240− description = "Tor daemon user";
282+ { description = "Tor Daemon User";
241− home = stateDir;
283+ createHome = true;
284+ home = torDirectory;
286+ uid = config.ids.uids.tor;
245− tor = { name = "tor";
247− startOn = "started network-interfaces";
248− stopOn = "stopping network-interfaces";
251− mkdir -m 0755 -p ${stateDir}
252− chown ${torUser} ${stateDir}
254− exec = "${tor}/bin/tor -f ${pkgs.writeText "torrc" cfg.config}";
256− // optionalAttrs (cfg.client.privoxy.enable && cfg.client.enable) {
257− torPrivoxy = { name = "tor-privoxy";
259− startOn = "started network-interfaces";
260− stopOn = "stopping network-interfaces";
263− mkdir -m 0755 -p ${privoxyDir}
264− chown ${torUser} ${privoxyDir}
266− exec = "${privoxy}/sbin/privoxy --no-daemon --user ${torUser} ${pkgs.writeText "torPrivoxy.conf" cfg.client.privoxy.config}";
269− services.tor.config = ''
289+ systemd.services.tor =
270− DataDirectory ${stateDir}
290+ { description = "Tor Daemon";
271− User ${torUser}
291+ path = [ pkgs.tor ];
273− + optionalString cfg.client.enable ''
293+ wantedBy = [ "multi-user.target" ];
274− SOCKSPort ${cfg.client.socksListenAddress} IsolateDestAddr
294+ after = [ "network.target" ];
275− SOCKSPort ${cfg.client.socksListenAddressFaster}
295+ restartTriggers = [ torRcFile ];
276− ${opt "SocksPolicy" cfg.client.socksPolicy}
296+
277− ''
297+ # Translated from the upstream contrib/dist/tor.service.in
278− + optionalString cfg.relay.enable ''
298+ serviceConfig =
279− ORPort ${toString cfg.relay.port}
299+ { Type = "simple";
280− ${opt "ORListenAddress" cfg.relay.listenAddress }
300+ ExecStartPre = "${pkgs.tor}/bin/tor -f ${torRcFile} --verify-config";
281− ${opt "Nickname" cfg.relay.nickname}
301+ ExecStart = "${pkgs.tor}/bin/tor -f ${torRcFile} --RunAsDaemon 0";
282− ${optint "RelayBandwidthRate" cfg.relay.bandwidthRate}
302+ ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
283− ${optint "RelayBandwidthBurst" cfg.relay.bandwidthBurst}
303+ KillSignal = "SIGINT";
284− ${if cfg.relay.isExit then opt "ExitPolicy" cfg.relay.exitPolicy else "ExitPolicy reject *:*"}
304+ TimeoutSec = 30;
285− ${if cfg.relay.isBridge then ''
305+ Restart = "on-failure";
286− BridgeRelay 1
306+ LimitNOFILE = 32768;
287− ServerTransportPlugin obfs2,obfs3 exec ${pkgs.pythonPackages.obfsproxy}/bin/obfsproxy managed
307+
288− '' else ""}
308+ # Hardening
289− '';
309+ # Note: DevicePolicy is set to 'closed', although the
290−
310+ # minimal permissions are really:
291− services.tor.client.privoxy.config = ''
311+ # DeviceAllow /dev/null rw
292− # Generally, this file goes in /etc/privoxy/config
312+ # DeviceAllow /dev/urandom r
293− #
313+ # .. but we can't specify DeviceAllow multiple times. 'closed'
294− # Tor listens as a SOCKS4a proxy here:
314+ # is close enough.
295− forward-socks4a / ${cfg.client.socksListenAddressFaster} .
315+ PrivateTmp = "yes";
296− confdir ${privoxy}/etc
316+ DevicePolicy = "closed";
297− logdir ${privoxyDir}
317+ InaccessibleDirectories = "/home";
298− # actionsfile standard # Internal purpose, recommended
318+ ReadOnlyDirectories = "/";
299− actionsfile default.action # Main actions file
319+ ReadWriteDirectories = torDirectory;
300− actionsfile user.action # User customizations
320+ NoNewPrivileges = "yes";
301− filterfile default.filter
321+ };
303− # Don't log interesting things, only startup messages, warnings and errors
306− #debug 0 # show each GET/POST/CONNECT request
307− debug 4096 # Startup banner and warnings
308− debug 8192 # Errors - *we highly recommended enabling this*
310− user-manual ${privoxy}/doc/privoxy/user-manual
311− listen-address ${cfg.client.privoxy.listenAddress}
313− enable-remote-toggle 0
314− enable-edit-actions 0
315− enable-remote-http-toggle 0
318− # Extra config goes here
324+ environment.systemPackages = [ pkgs.tor ];