From 77af2e45389f02595f8c5d8c11a1aef0c3bb9528 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Mon, 13 Sep 2021 10:23:49 +0200 Subject: [PATCH] makeShell: improve `update-nix-bitcoin` - Don't overwrite `nix-bitcoin-release.nix` on errors - Show a message to indicate whether `nix-bitcoin-release.nix` was updated - Don't start a shell when called noninteractively Also, update `usage.md` and reformat `shell.nix`. --- docs/usage.md | 5 +++-- examples/shell.nix | 2 +- helper/makeShell.nix | 29 +++++++++++++++++++++-------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 9130e26..f7612e5 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,9 +1,10 @@ Updating --- -In your deployment directory, enter the nix shell with `nix-shell` and run +In your deployment directory, enter the nix shell with `nix-shell` and run the +following to update `nix-bitcoin-release.nix`: ``` -fetch-release > nix-bitcoin-release.nix +update-nix-bitcoin ``` Nodeinfo diff --git a/examples/shell.nix b/examples/shell.nix index d378e67..882fc2f 100644 --- a/examples/shell.nix +++ b/examples/shell.nix @@ -4,5 +4,5 @@ in import "${nix-bitcoin}/helper/makeShell.nix" { configDir = ./.; # Set this to modify your shell - # extraShellInitCmds = (pkgs: ''''); + # extraShellInitCmds = pkgs: ''''; } diff --git a/helper/makeShell.nix b/helper/makeShell.nix index c674d82..ab92b68 100644 --- a/helper/makeShell.nix +++ b/helper/makeShell.nix @@ -17,6 +17,11 @@ stdenv.mkDerivation rec { export NIX_BITCOIN_EXAMPLES_DIR="${cfgDir}" + # Set isInteractive=1 if + # 1. stdout is a TTY, i.e. we're not piping the output + # 2. the shell is interactive + if [[ -t 1 && $- == *i* ]]; then isInteractive=1; else isInteractive=; fi + help() { echo "nix-bitcoin path: ${toString ../.}" echo @@ -47,10 +52,21 @@ stdenv.mkDerivation rec { ${toString ./fetch-release} } - update-nix-bitcoin() { - fetch-release > "${cfgDir}/nix-bitcoin-release.nix" - exec nix-shell - } + update-nix-bitcoin() {( + set -euo pipefail + releaseFile="${cfgDir}/nix-bitcoin-release.nix" + current=$(cat "$releaseFile" 2>/dev/null || true) + new=$(fetch-release) + if [[ $new == $current ]]; then + echo "nix-bitcoin-release.nix already contains the latest release" + else + echo "$new" > "$releaseFile" + echo "Updated nix-bitcoin-release.nix" + if [[ $isInteractive ]]; then + exec nix-shell + fi + fi + )} generate-secrets() {( set -euo pipefail @@ -94,10 +110,7 @@ stdenv.mkDerivation rec { done } - # Print welcome message if - # 1. stdout is a TTY, i.e. we're not piping the output - # 2. the shell is interactive - if [[ -t 1 && $- == *i* ]]; then + if [[ $isInteractive ]]; then ${figlet}/bin/figlet "nix-bitcoin" echo 'Enter "h" or "help" for documentation.' fi