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
  `<nixpkgs>` 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
This commit is contained in:
Erik Arvstedt 2022-08-22 14:57:39 +02:00
parent 4cd9d0c6de
commit af115d746b
No known key found for this signature in database
GPG Key ID: 33312B944DD97846
12 changed files with 39 additions and 18 deletions

View File

@ -39,4 +39,4 @@ task:
- name: shellcheck - name: shellcheck
build_script: build_script:
- nix shell --inputs-from . nixpkgs#{shellcheck,findutils,gnugrep} -c ./test/shellcheck.sh - ./test/shellcheck.sh

20
helper/run-in-nix-env Normal file
View File

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

View File

@ -1,6 +1,6 @@
#! /usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p git
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git" "$@"
archive_hash () { archive_hash () {
repo=$1 repo=$1

View File

@ -1,6 +1,6 @@
#!/usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p gnupg wget gnused
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "gnupg wget gnused" "$@"
version="0.9.0" version="0.9.0"
repo=https://github.com/Ride-The-Lightning/c-lightning-REST repo=https://github.com/Ride-The-Lightning/c-lightning-REST

View File

@ -1,6 +1,6 @@
#! /usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p git gnupg
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git gnupg" "$@"
TMPDIR="$(mktemp -d -p /tmp)" TMPDIR="$(mktemp -d -p /tmp)"
trap 'rm -rf $TMPDIR' EXIT trap 'rm -rf $TMPDIR' EXIT

View File

@ -1,6 +1,6 @@
#! /usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p git
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git" "$@"
archive_hash () { archive_hash () {
repo=$1 repo=$1

View File

@ -1,6 +1,6 @@
#! /usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p git gnupg curl jq
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "git gnupg curl jq" "$@"
TMPDIR=$(mktemp -d -p /tmp) TMPDIR=$(mktemp -d -p /tmp)
trap 'rm -rf $TMPDIR' EXIT trap 'rm -rf $TMPDIR' EXIT

View File

@ -1,6 +1,6 @@
#! /usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p git gnupg
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../../helper/run-in-nix-env" "git gnupg" "$@"
TMPDIR=$(mktemp -d -p /tmp) TMPDIR=$(mktemp -d -p /tmp)
trap 'rm -rf $TMPDIR' EXIT trap 'rm -rf $TMPDIR' EXIT

View File

@ -1,6 +1,6 @@
#!/usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p gnupg wget gnused
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "gnupg wget gnused" "$@"
version="0.13.1" version="0.13.1"
repo=https://github.com/Ride-The-Lightning/RTL repo=https://github.com/Ride-The-Lightning/RTL

View File

@ -1,6 +1,6 @@
#!/usr/bin/env nix-shell #!/usr/bin/env bash
#! nix-shell -i bash -p nodePackages.node2nix gnupg wget jq moreutils gnused
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../../helper/run-in-nix-env" "nodePackages.node2nix gnupg wget jq moreutils gnused" "$@"
TMPDIR=$(mktemp -d -p /tmp) TMPDIR=$(mktemp -d -p /tmp)
trap 'rm -rf $TMPDIR' EXIT trap 'rm -rf $TMPDIR' EXIT

View File

@ -322,7 +322,7 @@ examples() {
shellcheck() { shellcheck() {
if ! checkFlakeSupport "shellcheck"; then return; fi if ! checkFlakeSupport "shellcheck"; then return; fi
nix shell --inputs-from "$scriptDir/.." nixpkgs#shellcheck -c "$scriptDir/shellcheck.sh" "$scriptDir/shellcheck.sh"
} }
all() { all() {

View File

@ -1,5 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
. "${BASH_SOURCE[0]%/*}/../helper/run-in-nix-env" "shellcheck findutils gnugrep" "$@"
cd "${BASH_SOURCE[0]%/*}/.." cd "${BASH_SOURCE[0]%/*}/.."
{ {