forked from lix-project/lix
bbe97dff8b
Most functions now take a StorePath argument rather than a Path (which is just an alias for std::string). The StorePath constructor ensures that the path is syntactically correct (i.e. it looks like <store-dir>/<base32-hash>-<name>). Similarly, functions like buildPaths() now take a StorePathWithOutputs, rather than abusing Path by adding a '!<outputs>' suffix. Note that the StorePath type is implemented in Rust. This involves some hackery to allow Rust values to be used directly in C++, via a helper type whose destructor calls the Rust type's drop() function. The main issue is the dynamic nature of C++ move semantics: after we have moved a Rust value, we should not call the drop function on the original value. So when we move a value, we set the original value to bitwise zero, and the destructor only calls drop() if the value is not bitwise zero. This should be sufficient for most types. Also lots of minor cleanups to the C++ API to make it more modern (e.g. using std::optional and std::string_view in some places).
117 lines
3.4 KiB
C++
117 lines
3.4 KiB
C++
#include "parsed-derivations.hh"
|
|
|
|
namespace nix {
|
|
|
|
ParsedDerivation::ParsedDerivation(StorePath && drvPath, BasicDerivation & drv)
|
|
: drvPath(std::move(drvPath)), drv(drv)
|
|
{
|
|
/* Parse the __json attribute, if any. */
|
|
auto jsonAttr = drv.env.find("__json");
|
|
if (jsonAttr != drv.env.end()) {
|
|
try {
|
|
structuredAttrs = nlohmann::json::parse(jsonAttr->second);
|
|
} catch (std::exception & e) {
|
|
throw Error("cannot process __json attribute of '%s': %s", drvPath.to_string(), e.what());
|
|
}
|
|
}
|
|
}
|
|
|
|
std::optional<std::string> ParsedDerivation::getStringAttr(const std::string & name) const
|
|
{
|
|
if (structuredAttrs) {
|
|
auto i = structuredAttrs->find(name);
|
|
if (i == structuredAttrs->end())
|
|
return {};
|
|
else {
|
|
if (!i->is_string())
|
|
throw Error("attribute '%s' of derivation '%s' must be a string", name, drvPath.to_string());
|
|
return i->get<std::string>();
|
|
}
|
|
} else {
|
|
auto i = drv.env.find(name);
|
|
if (i == drv.env.end())
|
|
return {};
|
|
else
|
|
return i->second;
|
|
}
|
|
}
|
|
|
|
bool ParsedDerivation::getBoolAttr(const std::string & name, bool def) const
|
|
{
|
|
if (structuredAttrs) {
|
|
auto i = structuredAttrs->find(name);
|
|
if (i == structuredAttrs->end())
|
|
return def;
|
|
else {
|
|
if (!i->is_boolean())
|
|
throw Error("attribute '%s' of derivation '%s' must be a Boolean", name, drvPath.to_string());
|
|
return i->get<bool>();
|
|
}
|
|
} else {
|
|
auto i = drv.env.find(name);
|
|
if (i == drv.env.end())
|
|
return def;
|
|
else
|
|
return i->second == "1";
|
|
}
|
|
}
|
|
|
|
std::optional<Strings> ParsedDerivation::getStringsAttr(const std::string & name) const
|
|
{
|
|
if (structuredAttrs) {
|
|
auto i = structuredAttrs->find(name);
|
|
if (i == structuredAttrs->end())
|
|
return {};
|
|
else {
|
|
if (!i->is_array())
|
|
throw Error("attribute '%s' of derivation '%s' must be a list of strings", name, drvPath.to_string());
|
|
Strings res;
|
|
for (auto j = i->begin(); j != i->end(); ++j) {
|
|
if (!j->is_string())
|
|
throw Error("attribute '%s' of derivation '%s' must be a list of strings", name, drvPath.to_string());
|
|
res.push_back(j->get<std::string>());
|
|
}
|
|
return res;
|
|
}
|
|
} else {
|
|
auto i = drv.env.find(name);
|
|
if (i == drv.env.end())
|
|
return {};
|
|
else
|
|
return tokenizeString<Strings>(i->second);
|
|
}
|
|
}
|
|
|
|
StringSet ParsedDerivation::getRequiredSystemFeatures() const
|
|
{
|
|
StringSet res;
|
|
for (auto & i : getStringsAttr("requiredSystemFeatures").value_or(Strings()))
|
|
res.insert(i);
|
|
return res;
|
|
}
|
|
|
|
bool ParsedDerivation::canBuildLocally() const
|
|
{
|
|
if (drv.platform != settings.thisSystem.get()
|
|
&& !settings.extraPlatforms.get().count(drv.platform)
|
|
&& !drv.isBuiltin())
|
|
return false;
|
|
|
|
for (auto & feature : getRequiredSystemFeatures())
|
|
if (!settings.systemFeatures.get().count(feature)) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool ParsedDerivation::willBuildLocally() const
|
|
{
|
|
return getBoolAttr("preferLocalBuild") && canBuildLocally();
|
|
}
|
|
|
|
bool ParsedDerivation::substitutesAllowed() const
|
|
{
|
|
return getBoolAttr("allowSubstitutes", true);
|
|
}
|
|
|
|
}
|