From fee7efbc0968cc7bace7c55e5b7d013394a63671 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Sun, 15 Sep 2024 22:52:27 +0200 Subject: [PATCH] Add an 'add-root' comamnd to manually add indirect roots --- src/nix/add-root.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++ src/nix/add-root.md | 18 +++++++++++++ src/nix/meson.build | 1 + 3 files changed, 80 insertions(+) create mode 100644 src/nix/add-root.cc create mode 100644 src/nix/add-root.md diff --git a/src/nix/add-root.cc b/src/nix/add-root.cc new file mode 100644 index 000000000..6773ebb05 --- /dev/null +++ b/src/nix/add-root.cc @@ -0,0 +1,61 @@ +#include "command.hh" +#include "args.hh" +#include "shared.hh" +#include "store-cast.hh" +#include "indirect-root-store.hh" +#include "common-args.hh" +#include "strings.hh" +#include "installable-derived-path.hh" + +using namespace nix; + +struct CmdAddRoot: StoreCommand +{ + std::vector links; + bool checkResults = true; + + CmdAddRoot() + { + expectArgs({ + .label = "indirect-roots", + .handler = {&links}, + .completer = completePath, + }); + } + + std::string description() override + { + return "Add indirect gc-roots through the symlink arguments"; + } + + std::string doc() override + { + return + #include "add-root.md" + ; + } + + Category category() override { return catSecondary; } + + void run(ref store) override + { + auto & indirectRootStore = require(*store); + + for (auto &link: links) { + auto indirectPath = absPath(link); + if (indirectRootStore.isInStore(indirectPath)) { + throw Error("Indirect root '%1%' must not be in the Nix store", link); + } + + if (checkResults) { + auto path = indirectRootStore.followLinksToStorePath(indirectPath); + indirectRootStore.addTempRoot(path); + // TODO: ensure the path is safe from concurrent GC of fail. + } + + indirectRootStore.addIndirectRoot(indirectPath); + } + } +}; + +static auto rCmdAddRoot = registerCommand("add-root"); diff --git a/src/nix/add-root.md b/src/nix/add-root.md new file mode 100644 index 000000000..5a932a950 --- /dev/null +++ b/src/nix/add-root.md @@ -0,0 +1,18 @@ +R""( + +# Examples + + ```console + $ readlink foo + /nix/store/xxx + $ nix add-root foo + $ nix-store -q --roots /nix/store/xxx + .../foo -> /nix/store/xxx + ``` + +# Description + +This command adds garbage collector root to the paths referenced by the symlinks passed as arguments. +These are called indirect roots, as the root will disappear as soon as the intermediate symlink gets deleted. + +)"" diff --git a/src/nix/meson.build b/src/nix/meson.build index 6edb768e3..68c55495a 100644 --- a/src/nix/meson.build +++ b/src/nix/meson.build @@ -68,6 +68,7 @@ subdir('build-utils-meson/diagnostics') subdir('build-utils-meson/generate-header') nix_sources = [config_h] + files( + 'add-root.cc', 'add-to-store.cc', 'app.cc', 'self-exe.cc',