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:-}"
|
|
|
|
|
2023-10-19 03:25:06 +00:00
|
|
|
with_nix_develop() {
|
|
|
|
nix develop --ignore-environment "${arguments[@]}" --command "$@"
|
|
|
|
}
|
|
|
|
|
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 12)
|
|
|
|
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}"
|
2023-10-19 03:25:06 +00:00
|
|
|
done < <(with_nix_develop env -0)
|
2023-10-18 23:53:26 +00:00
|
|
|
|
|
|
|
# Read the nix environment's $PATH into an array
|
2023-10-19 03:25:06 +00:00
|
|
|
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]}"
|
|
|
|
# Don't add anything that's already present in the $PATH
|
|
|
|
if echo "$PATH" | grep "$nix_path_entry" --silent; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
echo "$nix_path_entry" >>"${GITHUB_PATH:-/dev/stderr}"
|
|
|
|
done
|