Skip to content

Commit 2093894

Browse files
Ericson2314haenoe
andcommitted
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>
1 parent 106195a commit 2093894

8 files changed

Lines changed: 44 additions & 30 deletions

File tree

src/libstore-tests/derivation-advanced-attrs.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ TEST_F(DerivationAdvancedAttrsTest, Derivation_advancedAttributes_defaults)
8181

8282
auto drvPath = writeDerivation(*store, got, NoRepair, true);
8383

84-
ParsedDerivation parsedDrv(got);
84+
ParsedDerivation parsedDrv(got.env);
8585
DerivationOptions options = DerivationOptions::fromParsedDerivation(*store, parsedDrv);
8686

8787
EXPECT_TRUE(!parsedDrv.hasStructuredAttrs());
@@ -116,7 +116,7 @@ TEST_F(DerivationAdvancedAttrsTest, Derivation_advancedAttributes)
116116

117117
auto drvPath = writeDerivation(*store, got, NoRepair, true);
118118

119-
ParsedDerivation parsedDrv(got);
119+
ParsedDerivation parsedDrv(got.env);
120120
DerivationOptions options = DerivationOptions::fromParsedDerivation(*store, parsedDrv);
121121

122122
StringSet systemFeatures{"rainbow", "uid-range"};
@@ -157,7 +157,7 @@ TEST_F(DerivationAdvancedAttrsTest, Derivation_advancedAttributes_structuredAttr
157157

158158
auto drvPath = writeDerivation(*store, got, NoRepair, true);
159159

160-
ParsedDerivation parsedDrv(got);
160+
ParsedDerivation parsedDrv(got.env);
161161
DerivationOptions options = DerivationOptions::fromParsedDerivation(*store, parsedDrv);
162162

163163
EXPECT_TRUE(parsedDrv.hasStructuredAttrs());
@@ -191,7 +191,7 @@ TEST_F(DerivationAdvancedAttrsTest, Derivation_advancedAttributes_structuredAttr
191191

192192
auto drvPath = writeDerivation(*store, got, NoRepair, true);
193193

194-
ParsedDerivation parsedDrv(got);
194+
ParsedDerivation parsedDrv(got.env);
195195
DerivationOptions options = DerivationOptions::fromParsedDerivation(*store, parsedDrv);
196196

197197
StringSet systemFeatures{"rainbow", "uid-range"};

src/libstore/build/derivation-goal.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Goal::Co DerivationGoal::haveDerivation()
180180
{
181181
trace("have derivation");
182182

183-
parsedDrv = std::make_unique<ParsedDerivation>(*drv);
183+
parsedDrv = std::make_unique<ParsedDerivation>(drv->env);
184184
try {
185185
drvOptions = std::make_unique<DerivationOptions>(
186186
DerivationOptions::fromParsedDerivation(worker.store, *parsedDrv));

src/libstore/derivation-options.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ DerivationOptions::fromParsedDerivation(const StoreDirConfig & store, const Pars
117117
.passAsFile =
118118
[&] {
119119
StringSet res;
120-
if (auto * passAsFileString = get(parsed.drv.env, "passAsFile")) {
120+
if (auto * passAsFileString = get(parsed.env, "passAsFile")) {
121121
if (parsed.hasStructuredAttrs()) {
122122
if (shouldWarn) {
123123
warn(
@@ -144,7 +144,7 @@ DerivationOptions::fromParsedDerivation(const StoreDirConfig & store, const Pars
144144
ret.insert_or_assign(key, std::move(storePaths));
145145
}
146146
} else {
147-
auto s = getOr(parsed.drv.env, "exportReferencesGraph", "");
147+
auto s = getOr(parsed.env, "exportReferencesGraph", "");
148148
Strings ss = tokenizeString<Strings>(s);
149149
if (ss.size() % 2 != 0)
150150
throw BuildError("odd number of tokens in 'exportReferencesGraph': '%1%'", s);

src/libstore/misc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void Store::queryMissing(const std::vector<DerivedPath> & targets,
222222
if (knownOutputPaths && invalid.empty()) return;
223223

224224
auto drv = make_ref<Derivation>(derivationFromPath(drvPath));
225-
ParsedDerivation parsedDrv(*drv);
225+
ParsedDerivation parsedDrv(drv->env);
226226
DerivationOptions drvOptions;
227227
try {
228228
drvOptions = DerivationOptions::fromParsedDerivation(*this, parsedDrv);

src/libstore/parsed-derivations.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
namespace nix {
88

9-
ParsedDerivation::ParsedDerivation(BasicDerivation & drv)
10-
: drv(drv)
9+
ParsedDerivation::ParsedDerivation(const StringPairs & env)
10+
: env(env)
1111
{
1212
/* Parse the __json attribute, if any. */
13-
auto jsonAttr = drv.env.find("__json");
14-
if (jsonAttr != drv.env.end()) {
13+
auto jsonAttr = env.find("__json");
14+
if (jsonAttr != env.end()) {
1515
try {
1616
structuredAttrs = std::make_unique<nlohmann::json>(nlohmann::json::parse(jsonAttr->second));
1717
} catch (std::exception & e) {
@@ -34,8 +34,8 @@ std::optional<std::string> ParsedDerivation::getStringAttr(const std::string & n
3434
return i->get<std::string>();
3535
}
3636
} else {
37-
auto i = drv.env.find(name);
38-
if (i == drv.env.end())
37+
auto i = env.find(name);
38+
if (i == env.end())
3939
return {};
4040
else
4141
return i->second;
@@ -54,8 +54,8 @@ bool ParsedDerivation::getBoolAttr(const std::string & name, bool def) const
5454
return i->get<bool>();
5555
}
5656
} else {
57-
auto i = drv.env.find(name);
58-
if (i == drv.env.end())
57+
auto i = env.find(name);
58+
if (i == env.end())
5959
return def;
6060
else
6161
return i->second == "1";
@@ -80,8 +80,8 @@ std::optional<Strings> ParsedDerivation::getStringsAttr(const std::string & name
8080
return res;
8181
}
8282
} else {
83-
auto i = drv.env.find(name);
84-
if (i == drv.env.end())
83+
auto i = env.find(name);
84+
if (i == env.end())
8585
return {};
8686
else
8787
return tokenizeString<Strings>(i->second);
@@ -155,17 +155,18 @@ static nlohmann::json pathInfoToJSON(
155155
std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(
156156
Store & store,
157157
const DerivationOptions & drvOptions,
158-
const StorePathSet & inputPaths)
158+
const StorePathSet & inputPaths,
159+
const DerivationOutputs & outputs)
159160
{
160161
if (!structuredAttrs) return std::nullopt;
161162

162163
auto json = *structuredAttrs;
163164

164165
/* Add an "outputs" object containing the output paths. */
165-
nlohmann::json outputs;
166-
for (auto & i : drv.outputs)
167-
outputs[i.first] = hashPlaceholder(i.first);
168-
json["outputs"] = outputs;
166+
nlohmann::json outputsJson;
167+
for (auto & i : outputs)
168+
outputsJson[i.first] = hashPlaceholder(i.first);
169+
json["outputs"] = std::move(outputsJson);
169170

170171
/* Handle exportReferencesGraph. */
171172
for (auto & [key, storePaths] : drvOptions.exportReferencesGraph) {

src/libstore/parsed-derivations.hh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct DerivationOptions;
1212

1313
class ParsedDerivation
1414
{
15-
BasicDerivation & drv;
15+
const StringPairs & env;
1616
std::unique_ptr<nlohmann::json> structuredAttrs;
1717

1818
std::optional<std::string> getStringAttr(const std::string & name) const;
@@ -33,7 +33,7 @@ class ParsedDerivation
3333

3434
public:
3535

36-
ParsedDerivation(BasicDerivation & drv);
36+
ParsedDerivation(const StringPairs & env);
3737

3838
~ParsedDerivation();
3939

@@ -42,8 +42,11 @@ public:
4242
return static_cast<bool>(structuredAttrs);
4343
}
4444

45-
std::optional<nlohmann::json>
46-
prepareStructuredAttrs(Store & store, const DerivationOptions & drvOptions, const StorePathSet & inputPaths);
45+
std::optional<nlohmann::json> prepareStructuredAttrs(
46+
Store & store,
47+
const DerivationOptions & drvOptions,
48+
const StorePathSet & inputPaths,
49+
const DerivationOutputs & outputs);
4750
};
4851

4952
std::string writeStructuredAttrsShell(const nlohmann::json & json);

src/libstore/unix/build/local-derivation-goal.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,12 @@ void LocalDerivationGoal::initEnv()
15731573

15741574
void LocalDerivationGoal::writeStructuredAttrs()
15751575
{
1576-
if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(worker.store, *drvOptions, inputPaths)) {
1576+
if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(
1577+
worker.store,
1578+
*drvOptions,
1579+
inputPaths,
1580+
drv->outputs))
1581+
{
15771582
auto json = structAttrsJson.value();
15781583
nlohmann::json rewritten;
15791584
for (auto & [i, v] : json["outputs"].get<nlohmann::json::object_t>()) {

src/nix-build/nix-build.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ static void main_nix_build(int argc, char * * argv)
544544
env["NIX_STORE"] = store->storeDir;
545545
env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores);
546546

547-
ParsedDerivation parsedDrv(drv);
547+
ParsedDerivation parsedDrv(drv.env);
548548
DerivationOptions drvOptions;
549549
try {
550550
drvOptions = DerivationOptions::fromParsedDerivation(*store, parsedDrv);
@@ -584,7 +584,12 @@ static void main_nix_build(int argc, char * * argv)
584584
for (const auto & [inputDrv, inputNode] : drv.inputDrvs.map)
585585
accumInputClosure(inputDrv, inputNode);
586586

587-
if (auto structAttrs = parsedDrv.prepareStructuredAttrs(*store, drvOptions, inputs)) {
587+
if (auto structAttrs = parsedDrv.prepareStructuredAttrs(
588+
*store,
589+
drvOptions,
590+
inputs,
591+
drv.outputs))
592+
{
588593
auto json = structAttrs.value();
589594
structuredAttrsRC = writeStructuredAttrsShell(json);
590595

0 commit comments

Comments
 (0)