Skip to content

Instantly share code, notes, and snippets.

@grahamc
Created May 21, 2020 18:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamc/79967813bc9505f9bb14067ceac63af1 to your computer and use it in GitHub Desktop.
Save grahamc/79967813bc9505f9bb14067ceac63af1 to your computer and use it in GitHub Desktop.
mkSafeScript = { name, script, tools }:
let
as-written = pkgs.writeScript "${name}-as-written" ''
#!/usr/bin/env bash
${script}
'';
shellchecked-as-written = pkgs.runCommand "${name}-as-writtenshellcheck" {} ''
cp ${as-written} ./script
${pkgs.shellcheck}/bin/shellcheck ./script
mkdir $out
cp ./script $out/script
'';
resholved = buildResholvedPackage {
pname = name;
version = "0.0.0";
src = shellchecked-as-written;
scripts = [ "script" ];
inputs = tools;
installPhase = ''
${pkgs.shellcheck}/bin/shellcheck ./script
mkdir -p $out/bin
mv ./script $out/bin/script
'';
};
script-only = pkgs.runCommand name {} ''
cp ${resholved}/bin/script $out
chmod +x $out
'';
result = script-only;
in
result;
tools = with pkgs; [
findutils
gnugrep
coreutils
utillinux
];
script = ''
mount \
| grep /mnt \
| cut -d' ' -f3 \
| tac \
| xargs -n1 umount
find /dev/disk/by-path -mindepth 1 -not -name '*-part*' -print0 \
| xargs -0 -n1 wipefs -af
'';
and if I take one of those tools out of that list
10:30
mount \
^~~~~
/build/erase-disks-as-writtenshellcheck/script:2: Can't resolve command 'mount' to a known function or executable
builder for '/nix/store/iy8i68sxcd7aaiavi62dc8x7zbs9frb0-erase-disks-0.0.0.drv' failed with exit code 3
#!/nix/store/81wybawvkr95c7j8gj5ab3y740mq1fli-bash-4.4-p23/bin/bash
set -x
/nix/store/f5sg1v7n2as78f09lfx20qjbbyk9bmml-util-linux-2.35.1-bin/bin/mount \
| /nix/store/hi5655s7r4p82blynf5s0xzavjw36mgn-gnugrep-3.4/bin/grep /mnt \
| /nix/store/3qgzqqamhnwm178amyb46pdrzx2nakl5-coreutils-8.31/bin/cut -d' ' -f3 \
| /nix/store/3qgzqqamhnwm178amyb46pdrzx2nakl5-coreutils-8.31/bin/tac \
| /nix/store/hd2bn6gccjdwl1diikqvjjgvkz94rgk7-findutils-4.7.0/bin/xargs -n1 umount
/nix/store/hd2bn6gccjdwl1diikqvjjgvkz94rgk7-findutils-4.7.0/bin/find /dev/disk/by-path -mindepth 1 -not -name '*-part*' -print0 \
| /nix/store/hd2bn6gccjdwl1diikqvjjgvkz94rgk7-findutils-4.7.0/bin/xargs -0 -n1 wipefs -af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment