about summary refs log tree commit diff
path: root/pkgs/stdenv/generic/setup.sh
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/stdenv/generic/setup.sh')
-rw-r--r--pkgs/stdenv/generic/setup.sh23
1 files changed, 17 insertions, 6 deletions
diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh
index 9399ff7a76437..ce5feac3c66d2 100644
--- a/pkgs/stdenv/generic/setup.sh
+++ b/pkgs/stdenv/generic/setup.sh
@@ -398,7 +398,7 @@ substitute() {
     content="${content%X}"
 
     for ((n = 2; n < ${#params[*]}; n += 1)); do
-        p=${params[$n]}
+        p="${params[$n]}"
 
         if [ "$p" = --replace ]; then
             pattern="${params[$((n + 1))]}"
@@ -408,9 +408,16 @@ substitute() {
 
         if [ "$p" = --subst-var ]; then
             varName="${params[$((n + 1))]}"
+            n=$((n + 1))
+            # check if the used nix attribute name is a valid bash name
+            if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
+                echo "WARNING: substitution variables should be valid bash names,"
+                echo "  \"$varName\" isn't and therefore was skipped; it might be caused"
+                echo "  by multi-line phases in variables - see #14907 for details."
+                continue
+            fi
             pattern="@$varName@"
             replacement="${!varName}"
-            n=$((n + 1))
         fi
 
         if [ "$p" = --subst-var-by ]; then
@@ -434,19 +441,23 @@ substituteInPlace() {
 }
 
 
+# Substitute all environment variables that do not start with an upper-case
+# character or underscore. Note: other names that aren't bash-valid
+# will cause an error during `substitute --subst-var`.
 substituteAll() {
     local input="$1"
     local output="$2"
+    local -a args=()
 
     # Select all environment variables that start with a lowercase character.
-    for envVar in $(env | sed -e $'s/^\([a-z][^=]*\)=.*/\\1/; t \n d'); do
+    for varName in $(env | sed -e $'s/^\([a-z][^= \t]*\)=.*/\\1/; t \n d'); do
         if [ "$NIX_DEBUG" = "1" ]; then
-            echo "$envVar -> ${!envVar}"
+            echo "@${varName}@ -> '${!varName}'"
         fi
-        args="$args --subst-var $envVar"
+        args+=("--subst-var" "$varName")
     done
 
-    substitute "$input" "$output" $args
+    substitute "$input" "$output" "${args[@]}"
 }