From d7cbfcccbd5e1bd9d8ae1c744325b3a08bca3818 Mon Sep 17 00:00:00 2001 From: Nick Novitski Date: Sun, 27 Oct 2024 10:40:27 -0700 Subject: [PATCH] Handle stdout from shell hooks Before this commit, any `shellHook` which emitted to stdout would cause the action to fail with a message like `: Invalid variable name`, as the hook's output would get interpreted as part of the output of `env -0` and `echo $PATH`. After this commit, those commands are prefixed by a random delimiter, and all output before that delimiter is ignored. Type: fix Issue: 11 --- flake.nix | 2 +- nix-develop-gha.sh | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index 2f21729..bc81859 100644 --- a/flake.nix +++ b/flake.nix @@ -16,7 +16,7 @@ packages = eachSystem ({pkgs, ...}: { default = pkgs.writeShellApplication { name = "nix-develop-gha"; - runtimeInputs = [pkgs.gnugrep pkgs.openssl.bin pkgs.coreutils]; + runtimeInputs = [pkgs.gnugrep pkgs.openssl.bin pkgs.coreutils pkgs.gnused]; text = builtins.readFile ./nix-develop-gha.sh; }; }); diff --git a/nix-develop-gha.sh b/nix-develop-gha.sh index 82d8bf5..629ce5a 100755 --- a/nix-develop-gha.sh +++ b/nix-develop-gha.sh @@ -6,7 +6,8 @@ set -euo pipefail IFS=" " read -r -a arguments <<<"${@:-./#default}" with_nix_develop() { - nix develop "${arguments[@]}" --command "$@" + delimiter=$(openssl rand -base64 18) + nix develop "${arguments[@]}" --command bash -c "echo $delimiter; $*" | sed -n "\|$delimiter|,\$p" | tail -n +2 } with_nix_develop true # Exit immediately if build fails @@ -35,10 +36,10 @@ while IFS='=' read -r -d '' n v; do continue fi printf "%s=%s\n" "$n" "$v" >>"${GITHUB_ENV:-/dev/stderr}" -done < <(with_nix_develop env -0) +done < <(with_nix_develop "env -0") # Read the nix environment's $PATH into an array -IFS=":" read -r -a nix_path_array <<<"$(with_nix_develop bash -c "echo \$PATH")" +IFS=":" read -r -a nix_path_array <<<"$(with_nix_develop "echo \$PATH")" # Iterate over the PATH array in reverse #