From 5f64655ff429be08aa0787761697787e7050f373 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 15 May 2020 14:38:10 +0200 Subject: [PATCH] Move registry-related commands from 'nix flake' to 'nix registry' This makes 'nix flake' less cluttered and more consistent (it's only subcommands that operator on a flake). Also, the registry is not inherently flake-related (e.g. fetchTree could also use it to remap inputs). --- src/nix/flake.cc | 108 ------------------------------- src/nix/registry.cc | 150 ++++++++++++++++++++++++++++++++++++++++++++ src/nix/run.cc | 1 - tests/flakes.sh | 18 +++--- 4 files changed, 159 insertions(+), 118 deletions(-) create mode 100644 src/nix/registry.cc diff --git a/src/nix/flake.cc b/src/nix/flake.cc index b6cc7eb54..d255bdda5 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -55,34 +55,6 @@ public: } }; -struct CmdFlakeList : EvalCommand -{ - std::string description() override - { - return "list available Nix flakes"; - } - - void run(nix::ref store) override - { - using namespace fetchers; - - auto registries = getRegistries(store); - - for (auto & registry : registries) { - for (auto & entry : registry->entries) { - // FIXME: format nicely - logger->stdout("%s %s %s", - registry->type == Registry::Flag ? "flags " : - registry->type == Registry::User ? "user " : - registry->type == Registry::System ? "system" : - "global", - entry.from->to_string(), - entry.to->to_string()); - } - } - } -}; - static void printFlakeInfo(const Store & store, const Flake & flake) { logger->stdout("Resolved URL: %s", flake.resolvedRef.to_string()); @@ -472,82 +444,6 @@ struct CmdFlakeCheck : FlakeCommand } }; -struct CmdFlakeAdd : MixEvalArgs, Command -{ - std::string fromUrl, toUrl; - - std::string description() override - { - return "upsert flake in user flake registry"; - } - - CmdFlakeAdd() - { - expectArg("from-url", &fromUrl); - expectArg("to-url", &toUrl); - } - - void run() override - { - auto fromRef = parseFlakeRef(fromUrl); - auto toRef = parseFlakeRef(toUrl); - fetchers::Attrs extraAttrs; - if (toRef.subdir != "") extraAttrs["dir"] = toRef.subdir; - auto userRegistry = fetchers::getUserRegistry(); - userRegistry->remove(fromRef.input); - userRegistry->add(fromRef.input, toRef.input, extraAttrs); - userRegistry->write(fetchers::getUserRegistryPath()); - } -}; - -struct CmdFlakeRemove : virtual Args, MixEvalArgs, Command -{ - std::string url; - - std::string description() override - { - return "remove flake from user flake registry"; - } - - CmdFlakeRemove() - { - expectArg("url", &url); - } - - void run() override - { - auto userRegistry = fetchers::getUserRegistry(); - userRegistry->remove(parseFlakeRef(url).input); - userRegistry->write(fetchers::getUserRegistryPath()); - } -}; - -struct CmdFlakePin : virtual Args, EvalCommand -{ - std::string url; - - std::string description() override - { - return "pin a flake to its current version in user flake registry"; - } - - CmdFlakePin() - { - expectArg("url", &url); - } - - void run(nix::ref store) override - { - auto ref = parseFlakeRef(url); - auto userRegistry = fetchers::getUserRegistry(); - userRegistry->remove(ref.input); - auto [tree, resolved] = ref.resolve(store).input->fetchTree(store); - fetchers::Attrs extraAttrs; - if (ref.subdir != "") extraAttrs["dir"] = ref.subdir; - userRegistry->add(ref.input, resolved, extraAttrs); - } -}; - struct CmdFlakeInit : virtual Args, Command { std::string description() override @@ -836,14 +732,10 @@ struct CmdFlake : virtual MultiCommand, virtual Command { CmdFlake() : MultiCommand({ - {"list", []() { return make_ref(); }}, {"update", []() { return make_ref(); }}, {"info", []() { return make_ref(); }}, {"list-inputs", []() { return make_ref(); }}, {"check", []() { return make_ref(); }}, - {"add", []() { return make_ref(); }}, - {"remove", []() { return make_ref(); }}, - {"pin", []() { return make_ref(); }}, {"init", []() { return make_ref(); }}, {"clone", []() { return make_ref(); }}, {"archive", []() { return make_ref(); }}, diff --git a/src/nix/registry.cc b/src/nix/registry.cc new file mode 100644 index 000000000..e9c76e6c6 --- /dev/null +++ b/src/nix/registry.cc @@ -0,0 +1,150 @@ +#include "command.hh" +#include "common-args.hh" +#include "shared.hh" +#include "eval.hh" +#include "flake/flake.hh" +#include "store-api.hh" +#include "fetchers.hh" +#include "registry.hh" + +using namespace nix; +using namespace nix::flake; + +struct CmdRegistryList : StoreCommand +{ + std::string description() override + { + return "list available Nix flakes"; + } + + void run(nix::ref store) override + { + using namespace fetchers; + + auto registries = getRegistries(store); + + for (auto & registry : registries) { + for (auto & entry : registry->entries) { + // FIXME: format nicely + logger->stdout("%s %s %s", + registry->type == Registry::Flag ? "flags " : + registry->type == Registry::User ? "user " : + registry->type == Registry::System ? "system" : + "global", + entry.from->to_string(), + entry.to->to_string()); + } + } + } +}; + +struct CmdRegistryAdd : MixEvalArgs, Command +{ + std::string fromUrl, toUrl; + + std::string description() override + { + return "add/replace flake in user flake registry"; + } + + CmdRegistryAdd() + { + expectArg("from-url", &fromUrl); + expectArg("to-url", &toUrl); + } + + void run() override + { + auto fromRef = parseFlakeRef(fromUrl); + auto toRef = parseFlakeRef(toUrl); + fetchers::Attrs extraAttrs; + if (toRef.subdir != "") extraAttrs["dir"] = toRef.subdir; + auto userRegistry = fetchers::getUserRegistry(); + userRegistry->remove(fromRef.input); + userRegistry->add(fromRef.input, toRef.input, extraAttrs); + userRegistry->write(fetchers::getUserRegistryPath()); + } +}; + +struct CmdRegistryRemove : virtual Args, MixEvalArgs, Command +{ + std::string url; + + std::string description() override + { + return "remove flake from user flake registry"; + } + + CmdRegistryRemove() + { + expectArg("url", &url); + } + + void run() override + { + auto userRegistry = fetchers::getUserRegistry(); + userRegistry->remove(parseFlakeRef(url).input); + userRegistry->write(fetchers::getUserRegistryPath()); + } +}; + +struct CmdRegistryPin : virtual Args, EvalCommand +{ + std::string url; + + std::string description() override + { + return "pin a flake to its current version in user flake registry"; + } + + CmdRegistryPin() + { + expectArg("url", &url); + } + + void run(nix::ref store) override + { + auto ref = parseFlakeRef(url); + auto userRegistry = fetchers::getUserRegistry(); + userRegistry->remove(ref.input); + auto [tree, resolved] = ref.resolve(store).input->fetchTree(store); + fetchers::Attrs extraAttrs; + if (ref.subdir != "") extraAttrs["dir"] = ref.subdir; + userRegistry->add(ref.input, resolved, extraAttrs); + } +}; + +struct CmdRegistry : virtual MultiCommand, virtual Command +{ + CmdRegistry() + : MultiCommand({ + {"list", []() { return make_ref(); }}, + {"add", []() { return make_ref(); }}, + {"remove", []() { return make_ref(); }}, + {"pin", []() { return make_ref(); }}, + }) + { + } + + std::string description() override + { + return "manage the flake registry"; + } + + Category category() override { return catSecondary; } + + void run() override + { + if (!command) + throw UsageError("'nix registry' requires a sub-command."); + command->second->prepare(); + command->second->run(); + } + + void printHelp(const string & programName, std::ostream & out) override + { + MultiCommand::printHelp(programName, out); + } +}; + +static auto r1 = registerCommand("registry"); diff --git a/src/nix/run.cc b/src/nix/run.cc index f9b1298f1..2ab9d9ef4 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -108,7 +108,6 @@ struct CmdShell : InstallablesCommand, RunCommon, MixEnvironment auto accessor = store->getFSAccessor(); - std::unordered_set done; std::queue todo; for (auto & path : outPaths) todo.push(path.clone()); diff --git a/tests/flakes.sh b/tests/flakes.sh index 1b234c992..d28beb486 100644 --- a/tests/flakes.sh +++ b/tests/flakes.sh @@ -151,7 +151,7 @@ cat > $registry <