nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 runCommand,
4 transmission_3_noSystemd,
5 rqbit,
6 writeShellScript,
7 formats,
8 cacert,
9 rsync,
10}:
11let
12 urlRegexp = ''.*xt=urn:bt[im]h:([^&]{64}|[^&]{40}).*'';
13in
14{
15 url,
16 name ?
17 if (builtins.match urlRegexp url) == null then
18 "bittorrent"
19 else
20 "bittorrent-" + builtins.head (builtins.match urlRegexp url),
21 config ?
22 if (backend == "transmission") then
23 { }
24 else
25 throw "json config for configuring fetchFromBitorrent only works with the transmission backend",
26 hash,
27 backend ? "transmission",
28 recursiveHash ? true,
29 postFetch ? "",
30 postUnpack ? "",
31 meta ? { },
32}:
33let
34 afterSuccess = writeShellScript "fetch-bittorrent-done.sh" ''
35 ${postUnpack}
36 # Flatten the directory, so that only the torrent contents are in $out, not
37 # the folder name
38 shopt -s dotglob
39 mv -v $downloadedDirectory/*/* $out
40 rm -v -rf $downloadedDirectory
41 unset downloadedDirectory
42 ${postFetch}
43 kill $PPID
44 '';
45 jsonConfig = (formats.json { }).generate "jsonConfig" config;
46in
47runCommand name
48 {
49 inherit meta;
50 nativeBuildInputs = [
51 cacert
52 ]
53 ++ (
54 if (backend == "transmission") then
55 [ transmission_3_noSystemd ]
56 else if (backend == "rqbit") then
57 [ rqbit ]
58 else
59 throw "rqbit or transmission are the only available backends for fetchtorrent"
60 );
61 outputHashAlgo = if hash != "" then null else "sha256";
62 outputHash = hash;
63 outputHashMode = if recursiveHash then "recursive" else "flat";
64
65 # url will be written to the derivation, meaning it can be parsed and utilized
66 # by external tools, such as tools that may want to seed fetchtorrent calls
67 # in nixpkgs
68 inherit url;
69 }
70 (
71 if (backend == "transmission") then
72 ''
73 export HOME=$TMP
74 export downloadedDirectory=$out/downloadedDirectory
75 mkdir -p $downloadedDirectory
76 mkdir -p $HOME/.config/transmission
77 cp ${jsonConfig} $HOME/.config/transmission/settings.json
78 function handleChild {
79 # This detects failures and logs the contents of the transmission fetch
80 find $out
81 exit 0
82 }
83 trap handleChild CHLD
84 transmission-cli --port $(shuf -n 1 -i 49152-65535) --portmap --finish ${afterSuccess} --download-dir $downloadedDirectory --config-dir "$HOME"/.config/transmission "$url"
85 ''
86 else
87 ''
88 export HOME=$TMP
89 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"
90 ''
91 )