From fdec72c6cc720be899431c32f99221e8c4b88cd0 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 25 Aug 2004 15:39:13 +0000 Subject: [PATCH] * `nix-collect-garbage' now actually performs a garbage collection, it doesn't just print the set of paths that should be deleted. So there is no more need to pipe the result into `nix-store --delete' (which doesn't even exist anymore). --- doc/manual/nix-collect-garbage.xml | 57 ++++++++++++------------ doc/manual/nix-store.xml | 70 +++++++++++++++++++++++++----- doc/manual/quick-start.xml | 2 +- scripts/nix-collect-garbage.in | 44 ++++++------------- src/nix-store/main.cc | 2 +- 5 files changed, 102 insertions(+), 73 deletions(-) diff --git a/doc/manual/nix-collect-garbage.xml b/doc/manual/nix-collect-garbage.xml index 8ff741e22..adc6c1e73 100644 --- a/doc/manual/nix-collect-garbage.xml +++ b/doc/manual/nix-collect-garbage.xml @@ -1,14 +1,16 @@ nix-collect-garbage - determine the set of unreachable store paths + remove unreachable store paths nix-collect-garbage - - + + + + @@ -16,10 +18,22 @@ Description - The command nix-collect-garbage determines - the paths in the Nix store that are garbage, that is, not - reachable from outside of the store. These paths can be safely - deleted without affecting the integrity of the system. + The command nix-collect-garbage performs a + garbage collection on the Nix store: any paths in the Nix store + that are garbage (not reachable from a set of root store + expressions) are deleted. + + + + The roots of the garbage collector are the store expressions + mentioned in the files in the directory + prefix/var/nix/gcroots. + By default, the roots are all user environments in + prefix/var/nix/profiles. + You can register other store expressions as roots by writing the + full path of the store expression to an arbitrary file in the + gcroots directory (or a subdirectory + thereof). @@ -30,27 +44,14 @@ - + / - Causes the set of reachable paths to - be printed, rather than the unreachable paths. These are - the paths that may not be deleted. - - - - - - - - - Causes nix-collect-garbage not to - follow successor relations. By default, if a derivation - store expression is reachable, its successor (i.e., a - closure store expression) is also considered to be - reachable. This option is always safe, but garbage - collecting successors may cause undesirable rebuilds later - on. + These options cause the set of live or dead paths to be + printed, respectively, rather than performing an actual + garbage collector. They correspond exactly with the + sub-operations in nix-store + . @@ -63,10 +64,10 @@ Examples - To delete all unreachable paths, do the following: + To delete all unreachable paths, just do: -$ nix-collect-garbage | xargs nix-store --delete +$ nix-collect-garbage diff --git a/doc/manual/nix-store.xml b/doc/manual/nix-store.xml index a3fcad063..febe02fd4 100644 --- a/doc/manual/nix-store.xml +++ b/doc/manual/nix-store.xml @@ -135,17 +135,18 @@ - Operation <option>--delete</option> + Operation <option>--gc</option> Synopsis nix-store + + + - - paths @@ -153,19 +154,64 @@ Description - The operation unconditionally deletes the - paths paths from the Nix store. It is an - error to attempt to delete paths outside of the store. + The operation performs a garbage + collection on the Nix store. What it does specifically is + determined by the sub-operation, which is one of the + following: + + + + + + + + + This operation prints on standard output the set of + live store paths, which are all the store + paths reachable from a set of root store + expressions read from standard input. Live paths should + never be deleted, since that would break consistency + — it would become possible that applications are + installed that reference things that are no longer + present in the store. + + + + + + + + + This operation prints out on standard output the set of + dead store paths, which is just the + opposite of the set of live paths: any path in the store + that is not live (with respect to the roots) is dead. + + + + + + + + + This operation performs an actual garbage collection. + All dead paths are removed from the store. + + + + + + + + The set of root store expressions is read from standard input. + Each line should contain exactly one store path. - This operation should almost never be called directly, since no - attempt is made to verify that no references exist to the paths to - be deleted. Therefore, careless deletion can result in an - inconsistent system. Deletion of paths in the store is done by the - garbage collector (which uses to delete - unreferenced paths). + You generally will want to use the command + nix-collect-garbage, which figures out + the roots and then calls this command automatically. diff --git a/doc/manual/quick-start.xml b/doc/manual/quick-start.xml index 294fbcba2..badbfa294 100644 --- a/doc/manual/quick-start.xml +++ b/doc/manual/quick-start.xml @@ -131,7 +131,7 @@ $ nix-env -uBf nixpkgs-version/ '*' actual delete them: -$ nix-collect-garbage | xargs nix-store --delete +$ nix-collect-garbage diff --git a/scripts/nix-collect-garbage.in b/scripts/nix-collect-garbage.in index 8b571536d..539979cbb 100755 --- a/scripts/nix-collect-garbage.in +++ b/scripts/nix-collect-garbage.in @@ -8,17 +8,17 @@ my $storeDir = "@storedir@"; my %alive; +my $gcOper = "--delete"; my $keepSuccessors = 1; -my $invert = 0; my @roots = (); # Parse the command line. foreach my $arg (@ARGV) { - if ($arg eq "--no-successors") { $keepSuccessors = 0; } - elsif ($arg eq "--invert") { $invert = 1; } - else { die "unknown argument `$arg'" }; + if ($arg eq "--delete" || $arg eq "--print-live" || $arg eq "--print-dead") { + $gcOper = $arg; + } else { die "unknown argument `$arg'" }; } @@ -68,33 +68,15 @@ sub findRoots { findRoots 1, $rootsDir; -# Determine all store paths reachable from the roots. -my $extraarg = ""; -if ($keepSuccessors) { $extraarg = "--include-successors"; }; -my $pid = open2(\*READ, \*WRITE, "@bindir@/nix-store --query --requisites $extraarg @roots") - or die "determining live paths"; -close WRITE; -while () { - chomp; - $alive{$_} = 1; - if ($invert) { print "$_\n"; }; +# Run the collector with the roots we found. +my $pid = open2(">&1", \*WRITE, "@bindir@/nix-store --gc $gcOper") + or die "cannot run `nix-store --gc'"; + +foreach my $root (@roots) { + print WRITE "$root\n"; } -close READ; + +close WRITE; waitpid $pid, 0; -$? == 0 or die "determining live paths"; - -exit 0 if ($invert); - - -# Using that information, find all store paths *not* reachable from -# the roots. -opendir(DIR, $storeDir) or die "cannot open directory $storeDir: $!"; -foreach my $name (readdir DIR) { - next if ($name eq "." || $name eq ".."); - $name = "$storeDir/$name"; - if (!$alive{$name}) { - print "$name\n"; - } -} -closedir DIR; +$? == 0 or die "`nix-store --gc' failed"; diff --git a/src/nix-store/main.cc b/src/nix-store/main.cc index 78599e308..7bc8565d2 100644 --- a/src/nix-store/main.cc +++ b/src/nix-store/main.cc @@ -222,7 +222,7 @@ static void opGC(Strings opFlags, Strings opArgs) if (flag == "--print-live") subOp = soPrintLive; else if (flag == "--print-dead") subOp = soPrintDead; else if (flag == "--delete") subOp = soDelete; - else throw UsageError(format("bad sub-operation `%1% in GC") % flag); + else throw UsageError(format("bad sub-operation `%1%' in GC") % flag); Paths roots; while (1) {