nix-develop/nix-develop-gha.sh

50 lines
1.6 KiB
Bash
Raw Normal View History

2023-10-18 23:53:26 +00:00
#!/usr/bin/env bash
set -euo pipefail
# Read the arguments input into an array, so it can be added to a command line.
IFS=" " read -r -a arguments <<<"${INPUT_ARGUMENTS:-}"
with_nix_develop() {
nix develop --ignore-environment "${arguments[@]}" --command "$@"
}
contains() {
grep "$1" --silent <<<"$2"
}
2023-10-18 23:53:26 +00:00
# Add all environment variables except for PATH to GITHUB_ENV.
while IFS='=' read -r -d '' n v; do
if [ "$n" == "PATH" ]; then
continue
fi
if (("$(wc -l <<<"$v")" > 1)); then
delimiter=$(openssl rand -base64 18)
if contains "$delimiter" "$v"; then
echo "Environment variable $n contains randomly generated string $delimiter, file an issue and buy a lottery ticket."
exit 1
fi
2023-10-18 23:53:26 +00:00
printf "%s<<%s\n%s%s\n" "$n" "$delimiter" "$v" "$delimiter" >>"${GITHUB_ENV:-/dev/stderr}"
continue
fi
printf "%s=%s\n" "$n" "$v" >>"${GITHUB_ENV:-/dev/stderr}"
done < <(with_nix_develop env -0)
2023-10-18 23:53:26 +00:00
# Read the nix environment's $PATH into an array
IFS=":" read -r -a nix_path_array <<<"$(with_nix_develop bash -c "echo \$PATH")"
2023-10-18 23:53:26 +00:00
# Iterate over the PATH array in reverse
#
# Why in reverse? Appending a directory to $GITHUB_PATH causes that directory
# to be _prepended_ to $PATH in subsequent steps, so if we append in
# first-to-last order, the result will be in last-to-first order. Order in
# PATH elements is significant, since it determines lookup order, thus we
# preserve their order by reversing them before they are reversed again.
for ((i = ${#nix_path_array[@]} - 1; i >= 0; i--)); do
nix_path_entry="${nix_path_array[$i]}"
if contains "$nix_path_entry" "$PATH"; then
2023-10-18 23:53:26 +00:00
continue
fi
echo "$nix_path_entry" >>"${GITHUB_PATH:-/dev/stderr}"
done