Registry: Use attr notation instead of URLs

This commit is contained in:
Eelco Dolstra 2020-02-06 14:27:31 +01:00
parent be2580be01
commit 379852a152
5 changed files with 110 additions and 47 deletions

View file

@ -8,18 +8,7 @@ namespace nix::flake {
FlakeRef flakeRefFromJson(const nlohmann::json & json) FlakeRef flakeRefFromJson(const nlohmann::json & json)
{ {
fetchers::Input::Attrs attrs; return FlakeRef::fromAttrs(jsonToAttrs(json));
for (auto & i : json.items()) {
if (i.value().is_number())
attrs.emplace(i.key(), i.value().get<int64_t>());
else if (i.value().is_string())
attrs.emplace(i.key(), i.value().get<std::string>());
else
throw Error("unsupported input attribute type in lock file");
}
return FlakeRef::fromAttrs(attrs);
} }
FlakeRef getFlakeRef( FlakeRef getFlakeRef(

View file

@ -37,6 +37,22 @@ std::unique_ptr<Input> inputFromAttrs(const Input::Attrs & attrs)
throw Error("input '%s' is unsupported", attrsToJson(attrs)); throw Error("input '%s' is unsupported", attrsToJson(attrs));
} }
Input::Attrs jsonToAttrs(const nlohmann::json & json)
{
fetchers::Input::Attrs attrs;
for (auto & i : json.items()) {
if (i.value().is_number())
attrs.emplace(i.key(), i.value().get<int64_t>());
else if (i.value().is_string())
attrs.emplace(i.key(), i.value().get<std::string>());
else
throw Error("unsupported input attribute type in lock file");
}
return attrs;
}
nlohmann::json attrsToJson(const fetchers::Input::Attrs & attrs) nlohmann::json attrsToJson(const fetchers::Input::Attrs & attrs)
{ {
nlohmann::json json; nlohmann::json json;

View file

@ -98,6 +98,8 @@ std::unique_ptr<Input> inputFromAttrs(const Input::Attrs & attrs);
void registerInputScheme(std::unique_ptr<InputScheme> && fetcher); void registerInputScheme(std::unique_ptr<InputScheme> && fetcher);
Input::Attrs jsonToAttrs(const nlohmann::json & json);
nlohmann::json attrsToJson(const Input::Attrs & attrs); nlohmann::json attrsToJson(const Input::Attrs & attrs);
std::optional<std::string> maybeGetStrAttr(const Input::Attrs & attrs, const std::string & name); std::optional<std::string> maybeGetStrAttr(const Input::Attrs & attrs, const std::string & name);

View file

@ -19,12 +19,11 @@ std::shared_ptr<Registry> Registry::read(
auto json = nlohmann::json::parse(readFile(path)); auto json = nlohmann::json::parse(readFile(path));
auto version = json.value("version", 0); auto version = json.value("version", 0);
if (version != 1)
throw Error("flake registry '%s' has unsupported version %d", path, version);
// FIXME: remove soon
if (version == 1) {
auto flakes = json["flakes"]; auto flakes = json["flakes"];
for (auto i = flakes.begin(); i != flakes.end(); ++i) { for (auto i = flakes.begin(); i != flakes.end(); ++i) {
// FIXME: remove 'uri' soon.
auto url = i->value("url", i->value("uri", "")); auto url = i->value("url", i->value("uri", ""));
if (url.empty()) if (url.empty())
throw Error("flake registry '%s' lacks a 'url' attribute for entry '%s'", throw Error("flake registry '%s' lacks a 'url' attribute for entry '%s'",
@ -32,18 +31,39 @@ std::shared_ptr<Registry> Registry::read(
registry->entries.push_back( registry->entries.push_back(
{inputFromURL(i.key()), inputFromURL(url)}); {inputFromURL(i.key()), inputFromURL(url)});
} }
}
else if (version == 2) {
for (auto & i : json["flakes"])
registry->entries.push_back(
{ inputFromAttrs(jsonToAttrs(i["from"]))
, inputFromAttrs(jsonToAttrs(i["to"]))
});
}
else
throw Error("flake registry '%s' has unsupported version %d", path, version);
return registry; return registry;
} }
void Registry::write(const Path & path) void Registry::write(const Path & path)
{ {
nlohmann::json arr;
for (auto & elem : entries) {
nlohmann::json obj;
obj["from"] = attrsToJson(elem.first->toAttrs());
obj["to"] = attrsToJson(elem.second->toAttrs());
arr.emplace_back(std::move(obj));
}
nlohmann::json json; nlohmann::json json;
json["version"] = 1; json["version"] = 2;
for (auto & elem : entries) json["flakes"] = std::move(arr);
json["flakes"][elem.first->to_string()] = { {"url", elem.second->to_string()} };
createDirs(dirOf(path)); createDirs(dirOf(path));
writeFile(path, json.dump(4)); writeFile(path, json.dump(2));
} }
void Registry::add( void Registry::add(

View file

@ -94,27 +94,63 @@ git -C $nonFlakeDir commit -m 'Initial'
cat > $registry <<EOF cat > $registry <<EOF
{ {
"flakes": { "version": 2,
"flake:flake1": { "flakes": [
"url": "git+file://$flake1Dir" { "from": {
"type": "indirect",
"id": "flake1"
}, },
"flake:flake2": { "to": {
"url": "git+file://$flake2Dir" "type": "git",
}, "url": "file://$flake1Dir"
"flake:flake3": {
"url": "git+file://$flake3Dir"
},
"flake:flake4": {
"url": "flake:flake3"
},
"flake:flake5": {
"url": "hg+file://$flake5Dir"
},
"flake:nixpkgs": {
"url": "flake:flake1"
} }
}, },
"version": 1 { "from": {
"type": "indirect",
"id": "flake2"
},
"to": {
"type": "git",
"url": "file://$flake2Dir"
}
},
{ "from": {
"type": "indirect",
"id": "flake3"
},
"to": {
"type": "git",
"url": "file://$flake3Dir"
}
},
{ "from": {
"type": "indirect",
"id": "flake4"
},
"to": {
"type": "indirect",
"id": "flake3"
}
},
{ "from": {
"type": "indirect",
"id": "flake5"
},
"to": {
"type": "hg",
"url": "file://$flake5Dir"
}
},
{ "from": {
"type": "indirect",
"id": "nixpkgs"
},
"to": {
"type": "indirect",
"id": "flake1"
}
}
]
} }
EOF EOF