From e374dbf89b0ba9a4f5835ef9ac30eda6df1dce6a Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 15 Aug 2003 10:13:41 +0000 Subject: [PATCH] * A script `nix-prefetch-url' to fetch a URL, place it in the Nix store, and print its hash. --- corepkgs/fetchurl/fetchurl.sh.in | 11 +++++++- scripts/Makefile.am | 3 +- scripts/nix-prefetch-url.in | 48 ++++++++++++++++++++++++++++++++ scripts/nix-pull.in | 7 +++-- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 scripts/nix-prefetch-url.in diff --git a/corepkgs/fetchurl/fetchurl.sh.in b/corepkgs/fetchurl/fetchurl.sh.in index 7e876a25e2..88e4d81f2e 100644 --- a/corepkgs/fetchurl/fetchurl.sh.in +++ b/corepkgs/fetchurl/fetchurl.sh.in @@ -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 diff --git a/scripts/Makefile.am b/scripts/Makefile.am index d1ab6e4cdc..4a1be7f8f0 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -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 diff --git a/scripts/nix-prefetch-url.in b/scripts/nix-prefetch-url.in new file mode 100644 index 0000000000..f5539eb98d --- /dev/null +++ b/scripts/nix-prefetch-url.in @@ -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 = ; +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; diff --git a/scripts/nix-pull.in b/scripts/nix-pull.in index d7b0523d6c..a3d23ea167 100644 --- a/scripts/nix-pull.in +++ b/scripts/nix-pull.in @@ -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 () { 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; }