diff --git a/pkgs/additional/sane-scripts/default.nix b/pkgs/additional/sane-scripts/default.nix index 302a386b..8e256e36 100644 --- a/pkgs/additional/sane-scripts/default.nix +++ b/pkgs/additional/sane-scripts/default.nix @@ -108,11 +108,17 @@ let }; py-scripts = { + # anything added to this attrset gets symlink-joined into into `sane-scripts` bt-search = static-nix-shell.mkPython3Bin { pname = "sane-bt-search"; src = ./src; pyPkgs = [ "natsort" "requests" ]; }; + bt-rm = static-nix-shell.mkBash { + pname = "sane-bt-rm"; + src = ./src; + pkgs = [ "transmission" ]; + }; date-math = static-nix-shell.mkPython3Bin { pname = "sane-date-math"; src = ./src; diff --git a/pkgs/additional/sane-scripts/src/sane-bt-rm b/pkgs/additional/sane-scripts/src/sane-bt-rm new file mode 100755 index 00000000..5d2908b7 --- /dev/null +++ b/pkgs/additional/sane-scripts/src/sane-bt-rm @@ -0,0 +1,11 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p transmission + +# removes a torrent and trashes its data +# usage: sane-bt-rm +# where is a magnet URL, or an identifier from sane-bt-show (e.g. 132) + +endpoint=https://bt.uninsane.org/transmission/rpc +PASS=$(sudo cat /run/secrets/transmission_passwd) + +transmission-remote "$endpoint" --auth "colin:$PASS" --torrent "$1" --remove-and-delete diff --git a/pkgs/additional/static-nix-shell/default.nix b/pkgs/additional/static-nix-shell/default.nix index c0f5e061..3c7db5d8 100644 --- a/pkgs/additional/static-nix-shell/default.nix +++ b/pkgs/additional/static-nix-shell/default.nix @@ -9,7 +9,49 @@ let inherit (builtins) attrNames attrValues concatStringsSep foldl' map typeOf; inherit (lib) concatMapAttrs; pkgs' = pkgs; + # create an attrset of + # = expected string in the nix-shell invocation + # = package to provide + pkgsToAttrs = prefix: pkgSet: expr: ({ + "lambda" = expr: pkgsToAttrs prefix pkgSet (expr pkgSet); + "list" = expr: foldl' (acc: pname: acc // { + "${prefix + pname}" = pkgSet."${pname}"; + }) {} expr; + "set" = expr: expr; + })."${typeOf expr}" expr; in { + mkBash = { pname, pkgs ? {}, srcPath ? pname, ...}@attrs: + let + pkgsAsAttrs = pkgsToAttrs "" pkgs' pkgs; + pkgsEnv = attrValues pkgsAsAttrs; + pkgsStr = concatStringsSep "" (map + (pname: " -p ${pname}") + (attrNames pkgsAsAttrs) + ); + in stdenv.mkDerivation ({ + version = "0.1.0"; # default version + patchPhase = '' + substituteInPlace ${srcPath} \ + --replace '#!/usr/bin/env nix-shell' '#!${pkgs'.bash}/bin/bash' \ + --replace \ + '#!nix-shell -i bash -p ${pkgsStr}' \ + '# nix deps evaluated statically' + ''; + nativeBuildInputs = [ makeWrapper ]; + installPhase = '' + mkdir -p $out/bin + mv ${srcPath} $out/bin/${srcPath} + + # ensure that all nix-shell references were substituted + ! grep nix-shell $out/bin/${srcPath} + + # add runtime dependencies to PATH + wrapProgram $out/bin/${srcPath} \ + --suffix PATH : ${lib.makeBinPath pkgsEnv } + ''; + } // (removeAttrs attrs [ "pkgs" "pyPkgs" "srcPath" ]) + ); + # transform a file which uses `#!/usr/bin/env nix-shell` shebang with a `python3` interpreter # into a derivation that can be built statically. # @@ -21,16 +63,6 @@ in { # for pyPkgs, names are assumed to be relative to `"ps"` if specified in list form. mkPython3Bin = { pname, pkgs ? {}, pyPkgs ? {}, srcPath ? pname, ... }@attrs: let - # create an attrset of - # = expected string in the nix-shell invocation - # = package to provide - pkgsToAttrs = prefix: pkgSet: expr: ({ - "lambda" = expr: pkgsToAttrs prefix pkgSet (expr pkgSet); - "list" = expr: foldl' (acc: pname: acc // { - "${prefix + pname}" = pkgSet."${pname}"; - }) {} expr; - "set" = expr: expr; - })."${typeOf expr}" expr; pyEnv = python3.withPackages (ps: attrValues ( pkgsToAttrs "ps." ps pyPkgs ));