Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup.sh: substitute uses only valid bash names #14907

Merged
merged 2 commits into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion doc/stdenv.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,15 @@ PATH=/nix/store/68afga4khv0w...-coreutils-6.12/bin
echo @foo@
</programlisting>

That is, no substitution is performed for undefined variables.</para></listitem>
That is, no substitution is performed for undefined variables.</para>

<para>Environment variables that start with an uppercase letter are filtered out,
to prevent global variables (like <literal>HOME</literal>) from accidentally
getting substituted.
The variables also have to be valid bash “names”, as
defined in the bash manpage (alphanumeric or <literal>_</literal>, must not
start with a number).</para>
</listitem>
</varlistentry>


Expand Down
1 change: 1 addition & 0 deletions pkgs/build-support/substitute/substitute-all.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

args:

# see the substituteAll in the nixpkgs documentation for usage and constaints
stdenv.mkDerivation ({
name = if args ? name then args.name else baseNameOf (toString args.src);
builder = ./substitute-all.sh;
Expand Down
8 changes: 7 additions & 1 deletion pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ substitute() {

if [ "$p" = --subst-var ]; then
varName="${params[$((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 "substitution variables must be valid bash names, \"$varName\" isn't."
exit 1;
fi
pattern="@$varName@"
replacement="${!varName}"
n=$((n + 1))
Expand Down Expand Up @@ -439,7 +444,8 @@ substituteAll() {
local output="$2"

# Select all environment variables that start with a lowercase character.
for envVar in $(env | sed -e $'s/^\([a-z][^=]*\)=.*/\\1/; t \n d'); do
# Will not work with nix attribute names (and thus env variables) containing '\n'.
for envVar in $(set | sed -e $'s/^\([a-z][^=]*\)=.*/\\1/; t \n d'); do
if [ "$NIX_DEBUG" = "1" ]; then
echo "$envVar -> ${!envVar}"
fi
Expand Down