1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 19:01:16 +02:00

Limit ParsedDerivation just to the derivation's environment

This moves us towards getting rid of `ParsedDerivation` and just having
`DerivationOptions`.

Co-Authored-By: HaeNoe <git@haenoe.party>
This commit is contained in:
John Ericson 2025-02-03 11:41:33 -05:00
parent d285b80033
commit 1e31b60043
8 changed files with 52 additions and 38 deletions

View file

@ -108,7 +108,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_defaults)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_TRUE(!parsedDrv.hasStructuredAttrs()); EXPECT_TRUE(!parsedDrv.hasStructuredAttrs());
@ -143,7 +143,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes_defaults)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{}); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{});
@ -157,7 +157,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_defaults)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{"ca-derivations"}); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{"ca-derivations"});
@ -171,7 +171,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_TRUE(!parsedDrv.hasStructuredAttrs()); EXPECT_TRUE(!parsedDrv.hasStructuredAttrs());
@ -195,7 +195,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ( EXPECT_EQ(
@ -243,7 +243,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ( EXPECT_EQ(
@ -296,7 +296,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_structuredAttrs_d
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_TRUE(parsedDrv.hasStructuredAttrs()); EXPECT_TRUE(parsedDrv.hasStructuredAttrs());
@ -330,7 +330,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs_defaults)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{}); EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{});
@ -344,7 +344,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs_default
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ(options.getRequiredSystemFeatures(got), StringSet{"ca-derivations"}); 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); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_TRUE(parsedDrv.hasStructuredAttrs()); EXPECT_TRUE(parsedDrv.hasStructuredAttrs());
@ -392,7 +392,7 @@ TEST_F(DerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ( EXPECT_EQ(
@ -445,7 +445,7 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs)
auto drvPath = writeDerivation(*this->store, got, NoRepair, true); auto drvPath = writeDerivation(*this->store, got, NoRepair, true);
ParsedDerivation parsedDrv(got); ParsedDerivation parsedDrv(got.env);
DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv); DerivationOptions options = DerivationOptions::fromParsedDerivation(parsedDrv);
EXPECT_EQ( EXPECT_EQ(

View file

@ -180,7 +180,7 @@ Goal::Co DerivationGoal::haveDerivation()
{ {
trace("have derivation"); trace("have derivation");
parsedDrv = std::make_unique<ParsedDerivation>(*drv); parsedDrv = std::make_unique<ParsedDerivation>(drv->env);
try { try {
drvOptions = std::make_unique<DerivationOptions>(DerivationOptions::fromParsedDerivation(*parsedDrv)); drvOptions = std::make_unique<DerivationOptions>(DerivationOptions::fromParsedDerivation(*parsedDrv));
} catch (Error & e) { } catch (Error & e) {

View file

@ -116,7 +116,7 @@ DerivationOptions DerivationOptions::fromParsedDerivation(const ParsedDerivation
.passAsFile = .passAsFile =
[&] { [&] {
StringSet res; StringSet res;
if (auto * passAsFileString = get(parsed.drv.env, "passAsFile")) { if (auto * passAsFileString = get(parsed.env, "passAsFile")) {
if (parsed.hasStructuredAttrs()) { if (parsed.hasStructuredAttrs()) {
if (shouldWarn) { if (shouldWarn) {
warn( warn(
@ -140,7 +140,7 @@ DerivationOptions DerivationOptions::fromParsedDerivation(const ParsedDerivation
ret.insert_or_assign(key, storePathsJson); ret.insert_or_assign(key, storePathsJson);
} }
} else { } else {
auto s = getOr(parsed.drv.env, "exportReferencesGraph", ""); auto s = getOr(parsed.env, "exportReferencesGraph", "");
Strings ss = tokenizeString<Strings>(s); Strings ss = tokenizeString<Strings>(s);
if (ss.size() % 2 != 0) if (ss.size() % 2 != 0)
throw BuildError("odd number of tokens in 'exportReferencesGraph': '%1%'", s); throw BuildError("odd number of tokens in 'exportReferencesGraph': '%1%'", s);

View file

@ -12,7 +12,7 @@ struct DerivationOptions;
class ParsedDerivation class ParsedDerivation
{ {
BasicDerivation & drv; const StringPairs & env;
std::unique_ptr<nlohmann::json> structuredAttrs; std::unique_ptr<nlohmann::json> structuredAttrs;
std::optional<std::string> getStringAttr(const std::string & name) const; std::optional<std::string> getStringAttr(const std::string & name) const;
@ -33,7 +33,7 @@ class ParsedDerivation
public: public:
ParsedDerivation(BasicDerivation & drv); ParsedDerivation(const StringPairs & env);
~ParsedDerivation(); ~ParsedDerivation();
@ -42,8 +42,11 @@ public:
return static_cast<bool>(structuredAttrs); return static_cast<bool>(structuredAttrs);
} }
std::optional<nlohmann::json> std::optional<nlohmann::json> prepareStructuredAttrs(
prepareStructuredAttrs(Store & store, const DerivationOptions & drvOptions, const StorePathSet & inputPaths); Store & store,
const DerivationOptions & drvOptions,
const StorePathSet & inputPaths,
const DerivationOutputs & outputs);
}; };
std::string writeStructuredAttrsShell(const nlohmann::json & json); std::string writeStructuredAttrsShell(const nlohmann::json & json);

View file

@ -222,7 +222,7 @@ void Store::queryMissing(const std::vector<DerivedPath> & targets,
if (knownOutputPaths && invalid.empty()) return; if (knownOutputPaths && invalid.empty()) return;
auto drv = make_ref<Derivation>(derivationFromPath(drvPath)); auto drv = make_ref<Derivation>(derivationFromPath(drvPath));
ParsedDerivation parsedDrv(*drv); ParsedDerivation parsedDrv(drv->env);
DerivationOptions drvOptions; DerivationOptions drvOptions;
try { try {
drvOptions = DerivationOptions::fromParsedDerivation(parsedDrv); drvOptions = DerivationOptions::fromParsedDerivation(parsedDrv);

View file

@ -6,12 +6,12 @@
namespace nix { namespace nix {
ParsedDerivation::ParsedDerivation(BasicDerivation & drv) ParsedDerivation::ParsedDerivation(const StringPairs & env)
: drv(drv) : env(env)
{ {
/* Parse the __json attribute, if any. */ /* Parse the __json attribute, if any. */
auto jsonAttr = drv.env.find("__json"); auto jsonAttr = env.find("__json");
if (jsonAttr != drv.env.end()) { if (jsonAttr != env.end()) {
try { try {
structuredAttrs = std::make_unique<nlohmann::json>(nlohmann::json::parse(jsonAttr->second)); structuredAttrs = std::make_unique<nlohmann::json>(nlohmann::json::parse(jsonAttr->second));
} catch (std::exception & e) { } catch (std::exception & e) {
@ -34,8 +34,8 @@ std::optional<std::string> ParsedDerivation::getStringAttr(const std::string & n
return i->get<std::string>(); return i->get<std::string>();
} }
} else { } else {
auto i = drv.env.find(name); auto i = env.find(name);
if (i == drv.env.end()) if (i == env.end())
return {}; return {};
else else
return i->second; return i->second;
@ -54,8 +54,8 @@ bool ParsedDerivation::getBoolAttr(const std::string & name, bool def) const
return i->get<bool>(); return i->get<bool>();
} }
} else { } else {
auto i = drv.env.find(name); auto i = env.find(name);
if (i == drv.env.end()) if (i == env.end())
return def; return def;
else else
return i->second == "1"; return i->second == "1";
@ -80,8 +80,8 @@ std::optional<Strings> ParsedDerivation::getStringsAttr(const std::string & name
return res; return res;
} }
} else { } else {
auto i = drv.env.find(name); auto i = env.find(name);
if (i == drv.env.end()) if (i == env.end())
return {}; return {};
else else
return tokenizeString<Strings>(i->second); return tokenizeString<Strings>(i->second);
@ -155,17 +155,18 @@ static nlohmann::json pathInfoToJSON(
std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs( std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(
Store & store, Store & store,
const DerivationOptions & drvOptions, const DerivationOptions & drvOptions,
const StorePathSet & inputPaths) const StorePathSet & inputPaths,
const DerivationOutputs & outputs)
{ {
if (!structuredAttrs) return std::nullopt; if (!structuredAttrs) return std::nullopt;
auto json = *structuredAttrs; auto json = *structuredAttrs;
/* Add an "outputs" object containing the output paths. */ /* Add an "outputs" object containing the output paths. */
nlohmann::json outputs; nlohmann::json outputsJson;
for (auto & i : drv.outputs) for (auto & i : outputs)
outputs[i.first] = hashPlaceholder(i.first); outputsJson[i.first] = hashPlaceholder(i.first);
json["outputs"] = outputs; json["outputs"] = std::move(outputsJson);
/* Handle exportReferencesGraph. */ /* Handle exportReferencesGraph. */
for (auto & [key, inputPaths] : drvOptions.exportReferencesGraph) { for (auto & [key, inputPaths] : drvOptions.exportReferencesGraph) {

View file

@ -1576,7 +1576,12 @@ void LocalDerivationGoal::initEnv()
void LocalDerivationGoal::writeStructuredAttrs() 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(); auto json = structAttrsJson.value();
nlohmann::json rewritten; nlohmann::json rewritten;
for (auto & [i, v] : json["outputs"].get<nlohmann::json::object_t>()) { for (auto & [i, v] : json["outputs"].get<nlohmann::json::object_t>()) {

View file

@ -544,7 +544,7 @@ static void main_nix_build(int argc, char * * argv)
env["NIX_STORE"] = store->storeDir; env["NIX_STORE"] = store->storeDir;
env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores); env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores);
ParsedDerivation parsedDrv(drv); ParsedDerivation parsedDrv(drv.env);
DerivationOptions drvOptions; DerivationOptions drvOptions;
try { try {
drvOptions = DerivationOptions::fromParsedDerivation(parsedDrv); 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) for (const auto & [inputDrv, inputNode] : drv.inputDrvs.map)
accumInputClosure(inputDrv, inputNode); 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(); auto json = structAttrs.value();
structuredAttrsRC = writeStructuredAttrsShell(json); structuredAttrsRC = writeStructuredAttrsShell(json);