about summary refs log tree commit diff
path: root/scripts/copy-from-other-stores.pl.in
blob: 6a0de1f050e5b05eba9a0b88cbc2e8d1af3ad4ad (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! @perl@ -w @perlFlags@

use strict;
use File::Basename;
use IO::Handle;

my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@";


STDOUT->autoflush(1);

my @remoteStoresAll = split ':', ($ENV{"NIX_OTHER_STORES"} or "");

my @remoteStores;
foreach my $dir (@remoteStoresAll) {
    push @remoteStores, glob($dir);
}

exit if scalar @remoteStores == 0;
print "\n";


$ENV{"NIX_REMOTE"} = "";


sub findStorePath {
    my $storePath = shift;
    foreach my $store (@remoteStores) {
        my $sourcePath = "$store/store/" . basename $storePath;
        next unless -e $sourcePath || -l $sourcePath;
        $ENV{"NIX_DB_DIR"} = "$store/var/nix/db";
        return ($store, $sourcePath) if
            system("@bindir@/nix-store --check-validity $storePath") == 0;
    }
    return undef;
}


if ($ARGV[0] eq "--query") {

    while (<STDIN>) {
        chomp;
        my ($cmd, @args) = split " ", $_;

        if ($cmd eq "have") {
            foreach my $storePath (@args) {
                print "$storePath\n" if defined findStorePath($storePath);
            }
            print "\n";
        }

        elsif ($cmd eq "info") {
            foreach my $storePath (@args) {
                my ($store, $sourcePath) = findStorePath($storePath);
                next unless defined $store;

                $ENV{"NIX_DB_DIR"} = "$store/var/nix/db";

                my $deriver = `@bindir@/nix-store --query --deriver $storePath`;
                die "cannot query deriver of `$storePath'" if $? != 0;
                chomp $deriver;
                $deriver = "" if $deriver eq "unknown-deriver";

                my @references = split "\n",
                    `@bindir@/nix-store --query --references $storePath`;
                die "cannot query references of `$storePath'" if $? != 0;

                my $narSize = `@bindir@/nix-store --query --size $storePath`;
                die "cannot query size of `$storePath'" if $? != 0;
                chomp $narSize;

                print "$storePath\n";
                print "$deriver\n";
                print scalar @references, "\n";
                print "$_\n" foreach @references;
                print "0\n";
                print "$narSize\n";
            }

            print "\n";
        }

        else { die "unknown command `$cmd'"; }
    }
}


elsif ($ARGV[0] eq "--substitute") {
    die unless scalar @ARGV == 3;
    my $storePath = $ARGV[1];
    my $destPath = $ARGV[2];
    my ($store, $sourcePath) = findStorePath $storePath;
    die unless $store;
    print STDERR "\n*** Copying `$storePath' from `$sourcePath'\n\n";
    system("$binDir/nix-store --dump $sourcePath | $binDir/nix-store --restore $destPath") == 0
        or die "cannot copy `$sourcePath' to `$storePath'";
    print "\n"; # no hash to verify
}


else { die; }