From 9e0d0423fe4a97d339addc8b7bd7f8ac8677c2a9 Mon Sep 17 00:00:00 2001 From: Vladimír Čunát Date: Fri, 29 Apr 2016 11:22:47 +0200 Subject: stdenv substituteAll: use more robust code The set/env fix in #14907 wasn't very good, so let's use a null-delimited approach. Suggested by Aszlig. In particular, this should fix a mass-breakage on Darwin, though I was unable to test that. --- pkgs/stdenv/generic/setup.sh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'pkgs/stdenv/generic/setup.sh') diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index a183aabed0e3..3e156b579bc4 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -409,7 +409,7 @@ 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 + if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then echo "substitution variables must be valid bash names, \"$varName\" isn't." exit 1; fi @@ -439,20 +439,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. - # 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 + # We need to be careful due to vars with multi-line contents or weird names. + while IFS= read -r -d '' varName; do if [ "$NIX_DEBUG" = "1" ]; then - echo "$envVar -> ${!envVar}" + echo "@varName@ -> '${varName}'" fi - args="$args --subst-var $envVar" - done + args+=("--subst-var" "$varName") + done < <(env -0 | cut -z -d= -f1 | grep -z -v '^[_A-Z]') - substitute "$input" "$output" $args + substitute "$input" "$output" "${args[@]}" } -- cgit 1.4.1