···1169echo @foo@
1170</programlisting>
11711172- That is, no substitution is performed for undefined variables.</para></listitem>
000000001173 </varlistentry>
11741175
···1169echo @foo@
1170</programlisting>
11711172+ That is, no substitution is performed for undefined variables.</para>
1173+1174+ <para>Environment variables that start with an uppercase letter are filtered out,
1175+ to prevent global variables (like <literal>HOME</literal>) from accidentally
1176+ getting substituted.
1177+ The variables also have to be valid bash “names”, as
1178+ defined in the bash manpage (alphanumeric or <literal>_</literal>, must not
1179+ start with a number).</para>
1180+ </listitem>
1181 </varlistentry>
11821183
+1
pkgs/build-support/substitute/substitute-all.nix
···23args:
405stdenv.mkDerivation ({
6 name = if args ? name then args.name else baseNameOf (toString args.src);
7 builder = ./substitute-all.sh;
···23args:
45+# see the substituteAll in the nixpkgs documentation for usage and constaints
6stdenv.mkDerivation ({
7 name = if args ? name then args.name else baseNameOf (toString args.src);
8 builder = ./substitute-all.sh;
+7-1
pkgs/stdenv/generic/setup.sh
···408409 if [ "$p" = --subst-var ]; then
410 varName="${params[$((n + 1))]}"
00000411 pattern="@$varName@"
412 replacement="${!varName}"
413 n=$((n + 1))
···439 local output="$2"
440441 # Select all environment variables that start with a lowercase character.
442- for envVar in $(env | sed -e $'s/^\([a-z][^=]*\)=.*/\\1/; t \n d'); do
0443 if [ "$NIX_DEBUG" = "1" ]; then
444 echo "$envVar -> ${!envVar}"
445 fi
···408409 if [ "$p" = --subst-var ]; then
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
416 pattern="@$varName@"
417 replacement="${!varName}"
418 n=$((n + 1))
···444 local output="$2"
445446 # Select all environment variables that start with a lowercase character.
447+ # Will not work with nix attribute names (and thus env variables) containing '\n'.
448+ for envVar in $(set | sed -e $'s/^\([a-z][^=]*\)=.*/\\1/; t \n d'); do
449 if [ "$NIX_DEBUG" = "1" ]; then
450 echo "$envVar -> ${!envVar}"
451 fi