about summary refs log tree commit diff
path: root/scripts/nix-pack-closure.in
blob: 97de927bfcd05bae4a518be9a807a7b2731c4258 (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
#! @perl@ -w

# This tool computes the closure of a path (using "nix-store --query
# --requisites") and puts the contents of each path in the closure in
# a big NAR archive that can be installed on another Nix installation
# using "nix-unpack-closure".

# TODO: make this program "streamy", i.e., don't use a temporary
# directory.

use strict;
use POSIX qw(tmpnam);

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

my $tmpDir;
do { $tmpDir = tmpnam(); }
until mkdir $tmpDir, 0777;
END { system "@coreutils@/rm -rf '$tmpDir'"; }
mkdir "$tmpDir/contents", 0777 or die;
mkdir "$tmpDir/references", 0777 or die;
mkdir "$tmpDir/derivers", 0777 or die;



my %storePaths;


while (@ARGV) {
    my $storePath = shift @ARGV;

    # Get the closure of this path.
    my $pid = open(READ,
        "$binDir/nix-store --query --requisites '$storePath'|") or die;
    
    while (<READ>) {
        chomp;
        die "bad: $_" unless /^\//;
        $storePaths{$_} = "";
    }

    close READ or die "nix-store failed: $?";
}


foreach my $storePath (sort(keys %storePaths)) {
    print STDERR "packing `$storePath'...\n";

    $storePath =~ /\/([^\/]+)$/;
    my $name = $1;

    system("$binDir/nix-store --dump '$storePath' > $tmpDir/contents/$name") == 0
        or die "nix-store --dump failed on `$storePath': $?";

    system("$binDir/nix-store --query --references '$storePath' > $tmpDir/references/$name") == 0
        or die "nix-store --query --references failed on `$storePath': $?";

    system("$binDir/nix-store --query --deriver '$storePath' > $tmpDir/derivers/$name") == 0
        or die "nix-store --query --deriver failed on `$storePath': $?";
}


# Write a NAR archive of everything to standard output.
system("nix-store --dump '$tmpDir'") == 0
    or die "nix-store --dump failed";