about summary refs log tree commit diff
path: root/scripts/nix-collect-garbage.in
blob: eaa706578117f550463320916edacb99f5057470 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /usr/bin/perl -w

my $linkdir = "@localstatedir@/nix/links";
my $storedir = "@prefix@/store";

my %alive;

my $keepsuccessors = 0;
my $invert = 0;

foreach my $arg (@ARGV) {
    if ($arg eq "--keep-successors") { $keepsuccessors = 1; }
    if ($arg eq "--invert") { $invert = 1; }
    else { die "unknown argument `$arg'" };
}

my $extraarg = "";
if ($keepsuccessors) { $extraarg = "--include-successors"; };
open HASHES, "nix --query --requisites $extraarg \$(cat $linkdir/*.id) |" or die "in `nix -qrh'";
while (<HASHES>) {
	chomp;
	$alive{$_} = 1;
	if ($invert) { print "$_\n"; };
}
close HASHES;

exit 0 if ($invert);

opendir(DIR, $storedir) or die "cannot opendir $storedir: $!";
my @names = readdir(DIR);
closedir DIR;

foreach my $name (@names) {
    next if ($name eq "." || $name eq "..");
    $name = "$storedir/$name";
    if (!$alive{$name}) {
        print "$name\n";
    }
}