1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 15:02:42 +01:00

Further consolidate environment variable processing outside DerivationBuilder

Now, `DerivationBuilder` only concerns itself with `finalEnv` and
`extraFiles`, in straightforward unconditional code. All the fancy
desugaring logic is consolidated in `DerivationBuildingGoal`.

We should better share the pulled-out logic with `nix-shell`/`nix
develop`, which would fill in some missing features, arguably fixing
bugs.
This commit is contained in:
John Ericson 2025-08-18 18:08:35 -04:00
parent e3c74f5a13
commit 1d3ddb21fa
3 changed files with 38 additions and 33 deletions

View file

@ -1016,37 +1016,20 @@ void DerivationBuilderImpl::initEnv()
/* The maximum number of cores to utilize for parallel building. */
env["NIX_BUILD_CORES"] = fmt("%d", settings.buildCores ? settings.buildCores : settings.getDefaultCores());
auto writeEnv = [&](const std::string & envVarName, const std::string & fileName, const std::string & value) {
writeBuilderFile(fileName, rewriteStrings(value, inputRewrites));
env[envVarName] = tmpDirInSandbox() + "/" + fileName;
};
/* In non-structured mode, set all bindings either directory in the
environment or via a file, as specified by
`DerivationOptions::passAsFile`. */
if (!drv.structuredAttrs) {
for (auto & i : drv.env) {
if (drvOptions.passAsFile.find(i.first) == drvOptions.passAsFile.end()) {
env[i.first] = i.second;
} else {
auto hash = hashString(HashAlgorithm::SHA256, i.first);
std::string fn = ".attr-" + hash.to_string(HashFormat::Nix32, false);
writeEnv(i.first + "Path", fn, i.second);
}
}
}
/* Do this with or without structured attrs --- actually, this is
used to desugar structured attrs. */
for (const auto & [name, info] : extraEnv) {
/* Write the final environment. Note that this is intentionally
*not* `drv.env`, because we've desugared things like like
"passAFile", "expandReferencesGraph", structured attrs, etc. */
for (const auto & [name, info] : finalEnv) {
if (info.nameOfPassAsFile) {
writeEnv(name, *info.nameOfPassAsFile, info.value);
auto & fileName = *info.nameOfPassAsFile;
writeBuilderFile(fileName, rewriteStrings(info.value, inputRewrites));
env[name] = tmpDirInSandbox() + "/" + fileName;
} else {
env[name] = info.value;
}
}
/* Add extra files, analogous to `extraEnv` */
/* Add extra files, similar to `finalEnv` */
for (const auto & [fileName, value] : extraFiles) {
writeBuilderFile(fileName, value);
}