lix/src/nix/registry.cc

243 lines
5.9 KiB
C++
Raw Normal View History

#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 "url-parts.hh"
#include "registry.hh"
using namespace nix;
using namespace nix::flake;
2021-06-30 19:13:32 +00:00
2021-07-07 08:02:55 +00:00
class RegistryCommand : virtual Args
2021-06-30 19:13:32 +00:00
{
std::string registry_path;
std::shared_ptr<fetchers::Registry> registry;
public:
RegistryCommand()
{
addFlag({
.longName = "registry",
.description = "The registry to operate on.",
.labels = {"registry"},
.handler = {&registry_path},
});
}
2021-07-07 08:02:55 +00:00
std::shared_ptr<fetchers::Registry> getRegistry()
{
2021-06-30 19:13:32 +00:00
if (registry) return registry;
if (registry_path.empty()) {
registry = fetchers::getUserRegistry();
} else {
registry = fetchers::getCustomRegistry(registry_path);
}
return registry;
}
2021-07-07 08:02:55 +00:00
Path getRegistryPath()
{
2021-06-30 19:13:32 +00:00
if (registry_path.empty()) {
return fetchers::getUserRegistryPath();
} else {
return registry_path;
}
}
};
struct CmdRegistryList : StoreCommand
{
std::string description() override
{
return "list available Nix flakes";
}
std::string doc() override
{
return
#include "registry-list.md"
;
}
void run(nix::ref<nix::Store> store) override
{
using namespace fetchers;
auto registries = getRegistries(store);
for (auto & registry : registries) {
for (auto & entry : registry->entries) {
// FIXME: format nicely
logger->cout("%s %s %s",
registry->type == Registry::Flag ? "flags " :
registry->type == Registry::User ? "user " :
registry->type == Registry::System ? "system" :
"global",
entry.from.toURLString(),
entry.to.toURLString(attrsToQuery(entry.extraAttrs)));
}
}
}
};
2021-06-30 19:13:32 +00:00
struct CmdRegistryAdd : MixEvalArgs, Command, RegistryCommand
{
std::string fromUrl, toUrl;
std::string description() override
{
return "add/replace flake in user flake registry";
}
std::string doc() override
{
return
#include "registry-add.md"
;
}
CmdRegistryAdd()
{
expectArg("from-url", &fromUrl);
expectArg("to-url", &toUrl);
}
void run() override
{
std::smatch match;
if (!std::regex_match(fromUrl, match, flakeShorthandRegex)) {
throw UsageError("'from-url' argument must be a shorthand like 'nixpkgs' or 'nixpkgs/nixos-20.03'");
}
auto fromRef = parseFlakeRef(fromUrl);
if (fromRef.input.direct) {
throw UsageError("'from-url' argument must be an indirect flakeref like 'nixpkgs' or 'flake:nixpkgs'");
}
auto toRef = parseFlakeRef(toUrl);
2021-06-30 19:13:32 +00:00
auto registry = getRegistry();
fetchers::Attrs extraAttrs;
if (toRef.subdir != "") extraAttrs["dir"] = toRef.subdir;
2021-06-30 19:13:32 +00:00
registry->remove(fromRef.input);
registry->add(fromRef.input, toRef.input, extraAttrs);
registry->write(getRegistryPath());
}
};
2021-06-30 19:13:32 +00:00
struct CmdRegistryRemove : RegistryCommand, Command
{
std::string url;
std::string description() override
{
return "remove flake from user flake registry";
}
std::string doc() override
{
return
#include "registry-remove.md"
;
}
CmdRegistryRemove()
{
expectArg("url", &url);
}
void run() override
{
2021-06-30 19:13:32 +00:00
auto registry = getRegistry();
registry->remove(parseFlakeRef(url).input);
registry->write(getRegistryPath());
}
};
2021-06-30 19:13:32 +00:00
struct CmdRegistryPin : RegistryCommand, EvalCommand
{
std::string url;
std::string locked;
std::string description() override
{
2021-07-07 08:02:55 +00:00
return "pin a flake to its current version or to the current version of a flake URL";
}
std::string doc() override
{
return
#include "registry-pin.md"
;
}
CmdRegistryPin()
{
expectArg("url", &url);
expectArgs({
.label = "locked",
.optional = true,
.handler = {&locked},
Overhaul completions, redo #6693 (#8131) As I complained in https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a comment on the wrong PR, sorry again!), #6693 introduced a second completions mechanism to fix a bug. Having two completion mechanisms isn't so nice. As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef` to `std::string` when collecting flake refs. Now it is `FlakeRefs` again. The underlying issue that sought to work around was that completion of arguments not at the end can still benefit from the information from latter arguments. To fix this better, we rip out that change and simply defer all completion processing until after all the (regular, already-complete) arguments have been passed. In addition, I noticed the original completion logic used some global variables. I do not like global variables, because even if they save lines of code, they also obfuscate the architecture of the code. I got rid of them moved them to a new `RootArgs` class, which now has `parseCmdline` instead of `Args`. The idea is that we have many argument parsers from subcommands and what-not, but only one root args that owns the other per actual parsing invocation. The state that was global is now part of the root args instead. This did, admittedly, add a bunch of new code. And I do feel bad about that. So I went and added a lot of API docs to try to at least make the current state of things clear to the next person. -- This is needed for RFC 134 (tracking issue #7868). It was very hard to modularize `Installable` parsing when there were two completion arguments. I wouldn't go as far as to say it is *easy* now, but at least it is less hard (and the completions test finally passed). Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io> Change-Id: If18cd5be78da4a70635e3fdcac6326dbfeea71a5 (cherry picked from commit 67eb37c1d0de28160cd25376e51d1ec1b1c8305b)
2024-03-19 02:23:20 +00:00
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
completeFlakeRef(completions, getStore(), prefix);
}}
});
}
void run(nix::ref<nix::Store> store) override
{
if (locked.empty()) locked = url;
2021-06-30 19:13:32 +00:00
auto registry = getRegistry();
auto ref = parseFlakeRef(url);
auto lockedRef = parseFlakeRef(locked);
2021-06-30 19:13:32 +00:00
registry->remove(ref.input);
auto [tree, resolved] = lockedRef.resolve(store).input.fetch(store);
fetchers::Attrs extraAttrs;
if (ref.subdir != "") extraAttrs["dir"] = ref.subdir;
2021-06-30 19:13:32 +00:00
registry->add(ref.input, resolved, extraAttrs);
registry->write(getRegistryPath());
}
};
struct CmdRegistry : virtual NixMultiCommand
{
CmdRegistry()
: MultiCommand({
{"list", []() { return make_ref<CmdRegistryList>(); }},
{"add", []() { return make_ref<CmdRegistryAdd>(); }},
{"remove", []() { return make_ref<CmdRegistryRemove>(); }},
{"pin", []() { return make_ref<CmdRegistryPin>(); }},
})
{
}
std::string description() override
{
return "manage the flake registry";
}
std::string doc() override
{
return
#include "registry.md"
;
}
Category category() override { return catSecondary; }
void run() override
{
experimentalFeatureSettings.require(Xp::Flakes);
if (!command)
throw UsageError("'nix registry' requires a sub-command.");
command->second->run();
}
};
static auto rCmdRegistry = registerCommand<CmdRegistry>("registry");