setup.hs: substitute uses only valid bash names

bash variable names may only contain alphanumeric ASCII-symbols and _,
and must not start with a number. Nix expression attribute names however
might contain nearly every character (in particular spaces and dashes).

Previously, a substitution that was not a valid bash name would be
expanded to an empty string. This commit introduce a check that throws
a (hopefully) helpful error when a wrong name is used in a substitution.

+6
+6
pkgs/stdenv/generic/setup.sh
··· 408 408 409 409 if [ "$p" = --subst-var ]; then 410 410 varName="${params[$((n + 1))]}" 411 + # check if the used nix attribute name is a valid bash name 412 + if ! [[ "$varName" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]]; then 413 + echo "substitution variables must be valid bash names, \"$varName\" isn't." 414 + exit 1; 415 + fi 411 416 pattern="@$varName@" 412 417 replacement="${!varName}" 413 418 n=$((n + 1)) ··· 439 444 local output="$2" 440 445 441 446 # Select all environment variables that start with a lowercase character. 447 + # Will not work with nix attribute names (and thus env variables) containing '\n'. 442 448 for envVar in $(env | sed -e $'s/^\([a-z][^=]*\)=.*/\\1/; t \n d'); do 443 449 if [ "$NIX_DEBUG" = "1" ]; then 444 450 echo "$envVar -> ${!envVar}"