From af115d746b2f96793cf39a1d5c1b11393da9b493 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Mon, 22 Aug 2022 14:57:39 +0200 Subject: [PATCH] internal scripts: use pinned, cached pkgs Instead of setting up the script PATH via nix-shell, use `nix shell` with inputs from the nix-bitcoin flake. Advantages: - Uses the nixpkgs version from the nix-bitcoin flake instead of `` from the user env (NIX_PATH), so the script runtime env is reproducible. - The pkg derivations for the runtime env are cached, which greatly increases script startup speed. This commit was generated by running the following script inside the repo root dir: def transform(path, src) if src =~ /#! *nix-shell +-i +bash +-p +(.*)/ pkgs = $1 if src =~ /^.*?(set -e.*?pipefail)\n/ set_statement = $1 src.sub!($&, '') end src.sub!(/\A.*?#! *nix-shell.*?\n/m, '') parents = ([ '..' ] * (path.split('/').count - 1)).join('/') [ '#!/usr/bin/env bash', *set_statement, %(. "${BASH_SOURCE[0]%/*}/#{parents}/helper/run-in-nix-env" "#{pkgs}" "$@"), nil, src ].join("\n") end end Dir['**/*.sh'].each do |f| src = File.read(f) if new_src = transform(f, src) puts "Changed file #{f}" File.write(f, new_src) end end --- .cirrus.yml | 2 +- helper/run-in-nix-env | 20 +++++++++++++++++++ pkgs/clightning-plugins/get-sha256.sh | 4 ++-- pkgs/clightning-rest/generate.sh | 4 ++-- pkgs/joinmarket/get-sha256.sh | 4 ++-- pkgs/krops/fetch-release.sh | 4 ++-- pkgs/lndinit/get-sha256.sh | 4 ++-- .../python-bitcointx/get-sha256.sh | 4 ++-- pkgs/rtl/generate.sh | 4 ++-- pkgs/spark-wallet/generate.sh | 4 ++-- test/run-tests.sh | 2 +- test/shellcheck.sh | 1 + 12 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 helper/run-in-nix-env diff --git a/.cirrus.yml b/.cirrus.yml index 3b56692..430078f 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -39,4 +39,4 @@ task: - name: shellcheck build_script: - - nix shell --inputs-from . nixpkgs#{shellcheck,findutils,gnugrep} -c ./test/shellcheck.sh + - ./test/shellcheck.sh diff --git a/helper/run-in-nix-env b/helper/run-in-nix-env new file mode 100644 index 0000000..d2e0976 --- /dev/null +++ b/helper/run-in-nix-env @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +# Usage: +# source "path/to/run-in-nix-env" "pkg1 pkg2 ..." "$@" +# +# Runs the calling script with pkgs1, pkg2, ... in PATH. +# The packages are sourced from the nixpkgs input of the nix-bitcoin root flake. + +if [[ ${_NIX_BITCOIN_ENV_PKGS:-} != "$1" ]]; then + export _NIX_BITCOIN_ENV_PKGS=$1 + shift + + _shell_pkgs=() + for _pkg in $_NIX_BITCOIN_ENV_PKGS; do + _shell_pkgs+=("nixpkgs#$_pkg") + done + # BASH_SOURCE[0] is this file + # BASH_SOURCE[-1] is the root src file + exec nix shell --inputs-from "${BASH_SOURCE[0]%/*}/.." "${_shell_pkgs[@]}" -c "${BASH_SOURCE[-1]}" "$@" +fi diff --git a/pkgs/clightning-plugins/get-sha256.sh b/pkgs/clightning-plugins/get-sha256.sh index 886d5f8..b66906f 100755 --- a/pkgs/clightning-plugins/get-sha256.sh +++ b/pkgs/clightning-plugins/get-sha256.sh @@ -1,6 +1,6 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i bash -p git +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git" "$@" archive_hash () { repo=$1 diff --git a/pkgs/clightning-rest/generate.sh b/pkgs/clightning-rest/generate.sh index 038be02..c03bdf3 100755 --- a/pkgs/clightning-rest/generate.sh +++ b/pkgs/clightning-rest/generate.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env nix-shell -#! nix-shell -i bash -p gnupg wget gnused +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "gnupg wget gnused" "$@" version="0.9.0" repo=https://github.com/Ride-The-Lightning/c-lightning-REST diff --git a/pkgs/joinmarket/get-sha256.sh b/pkgs/joinmarket/get-sha256.sh index 111aa68..1a36b12 100755 --- a/pkgs/joinmarket/get-sha256.sh +++ b/pkgs/joinmarket/get-sha256.sh @@ -1,6 +1,6 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i bash -p git gnupg +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git gnupg" "$@" TMPDIR="$(mktemp -d -p /tmp)" trap 'rm -rf $TMPDIR' EXIT diff --git a/pkgs/krops/fetch-release.sh b/pkgs/krops/fetch-release.sh index 8fa5c5a..44a56b9 100755 --- a/pkgs/krops/fetch-release.sh +++ b/pkgs/krops/fetch-release.sh @@ -1,6 +1,6 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i bash -p git +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git" "$@" archive_hash () { repo=$1 diff --git a/pkgs/lndinit/get-sha256.sh b/pkgs/lndinit/get-sha256.sh index cca5bca..7b3d310 100755 --- a/pkgs/lndinit/get-sha256.sh +++ b/pkgs/lndinit/get-sha256.sh @@ -1,6 +1,6 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i bash -p git gnupg curl jq +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git gnupg curl jq" "$@" TMPDIR=$(mktemp -d -p /tmp) trap 'rm -rf $TMPDIR' EXIT diff --git a/pkgs/python-packages/python-bitcointx/get-sha256.sh b/pkgs/python-packages/python-bitcointx/get-sha256.sh index 745ffb1..ce70cc8 100755 --- a/pkgs/python-packages/python-bitcointx/get-sha256.sh +++ b/pkgs/python-packages/python-bitcointx/get-sha256.sh @@ -1,6 +1,6 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i bash -p git gnupg +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../../helper/run-in-nix-env" "git gnupg" "$@" TMPDIR=$(mktemp -d -p /tmp) trap 'rm -rf $TMPDIR' EXIT diff --git a/pkgs/rtl/generate.sh b/pkgs/rtl/generate.sh index 0e19d44..937c0d2 100755 --- a/pkgs/rtl/generate.sh +++ b/pkgs/rtl/generate.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env nix-shell -#! nix-shell -i bash -p gnupg wget gnused +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "gnupg wget gnused" "$@" version="0.13.1" repo=https://github.com/Ride-The-Lightning/RTL diff --git a/pkgs/spark-wallet/generate.sh b/pkgs/spark-wallet/generate.sh index 50c3a83..ecbbd65 100755 --- a/pkgs/spark-wallet/generate.sh +++ b/pkgs/spark-wallet/generate.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env nix-shell -#! nix-shell -i bash -p nodePackages.node2nix gnupg wget jq moreutils gnused +#!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "nodePackages.node2nix gnupg wget jq moreutils gnused" "$@" TMPDIR=$(mktemp -d -p /tmp) trap 'rm -rf $TMPDIR' EXIT diff --git a/test/run-tests.sh b/test/run-tests.sh index 54f25a9..b380853 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -322,7 +322,7 @@ examples() { shellcheck() { if ! checkFlakeSupport "shellcheck"; then return; fi - nix shell --inputs-from "$scriptDir/.." nixpkgs#shellcheck -c "$scriptDir/shellcheck.sh" + "$scriptDir/shellcheck.sh" } all() { diff --git a/test/shellcheck.sh b/test/shellcheck.sh index 822f5d9..f6b697e 100755 --- a/test/shellcheck.sh +++ b/test/shellcheck.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash set -euo pipefail +. "${BASH_SOURCE[0]%/*}/../helper/run-in-nix-env" "shellcheck findutils gnugrep" "$@" cd "${BASH_SOURCE[0]%/*}/.." {