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 all 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
1 change: 1 addition & 0 deletions src/nix/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ nix_sources = [config_h] + files(
'run.cc',
'search.cc',
'sigs.cc',
'store-add-gc-root.cc',
'store-copy-log.cc',
'store-delete.cc',
'store-gc.cc',
Expand Down
68 changes: 68 additions & 0 deletions src/nix/store-add-gc-root.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "command.hh"
#include "args.hh"
#include "shared.hh"
#include "store-cast.hh"
#include "indirect-root-store.hh"
#include "common-args.hh"

using namespace nix;

struct CmdAddGCRoot : StoreCommand
{
std::vector<std::string> links;
bool checkResults = true;

CmdAddGCRoot()
{
expectArgs({
.label = "indirect-roots",
.handler = {&links},
.completer = completePath,
});

addFlag({
.longName = "no-check",
.description = "Do not test the validity of created roots.",
.handler = {&checkResults, false},
});
}

std::string description() override
{
return "Add indirect gc roots through the symlink arguments";
}

std::string doc() override
{
return
#include "store-add-gc-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 `path` is safe from concurrent GC or fail.
}

indirectRootStore.addIndirectRoot(indirectPath);
}
}
};

static auto rCmdAddGCRoot = registerCommand2<CmdAddGCRoot>({"store", "add-gc-root"});
17 changes: 17 additions & 0 deletions src/nix/store-add-gc-root.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
R""(

# Examples

```console
$ ln -s /nix/store/xxx foo
$ nix store add-gc-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.

)""
1 change: 1 addition & 0 deletions tests/functional/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ suites = [
'debugger.sh',
'extra-sandbox-profile.sh',
'help.sh',
'store-add-gc-root.sh',
],
'workdir': meson.current_build_dir(),
},
Expand Down
Loading