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
70 changes: 70 additions & 0 deletions src/nix/store-add-gc-root.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#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);
if (!indirectRootStore.isValidPath(path)) {
throw Error("Indirect root '%1%' is no a symbolic link to a valid store path", link);
}
}

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/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ nix_tests = \
nix-profile.sh \
suggestions.sh \
store-info.sh \
store-add-gc-root.sh \
fetchClosure.sh \
completions.sh \
impure-derivations.sh \
Expand Down
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
55 changes: 55 additions & 0 deletions tests/functional/store-add-gc-root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

source common.sh

TODO_NixOS

clearStore

function delete() { nix-store --delete "$storePath"; }
function build() { nix build --no-link --print-out-paths -f simple.nix; }

# Check that path can be deleted
storePath=$(build)
delete

# Check that add-gc-root prevents deletion,
# and removing the root make it deletable again.
storePath=$(build)
ln -sn "$storePath" myroot
nix store add-gc-root myroot
if delete; then false; fi
rm myroot
delete

# Create several roots at once
storePath=$(build)
ln -sn "$storePath" myroot1
ln -sn "$storePath" myroot2
ln -sn "$storePath" myroot3
nix store add-gc-root myroot1 myroot2 myroot3
if delete; then false; fi
rm myroot3 myroot2
if delete; then false; fi
rm myroot1
delete

# Test detection of invalid roots
# 1. path deleted before root creation
storePath=$(build)
delete
ln -sn "$storePath" myroot
if nix store add-gc-root myroot; then false; fi
nix store add-gc-root --no-check myroot
rm myroot

# 2. invalid path
ln -sn /invalid-target myroot
if nix store add-gc-root myroot; then false; fi
nix store add-gc-root --no-check myroot
rm myroot

# Fail when trying to setup a direct root
storePath=$(build)
if nix store add-gc-root "$storePath"; then false; fi

Loading