stdenv.mkDerivation: support structuredAttrs in inputDerivation

The goal is to print all store references into $out.

First, $out itself is not defined with structuredAttrs, but we can work
around that with placeholder. Alternatively we could source
$stdenv/setup after sourcing the attrs.sh file, but that feels like
overkill.

To support structuredAttrs we source the attrs.sh file. export will not
be enough anymore, because the attrs file sets bash variables, not
environment variables. Thus we resort to declare -p.

Resolves #321005
This commit is contained in:
Wolfgang Walther
2024-12-03 18:32:05 +01:00
parent 53f3c92b4d
commit 6648c58eae
+9 -3
View File
@@ -613,13 +613,19 @@ extendDerivation
_derivation_original_args = derivationArg.args;
builder = stdenv.shell;
# The bash builtin `export` dumps all current environment variables,
# The builtin `declare -p` dumps all bash and environment variables,
# which is where all build input references end up (e.g. $PATH for
# binaries). By writing this to $out, Nix can find and register
# them as runtime dependencies (since Nix greps for store paths
# through $out to find them)
# through $out to find them). Using placeholder for $out works with
# and without structuredAttrs.
# This build script does not use setup.sh or stdenv, to keep
# the env most pristine. This gives us a very bare bones env,
# hence the extra/duplicated compatibility logic and "pure bash" style.
args = [ "-c" ''
export > $out
out="${placeholder "out"}"
if [ -e "$NIX_ATTRS_SH_FILE" ]; then . "$NIX_ATTRS_SH_FILE"; elif [ -f .attrs.sh ]; then . .attrs.sh; fi
declare -p > $out
for var in $passAsFile; do
pathVar="''${var}Path"
printf "%s" "$(< "''${!pathVar}")" >> $out