From 3944a120ec6986c723bf36bfade9b331dd4af68a Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 9 May 2021 17:46:16 +0200 Subject: [PATCH] Set environment variables for .attrs.json & .attrs.sh This way no derivation has to expect that these files are in the `cwd` during the build. This is problematic for `nix-shell` where these files would have to be inserted into the nix-shell's `cwd` which can become problematic with e.g. recursive `nix-shell`. To remain backwards-compatible, the location inside the build sandbox will be kept, however using these files directly should be deprecated from now on. --- src/libstore/build/local-derivation-goal.cc | 2 ++ src/nix-build/nix-build.cc | 13 +++++++------ tests/structured-attrs.nix | 2 +- tests/structured-attrs.sh | 4 +--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 9bb6f276c..e6b552b94 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -1093,8 +1093,10 @@ void LocalDerivationGoal::writeStructuredAttrs() writeFile(tmpDir + "/.attrs.sh", rewriteStrings(jsonSh, inputRewrites)); chownToBuilder(tmpDir + "/.attrs.sh"); + env["ATTRS_SH_FILE"] = tmpDir + "/.attrs.sh"; writeFile(tmpDir + "/.attrs.json", rewriteStrings(json.dump(), inputRewrites)); chownToBuilder(tmpDir + "/.attrs.json"); + env["ATTRS_JSON_FILE"] = tmpDir + "/.attrs.json"; } } diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index f91a56d6a..9bd7869f9 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -428,7 +428,6 @@ static void main_nix_build(int argc, char * * argv) env[var.first] = var.second; std::string structuredAttrsRC; - std::string exitCmd; if (env.count("__json")) { StorePathSet inputs; @@ -448,11 +447,13 @@ static void main_nix_build(int argc, char * * argv) if (auto structAttrs = parsedDrv.generateStructuredAttrs(std::nullopt, *store, inputs)) { auto val = structAttrs.value(); structuredAttrsRC = val.first; - auto attrsJSON = std::filesystem::current_path().string() + "/.attrs.json"; + auto attrsJSON = (Path) tmpDir + "/.attrs.json"; writeFile(attrsJSON, val.second.dump()); - exitCmd = "\n_rm_attrs_json() { rm -f " + attrsJSON + "; }" - + "\nexitHooks+=(_rm_attrs_json)" - + "\nfailureHooks+=(_rm_attrs_json)\n"; + auto attrsSH = (Path) tmpDir + "/.attrs.sh"; + writeFile(attrsSH, val.first); + env["ATTRS_SH_FILE"] = attrsSH; + env["ATTRS_JSON_FILE"] = attrsJSON; + keepTmp = true; } } @@ -471,7 +472,7 @@ static void main_nix_build(int argc, char * * argv) (pure ? "" : "[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc;") + "%2%" "dontAddDisableDepTrack=1;\n" - + structuredAttrsRC + exitCmd + + + structuredAttrsRC + "\n[ -e $stdenv/setup ] && source $stdenv/setup; " "%3%" "PATH=%4%:\"$PATH\"; " diff --git a/tests/structured-attrs.nix b/tests/structured-attrs.nix index c39c3a346..f69ee45e9 100644 --- a/tests/structured-attrs.nix +++ b/tests/structured-attrs.nix @@ -36,7 +36,7 @@ mkDerivation { echo bar > $dest echo foo > $dest2 - json=$(cat .attrs.json) + json=$(cat $ATTRS_JSON_FILE) [[ $json =~ '"narHash":"sha256:1r7yc43zqnzl5b0als5vnyp649gk17i37s7mj00xr8kc47rjcybk"' ]] [[ $json =~ '"narSize":288' ]] [[ $json =~ '"closureSize":288' ]] diff --git a/tests/structured-attrs.sh b/tests/structured-attrs.sh index c0fcb021a..241835539 100644 --- a/tests/structured-attrs.sh +++ b/tests/structured-attrs.sh @@ -10,7 +10,5 @@ nix-build structured-attrs.nix -A all -o $TEST_ROOT/result [[ $(cat $TEST_ROOT/result-dev/foo) = foo ]] export NIX_BUILD_SHELL=$SHELL -[[ ! -e '.attrs.json' ]] env NIX_PATH=nixpkgs=shell.nix nix-shell structured-attrs-shell.nix \ - --run 'test -e .attrs.json; test "3" = "$(jq ".my.list|length" < .attrs.json)"' -[[ ! -e '.attrs.json' ]] + --run 'test -e .attrs.json; test "3" = "$(jq ".my.list|length" < $ATTRS_JSON_FILE)"'