Skip to content

Commit

Permalink
nix-collect-garbage: Add --delete-older-than option
Browse files Browse the repository at this point in the history
  • Loading branch information
wizeman authored and edolstra committed Mar 29, 2014
1 parent 7ef7597 commit 034b6f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
18 changes: 12 additions & 6 deletions doc/manual/nix-collect-garbage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<command>nix-collect-garbage</command>
<arg><option>--delete-old</option></arg>
<arg><option>-d</option></arg>
<arg><option>--delete-older-than</option> <replaceable>period</replaceable></arg>
<group choice='opt'>
<arg choice='plain'><option>--print-roots</option></arg>
<arg choice='plain'><option>--print-live</option></arg>
Expand All @@ -35,13 +36,18 @@
<para>The command <command>nix-collect-garbage</command> is mostly an
alias of <link linkend="rsec-nix-store-gc"><command>nix-store
--gc</command></link>, that is, it deletes all unreachable paths in
the Nix store to clean up your system. However, it provides an
additional option <option>-d</option> (<option>--delete-old</option>)
that deletes all old generations of all profiles in
the Nix store to clean up your system. However, it provides two
additional options: <option>-d</option> (<option>--delete-old</option>),
which deletes all old generations of all profiles in
<filename>/nix/var/nix/profiles</filename> by invoking
<literal>nix-env --delete-generations old</literal> on all profiles.
Of course, this makes rollbacks to previous configurations
impossible.</para>
<literal>nix-env --delete-generations old</literal> on all profiles
(of course, this makes rollbacks to previous configurations
impossible); and
<option>--delete-older-than</option> <replaceable>period</replaceable>,
where period is a value such as <literal>30d</literal>, which deletes
all non-current generations that are older than the specified number of
days in all profiles in <filename>/nix/var/nix/profiles</filename>.
</para>

</refsection>

Expand Down
12 changes: 10 additions & 2 deletions scripts/nix-collect-garbage.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ my $profilesDir = "@localstatedir@/nix/profiles";

# Process the command line arguments.
my @args = ();
my $arg;

my $removeOld = 0;
my $gen;
my $dryRun = 0;

for my $arg (@ARGV) {
while ($arg = shift) {
if ($arg eq "--delete-old" || $arg eq "-d") {
$removeOld = 1;
$gen = "old";
} elsif ($arg eq "--delete-older-than") {
$removeOld = 1;
$gen = shift;
} elsif ($arg eq "--dry-run") {
$dryRun = 1;
} elsif ($arg eq "--help") {
Expand All @@ -40,7 +47,8 @@ sub removeOldGenerations {
$name = $dir . "/" . $name;
if (-l $name && (readlink($name) =~ /link/)) {
print STDERR "removing old generations of profile $name\n";
system("$Nix::Config::binDir/nix-env", "-p", $name, "--delete-generations", "old", $dryRun ? "--dry-run" : ());

system("$Nix::Config::binDir/nix-env", "-p", $name, "--delete-generations", $gen, $dryRun ? "--dry-run" : ());
}
elsif (! -l $name && -d $name) {
removeOldGenerations $name;
Expand Down

0 comments on commit 034b6f6

Please sign in to comment.