1 { config, lib, pkgs, ... }:
6 cfg = config.services.tor.torsocks;
7 optionalNullStr = b: v: optionalString (b != null) v;
9 configFile = server: ''
10 TorAddress ${toString (head (splitString ":" server))}
11 TorPort ${toString (tail (splitString ":" server))}
13 OnionAddrRange ${cfg.onionAddrRange}
15 ${optionalNullStr cfg.socks5Username
16 "SOCKS5Username ${cfg.socks5Username}"}
17 ${optionalNullStr cfg.socks5Password
18 "SOCKS5Password ${cfg.socks5Password}"}
20 AllowInbound ${if cfg.allowInbound then "1" else "0"}
23 wrapTorsocks = name: server: pkgs.writeTextFile {
26 #!${pkgs.runtimeShell}
27 TORSOCKS_CONF_FILE=${pkgs.writeText "torsocks.conf" (configFile server)} ${pkgs.torsocks}/bin/torsocks "$@"
30 destination = "/bin/${name}";
36 services.tor.torsocks = {
39 default = config.services.tor.enable && config.services.tor.client.enable;
40 defaultText = literalExpression "config.services.tor.enable && config.services.tor.client.enable";
42 Whether to build `/etc/tor/torsocks.conf`
43 containing the specified global torsocks configuration.
49 default = "127.0.0.1:9050";
50 example = "192.168.0.20:1234";
52 IP/Port of the Tor SOCKS server. Currently, hostnames are
53 NOT supported by torsocks.
57 fasterServer = mkOption {
59 default = "127.0.0.1:9063";
60 example = "192.168.0.20:1234";
62 IP/Port of the Tor SOCKS server for torsocks-faster wrapper suitable for HTTP.
63 Currently, hostnames are NOT supported by torsocks.
67 onionAddrRange = mkOption {
69 default = "127.42.42.0/24";
71 Tor hidden sites do not have real IP addresses. This
72 specifies what range of IP addresses will be handed to the
73 application as "cookies" for .onion names. Of course, you
74 should pick a block of addresses which you aren't going to
75 ever need to actually connect to. This is similar to the
76 MapAddress feature of the main tor daemon.
80 socks5Username = mkOption {
81 type = types.nullOr types.str;
85 SOCKS5 username. The `TORSOCKS_USERNAME`
86 environment variable overrides this option if it is set.
90 socks5Password = mkOption {
91 type = types.nullOr types.str;
95 SOCKS5 password. The `TORSOCKS_PASSWORD`
96 environment variable overrides this option if it is set.
100 allowInbound = mkOption {
104 Set Torsocks to accept inbound connections. If set to
105 `true`, listen() and accept() will be
106 allowed to be used with non localhost address.
113 config = mkIf cfg.enable {
114 environment.systemPackages = [ pkgs.torsocks (wrapTorsocks "torsocks-faster" cfg.fasterServer) ];
116 environment.etc."tor/torsocks.conf" =
118 source = pkgs.writeText "torsocks.conf" (configFile cfg.server);