1 { lib, runCommand, transmission_3_noSystemd, rqbit, writeShellScript, formats, cacert, rsync }:
3 urlRegexp = ''.*xt=urn:bt[im]h:([^&]{64}|[^&]{40}).*'';
7 if (builtins.match urlRegexp url) == null then
10 "bittorrent-" + builtins.head (builtins.match urlRegexp url)
11 , config ? if (backend == "transmission") then { } else throw "json config for configuring fetchFromBitorrent only works with the transmission backend"
13 , backend ? "transmission"
14 , recursiveHash ? true
20 afterSuccess = writeShellScript "fetch-bittorrent-done.sh" ''
22 # Flatten the directory, so that only the torrent contents are in $out, not
25 mv -v $downloadedDirectory/*/* $out
26 rm -v -rf $downloadedDirectory
27 unset downloadedDirectory
31 jsonConfig = (formats.json {}).generate "jsonConfig" config;
35 nativeBuildInputs = [ cacert ] ++ (if (backend == "transmission" ) then [ transmission_3_noSystemd ] else if (backend == "rqbit") then [ rqbit ] else throw "rqbit or transmission are the only available backends for fetchtorrent");
36 outputHashAlgo = if hash != "" then null else "sha256";
38 outputHashMode = if recursiveHash then "recursive" else "flat";
40 # url will be written to the derivation, meaning it can be parsed and utilized
41 # by external tools, such as tools that may want to seed fetchtorrent calls
45 (if (backend == "transmission") then ''
47 export downloadedDirectory=$out/downloadedDirectory
48 mkdir -p $downloadedDirectory
49 mkdir -p $HOME/.config/transmission
50 cp ${jsonConfig} $HOME/.config/transmission/settings.json
51 function handleChild {
52 # This detects failures and logs the contents of the transmission fetch
57 transmission-cli --port $(shuf -n 1 -i 49152-65535) --portmap --finish ${afterSuccess} --download-dir $downloadedDirectory --config-dir "$HOME"/.config/transmission "$url"
61 rqbit --disable-dht-persistence --http-api-listen-addr "127.0.0.1:$(shuf -n 1 -i 49152-65535)" download -o $out --exit-on-finish "$url"