Skip to content

Commit

Permalink
Perform input rewrites only in LocalDerivationGoal
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma27 committed Jun 24, 2021
1 parent 6f20654 commit 644415d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,17 @@ void LocalDerivationGoal::initEnv()

void LocalDerivationGoal::writeStructuredAttrs()
{
if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(inputRewrites, worker.store, inputPaths)) {
if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(worker.store, inputPaths)) {
auto json = structAttrsJson.value();
nlohmann::json rewritten;
for (auto & [i, v] : json["outputs"].get<nlohmann::json::object_t>()) {
/* The placeholder must have a rewrite, so we use it to cover both the
cases where we know or don't know the output path ahead of time. */
rewritten[i] = rewriteStrings(v, inputRewrites);
}

json["outputs"] = rewritten;

auto jsonSh = writeStructuredAttrsShell(json);

writeFile(tmpDir + "/.attrs.sh", rewriteStrings(jsonSh, inputRewrites));
Expand Down
11 changes: 2 additions & 9 deletions src/libstore/parsed-derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ bool ParsedDerivation::substitutesAllowed() const
}

static std::regex shVarName("[A-Za-z_][A-Za-z0-9_]*");
std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(std::optional<StringMap> inputRewrites, Store & store, const StorePathSet & inputPaths)
std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths)
{
auto structuredAttrs = getStructuredAttrs();
if (!structuredAttrs) return std::nullopt;
Expand All @@ -134,14 +134,7 @@ std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(std::opti
/* Add an "outputs" object containing the output paths. */
nlohmann::json outputs;
for (auto & i : drv.outputs) {
if (inputRewrites) {
/* The placeholder must have a rewrite, so we use it to cover both the
cases where we know or don't know the output path ahead of time. */
outputs[i.first] = rewriteStrings(hashPlaceholder(i.first), inputRewrites.value());
} else {
/* This case is only relevant for the nix-shell */
outputs[i.first] = hashPlaceholder(i.first);
}
outputs[i.first] = hashPlaceholder(i.first);
}
json["outputs"] = outputs;

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/parsed-derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public:

bool substitutesAllowed() const;

std::optional<nlohmann::json> prepareStructuredAttrs(std::optional<StringMap> inputRewrites, Store & store, const StorePathSet & inputPaths);
std::optional<nlohmann::json> prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths);
};

std::string writeStructuredAttrsShell(nlohmann::json & json);
Expand Down
2 changes: 1 addition & 1 deletion src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ static void main_nix_build(int argc, char * * argv)
drv
);

if (auto structAttrs = parsedDrv.prepareStructuredAttrs(std::nullopt, *store, inputs)) {
if (auto structAttrs = parsedDrv.prepareStructuredAttrs(*store, inputs)) {
auto json = structAttrs.value();
structuredAttrsRC = writeStructuredAttrsShell(json);

Expand Down

0 comments on commit 644415d

Please sign in to comment.