sane-scripts: add sane-bt-rm to remove torrents

also rework statix-nix-shell to support bash
This commit is contained in:
Colin 2023-05-13 10:31:48 +00:00
parent efd45c58f1
commit 0270ccdebd
3 changed files with 59 additions and 10 deletions

View File

@ -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;

View File

@ -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 <torrent>
# where <torrent> 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

View File

@ -9,7 +9,49 @@ let
inherit (builtins) attrNames attrValues concatStringsSep foldl' map typeOf;
inherit (lib) concatMapAttrs;
pkgs' = pkgs;
# create an attrset of
# <name> = expected string in the nix-shell invocation
# <value> = 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
# <name> = expected string in the nix-shell invocation
# <value> = 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
));