···641641# Textual substitution functions.
642642643643644644-substitute() {
645645- local input="$1"
646646- local output="$2"
647647- shift 2
648648-649649- if [ ! -f "$input" ]; then
650650- echo "substitute(): ERROR: file '$input' does not exist" >&2
651651- return 1
652652- fi
653653-654654- local content
655655- # read returns non-0 on EOF, so we want read to fail
656656- if IFS='' read -r -N 0 content < "$input"; then
657657- echo "substitute(): ERROR: File \"$input\" has null bytes, won't process" >&2
658658- return 1
659659- fi
644644+substituteStream() {
645645+ local var=$1
646646+ shift
660647661648 while (( "$#" )); do
662649 case "$1" in
···671658 shift 2
672659 # check if the used nix attribute name is a valid bash name
673660 if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
674674- echo "substitute(): ERROR: substitution variables must be valid Bash names, \"$varName\" isn't." >&2
661661+ echo "substituteStream(): ERROR: substitution variables must be valid Bash names, \"$varName\" isn't." >&2
675662 return 1
676663 fi
677664 pattern="@$varName@"
···685672 ;;
686673687674 *)
688688- echo "substitute(): ERROR: Invalid command line argument: $1" >&2
675675+ echo "substituteStream(): ERROR: Invalid command line argument: $1" >&2
689676 return 1
690677 ;;
691678 esac
692679693693- content="${content//"$pattern"/$replacement}"
680680+ eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}'
694681 done
695682696696- if [ -e "$output" ]; then chmod +w "$output"; fi
697697- printf "%s" "$content" > "$output"
683683+ printf "%s" "${!var}"
698684}
699685686686+consumeEntire() {
687687+ # read returns non-0 on EOF, so we want read to fail
688688+ if IFS='' read -r -N 0 $1; then
689689+ echo "consumeEntire(): ERROR: Input null bytes, won't process" >&2
690690+ return 1
691691+ fi
692692+}
693693+694694+substitute() {
695695+ local input="$1"
696696+ local output="$2"
697697+ shift 2
698698+699699+ if [ ! -f "$input" ]; then
700700+ echo "substitute(): ERROR: file '$input' does not exist" >&2
701701+ return 1
702702+ fi
703703+704704+ local content
705705+ consumeEntire content < "$input"
706706+707707+ if [ -e "$output" ]; then chmod +w "$output"; fi
708708+ substituteStream content "$@" > "$output"
709709+}
700710701711substituteInPlace() {
702712 local fileName="$1"
···704714 substitute "$fileName" "$fileName" "$@"
705715}
706716717717+_allFlags() {
718718+ for varName in $(awk 'BEGIN { for (v in ENVIRON) if (v ~ /^[a-z][a-zA-Z0-9_]*$/) print v }'); do
719719+ if (( "${NIX_DEBUG:-0}" >= 1 )); then
720720+ printf "@%s@ -> %q\n" "${varName}" "${!varName}"
721721+ fi
722722+ args+=("--subst-var" "$varName")
723723+ done
724724+}
725725+726726+substituteAllStream() {
727727+ local -a args=()
728728+ _allFlags
729729+730730+ substituteStream "$1" "${args[@]}"
731731+}
707732708733# Substitute all environment variables that start with a lowercase character and
709734# are valid Bash names.
710735substituteAll() {
711736 local input="$1"
712737 local output="$2"
738738+713739 local -a args=()
714714-715715- for varName in $(awk 'BEGIN { for (v in ENVIRON) if (v ~ /^[a-z][a-zA-Z0-9_]*$/) print v }'); do
716716- if (( "${NIX_DEBUG:-0}" >= 1 )); then
717717- printf "@%s@ -> %q\n" "${varName}" "${!varName}"
718718- fi
719719- args+=("--subst-var" "$varName")
720720- done
740740+ _allFlags
721741722742 substitute "$input" "$output" "${args[@]}"
723743}
···10891109 if [ -n "${setupHook:-}" ]; then
10901110 mkdir -p "${!outputDev}/nix-support"
10911111 substituteAll "$setupHook" "${!outputDev}/nix-support/setup-hook"
11121112+ fi
11131113+11141114+ # TODO(@Ericson2314): Remove after https://github.com/NixOS/nixpkgs/pull/31414
11151115+ if [ -n "${setupHooks:-}" ]; then
11161116+ mkdir -p "${!outputDev}/nix-support"
11171117+ local hook
11181118+ for hook in $setupHooks; do
11191119+ local content
11201120+ consumeEntire content < "$hook"
11211121+ substituteAllStream content >> "${!outputDev}/nix-support/setup-hook"
11221122+ unset -v content
11231123+ done
11241124+ unset -v hook
10921125 fi
1093112610941127 # Propagate user-env packages into the output with binaries, TODO?