nix-collect-garbage: Support --dry-run

This commit is contained in:
Eelco Dolstra 2012-09-13 18:09:20 -04:00
parent 9fd9dedf12
commit 983220bcd4
2 changed files with 7 additions and 3 deletions

View file

@ -26,6 +26,7 @@
<arg choice='plain'><option>--print-dead</option></arg> <arg choice='plain'><option>--print-dead</option></arg>
<arg choice='plain'><option>--delete</option></arg> <arg choice='plain'><option>--delete</option></arg>
</group> </group>
<arg><option>--dry-run</option></arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>

View file

@ -9,10 +9,13 @@ my $profilesDir = "@localstatedir@/nix/profiles";
# Process the command line arguments. # Process the command line arguments.
my @args = (); my @args = ();
my $removeOld = 0; my $removeOld = 0;
my $dryRun = 0;
for my $arg (@ARGV) { for my $arg (@ARGV) {
if ($arg eq "--delete-old" || $arg eq "-d") { if ($arg eq "--delete-old" || $arg eq "-d") {
$removeOld = 1; $removeOld = 1;
} elsif ($arg eq "--dry-run") {
$dryRun = 1;
} else { } else {
push @args, $arg; push @args, $arg;
} }
@ -35,13 +38,13 @@ sub removeOldGenerations {
$name = $dir . "/" . $name; $name = $dir . "/" . $name;
if (-l $name && (readlink($name) =~ /link/)) { if (-l $name && (readlink($name) =~ /link/)) {
print STDERR "removing old generations of profile $name\n"; print STDERR "removing old generations of profile $name\n";
system("$Nix::Config::binDir/nix-env", "-p", $name, "--delete-generations", "old"); system("$Nix::Config::binDir/nix-env", "-p", $name, "--delete-generations", "old", $dryRun ? "--dry-run" : ());
} }
elsif (! -l $name && -d $name) { elsif (! -l $name && -d $name) {
removeOldGenerations $name; removeOldGenerations $name;
} }
} }
closedir $dh or die; closedir $dh or die;
} }
@ -49,4 +52,4 @@ removeOldGenerations $profilesDir if $removeOld;
# Run the actual garbage collector. # Run the actual garbage collector.
exec "$Nix::Config::binDir/nix-store", "--gc", @args; exec "$Nix::Config::binDir/nix-store", "--gc", @args unless $dryRun;