2018-09-28 15:31:16 +03:00
|
|
|
#include "parsed-derivations.hh"
|
|
|
|
|
2019-10-21 23:11:21 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2018-09-28 15:31:16 +03:00
|
|
|
namespace nix {
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
ParsedDerivation::ParsedDerivation(StorePath && drvPath, BasicDerivation & drv)
|
|
|
|
: drvPath(std::move(drvPath)), drv(drv)
|
2018-09-28 15:31:16 +03:00
|
|
|
{
|
|
|
|
/* Parse the __json attribute, if any. */
|
|
|
|
auto jsonAttr = drv.env.find("__json");
|
|
|
|
if (jsonAttr != drv.env.end()) {
|
|
|
|
try {
|
2019-10-21 23:11:21 +03:00
|
|
|
structuredAttrs = std::make_unique<nlohmann::json>(nlohmann::json::parse(jsonAttr->second));
|
2018-09-28 15:31:16 +03:00
|
|
|
} catch (std::exception & e) {
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error("cannot process __json attribute of '%s': %s", drvPath.to_string(), e.what());
|
2018-09-28 15:31:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 23:11:21 +03:00
|
|
|
ParsedDerivation::~ParsedDerivation() { }
|
|
|
|
|
2019-02-12 14:43:32 +02:00
|
|
|
std::optional<std::string> ParsedDerivation::getStringAttr(const std::string & name) const
|
2018-09-28 15:31:16 +03:00
|
|
|
{
|
|
|
|
if (structuredAttrs) {
|
|
|
|
auto i = structuredAttrs->find(name);
|
|
|
|
if (i == structuredAttrs->end())
|
|
|
|
return {};
|
|
|
|
else {
|
|
|
|
if (!i->is_string())
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a string", name, drvPath.to_string());
|
2018-09-28 15:31:16 +03:00
|
|
|
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())
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a Boolean", name, drvPath.to_string());
|
2018-09-28 15:31:16 +03:00
|
|
|
return i->get<bool>();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto i = drv.env.find(name);
|
|
|
|
if (i == drv.env.end())
|
|
|
|
return def;
|
|
|
|
else
|
|
|
|
return i->second == "1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 14:43:32 +02:00
|
|
|
std::optional<Strings> ParsedDerivation::getStringsAttr(const std::string & name) const
|
2018-09-28 15:31:16 +03:00
|
|
|
{
|
|
|
|
if (structuredAttrs) {
|
|
|
|
auto i = structuredAttrs->find(name);
|
|
|
|
if (i == structuredAttrs->end())
|
|
|
|
return {};
|
|
|
|
else {
|
|
|
|
if (!i->is_array())
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a list of strings", name, drvPath.to_string());
|
2018-09-28 15:31:16 +03:00
|
|
|
Strings res;
|
|
|
|
for (auto j = i->begin(); j != i->end(); ++j) {
|
|
|
|
if (!j->is_string())
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a list of strings", name, drvPath.to_string());
|
2018-09-28 15:31:16 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:57:27 +03:00
|
|
|
StringSet ParsedDerivation::getRequiredSystemFeatures() const
|
|
|
|
{
|
|
|
|
StringSet res;
|
|
|
|
for (auto & i : getStringsAttr("requiredSystemFeatures").value_or(Strings()))
|
|
|
|
res.insert(i);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:31:16 +03:00
|
|
|
bool ParsedDerivation::canBuildLocally() const
|
|
|
|
{
|
2018-09-28 16:57:27 +03:00
|
|
|
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;
|
2018-09-28 15:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParsedDerivation::willBuildLocally() const
|
|
|
|
{
|
|
|
|
return getBoolAttr("preferLocalBuild") && canBuildLocally();
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:02:12 +03:00
|
|
|
bool ParsedDerivation::substitutesAllowed() const
|
|
|
|
{
|
|
|
|
return getBoolAttr("allowSubstitutes", true);
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:31:16 +03:00
|
|
|
}
|