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()); - else if (i.value().is_string()) - attrs.emplace(i.key(), i.value().get()); - 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 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()); + else if (i.value().is_string()) + attrs.emplace(i.key(), i.value().get()); + 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 inputFromAttrs(const Input::Attrs & attrs); void registerInputScheme(std::unique_ptr && fetcher); +Input::Attrs jsonToAttrs(const nlohmann::json & json); + nlohmann::json attrsToJson(const Input::Attrs & attrs); std::optional 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::read( auto json = nlohmann::json::parse(readFile(path)); auto version = json.value("version", 0); - if (version != 1) + + // 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); - 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)}); - } 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 <