Skip to content

Commit

Permalink
BasicDerivation: Add applyRewrites() method
Browse files Browse the repository at this point in the history
This is the first part of rewriteDerivation() factored out into its
own method. It's not used anywhere else at the moment, but it's useful
on lazy-trees for rewriting virtual paths.
  • Loading branch information
edolstra committed Nov 19, 2024
1 parent 3b76d01 commit 329b4a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1010,36 +1010,37 @@ void writeDerivation(Sink & out, const StoreDirConfig & store, const BasicDeriva
out << i.first << i.second;
}


std::string hashPlaceholder(const OutputNameView outputName)
void BasicDerivation::applyRewrites(const StringMap & rewrites)
{
// FIXME: memoize?
return "/" + hashString(HashAlgorithm::SHA256, concatStrings("nix-output:", outputName)).to_string(HashFormat::Nix32, false);
}

if (rewrites.empty()) return;

debug("rewriting the derivation");


static void rewriteDerivation(Store & store, BasicDerivation & drv, const StringMap & rewrites)
{
debug("Rewriting the derivation");

for (auto & rewrite : rewrites) {
for (auto & rewrite : rewrites)
debug("rewriting %s as %s", rewrite.first, rewrite.second);
}

drv.builder = rewriteStrings(drv.builder, rewrites);
for (auto & arg : drv.args) {
builder = rewriteStrings(builder, rewrites);
for (auto & arg : args)
arg = rewriteStrings(arg, rewrites);
}

StringPairs newEnv;
for (auto & envVar : drv.env) {
for (auto & envVar : env) {
auto envName = rewriteStrings(envVar.first, rewrites);
auto envValue = rewriteStrings(envVar.second, rewrites);
newEnv.emplace(envName, envValue);
}
drv.env = newEnv;
env = std::move(newEnv);
}

std::string hashPlaceholder(const OutputNameView outputName)
{
// FIXME: memoize?
return "/" + hashString(HashAlgorithm::SHA256, concatStrings("nix-output:", outputName)).to_string(HashFormat::Nix32, false);
}

static void rewriteDerivation(Store & store, BasicDerivation & drv, const StringMap & rewrites)
{
drv.applyRewrites(rewrites);

auto hashModulo = hashDerivationModulo(store, Derivation(drv), true);
for (auto & [outputName, output] : drv.outputs) {
Expand Down
6 changes: 6 additions & 0 deletions src/libstore/derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ struct BasicDerivation

static std::string_view nameFromPath(const StorePath & storePath);

/**
* Apply string rewrites to the `env`, `args` and `builder`
* fields.
*/
void applyRewrites(const StringMap & rewrites);

bool operator == (const BasicDerivation &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=> (const BasicDerivation &) const = default;
Expand Down

0 comments on commit 329b4a2

Please sign in to comment.