2003-12-05 13:25:38 +02:00
|
|
|
#! /usr/bin/perl -w -I@libexecdir@/nix
|
2003-07-10 16:41:28 +03:00
|
|
|
|
2003-08-15 13:13:41 +03:00
|
|
|
use strict;
|
2003-08-15 12:39:33 +03:00
|
|
|
use IPC::Open2;
|
2003-10-16 19:29:57 +03:00
|
|
|
use POSIX qw(tmpnam);
|
2003-12-05 13:25:38 +02:00
|
|
|
use readmanifest;
|
2003-08-15 12:39:33 +03:00
|
|
|
|
2003-10-16 19:29:57 +03:00
|
|
|
my $tmpdir;
|
|
|
|
do { $tmpdir = tmpnam(); }
|
|
|
|
until mkdir $tmpdir, 0777;
|
|
|
|
|
|
|
|
my $manifest = "$tmpdir/manifest";
|
2003-07-13 21:58:03 +03:00
|
|
|
my $conffile = "@sysconfdir@/nix/prebuilts.conf";
|
2003-07-10 18:11:48 +03:00
|
|
|
|
2003-10-16 19:29:57 +03:00
|
|
|
#END { unlink $manifest; rmdir $tmpdir; }
|
|
|
|
|
2003-07-10 18:24:50 +03:00
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
# Obtain URLs either from the command line or from a configuration file.
|
|
|
|
my %storepaths2urls;
|
|
|
|
my %urls2hashes;
|
|
|
|
my %successors;
|
|
|
|
sub doURL {
|
2003-11-24 13:11:40 +02:00
|
|
|
my $url = shift;
|
2003-12-05 13:25:38 +02:00
|
|
|
processURL $manifest, $url, \%storepaths2urls, \%urls2hashes, \%successors;
|
2003-11-24 13:11:40 +02:00
|
|
|
}
|
|
|
|
if (scalar @ARGV > 0) {
|
|
|
|
while (@ARGV) {
|
|
|
|
my $url = shift @ARGV;
|
2003-12-05 13:25:38 +02:00
|
|
|
doURL $url;
|
2003-11-24 13:11:40 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
open CONFFILE, "<$conffile";
|
|
|
|
while (<CONFFILE>) {
|
|
|
|
chomp;
|
|
|
|
if (/^\s*(\S+)\s*(\#.*)?$/) {
|
|
|
|
my $url = $1;
|
2003-12-05 13:25:38 +02:00
|
|
|
doURL $url;
|
2003-11-24 13:11:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close CONFFILE;
|
2003-07-10 18:11:48 +03:00
|
|
|
}
|
2003-07-10 18:24:50 +03:00
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
|
|
|
|
# Create a Nix expression for the substitutes.
|
|
|
|
my $fullexpr = "[";
|
|
|
|
|
|
|
|
my @storepaths;
|
|
|
|
foreach my $storepath (keys %storepaths2urls) {
|
|
|
|
# Construct a Nix expression that fetches and unpacks a
|
|
|
|
# Nix archive from the network.
|
|
|
|
my $url = $storepaths2urls{$storepath};
|
|
|
|
my $hash = $urls2hashes{$url};
|
|
|
|
my $fetch =
|
|
|
|
"(import @datadir@/nix/corepkgs/fetchurl) " .
|
2003-12-21 23:56:54 +02:00
|
|
|
"{url = $url; md5 = \"$hash\"; system = \"@system@\";}";
|
2003-12-05 13:25:38 +02:00
|
|
|
my $nixexpr =
|
|
|
|
"((import @datadir@/nix/corepkgs/nar/unnar.nix) " .
|
2003-12-21 23:56:54 +02:00
|
|
|
"{narFile = ($fetch); outPath = \"$storepath\"; system = \"@system@\";}) ";
|
2003-12-05 13:25:38 +02:00
|
|
|
$fullexpr .= $nixexpr; # !!! O(n^2)?
|
|
|
|
push @storepaths, $storepath;
|
|
|
|
}
|
|
|
|
|
2003-08-15 12:39:33 +03:00
|
|
|
$fullexpr .= "]";
|
2003-08-05 14:14:24 +03:00
|
|
|
|
2003-10-16 19:29:57 +03:00
|
|
|
|
2003-11-24 11:25:08 +02:00
|
|
|
# Instantiate store expressions from the Nix expressions we created above.
|
2004-01-14 16:20:33 +02:00
|
|
|
print STDERR "instantiating store expressions...\n";
|
2003-11-24 11:25:08 +02:00
|
|
|
my $pid = open2(\*READ, \*WRITE, "nix-instantiate -") or die "cannot run nix-instantiate";
|
2003-08-15 12:39:33 +03:00
|
|
|
|
|
|
|
print WRITE $fullexpr;
|
|
|
|
close WRITE;
|
2003-08-05 14:14:24 +03:00
|
|
|
my $i = 0;
|
2003-12-05 13:25:38 +02:00
|
|
|
my %substitutes;
|
2003-08-15 12:39:33 +03:00
|
|
|
while (<READ>) {
|
2003-08-05 14:14:24 +03:00
|
|
|
chomp;
|
2003-10-16 19:29:57 +03:00
|
|
|
die unless /^\//;
|
|
|
|
my $subpath = $_;
|
2003-12-05 13:25:38 +02:00
|
|
|
die unless ($i < scalar @storepaths);
|
|
|
|
$substitutes{$storepaths[$i++]} = $subpath;
|
2003-08-05 14:14:24 +03:00
|
|
|
}
|
|
|
|
|
2003-08-15 12:39:33 +03:00
|
|
|
waitpid $pid, 0;
|
2003-11-24 11:25:08 +02:00
|
|
|
$? == 0 or die "nix-instantiate failed";
|
2003-08-15 12:39:33 +03:00
|
|
|
|
2003-10-16 19:29:57 +03:00
|
|
|
|
2003-08-05 14:14:24 +03:00
|
|
|
# Register all substitutes.
|
|
|
|
print STDERR "registering substitutes...\n";
|
2003-12-05 13:25:38 +02:00
|
|
|
my @subs = %substitutes;
|
2003-12-04 16:38:31 +02:00
|
|
|
while (scalar @subs > 0) {
|
|
|
|
my $n = scalar @subs;
|
|
|
|
if ($n > 256) { $n = 256 };
|
|
|
|
my @subs2 = @subs[0..$n - 1];
|
|
|
|
@subs = @subs[$n..scalar @subs - 1];
|
|
|
|
system "nix-store --substitute @subs2";
|
|
|
|
if ($?) { die "`nix-store --substitute' failed"; }
|
|
|
|
}
|
2003-07-10 23:13:32 +03:00
|
|
|
|
2003-10-16 19:29:57 +03:00
|
|
|
|
2003-08-05 14:14:24 +03:00
|
|
|
# Register all successors.
|
|
|
|
print STDERR "registering successors...\n";
|
2003-12-05 13:25:38 +02:00
|
|
|
my @sucs = %successors;
|
2003-12-04 16:38:31 +02:00
|
|
|
while (scalar @sucs > 0) {
|
|
|
|
my $n = scalar @sucs;
|
|
|
|
if ($n > 256) { $n = 256 };
|
|
|
|
my @sucs2 = @sucs[0..$n - 1];
|
|
|
|
@sucs = @sucs[$n..scalar @sucs - 1];
|
2003-12-21 23:56:54 +02:00
|
|
|
system "nix-store --successor @sucs2";
|
2003-12-04 16:38:31 +02:00
|
|
|
if ($?) { die "`nix-store --successor' failed"; }
|
|
|
|
}
|