From 0c06237cacc9ab784ae7dae72e40704c98f8a991 Mon Sep 17 00:00:00 2001 From: Jan-Bulthuis Date: Wed, 18 Nov 2026 12:24:18 +0100 Subject: [PATCH] feat: seedbox setup --- hosts/ws-home/configuration.nix | 5 +- hosts/ws-home/seedbox.nix | 213 ++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 hosts/ws-home/seedbox.nix diff --git a/hosts/ws-home/configuration.nix b/hosts/ws-home/configuration.nix index c08a030..dff4901 100644 --- a/hosts/ws-home/configuration.nix +++ b/hosts/ws-home/configuration.nix @@ -93,5 +93,8 @@ }; # Set up hardware - imports = [ ./hardware-configuration.nix ]; + imports = [ + ./hardware-configuration.nix + ./seedbox.nix + ]; } diff --git a/hosts/ws-home/seedbox.nix b/hosts/ws-home/seedbox.nix new file mode 100644 index 0000000..6694c50 --- /dev/null +++ b/hosts/ws-home/seedbox.nix @@ -0,0 +1,213 @@ +{ + config, + inputs, + lib, + pkgs, + ... +}: + +# Always-on torrent client, confined to a VPN-only network namespace, plus a +# read-only, password-protected WebDAV share of its downloads for the local network. +# +# Everything provider-specific lives in secrets: +# seedbox/wireguard wg-quick style config (keys, peer, Address, DNS, MTU) +# seedbox/port the incoming port forwarded by the VPN +# seedbox/share-password password for the share (user "seedbox") +# +# Web interface: only on this host's localhost, e.g. `ssh -L 8081:127.0.0.1:8081 ws-home` +let + netns = "seedbox"; + wgIf = "wg-seedbox"; + dataDir = "/var/lib/seedbox"; + downloadsDir = "${dataDir}/downloads"; + webuiProxyPort = 8081; # localhost of this host only + webuiPort = 8080; # inside the namespace + sharePort = 8091; + + secret = name: config.sops.secrets."seedbox/${name}".path; + secretFile = "${inputs.secrets}/secrets/ws-home.enc.yaml"; + + user = config.services.qbittorrent.user; + group = config.services.qbittorrent.group; + inNetns = { + NetworkNamespacePath = "/run/netns/${netns}"; + }; + netnsDeps = { + bindsTo = [ "seedbox-netns.service" ]; + after = [ "seedbox-netns.service" ]; + }; +in +{ + sops.secrets = { + "seedbox/wireguard".sopsFile = secretFile; + "seedbox/port" = { + sopsFile = secretFile; + owner = user; + }; + "seedbox/share-password" = { + sopsFile = secretFile; + owner = user; + }; + }; + + modules.impermanence.directories = [ + { + directory = dataDir; + inherit user group; + mode = "0750"; + } + ]; + systemd.tmpfiles.rules = [ "d ${downloadsDir} 0750 ${user} ${group} -" ]; + + # Network namespace whose only route out is the WireGuard interface. The interface + # is created here and moved into the namespace, so its encrypted traffic uses the + # host's network, but nothing inside the namespace can reach anything else. + systemd.services.seedbox-netns = { + description = "Network namespace for the seedbox"; + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + path = with pkgs; [ + iproute2 + wireguard-tools + gnugrep + gnused + coreutils + ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + RuntimeDirectory = "seedbox"; + RuntimeDirectoryPreserve = true; + }; + script = '' + set -euo pipefail + conf=${secret "wireguard"} + field() { grep -iE "^\s*$1\s*=" "$conf" | cut -d= -f2- | tr ',' '\n' | sed 's/ //g'; } + + ip netns del ${netns} 2>/dev/null || true + ip link del ${wgIf} 2>/dev/null || true + + ip netns add ${netns} + ip -n ${netns} link set lo up + + ip link add ${wgIf} type wireguard + wg setconf ${wgIf} <(wg-quick strip "$conf") + ip link set ${wgIf} netns ${netns} + for addr in $(field Address); do + ip -n ${netns} addr add "$addr" dev ${wgIf} + done + mtu=$(field MTU | head -1) + ip -n ${netns} link set ${wgIf} mtu "''${mtu:-1420}" up + ip -n ${netns} route add default dev ${wgIf} + if field Address | grep -q ':'; then + ip -n ${netns} -6 route add default dev ${wgIf} + fi + + # DNS only through the tunnel + field DNS | grep -v ':' | sed 's/^/nameserver /' > /run/seedbox/resolv.conf + echo 'hosts: files dns' > /run/seedbox/nsswitch.conf + ''; + preStop = '' + ip netns del ${netns} || true + ''; + }; + + services.qbittorrent = { + enable = true; + profileDir = dataDir; + inherit webuiPort; + extraArgs = [ "--confirm-legal-notice" ]; + }; + + systemd.services.qbittorrent = netnsDeps // { + serviceConfig = inNetns // { + # Resolve names only with the tunnel's DNS. The host's nscd would resolve them + # outside the namespace, so it is hidden. + BindReadOnlyPaths = [ + "/run/seedbox/resolv.conf:/etc/resolv.conf" + "/run/seedbox/nsswitch.conf:/etc/nsswitch.conf" + ]; + InaccessiblePaths = [ "-/run/nscd" ]; + ReadWritePaths = [ dataDir ]; + + # Initial settings, and the incoming port from the secret on every start. Other + # settings are left to the web interface. + ExecStartPre = pkgs.writeShellScript "seedbox-config" '' + set -euo pipefail + PATH=${ + lib.makeBinPath ( + with pkgs; + [ + coreutils + gnused + ] + ) + } + port=$(tr -dc 0-9 < ${secret "port"}) + cfg=${dataDir}/qBittorrent/config/qBittorrent.conf + mkdir -p ${downloadsDir} "$(dirname "$cfg")" + if [ ! -f "$cfg" ]; then + cat > "$cfg" <