nix-super/scripts/nix-pull.in

110 lines
2.7 KiB
Text
Raw Normal View History

2004-03-15 17:23:53 +02:00
#! @perl@ -w -I@libexecdir@/nix
use strict;
2003-08-15 12:39:33 +03:00
use IPC::Open2;
use POSIX qw(tmpnam);
use readmanifest;
2003-08-15 12:39:33 +03:00
my $tmpdir;
do { $tmpdir = tmpnam(); }
until mkdir $tmpdir, 0777;
my $manifest = "$tmpdir/manifest";
my $conffile = "@sysconfdir@/nix/prebuilts.conf";
#END { unlink $manifest; rmdir $tmpdir; }
2003-07-10 18:24:50 +03:00
# Obtain URLs either from the command line or from a configuration file.
my %storepaths2urls;
my %urls2hashes;
my %successors;
sub doURL {
my $url = shift;
processURL $manifest, $url, \%storepaths2urls, \%urls2hashes, \%successors;
}
if (scalar @ARGV > 0) {
while (@ARGV) {
my $url = shift @ARGV;
doURL $url;
}
} else {
open CONFFILE, "<$conffile";
while (<CONFFILE>) {
chomp;
if (/^\s*(\S+)\s*(\#.*)?$/) {
my $url = $1;
doURL $url;
}
}
close CONFFILE;
}
2003-07-10 18:24:50 +03: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@\";}";
my $nixexpr =
"((import @datadir@/nix/corepkgs/nar/unnar.nix) " .
2003-12-21 23:56:54 +02:00
"{narFile = ($fetch); outPath = \"$storepath\"; system = \"@system@\";}) ";
$fullexpr .= $nixexpr; # !!! O(n^2)?
push @storepaths, $storepath;
}
2003-08-15 12:39:33 +03:00
$fullexpr .= "]";
2003-11-24 11:25:08 +02:00
# Instantiate store expressions from the Nix expressions we created above.
print STDERR "instantiating store expressions...\n";
my $pid = open2(\*READ, \*WRITE, "@bindir@/nix-instantiate -") or die "cannot run nix-instantiate";
2003-08-15 12:39:33 +03:00
print WRITE $fullexpr;
close WRITE;
my $i = 0;
my %substitutes;
2003-08-15 12:39:33 +03:00
while (<READ>) {
chomp;
die unless /^\//;
my $subpath = $_;
die unless ($i < scalar @storepaths);
$substitutes{$storepaths[$i++]} = $subpath;
}
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
# Register all substitutes.
print STDERR "registering substitutes...\n";
my @subs = %substitutes;
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 "@bindir@/nix-store --substitute @subs2";
if ($?) { die "`nix-store --substitute' failed"; }
}
2003-07-10 23:13:32 +03:00
# Register all successors.
print STDERR "registering successors...\n";
my @sucs = %successors;
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];
system "@bindir@/nix-store --successor @sucs2";
if ($?) { die "`nix-store --successor' failed"; }
}