about summary refs log tree commit diff
path: root/scripts/nix-push.in
blob: c0aba5322b8f134f5b9409d49eecabf623f90647 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/perl -w

use strict;
use POSIX qw(tmpnam);

my $tmpdir;
do { $tmpdir = tmpnam(); }
until mkdir $tmpdir, 0777;

my $fixfile = "$tmpdir/create-nars.fix";
my $manifest = "$tmpdir/MANIFEST";

END { unlink $manifest; unlink $fixfile; rmdir $tmpdir; }

open FIX, ">$fixfile";
print FIX "[";
my $first = 1;

my @paths;

foreach my $id (@ARGV) {

    die unless $id =~ /^\//;

    # Get all paths referenced by the normalisation of the given 
    # Nix expression.
    system "nix --install $id > /dev/null";
    if ($?) { die "`nix --install' failed"; }

    open PATHS, "nix --query --requisites --include-successors $id 2> /dev/null |" or die "nix -qr";
    while (<PATHS>) {
        chomp;
        die "bad: $_" unless /^\//;
        push @paths, $_;
    }
    close PATHS;

    # For each path, create a Fix expression that turns the path into
    # a Nix archive.
    foreach my $path (@paths) {

	die unless ($path =~ /\/[0-9a-z]{32}.*$/);
	print "$path\n";

        # Construct a Fix expression that creates a Nix archive.
        my $fixexpr = 
          "Call(IncludeFix(\"nar/nar.fix\"), " .
          "[ (\"path\", Closure([\"$path\"], [(\"$path\", [])]))" .
          "])";
	
	print FIX "," unless ($first);
	$first = 0;
        print FIX $fixexpr;

    }
}

print FIX "]";
close FIX;


# Instantiate a Nix expression from the Fix expression.
my @nids;
print STDERR "running fix...\n";
open NIDS, "fix $fixfile |" or die "cannot run fix";
while (<NIDS>) {
    chomp;
    die unless /^\//;
    push @nids, $_;
}
close NIDS;


# Realise the Nix expression.
print STDERR "creating archives...\n";
system "nix --install -v @nids > /dev/null";
if ($?) { die "`nix --install' failed"; }

my @narpaths;
open NIDS, "nix --query --list @nids |" or die "cannot run nix";
while (<NIDS>) {
    chomp;
    die unless (/^\//);
    push @narpaths, "$_";
}
close NIDS;

    
# Create the manifest.
print STDERR "creating manifest...\n";

open MANIFEST, ">$manifest";

my @pushlist;
push @pushlist, $manifest;

for (my $n = 0; $n < scalar @paths; $n++) {
    my $storepath = $paths[$n];
    my $nardir = $narpaths[$n];
    
    $storepath =~ /\/([^\/]*)$/;
    my $basename = $1;
    defined $basename or die;

    my $narname = "$basename.nar.bz2";

    my $narfile = "$nardir/$narname";
    (-f $narfile) or die "narfile for $storepath not found";
    push @pushlist, $narfile;

    open MD5, "$nardir/md5" or die "cannot open hash";
    my $hash = <MD5>;
    chomp $hash;
    $hash =~ /^[0-9a-z]{32}$/ or die "invalid hash";
    close MD5;

    print MANIFEST "{\n";
    print MANIFEST "  StorePath: $storepath\n";
    print MANIFEST "  NarPath: $basename\n";
    print MANIFEST "  MD5: $hash\n";

    if ($storepath =~ /\.nix$/) {
	open PREDS, "nix --query --predecessors $storepath |" or die "cannot run nix";
	while (<PREDS>) {
	    chomp;
	    die unless (/^\//);
	    print MANIFEST "  SuccOf: $_\n";
	}
	close PREDS;
    }

    print MANIFEST "}\n";
}

close MANIFEST;


# Push the prebuilts to the server. !!! FIXME
print STDERR "pushing to server...\n";
if (scalar @pushlist > 0) {
    system "rsync -av -e ssh @pushlist eelco\@losser.st-lab.cs.uu.nl:/home/eelco/public_html/nix-dist/";
}