/ modules / common / tor.nix
tor.nix
 1  {
 2    config,
 3    pkgs,
 4    lib,
 5    ...
 6  }:
 7  let
 8    inherit (lib) mkIf;
 9  in
10  {
11    home-manager.sharedModules = [
12      {
13        # Create a .torrc file in the home directory
14        home.file.".torrc".text = ''
15          # General Tor configuration
16          DataDirectory ~/.tor
17          RunAsDaemon 1
18          ControlPort 9051
19          CookieAuthentication 1
20          CookieAuthFile ~/.tor/control_auth_cookie
21  
22          # Radicle hidden service configuration
23          HiddenServiceDir ~/.tor/radicle_service/
24          HiddenServicePort 8776 127.0.0.1:8776
25  
26          # Connection settings
27          SocksPort 9050
28          DNSPort 9053
29        '';
30  
31        # Add a launcher script for radicle with tor
32        home.file.".local/bin/rad-tor" = {
33          executable = true;
34          text = ''
35            #!/bin/sh
36            # Ensure Tor is running
37            if ! pgrep -x tor > /dev/null; then
38              echo "Starting Tor..."
39              ${pkgs.tor}/bin/tor -f ~/.torrc &
40              sleep 3
41            fi
42  
43            # Get the onion address
44            ONION_ADDRESS=$(cat ~/.tor/radicle_service/hostname 2>/dev/null || echo "Onion address not yet available")
45            echo "Radicle available at: $ONION_ADDRESS"
46  
47            # Launch radicle node with tor proxy settings
48            export http_proxy=socks5://127.0.0.1:9050
49            export https_proxy=socks5://127.0.0.1:9050
50            ${pkgs.radicle-node}/bin/rad "$@"
51          '';
52        };
53  
54        # Create required directories - using direct activation syntax
55        home.activation.setupTorDirs = {
56          after = [ "writeBoundary" ];
57          before = [ ];
58          data = ''
59            mkdir -p ~/.tor/radicle_service
60            chmod 700 ~/.tor
61            chmod 700 ~/.tor/radicle_service
62            mkdir -p ~/.local/bin
63          '';
64        };
65      }
66    ];
67  }