lix/scripts/nix-pull.in

110 lines
2.7 KiB
Text
Raw Normal View History

2004-03-15 16:23:53 +01:00
#! @perl@ -w -I@libexecdir@/nix
use strict;
2003-08-15 11:39:33 +02:00
use IPC::Open2;
use POSIX qw(tmpnam);
use readmanifest;
2003-08-15 11:39:33 +02: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 17:24:50 +02: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 17:24:50 +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 22:56:54 +01:00
"{url = $url; md5 = \"$hash\"; system = \"@system@\";}";
my $nixexpr =
"((import @datadir@/nix/corepkgs/nar/unnar.nix) " .
2003-12-21 22:56:54 +01:00
"{narFile = ($fetch); outPath = \"$storepath\"; system = \"@system@\";}) ";
$fullexpr .= $nixexpr; # !!! O(n^2)?
push @storepaths, $storepath;
}
2003-08-15 11:39:33 +02:00
$fullexpr .= "]";
2003-11-24 10:25:08 +01: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 11:39:33 +02:00
print WRITE $fullexpr;
close WRITE;
my $i = 0;
my %substitutes;
2003-08-15 11:39:33 +02:00
while (<READ>) {
chomp;
die unless /^\//;
my $subpath = $_;
die unless ($i < scalar @storepaths);
$substitutes{$storepaths[$i++]} = $subpath;
}
2003-08-15 11:39:33 +02:00
waitpid $pid, 0;
2003-11-24 10:25:08 +01:00
$? == 0 or die "nix-instantiate failed";
2003-08-15 11:39:33 +02: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 22:13:32 +02: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"; }
}