Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'store add-gc-root' command to manually add indirect roots #11505

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/nix/add-root.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#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<std::string> 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> store) override
{
auto & indirectRootStore = require<IndirectRootStore>(*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<CmdAddRoot>("add-root");
18 changes: 18 additions & 0 deletions src/nix/add-root.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
R""(

# Examples

```console
$ readlink foo
/nix/store/xxx
$ nix add-root foo
Copy link
Member

@Mic92 Mic92 Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intuitively I would have expected such as subcommand here:

Suggested change
$ nix add-root foo
$ nix store add-gc-root foo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to add it in nix gc add-root, but nix gc seems mostly unused and intended to start a gc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently don't have a top-level nix gc subcommand group, but a nix store gc command.

To help keep some consistency between commands, the subcommands are ideally nix <noun> <verb> with the exception of a small number of frequently used commands.
gc is a verb, since the "garbage" part is subordinate to the "collect" part (as a noun adjunct to be technical).
Since our commands end in verbs, this would create the expectation that nix gc is not a subcommand group, but a single action.

I think nix store add-root would be ok, but perhaps nix store add-gc-root would be better, as I think the hint about gc would be welcomed when reading scripts that call this command.
Interactive use should not be common, because that would usually come after nix build, which already produces a root.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who makes final decisions about that ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nix team members, I would say. Often happens in those regular meetings.

$ 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.

)""
1 change: 1 addition & 0 deletions src/nix/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading