2020-03-30 12:03:28 +00:00
|
|
|
#include "registry.hh"
|
|
|
|
#include "fetchers.hh"
|
2020-01-21 15:27:53 +00:00
|
|
|
#include "util.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "download.hh"
|
2020-03-18 13:08:25 +00:00
|
|
|
#include "store-api.hh"
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
|
|
|
std::shared_ptr<Registry> Registry::read(
|
|
|
|
const Path & path, RegistryType type)
|
|
|
|
{
|
2020-01-22 19:00:58 +00:00
|
|
|
auto registry = std::make_shared<Registry>(type);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
if (!pathExists(path))
|
2020-01-22 19:00:58 +00:00
|
|
|
return std::make_shared<Registry>(type);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
auto json = nlohmann::json::parse(readFile(path));
|
|
|
|
|
|
|
|
auto version = json.value("version", 0);
|
|
|
|
|
2020-02-06 13:27:31 +00:00
|
|
|
// FIXME: remove soon
|
|
|
|
if (version == 1) {
|
|
|
|
auto flakes = json["flakes"];
|
|
|
|
for (auto i = flakes.begin(); i != flakes.end(); ++i) {
|
|
|
|
auto url = i->value("url", i->value("uri", ""));
|
|
|
|
if (url.empty())
|
|
|
|
throw Error("flake registry '%s' lacks a 'url' attribute for entry '%s'",
|
|
|
|
path, i.key());
|
|
|
|
registry->entries.push_back(
|
2020-02-20 21:14:44 +00:00
|
|
|
{inputFromURL(i.key()), inputFromURL(url), {}});
|
2020-02-06 13:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (version == 2) {
|
2020-02-20 21:14:44 +00:00
|
|
|
for (auto & i : json["flakes"]) {
|
|
|
|
auto toAttrs = jsonToAttrs(i["to"]);
|
2020-03-17 19:54:36 +00:00
|
|
|
Attrs extraAttrs;
|
2020-02-20 22:44:06 +00:00
|
|
|
auto j = toAttrs.find("dir");
|
2020-02-20 21:14:44 +00:00
|
|
|
if (j != toAttrs.end()) {
|
|
|
|
extraAttrs.insert(*j);
|
|
|
|
toAttrs.erase(j);
|
|
|
|
}
|
2020-04-01 21:12:45 +00:00
|
|
|
auto exact = i.find("exact");
|
2020-02-06 13:27:31 +00:00
|
|
|
registry->entries.push_back(
|
2020-04-01 21:03:27 +00:00
|
|
|
Entry {
|
|
|
|
.from = inputFromAttrs(jsonToAttrs(i["from"])),
|
|
|
|
.to = inputFromAttrs(toAttrs),
|
|
|
|
.extraAttrs = extraAttrs,
|
2020-04-01 21:12:45 +00:00
|
|
|
.exact = exact != i.end() && exact.value()
|
2020-02-06 13:27:31 +00:00
|
|
|
});
|
2020-02-20 21:14:44 +00:00
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:27:31 +00:00
|
|
|
else
|
|
|
|
throw Error("flake registry '%s' has unsupported version %d", path, version);
|
|
|
|
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registry::write(const Path & path)
|
|
|
|
{
|
2020-02-06 13:27:31 +00:00
|
|
|
nlohmann::json arr;
|
2020-04-01 21:03:27 +00:00
|
|
|
for (auto & entry : entries) {
|
2020-02-06 13:27:31 +00:00
|
|
|
nlohmann::json obj;
|
2020-04-01 21:03:27 +00:00
|
|
|
obj["from"] = attrsToJson(entry.from->toAttrs());
|
|
|
|
obj["to"] = attrsToJson(entry.to->toAttrs());
|
|
|
|
if (!entry.extraAttrs.empty())
|
|
|
|
obj["to"].update(attrsToJson(entry.extraAttrs));
|
2020-04-01 21:12:45 +00:00
|
|
|
if (entry.exact)
|
|
|
|
obj["exact"] = true;
|
2020-02-06 13:27:31 +00:00
|
|
|
arr.emplace_back(std::move(obj));
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
nlohmann::json json;
|
2020-02-06 13:27:31 +00:00
|
|
|
json["version"] = 2;
|
|
|
|
json["flakes"] = std::move(arr);
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
createDirs(dirOf(path));
|
2020-02-06 13:27:31 +00:00
|
|
|
writeFile(path, json.dump(2));
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Registry::add(
|
|
|
|
const std::shared_ptr<const Input> & from,
|
2020-02-20 21:14:44 +00:00
|
|
|
const std::shared_ptr<const Input> & to,
|
2020-03-17 19:54:36 +00:00
|
|
|
const Attrs & extraAttrs)
|
2020-01-21 15:27:53 +00:00
|
|
|
{
|
2020-04-01 21:03:27 +00:00
|
|
|
entries.emplace_back(
|
|
|
|
Entry {
|
|
|
|
.from = from,
|
|
|
|
.to = to,
|
|
|
|
.extraAttrs = extraAttrs
|
|
|
|
});
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Registry::remove(const std::shared_ptr<const Input> & input)
|
|
|
|
{
|
|
|
|
// FIXME: use C++20 std::erase.
|
|
|
|
for (auto i = entries.begin(); i != entries.end(); )
|
2020-04-01 21:03:27 +00:00
|
|
|
if (*i->from == *input)
|
2020-01-21 15:27:53 +00:00
|
|
|
i = entries.erase(i);
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2020-04-01 20:56:50 +00:00
|
|
|
static Path getSystemRegistryPath()
|
|
|
|
{
|
|
|
|
return settings.nixConfDir + "/registry.json";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::shared_ptr<Registry> getSystemRegistry()
|
|
|
|
{
|
|
|
|
static auto systemRegistry =
|
|
|
|
Registry::read(getSystemRegistryPath(), Registry::System);
|
|
|
|
return systemRegistry;
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
Path getUserRegistryPath()
|
|
|
|
{
|
|
|
|
return getHome() + "/.config/nix/registry.json";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Registry> getUserRegistry()
|
|
|
|
{
|
2020-04-01 20:56:50 +00:00
|
|
|
static auto userRegistry =
|
|
|
|
Registry::read(getUserRegistryPath(), Registry::User);
|
|
|
|
return userRegistry;
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:00:58 +00:00
|
|
|
static std::shared_ptr<Registry> flagRegistry =
|
|
|
|
std::make_shared<Registry>(Registry::Flag);
|
|
|
|
|
|
|
|
std::shared_ptr<Registry> getFlagRegistry()
|
2020-01-21 15:27:53 +00:00
|
|
|
{
|
|
|
|
return flagRegistry;
|
|
|
|
}
|
2020-01-22 19:00:58 +00:00
|
|
|
|
|
|
|
void overrideRegistry(
|
|
|
|
const std::shared_ptr<const Input> & from,
|
2020-02-20 21:14:44 +00:00
|
|
|
const std::shared_ptr<const Input> & to,
|
2020-03-17 19:54:36 +00:00
|
|
|
const Attrs & extraAttrs)
|
2020-01-22 19:00:58 +00:00
|
|
|
{
|
2020-02-20 21:14:44 +00:00
|
|
|
flagRegistry->add(from, to, extraAttrs);
|
2020-01-22 19:00:58 +00:00
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
static std::shared_ptr<Registry> getGlobalRegistry(ref<Store> store)
|
|
|
|
{
|
|
|
|
static auto reg = [&]() {
|
|
|
|
auto path = settings.flakeRegistry;
|
|
|
|
|
2020-03-19 10:45:34 +00:00
|
|
|
if (!hasPrefix(path, "/")) {
|
|
|
|
auto storePath = downloadFile(store, path, "flake-registry.json", false).storePath;
|
|
|
|
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
|
|
|
|
store2->addPermRoot(storePath, getCacheDir() + "/nix/flake-registry.json", true);
|
|
|
|
path = store->toRealPath(storePath);
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
return Registry::read(path, Registry::Global);
|
|
|
|
}();
|
|
|
|
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Registries getRegistries(ref<Store> store)
|
|
|
|
{
|
|
|
|
Registries registries;
|
2020-01-22 19:00:58 +00:00
|
|
|
registries.push_back(getFlagRegistry());
|
2020-01-21 15:27:53 +00:00
|
|
|
registries.push_back(getUserRegistry());
|
2020-04-01 20:56:50 +00:00
|
|
|
registries.push_back(getSystemRegistry());
|
2020-01-21 15:27:53 +00:00
|
|
|
registries.push_back(getGlobalRegistry(store));
|
|
|
|
return registries;
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
std::pair<std::shared_ptr<const Input>, Attrs> lookupInRegistries(
|
2020-01-21 15:27:53 +00:00
|
|
|
ref<Store> store,
|
|
|
|
std::shared_ptr<const Input> input)
|
|
|
|
{
|
2020-03-17 19:54:36 +00:00
|
|
|
Attrs extraAttrs;
|
2020-01-21 15:27:53 +00:00
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
restart:
|
|
|
|
|
|
|
|
n++;
|
|
|
|
if (n > 100) throw Error("cycle detected in flake registr for '%s'", input);
|
|
|
|
|
|
|
|
for (auto & registry : getRegistries(store)) {
|
|
|
|
// FIXME: O(n)
|
|
|
|
for (auto & entry : registry->entries) {
|
2020-04-01 21:12:45 +00:00
|
|
|
if (entry.exact) {
|
|
|
|
if (*entry.from == *input) {
|
|
|
|
input = entry.to;
|
|
|
|
extraAttrs = entry.extraAttrs;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (entry.from->contains(*input)) {
|
|
|
|
input = entry.to->applyOverrides(
|
|
|
|
!entry.from->getRef() && input->getRef() ? input->getRef() : std::optional<std::string>(),
|
|
|
|
!entry.from->getRev() && input->getRev() ? input->getRev() : std::optional<Hash>());
|
|
|
|
extraAttrs = entry.extraAttrs;
|
|
|
|
goto restart;
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input->isDirect())
|
|
|
|
throw Error("cannot find flake '%s' in the flake registries", input->to_string());
|
|
|
|
|
2020-02-20 21:14:44 +00:00
|
|
|
return {input, extraAttrs};
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|