BasicDerivation: Add applyRewrites() method

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.
This commit is contained in:
Eelco Dolstra 2024-11-19 17:35:32 +01:00
parent 3b76d01f3b
commit df9ccdf31b
2 changed files with 20 additions and 12 deletions

View File

@ -1017,29 +1017,31 @@ std::string hashPlaceholder(const OutputNameView outputName)
return "/" + hashString(HashAlgorithm::SHA256, concatStrings("nix-output:", outputName)).to_string(HashFormat::Nix32, false); return "/" + hashString(HashAlgorithm::SHA256, concatStrings("nix-output:", outputName)).to_string(HashFormat::Nix32, false);
} }
void BasicDerivation::applyRewrites(const StringMap & rewrites)
static void rewriteDerivation(Store & store, BasicDerivation & drv, const StringMap & rewrites)
{ {
debug("Rewriting the derivation"); if (rewrites.empty()) return;
for (auto & rewrite : rewrites) { debug("rewriting the derivation");
for (auto & rewrite : rewrites)
debug("rewriting %s as %s", rewrite.first, rewrite.second); debug("rewriting %s as %s", rewrite.first, rewrite.second);
}
drv.builder = rewriteStrings(drv.builder, rewrites); builder = rewriteStrings(builder, rewrites);
for (auto & arg : drv.args) { for (auto & arg : args)
arg = rewriteStrings(arg, rewrites); arg = rewriteStrings(arg, rewrites);
}
StringPairs newEnv; StringPairs newEnv;
for (auto & envVar : drv.env) { for (auto & envVar : env) {
auto envName = rewriteStrings(envVar.first, rewrites); auto envName = rewriteStrings(envVar.first, rewrites);
auto envValue = rewriteStrings(envVar.second, rewrites); auto envValue = rewriteStrings(envVar.second, rewrites);
newEnv.emplace(envName, envValue); newEnv.emplace(envName, envValue);
} }
drv.env = newEnv; env = std::move(newEnv);
}
static void rewriteDerivation(Store & store, BasicDerivation & drv, const StringMap & rewrites)
{
drv.applyRewrites(rewrites);
auto hashModulo = hashDerivationModulo(store, Derivation(drv), true); auto hashModulo = hashDerivationModulo(store, Derivation(drv), true);
for (auto & [outputName, output] : drv.outputs) { for (auto & [outputName, output] : drv.outputs) {

View File

@ -325,6 +325,12 @@ struct BasicDerivation
static std::string_view nameFromPath(const StorePath & storePath); 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; bool operator == (const BasicDerivation &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet. // TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=> (const BasicDerivation &) const = default; //auto operator <=> (const BasicDerivation &) const = default;