mirror of
https://github.com/NixOS/nix
synced 2024-10-18 00:16:11 -04:00
Merge d01ee1dcc8
into 806a91f7bf
This commit is contained in:
commit
1102336137
|
@ -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',
|
||||
|
|
70
src/nix/store-add-gc-root.cc
Normal file
70
src/nix/store-add-gc-root.cc
Normal file
|
@ -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
src/nix/store-add-gc-root.md
Normal file
17
src/nix/store-add-gc-root.md
Normal file
|
@ -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.
|
||||
|
||||
)""
|
|
@ -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 \
|
||||
|
|
|
@ -184,6 +184,7 @@ suites = [
|
|||
'debugger.sh',
|
||||
'extra-sandbox-profile.sh',
|
||||
'help.sh',
|
||||
'store-add-gc-root.sh',
|
||||
],
|
||||
'workdir': meson.current_build_dir(),
|
||||
},
|
||||
|
|
55
tests/functional/store-add-gc-root.sh
Executable file
55
tests/functional/store-add-gc-root.sh
Executable file
|
@ -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…
Reference in a new issue