about summary refs log blame commit diff
path: root/scripts/nix-prefetch-url.in
blob: 71ba3caab94744dd29d9a491196a3b83f115dc7a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                               
                                               










                                                   


                                                 
                                                            

                         

                               

                                                                                          
 
                     

            

                     

                
                                        

          


                                                        

             
#! /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@ --passive-ftp '$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 Nix expression.
my $nixexpr =
    "(import @datadir@/nix/corepkgs/fetchurl) " .
    "{url = $url; md5 = \"$hash\"; system = \"@system@\";}";

print "expr: $nixexpr\n";

# Instantiate a Nix expression.
print STDERR "instantiating Nix expression...\n";
my $pid = open2(\*READ, \*WRITE, "nix-instantiate -") or die "cannot run nix-instantiate";

print WRITE $nixexpr;
close WRITE;

my $drvpath = <READ>;
chomp $drvpath;

waitpid $pid, 0;
$? == 0 or die "nix-instantiate failed";

# Run Nix.
print STDERR "realising store expression $drvpath...\n";
system "nix-store --realise $drvpath > /dev/null";
$? == 0 or die "realisation failed";

unlink $out2;