2018-09-28 12:31:16 +00:00
|
|
|
#include "parsed-derivations.hh"
|
|
|
|
|
2020-03-24 13:26:13 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-05-02 15:24:14 +00:00
|
|
|
#include <regex>
|
|
|
|
#include "json.hh"
|
2020-03-24 13:26:13 +00:00
|
|
|
|
2018-09-28 12:31:16 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2020-06-16 20:20:18 +00:00
|
|
|
ParsedDerivation::ParsedDerivation(const StorePath & drvPath, BasicDerivation & drv)
|
|
|
|
: drvPath(drvPath), drv(drv)
|
2018-09-28 12:31:16 +00:00
|
|
|
{
|
|
|
|
/* Parse the __json attribute, if any. */
|
|
|
|
auto jsonAttr = drv.env.find("__json");
|
|
|
|
if (jsonAttr != drv.env.end()) {
|
|
|
|
try {
|
2020-03-24 13:26:13 +00:00
|
|
|
structuredAttrs = std::make_unique<nlohmann::json>(nlohmann::json::parse(jsonAttr->second));
|
2018-09-28 12:31:16 +00:00
|
|
|
} catch (std::exception & e) {
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error("cannot process __json attribute of '%s': %s", drvPath.to_string(), e.what());
|
2018-09-28 12:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:26:13 +00:00
|
|
|
ParsedDerivation::~ParsedDerivation() { }
|
|
|
|
|
2019-02-12 12:43:32 +00:00
|
|
|
std::optional<std::string> ParsedDerivation::getStringAttr(const std::string & name) const
|
2018-09-28 12:31:16 +00:00
|
|
|
{
|
|
|
|
if (structuredAttrs) {
|
|
|
|
auto i = structuredAttrs->find(name);
|
|
|
|
if (i == structuredAttrs->end())
|
|
|
|
return {};
|
|
|
|
else {
|
|
|
|
if (!i->is_string())
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a string", name, drvPath.to_string());
|
2018-09-28 12:31:16 +00: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 18:11:09 +00:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a Boolean", name, drvPath.to_string());
|
2018-09-28 12:31:16 +00: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 12:43:32 +00:00
|
|
|
std::optional<Strings> ParsedDerivation::getStringsAttr(const std::string & name) const
|
2018-09-28 12:31:16 +00:00
|
|
|
{
|
|
|
|
if (structuredAttrs) {
|
|
|
|
auto i = structuredAttrs->find(name);
|
|
|
|
if (i == structuredAttrs->end())
|
|
|
|
return {};
|
|
|
|
else {
|
|
|
|
if (!i->is_array())
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a list of strings", name, drvPath.to_string());
|
2018-09-28 12:31:16 +00:00
|
|
|
Strings res;
|
|
|
|
for (auto j = i->begin(); j != i->end(); ++j) {
|
|
|
|
if (!j->is_string())
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error("attribute '%s' of derivation '%s' must be a list of strings", name, drvPath.to_string());
|
2018-09-28 12:31:16 +00: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 13:57:27 +00:00
|
|
|
StringSet ParsedDerivation::getRequiredSystemFeatures() const
|
|
|
|
{
|
|
|
|
StringSet res;
|
|
|
|
for (auto & i : getStringsAttr("requiredSystemFeatures").value_or(Strings()))
|
|
|
|
res.insert(i);
|
2021-06-11 06:37:59 +00:00
|
|
|
if (!derivationHasKnownOutputPaths(drv.type()))
|
|
|
|
res.insert("ca-derivations");
|
2018-09-28 13:57:27 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:32:36 +00:00
|
|
|
bool ParsedDerivation::canBuildLocally(Store & localStore) const
|
2018-09-28 12:31:16 +00:00
|
|
|
{
|
2018-09-28 13:57:27 +00:00
|
|
|
if (drv.platform != settings.thisSystem.get()
|
|
|
|
&& !settings.extraPlatforms.get().count(drv.platform)
|
|
|
|
&& !drv.isBuiltin())
|
|
|
|
return false;
|
|
|
|
|
2021-01-12 00:28:00 +00:00
|
|
|
if (settings.maxBuildJobs.get() == 0
|
|
|
|
&& !drv.isBuiltin())
|
|
|
|
return false;
|
|
|
|
|
2018-09-28 13:57:27 +00:00
|
|
|
for (auto & feature : getRequiredSystemFeatures())
|
2020-08-12 16:32:36 +00:00
|
|
|
if (!localStore.systemFeatures.get().count(feature)) return false;
|
2018-09-28 13:57:27 +00:00
|
|
|
|
|
|
|
return true;
|
2018-09-28 12:31:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 16:32:36 +00:00
|
|
|
bool ParsedDerivation::willBuildLocally(Store & localStore) const
|
2018-09-28 12:31:16 +00:00
|
|
|
{
|
2020-08-12 16:32:36 +00:00
|
|
|
return getBoolAttr("preferLocalBuild") && canBuildLocally(localStore);
|
2018-09-28 12:31:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:02:12 +00:00
|
|
|
bool ParsedDerivation::substitutesAllowed() const
|
|
|
|
{
|
|
|
|
return getBoolAttr("allowSubstitutes", true);
|
|
|
|
}
|
|
|
|
|
2021-05-02 15:24:14 +00:00
|
|
|
static std::regex shVarName("[A-Za-z_][A-Za-z0-9_]*");
|
2021-07-12 15:17:13 +00:00
|
|
|
|
2021-06-24 13:06:07 +00:00
|
|
|
std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths)
|
2021-05-02 15:24:14 +00:00
|
|
|
{
|
|
|
|
auto structuredAttrs = getStructuredAttrs();
|
|
|
|
if (!structuredAttrs) return std::nullopt;
|
|
|
|
|
|
|
|
auto json = *structuredAttrs;
|
|
|
|
|
|
|
|
/* Add an "outputs" object containing the output paths. */
|
|
|
|
nlohmann::json outputs;
|
2021-07-12 15:17:13 +00:00
|
|
|
for (auto & i : drv.outputs)
|
2021-06-24 13:06:07 +00:00
|
|
|
outputs[i.first] = hashPlaceholder(i.first);
|
2021-05-02 15:24:14 +00:00
|
|
|
json["outputs"] = outputs;
|
|
|
|
|
|
|
|
/* Handle exportReferencesGraph. */
|
|
|
|
auto e = json.find("exportReferencesGraph");
|
|
|
|
if (e != json.end() && e->is_object()) {
|
|
|
|
for (auto i = e->begin(); i != e->end(); ++i) {
|
|
|
|
std::ostringstream str;
|
|
|
|
{
|
|
|
|
JSONPlaceholder jsonRoot(str, true);
|
|
|
|
StorePathSet storePaths;
|
|
|
|
for (auto & p : *i)
|
|
|
|
storePaths.insert(store.parseStorePath(p.get<std::string>()));
|
|
|
|
store.pathInfoToJSON(jsonRoot,
|
|
|
|
store.exportReferences(storePaths, inputPaths), false, true);
|
|
|
|
}
|
|
|
|
json[i.key()] = nlohmann::json::parse(str.str()); // urgh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 14:11:56 +00:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2021-06-22 18:37:25 +00:00
|
|
|
/* As a convenience to bash scripts, write a shell file that
|
|
|
|
maps all attributes that are representable in bash -
|
|
|
|
namely, strings, integers, nulls, Booleans, and arrays and
|
|
|
|
objects consisting entirely of those values. (So nested
|
|
|
|
arrays or objects are not supported.) */
|
2021-07-12 15:17:13 +00:00
|
|
|
std::string writeStructuredAttrsShell(const nlohmann::json & json)
|
2021-05-13 14:11:56 +00:00
|
|
|
{
|
2021-05-02 15:24:14 +00:00
|
|
|
|
|
|
|
auto handleSimpleType = [](const nlohmann::json & value) -> std::optional<std::string> {
|
|
|
|
if (value.is_string())
|
|
|
|
return shellEscape(value);
|
|
|
|
|
|
|
|
if (value.is_number()) {
|
|
|
|
auto f = value.get<float>();
|
|
|
|
if (std::ceil(f) == f)
|
|
|
|
return std::to_string(value.get<int>());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_null())
|
|
|
|
return std::string("''");
|
|
|
|
|
|
|
|
if (value.is_boolean())
|
|
|
|
return value.get<bool>() ? std::string("1") : std::string("");
|
|
|
|
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string jsonSh;
|
|
|
|
|
2021-07-12 15:17:13 +00:00
|
|
|
for (auto & [key, value] : json.items()) {
|
2021-05-02 15:24:14 +00:00
|
|
|
|
2021-07-12 15:17:13 +00:00
|
|
|
if (!std::regex_match(key, shVarName)) continue;
|
2021-05-02 15:24:14 +00:00
|
|
|
|
|
|
|
auto s = handleSimpleType(value);
|
|
|
|
if (s)
|
2021-07-12 15:17:13 +00:00
|
|
|
jsonSh += fmt("declare %s=%s\n", key, *s);
|
2021-05-02 15:24:14 +00:00
|
|
|
|
|
|
|
else if (value.is_array()) {
|
|
|
|
std::string s2;
|
|
|
|
bool good = true;
|
|
|
|
|
2021-07-12 15:17:13 +00:00
|
|
|
for (auto & value2 : value) {
|
|
|
|
auto s3 = handleSimpleType(value2);
|
2021-05-02 15:24:14 +00:00
|
|
|
if (!s3) { good = false; break; }
|
|
|
|
s2 += *s3; s2 += ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (good)
|
2021-07-12 15:17:13 +00:00
|
|
|
jsonSh += fmt("declare -a %s=(%s)\n", key, s2);
|
2021-05-02 15:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (value.is_object()) {
|
|
|
|
std::string s2;
|
|
|
|
bool good = true;
|
|
|
|
|
2021-07-12 15:17:13 +00:00
|
|
|
for (auto & [key2, value2] : value.items()) {
|
|
|
|
auto s3 = handleSimpleType(value2);
|
2021-05-02 15:24:14 +00:00
|
|
|
if (!s3) { good = false; break; }
|
2021-07-12 15:17:13 +00:00
|
|
|
s2 += fmt("[%s]=%s ", shellEscape(key2), *s3);
|
2021-05-02 15:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (good)
|
2021-07-12 15:17:13 +00:00
|
|
|
jsonSh += fmt("declare -A %s=(%s)\n", key, s2);
|
2021-05-02 15:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 14:11:56 +00:00
|
|
|
return jsonSh;
|
2021-05-02 15:24:14 +00:00
|
|
|
}
|
2018-09-28 12:31:16 +00:00
|
|
|
}
|