* A script `nix-prefetch-url' to fetch a URL, place it in the Nix

store, and print its hash.
This commit is contained in:
Eelco Dolstra 2003-08-15 10:13:41 +00:00
parent 01e30360d4
commit e374dbf89b
4 changed files with 64 additions and 5 deletions

View file

@ -1,7 +1,16 @@
#! /bin/sh
export PATH=/bin:/usr/bin
echo "downloading $url into $out..."
@wget@ "$url" -O "$out" || exit 1
prefetch=@prefix@/store/nix-prefetch-url-$md5
if test -f "$prefetch"; then
echo "using prefetched $prefetch";
mv $prefetch $out || exit 1
else
@wget@ "$url" -O "$out" || exit 1
fi
actual=$(@bindir@/nix-hash --flat $out)
if test "$actual" != "$md5"; then

View file

@ -1,5 +1,5 @@
bin_SCRIPTS = nix-switch nix-collect-garbage \
nix-pull nix-push
nix-pull nix-push nix-prefetch-url
noinst_SCRIPTS = nix-profile.sh
@ -14,5 +14,6 @@ include ../substitute.mk
EXTRA_DIST = nix-switch.in nix-collect-garbage.in \
nix-pull.in nix-push.in nix-profile.sh.in \
nix-prefetch-url.in \
prebuilts.conf

View file

@ -0,0 +1,48 @@
#! /usr/bin/perl -w
use strict;
use IPC::Open2;
my $url = shift @ARGV;
defined $url or die;
print "fetching $url...\n";
my $out = "@prefix@/store/nix-prefetch-url-$$";
system "@wget@ '$url' -O '$out'";
$? == 0 or die "unable to fetch $url";
my $hash=`@bindir@/nix-hash --flat $out`;
$? == 0 or die "unable to hash $out";
chomp $hash;
print "file has hash $hash\n";
my $out2 = "@prefix@/store/nix-prefetch-url-$hash";
rename $out, $out2;
# Create a Fix expression.
my $fixexpr =
"App(IncludeFix(\"fetchurl/fetchurl.fix\"), " .
"[(\"url\", \"$url\"), (\"md5\", \"$hash\")])";
# Instantiate a Nix expression.
print STDERR "running fix...\n";
my $pid = open2(\*READ, \*WRITE, "fix -") or die "cannot run fix";
print WRITE $fixexpr;
close WRITE;
my $id = <READ>;
chomp $id;
waitpid $pid, 0;
$? == 0 or die "fix failed";
# Run Nix.
print STDERR "running nix...\n";
system "nix --install $id > /dev/null";
$? == 0 or die "`nix --install' failed";
unlink $out2;

View file

@ -1,5 +1,6 @@
#! /usr/bin/perl -w
use strict;
use IPC::Open2;
my $tmpfile = "@localstatedir@/nix/pull.tmp";
@ -85,7 +86,7 @@ $fullexpr .= "]";
# Instantiate Nix expressions from the Fix expressions we created above.
print STDERR "running fix...\n";
$pid = open2(\*READ, \*WRITE, "fix -") or die "cannot run fix";
my $pid = open2(\*READ, \*WRITE, "fix -") or die "cannot run fix";
print WRITE $fullexpr;
close WRITE;
@ -93,9 +94,9 @@ my $i = 0;
while (<READ>) {
chomp;
die unless /^([0-9a-z]{32})$/;
$nid = $1;
my $nid = $1;
die unless ($i < scalar @ids);
$id = $ids[$i++];
my $id = $ids[$i++];
push @subs, $id;
push @subs, $nid;
}