diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2020-02-06 14:27:31 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2020-02-06 14:27:31 +0100 |
commit | 379852a152cc2299bfd0a02e0229112ad322319c (patch) | |
tree | 773e8c806755379d03e033a50880fbbf1494ccb9 | |
parent | be2580be0172a29843ed64e01216da1c0ed9e935 (diff) |
Registry: Use attr notation instead of URLs
-rw-r--r-- | src/libexpr/flake/lockfile.cc | 13 | ||||
-rw-r--r-- | src/libstore/fetchers/fetchers.cc | 16 | ||||
-rw-r--r-- | src/libstore/fetchers/fetchers.hh | 2 | ||||
-rw-r--r-- | src/libstore/fetchers/registry.cc | 50 | ||||
-rw-r--r-- | tests/flakes.sh | 76 |
5 files changed, 110 insertions, 47 deletions
diff --git a/src/libexpr/flake/lockfile.cc b/src/libexpr/flake/lockfile.cc index 3d796eadc..bf55b4e3b 100644 --- a/src/libexpr/flake/lockfile.cc +++ b/src/libexpr/flake/lockfile.cc @@ -8,18 +8,7 @@ namespace nix::flake { FlakeRef flakeRefFromJson(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 FlakeRef::fromAttrs(attrs); + return FlakeRef::fromAttrs(jsonToAttrs(json)); } FlakeRef getFlakeRef( diff --git a/src/libstore/fetchers/fetchers.cc b/src/libstore/fetchers/fetchers.cc index 373786f38..90bdc0fc5 100644 --- a/src/libstore/fetchers/fetchers.cc +++ b/src/libstore/fetchers/fetchers.cc @@ -37,6 +37,22 @@ std::unique_ptr<Input> inputFromAttrs(const Input::Attrs & 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 json; diff --git a/src/libstore/fetchers/fetchers.hh b/src/libstore/fetchers/fetchers.hh index 9fafbffb6..4202e8339 100644 --- a/src/libstore/fetchers/fetchers.hh +++ b/src/libstore/fetchers/fetchers.hh @@ -98,6 +98,8 @@ std::unique_ptr<Input> inputFromAttrs(const Input::Attrs & attrs); void registerInputScheme(std::unique_ptr<InputScheme> && fetcher); +Input::Attrs jsonToAttrs(const nlohmann::json & json); + nlohmann::json attrsToJson(const Input::Attrs & attrs); std::optional<std::string> maybeGetStrAttr(const Input::Attrs & attrs, const std::string & name); diff --git a/src/libstore/fetchers/registry.cc b/src/libstore/fetchers/registry.cc index 9c74f118d..1fd42a169 100644 --- a/src/libstore/fetchers/registry.cc +++ b/src/libstore/fetchers/registry.cc @@ -19,31 +19,51 @@ std::shared_ptr<Registry> Registry::read( auto json = nlohmann::json::parse(readFile(path)); auto version = json.value("version", 0); - if (version != 1) - throw Error("flake registry '%s' has unsupported version %d", path, version); - auto flakes = json["flakes"]; - for (auto i = flakes.begin(); i != flakes.end(); ++i) { - // FIXME: remove 'uri' soon. - 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( - {inputFromURL(i.key()), inputFromURL(url)}); + // 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( + {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; } 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; - json["version"] = 1; - for (auto & elem : entries) - json["flakes"][elem.first->to_string()] = { {"url", elem.second->to_string()} }; + json["version"] = 2; + json["flakes"] = std::move(arr); + createDirs(dirOf(path)); - writeFile(path, json.dump(4)); + writeFile(path, json.dump(2)); } void Registry::add( diff --git a/tests/flakes.sh b/tests/flakes.sh index 434f5cb7a..6dbd534e0 100644 --- a/tests/flakes.sh +++ b/tests/flakes.sh @@ -94,27 +94,63 @@ git -C $nonFlakeDir commit -m 'Initial' cat > $registry <<EOF { - "flakes": { - "flake:flake1": { - "url": "git+file://$flake1Dir" - }, - "flake:flake2": { - "url": "git+file://$flake2Dir" - }, - "flake:flake3": { - "url": "git+file://$flake3Dir" - }, - "flake:flake4": { - "url": "flake:flake3" - }, - "flake:flake5": { - "url": "hg+file://$flake5Dir" - }, - "flake:nixpkgs": { - "url": "flake:flake1" - } + "version": 2, + "flakes": [ + { "from": { + "type": "indirect", + "id": "flake1" + }, + "to": { + "type": "git", + "url": "file://$flake1Dir" + } }, - "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 |