2012-01-05 21:33:46 +01:00
|
|
|
#! @perl@ -w @perlFlags@
|
2006-09-06 16:23:04 +02:00
|
|
|
|
|
|
|
use strict;
|
2012-01-03 01:47:27 +01:00
|
|
|
use Nix::Config;
|
2006-09-06 16:23:04 +02:00
|
|
|
|
|
|
|
my $profilesDir = "@localstatedir@/nix/profiles";
|
|
|
|
|
|
|
|
|
|
|
|
# Process the command line arguments.
|
|
|
|
my @args = ();
|
2014-03-11 22:16:00 +01:00
|
|
|
my $arg;
|
|
|
|
|
2006-09-06 16:23:04 +02:00
|
|
|
my $removeOld = 0;
|
2014-03-11 22:16:00 +01:00
|
|
|
my $gen;
|
2012-09-14 00:09:20 +02:00
|
|
|
my $dryRun = 0;
|
2006-09-06 16:23:04 +02:00
|
|
|
|
2014-03-11 22:16:00 +01:00
|
|
|
while ($arg = shift) {
|
2006-09-06 16:23:04 +02:00
|
|
|
if ($arg eq "--delete-old" || $arg eq "-d") {
|
|
|
|
$removeOld = 1;
|
2014-03-11 22:16:00 +01:00
|
|
|
$gen = "old";
|
|
|
|
} elsif ($arg eq "--delete-older-than") {
|
|
|
|
$removeOld = 1;
|
|
|
|
$gen = shift;
|
2012-09-14 00:09:20 +02:00
|
|
|
} elsif ($arg eq "--dry-run") {
|
|
|
|
$dryRun = 1;
|
2012-10-03 22:37:06 +02:00
|
|
|
} elsif ($arg eq "--help") {
|
|
|
|
exec "man nix-collect-garbage" or die;
|
2006-09-06 16:23:04 +02:00
|
|
|
} else {
|
|
|
|
push @args, $arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# If `-d' was specified, remove all old generations of all profiles.
|
|
|
|
# Of course, this makes rollbacks to before this point in time
|
|
|
|
# impossible.
|
|
|
|
|
2007-03-13 12:30:57 +01:00
|
|
|
sub removeOldGenerations;
|
|
|
|
sub removeOldGenerations {
|
|
|
|
my $dir = shift;
|
2006-09-06 16:23:04 +02:00
|
|
|
|
2007-03-13 12:30:57 +01:00
|
|
|
my $dh;
|
|
|
|
opendir $dh, $dir or die;
|
|
|
|
|
|
|
|
foreach my $name (sort (readdir $dh)) {
|
|
|
|
next if $name eq "." || $name eq "..";
|
|
|
|
$name = $dir . "/" . $name;
|
2006-09-06 16:23:04 +02:00
|
|
|
if (-l $name && (readlink($name) =~ /link/)) {
|
|
|
|
print STDERR "removing old generations of profile $name\n";
|
2014-03-11 22:16:00 +01:00
|
|
|
|
|
|
|
system("$Nix::Config::binDir/nix-env", "-p", $name, "--delete-generations", $gen, $dryRun ? "--dry-run" : ());
|
2006-09-06 16:23:04 +02:00
|
|
|
}
|
2007-03-13 12:30:57 +01:00
|
|
|
elsif (! -l $name && -d $name) {
|
|
|
|
removeOldGenerations $name;
|
|
|
|
}
|
2006-09-06 16:23:04 +02:00
|
|
|
}
|
2012-09-14 00:09:20 +02:00
|
|
|
|
2007-03-13 12:30:57 +01:00
|
|
|
closedir $dh or die;
|
2006-09-06 16:23:04 +02:00
|
|
|
}
|
|
|
|
|
2007-03-13 12:30:57 +01:00
|
|
|
removeOldGenerations $profilesDir if $removeOld;
|
|
|
|
|
2006-09-06 16:23:04 +02:00
|
|
|
|
|
|
|
# Run the actual garbage collector.
|
2012-09-14 00:09:20 +02:00
|
|
|
exec "$Nix::Config::binDir/nix-store", "--gc", @args unless $dryRun;
|