diff --git a/src/libstore-tests/derivation-advanced-attrs.cc b/src/libstore-tests/derivation-advanced-attrs.cc index 4408720e8..0eefc2673 100644 --- a/src/libstore-tests/derivation-advanced-attrs.cc +++ b/src/libstore-tests/derivation-advanced-attrs.cc @@ -108,7 +108,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_defaults) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_TRUE(!parsedDrv.hasStructuredAttrs()); @@ -143,7 +143,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes_defaults) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{}); @@ -157,7 +157,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_defaults) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{"ca-derivations"}); @@ -171,7 +171,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_TRUE(!parsedDrv.hasStructuredAttrs()); @@ -195,7 +195,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ( @@ -243,7 +243,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ( @@ -296,7 +296,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_structuredAttrs_d auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_TRUE(parsedDrv.hasStructuredAttrs()); @@ -330,7 +330,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs_defaults) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{}); @@ -344,7 +344,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs_default auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{"ca-derivations"}); @@ -358,7 +358,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_structuredAttrs) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_TRUE(parsedDrv.hasStructuredAttrs()); @@ -392,7 +392,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ( @@ -445,7 +445,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs) auto drvPath = writeDerivation(*this->store, got, NoRepair, true); - ParsedDerivation parsedDrv(got); + ParsedDerivation parsedDrv(got.env); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); EXPECT_EQ( diff --git a/src/libstore/build/derivation-goal.cc b/src/libstore/build/derivation-goal.cc index 8e43ab2ae..c2232b657 100644 --- a/src/libstore/build/derivation-goal.cc +++ b/src/libstore/build/derivation-goal.cc @@ -180,7 +180,7 @@ Goal::Co DerivationGoal::haveDerivation() { trace("have derivation"); - parsedDrv = std::make_unique(*drv); + parsedDrv = std::make_unique(drv->env); try { drvOptions = std::make_unique(DerivationOptions::fromParsedDerivation(*parsedDrv)); } catch (Error & e) { diff --git a/src/libstore/derivation-options.cc b/src/libstore/derivation-options.cc index 526e682f7..5d22f1f66 100644 --- a/src/libstore/derivation-options.cc +++ b/src/libstore/derivation-options.cc @@ -116,7 +116,7 @@ DerivationOptions DerivationOptions::fromParsedDerivation(const ParsedDerivation .passAsFile = [&] { StringSet res; - if (auto * passAsFileString = get(parsed.drv.env, "passAsFile")) { + if (auto * passAsFileString = get(parsed.env, "passAsFile")) { if (parsed.hasStructuredAttrs()) { if (shouldWarn) { warn( @@ -140,7 +140,7 @@ DerivationOptions DerivationOptions::fromParsedDerivation(const ParsedDerivation ret.insert_or_assign(key, storePathsJson); } } else { - auto s = getOr(parsed.drv.env, "exportReferencesGraph", ""); + auto s = getOr(parsed.env, "exportReferencesGraph", ""); Strings ss = tokenizeString(s); if (ss.size() % 2 != 0) throw BuildError("odd number of tokens in 'exportReferencesGraph': '%1%'", s); diff --git a/src/libstore/include/nix/store/parsed-derivations.hh b/src/libstore/include/nix/store/parsed-derivations.hh index e23eab1bb..30a294187 100644 --- a/src/libstore/include/nix/store/parsed-derivations.hh +++ b/src/libstore/include/nix/store/parsed-derivations.hh @@ -12,7 +12,7 @@ struct DerivationOptions; class ParsedDerivation { - BasicDerivation & drv; + const StringPairs & env; std::unique_ptr structuredAttrs; std::optional getStringAttr(const std::string & name) const; @@ -33,7 +33,7 @@ class ParsedDerivation public: - ParsedDerivation(BasicDerivation & drv); + ParsedDerivation(const StringPairs & env); ~ParsedDerivation(); @@ -42,8 +42,11 @@ public: return static_cast(structuredAttrs); } - std::optional - prepareStructuredAttrs(Store & store, const DerivationOptions & drvOptions, const StorePathSet & inputPaths); + std::optional prepareStructuredAttrs( + Store & store, + const DerivationOptions & drvOptions, + const StorePathSet & inputPaths, + const DerivationOutputs & outputs); }; std::string writeStructuredAttrsShell(const nlohmann::json & json); diff --git a/src/libstore/misc.cc b/src/libstore/misc.cc index b9e729092..cfa82845d 100644 --- a/src/libstore/misc.cc +++ b/src/libstore/misc.cc @@ -222,7 +222,7 @@ void Store::queryMissing(const std::vector & targets, if (knownOutputPaths && invalid.empty()) return; auto drv = make_ref(derivationFromPath(drvPath)); - ParsedDerivation parsedDrv(*drv); + ParsedDerivation parsedDrv(drv->env); DerivationOptions drvOptions; try { drvOptions = DerivationOptions::fromParsedDerivation(parsedDrv); diff --git a/src/libstore/parsed-derivations.cc b/src/libstore/parsed-derivations.cc index 218a1cf35..9aa16ff2c 100644 --- a/src/libstore/parsed-derivations.cc +++ b/src/libstore/parsed-derivations.cc @@ -6,12 +6,12 @@ namespace nix { -ParsedDerivation::ParsedDerivation(BasicDerivation & drv) - : drv(drv) +ParsedDerivation::ParsedDerivation(const StringPairs & env) + : env(env) { /* Parse the __json attribute, if any. */ - auto jsonAttr = drv.env.find("__json"); - if (jsonAttr != drv.env.end()) { + auto jsonAttr = env.find("__json"); + if (jsonAttr != env.end()) { try { structuredAttrs = std::make_unique(nlohmann::json::parse(jsonAttr->second)); } catch (std::exception & e) { @@ -34,8 +34,8 @@ std::optional ParsedDerivation::getStringAttr(const std::string & n return i->get(); } } else { - auto i = drv.env.find(name); - if (i == drv.env.end()) + auto i = env.find(name); + if (i == env.end()) return {}; else return i->second; @@ -54,8 +54,8 @@ bool ParsedDerivation::getBoolAttr(const std::string & name, bool def) const return i->get(); } } else { - auto i = drv.env.find(name); - if (i == drv.env.end()) + auto i = env.find(name); + if (i == env.end()) return def; else return i->second == "1"; @@ -80,8 +80,8 @@ std::optional ParsedDerivation::getStringsAttr(const std::string & name return res; } } else { - auto i = drv.env.find(name); - if (i == drv.env.end()) + auto i = env.find(name); + if (i == env.end()) return {}; else return tokenizeString(i->second); @@ -155,17 +155,18 @@ static nlohmann::json pathInfoToJSON( std::optional ParsedDerivation::prepareStructuredAttrs( Store & store, const DerivationOptions & drvOptions, - const StorePathSet & inputPaths) + const StorePathSet & inputPaths, + const DerivationOutputs & outputs) { if (!structuredAttrs) return std::nullopt; auto json = *structuredAttrs; /* Add an "outputs" object containing the output paths. */ - nlohmann::json outputs; - for (auto & i : drv.outputs) - outputs[i.first] = hashPlaceholder(i.first); - json["outputs"] = outputs; + nlohmann::json outputsJson; + for (auto & i : outputs) + outputsJson[i.first] = hashPlaceholder(i.first); + json["outputs"] = std::move(outputsJson); /* Handle exportReferencesGraph. */ for (auto & [key, inputPaths] : drvOptions.exportReferencesGraph) { diff --git a/src/libstore/unix/build/local-derivation-goal.cc b/src/libstore/unix/build/local-derivation-goal.cc index 965ece136..18300904e 100644 --- a/src/libstore/unix/build/local-derivation-goal.cc +++ b/src/libstore/unix/build/local-derivation-goal.cc @@ -1576,7 +1576,12 @@ void LocalDerivationGoal::initEnv() void LocalDerivationGoal::writeStructuredAttrs() { - if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(worker.store, *drvOptions, inputPaths)) { + if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs( + worker.store, + *drvOptions, + inputPaths, + drv->outputs)) + { auto json = structAttrsJson.value(); nlohmann::json rewritten; for (auto & [i, v] : json["outputs"].get()) { diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index c2368dc5b..7ace4d4b4 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -544,7 +544,7 @@ static void main_nix_build(int argc, char * * argv) env["NIX_STORE"] = store->storeDir; env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores); - ParsedDerivation parsedDrv(drv); + ParsedDerivation parsedDrv(drv.env); DerivationOptions drvOptions; try { drvOptions = DerivationOptions::fromParsedDerivation(parsedDrv); @@ -584,7 +584,12 @@ static void main_nix_build(int argc, char * * argv) for (const auto & [inputDrv, inputNode] : drv.inputDrvs.map) accumInputClosure(inputDrv, inputNode); - if (auto structAttrs = parsedDrv.prepareStructuredAttrs(*store, drvOptions, inputs)) { + if (auto structAttrs = parsedDrv.prepareStructuredAttrs( + *store, + drvOptions, + inputs, + drv.outputs)) + { auto json = structAttrs.value(); structuredAttrsRC = writeStructuredAttrsShell(json);