about summary refs log tree commit diff
path: root/scripts/nix-prefetch-url.in
blob: f5539eb98dcae92d1a7b3d165bf65af1d9e03cef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;