Skip to content

Commit

Permalink
Merge pull request #330830 from tie/nix-develop-unbound-variable
Browse files Browse the repository at this point in the history
stdenv: fix unbound NIX_LOG_FD in `nix develop`
  • Loading branch information
drupol authored Jul 30, 2024
2 parents 5488d27 + bd872b4 commit 5efe6c1
Showing 1 changed file with 48 additions and 54 deletions.
102 changes: 48 additions & 54 deletions pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,63 +47,57 @@ getAllOutputNames() {
fi
}

if [[ -n "${NIX_LOG_FD:-}" ]]; then
# Logs arguments to $NIX_LOG_FD, if it exists, no-op if it does not.
nixLog() {
echo "$@" >&"$NIX_LOG_FD"
}
# Logs arguments to $NIX_LOG_FD, if it exists, no-op if it does not.
nixLog() {
if [[ -z ${NIX_LOG_FD-} ]]; then
return
fi
echo "$@" >&"$NIX_LOG_FD"
}

# Log a hook, to be run before the hook is actually called.
# logging for "implicit" hooks -- the ones specified directly
# in derivation's arguments -- is done in _callImplicitHook instead.
_logHook() {
local hookKind="$1"
local hookExpr="$2"
shift 2

if declare -F "$hookExpr" > /dev/null 2>&1; then
nixLog "calling '$hookKind' function hook '$hookExpr'" "$@"
elif type -p "$hookExpr" > /dev/null; then
nixLog "sourcing '$hookKind' script hook '$hookExpr'"
elif [[ "$hookExpr" != "_callImplicitHook"* ]]; then
# Here we have a string hook to eval.
# Join lines onto one with literal \n characters unless NIX_DEBUG >= 2.
local exprToOutput
if (( "${NIX_DEBUG:-0}" >= 2 )); then
exprToOutput="$hookExpr"
else
# We have `r'\n'.join([line.lstrip() for lines in text.split('\n')])` at home.
local hookExprLine
while IFS= read -r hookExprLine; do
# These lines often have indentation,
# so let's remove leading whitespace.
hookExprLine="${hookExprLine#"${hookExprLine%%[![:space:]]*}"}"
# If this line wasn't entirely whitespace,
# then add it to our output
if [[ -n "$hookExprLine" ]]; then
exprToOutput+="$hookExprLine\\n "
fi
done <<< "$hookExpr"
# Log a hook, to be run before the hook is actually called.
# logging for "implicit" hooks -- the ones specified directly
# in derivation's arguments -- is done in _callImplicitHook instead.
_logHook() {
# Fast path in case nixLog is no-op.
if [[ -z ${NIX_LOG_FD-} ]]; then
return
fi

# And then remove the final, unnecessary, \n
exprToOutput="${exprToOutput%%\\n }"
fi
nixLog "evaling '$hookKind' string hook '$exprToOutput'"
fi
}
else
nixLog() {
# Stub.
# Note: because bash syntax, this colon is load bearing. Removing it
# will turn this function into a syntax error.
:
}
local hookKind="$1"
local hookExpr="$2"
shift 2

_logHook() {
# Load-bearing colon; same as above.
:
}
fi
if declare -F "$hookExpr" > /dev/null 2>&1; then
nixLog "calling '$hookKind' function hook '$hookExpr'" "$@"
elif type -p "$hookExpr" > /dev/null; then
nixLog "sourcing '$hookKind' script hook '$hookExpr'"
elif [[ "$hookExpr" != "_callImplicitHook"* ]]; then
# Here we have a string hook to eval.
# Join lines onto one with literal \n characters unless NIX_DEBUG >= 2.
local exprToOutput
if (( "${NIX_DEBUG:-0}" >= 2 )); then
exprToOutput="$hookExpr"
else
# We have `r'\n'.join([line.lstrip() for lines in text.split('\n')])` at home.
local hookExprLine
while IFS= read -r hookExprLine; do
# These lines often have indentation,
# so let's remove leading whitespace.
hookExprLine="${hookExprLine#"${hookExprLine%%[![:space:]]*}"}"
# If this line wasn't entirely whitespace,
# then add it to our output
if [[ -n "$hookExprLine" ]]; then
exprToOutput+="$hookExprLine\\n "
fi
done <<< "$hookExpr"

# And then remove the final, unnecessary, \n
exprToOutput="${exprToOutput%%\\n }"
fi
nixLog "evaling '$hookKind' string hook '$exprToOutput'"
fi
}

######################################################################
# Hook handling.
Expand Down

0 comments on commit 5efe6c1

Please sign in to comment.