From f1e281c4fe1263a0c848bc8aaf57a0e61a99fa93 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Thu, 13 May 2021 16:11:56 +0200 Subject: [PATCH] Split shell & json creation for build environments with structured attrs --- src/libstore/build/local-derivation-goal.cc | 7 +++---- src/libstore/parsed-derivations.cc | 10 +++++++--- src/libstore/parsed-derivations.hh | 3 ++- src/nix-build/nix-build.cc | 13 ++++++++----- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index e6b552b94..60495ae3e 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -1086,10 +1086,9 @@ void LocalDerivationGoal::initEnv() void LocalDerivationGoal::writeStructuredAttrs() { - if (auto structAttrs = parsedDrv->generateStructuredAttrs(inputRewrites, worker.store, inputPaths)) { - auto value = structAttrs.value(); - auto jsonSh = value.first; - auto json = value.second; + if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(inputRewrites, worker.store, inputPaths)) { + auto json = structAttrsJson.value(); + auto jsonSh = parsedDrv->writeStructuredAttrsShell(json); writeFile(tmpDir + "/.attrs.sh", rewriteStrings(jsonSh, inputRewrites)); chownToBuilder(tmpDir + "/.attrs.sh"); diff --git a/src/libstore/parsed-derivations.cc b/src/libstore/parsed-derivations.cc index 029da8bd3..5675600c4 100644 --- a/src/libstore/parsed-derivations.cc +++ b/src/libstore/parsed-derivations.cc @@ -124,8 +124,7 @@ bool ParsedDerivation::substitutesAllowed() const } static std::regex shVarName("[A-Za-z_][A-Za-z0-9_]*"); -std::optional ParsedDerivation::generateStructuredAttrs( - std::optional inputRewrites, Store & store, const StorePathSet & inputPaths) +std::optional ParsedDerivation::prepareStructuredAttrs(std::optional inputRewrites, Store & store, const StorePathSet & inputPaths) { auto structuredAttrs = getStructuredAttrs(); if (!structuredAttrs) return std::nullopt; @@ -163,6 +162,11 @@ std::optional ParsedDerivation::generateStructuredAt } } + return json; +} + +std::string ParsedDerivation::writeStructuredAttrsShell(nlohmann::json & json) +{ /* As a convenience to bash scripts, write a shell file that maps all attributes that are representable in bash - namely, strings, integers, nulls, Booleans, and arrays and @@ -229,6 +233,6 @@ std::optional ParsedDerivation::generateStructuredAt } } - return std::make_pair(jsonSh, json); + return jsonSh; } } diff --git a/src/libstore/parsed-derivations.hh b/src/libstore/parsed-derivations.hh index 4b8b8c8ff..37af36630 100644 --- a/src/libstore/parsed-derivations.hh +++ b/src/libstore/parsed-derivations.hh @@ -39,7 +39,8 @@ public: bool substitutesAllowed() const; - std::optional generateStructuredAttrs(std::optional inputRewrites, Store & store, const StorePathSet & inputPaths); + std::optional prepareStructuredAttrs(std::optional inputRewrites, Store & store, const StorePathSet & inputPaths); + std::string writeStructuredAttrsShell(nlohmann::json & json); }; } diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 9bd7869f9..4e4f27458 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -444,13 +444,16 @@ static void main_nix_build(int argc, char * * argv) drv ); - if (auto structAttrs = parsedDrv.generateStructuredAttrs(std::nullopt, *store, inputs)) { - auto val = structAttrs.value(); - structuredAttrsRC = val.first; + if (auto structAttrs = parsedDrv.prepareStructuredAttrs(std::nullopt, *store, inputs)) { + auto json = structAttrs.value(); + structuredAttrsRC = parsedDrv.writeStructuredAttrsShell(json); + auto attrsJSON = (Path) tmpDir + "/.attrs.json"; - writeFile(attrsJSON, val.second.dump()); + writeFile(attrsJSON, json.dump()); + auto attrsSH = (Path) tmpDir + "/.attrs.sh"; - writeFile(attrsSH, val.first); + writeFile(attrsSH, structuredAttrsRC); + env["ATTRS_SH_FILE"] = attrsSH; env["ATTRS_JSON_FILE"] = attrsJSON; keepTmp = true;