summary refs log tree commit diff
path: root/services/torrent/default.nix
blob: dbbdf7c358aee33e99d3b35e90ed7637324a0361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
  me,
  config,
  lib,
  pkgs,
  auxiliaryPkgs,
  ...
}:

let
  inherit (pkgs) dockerTools;
  inherit (auxiliaryPkgs) common;

  torrentLocalPort = 2018;
  torrentDir = "/srv/torrent";

  mtu = 1280;

  # gluetun openvpn likes to ignore my mtu settings,
  # so we set it forcefully every 15 seconds.
  #vpn-force-mtu = pkgs.writeTextFile {
  #  name = "vpn-force-mtu";
  #  destination = "/scripts/vpn-force-mtu.sh";
  #  executable = true;
  #  text = ''
  #    #!/bin/ash
  #    while true; do
  #      /bin/sleep 15
  #      /sbin/ip link set dev tun0 mtu ${toString mtu} 2>/dev/null || true
  #    done
  #  '';
  #};
  vpn-entry = pkgs.writeTextFile {
    name = "vpn-entry";
    destination = "/scripts/vpn-entry.sh";
    executable = true;
    text = ''
      #!/bin/ash
      #/scripts/vpn-force-mtu.sh &
      /gluetun-entrypoint
    '';
  };

  vpn-scripts = pkgs.symlinkJoin {
    name = "vpn-scripts";
    paths = [
      vpn-entry
      #vpn-force-mtu
    ];
  };

  gluetunImage = common.pullImage {
    name = "qmcgaw/gluetun";
    tag = "v3.39";
    digest = "sha256:6a8058e626763cbf735ac2f78c774dbb24fec2490bd9d9f7d67e22592cb4a991";
    x86.sha256 = "1cg43lmp3ql64zsfwp2f52kigijs30n3hnja12msr9npbgq8a8ga";
  };

  vpnImage = dockerTools.streamLayeredImage {
    name = "vpn";
    tag = "3.39.0-renard";
    fromImage = gluetunImage.base;
    contents = [ vpn-scripts ];
  };

  vpnCountries = [
    "Albania"
    "Austria"
    "Belgium"
    "Bulgaria"
    "Croatia"
    "Cyprus"
    "Czech Republic"
    "Spain"
    "Estonia"
    "Greece"
    "Hungary"
    "Italy"
    "Poland"
    "Portugal"
    "Romania"
    "Serbia"
    "Turkey"
    "Ukraine"
  ];
in
{
  imports = [
    ./flood.nix
    # pick current client through import
    ./transmission.nix
    #./qbittorrent.nix
  ];

  age.secrets.mullvad-gluetun = {
    file = ../../secrets/mullvad-gluetun.age;
  };

  foundation = {
    networks.vpn = {
      enable = true;
      driver = "bridge";
      # we currenly avoid ipv6 for vpn.
      ipv6.enable = false;
      # lower MTU to prevent packet non-deliverability
      inherit mtu;
    };

    services = {
      vpn = {
        network = "vpn";

        image = vpnImage;

        ports = [
          (common.tailnetPort me [
            torrentLocalPort
            torrentLocalPort
          ])
        ];

        volumes = [
          [
            "${torrentDir}/gluetun"
            "/gluetun"
          ]
        ];

        entrypoint = "/scripts/vpn-entry.sh";

        capabilities = [ "NET_ADMIN" ];
        devices = [ "/dev/net/tun" ];

        environment = {
          # the mullvad device representing this vpn container
          # is named "driven fish".
          VPN_SERVICE_PROVIDER = "mullvad";
          VPN_TYPE = "wireguard";
          WIREGUARD_ADDRESSES = "10.73.131.255/32";
          WIREGUARD_MTU = toString mtu;
          SERVER_COUNTRIES = lib.concatStringsSep "," vpnCountries;
        };

        environmentFiles = [ config.age.secrets.mullvad-gluetun.path ];
      };
    };
  };
}