1{ config, lib, pkgs, ... }:
2with lib;
3let
4
5 cfg = config.services.tor;
6
7 torify = pkgs.writeTextFile {
8 name = "tsocks";
9 text = ''
10 #!${pkgs.stdenv.shell}
11 TSOCKS_CONF_FILE=${pkgs.writeText "tsocks.conf" cfg.tsocks.config} LD_PRELOAD="${pkgs.tsocks}/lib/libtsocks.so $LD_PRELOAD" "$@"
12 '';
13 executable = true;
14 destination = "/bin/tsocks";
15 };
16
17in
18
19{
20
21 ###### interface
22
23 options = {
24
25 services.tor.tsocks = {
26
27 enable = mkOption {
28 default = cfg.enable && cfg.client.enable;
29 description = ''
30 Whether to build tsocks wrapper script to relay application traffic via TOR.
31 '';
32 };
33
34 server = mkOption {
35 default = "localhost:9050";
36 example = "192.168.0.20";
37 description = ''
38 IP address of TOR client to use.
39 '';
40 };
41
42 config = mkOption {
43 default = "";
44 description = ''
45 Extra configuration. Contents will be added verbatim to TSocks
46 configuration file.
47 '';
48 };
49
50 };
51
52 };
53
54 ###### implementation
55
56 config = mkIf cfg.tsocks.enable {
57
58 environment.systemPackages = [ torify ]; # expose it to the users
59
60 services.tor.tsocks.config = ''
61 server = ${toString(head (splitString ":" cfg.tsocks.server))}
62 server_port = ${toString(tail (splitString ":" cfg.tsocks.server))}
63
64 local = 127.0.0.0/255.128.0.0
65 local = 127.128.0.0/255.192.0.0
66 '';
67 };
68
69}