Skip to content

Commit

Permalink
<nix/fetchurl.nix>: Support downloading and unpacking NARs
Browse files Browse the repository at this point in the history
This removes the need to have multiple downloads in the stdenv
bootstrap process (like a separate busybox binary for Linux, or
curl/mkdir/sh/bzip2 for Darwin). Now all those files can be combined
into a single NAR.
  • Loading branch information
edolstra committed Oct 30, 2015
1 parent 5c28943 commit dae5dc7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
15 changes: 11 additions & 4 deletions corepkgs/fetchurl.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
with import <nix/config.nix>;

{system ? builtins.currentSystem, url, outputHash ? "", outputHashAlgo ? "", md5 ? "", sha1 ? "", sha256 ? "", executable ? false}:
{ system ? builtins.currentSystem
, url
, outputHash ? ""
, outputHashAlgo ? ""
, md5 ? "", sha1 ? "", sha256 ? ""
, executable ? false
, unpack ? false
, name ? baseNameOf (toString url)
}:

assert (outputHash != "" && outputHashAlgo != "")
|| md5 != "" || sha1 != "" || sha256 != "";

derivation {
name = baseNameOf (toString url);
builder = "builtin:fetchurl";

# New-style output content requirements.
outputHashAlgo = if outputHashAlgo != "" then outputHashAlgo else
if sha256 != "" then "sha256" else if sha1 != "" then "sha1" else "md5";
outputHash = if outputHash != "" then outputHash else
if sha256 != "" then sha256 else if sha1 != "" then sha1 else md5;
outputHashMode = if executable then "recursive" else "flat";
outputHashMode = if unpack || executable then "recursive" else "flat";

inherit system url executable;
inherit name system url executable unpack;

# No need to double the amount of network traffic
preferLocalBuild = true;
Expand Down
17 changes: 14 additions & 3 deletions src/libstore/builtins.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "builtins.hh"
#include "download.hh"
#include "store-api.hh"
#include "archive.hh"

namespace nix {

Expand All @@ -20,12 +22,21 @@ void builtinFetchurl(const BasicDerivation & drv)

auto out = drv.env.find("out");
if (out == drv.env.end()) throw Error("attribute ‘url’ missing");
writeFile(out->second, data.data);

Path storePath = out->second;
assertStorePath(storePath);

auto unpack = drv.env.find("unpack");
if (unpack != drv.env.end() && unpack->second == "1") {
StringSource source(data.data);
restorePath(storePath, source);
} else
writeFile(storePath, data.data);

auto executable = drv.env.find("executable");
if (executable != drv.env.end() && executable->second == "1") {
if (chmod(out->second.c_str(), 0755) == -1)
throw SysError(format("making ‘%1%’ executable") % out->second);
if (chmod(storePath.c_str(), 0755) == -1)
throw SysError(format("making ‘%1%’ executable") % storePath);
}
}

Expand Down
6 changes: 0 additions & 6 deletions tests/fetchurl.nix

This file was deleted.

24 changes: 21 additions & 3 deletions tests/fetchurl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ source common.sh

clearStore

hash=$(nix-hash --flat --type sha256 ./fetchurl.nix)
hash=$(nix-hash --flat --type sha256 ./fetchurl.sh)

outPath=$(nix-build ./fetchurl.nix --argstr filename $(pwd)/fetchurl.nix --argstr sha256 $hash --no-out-link)
outPath=$(nix-build '<nix/fetchurl.nix>' --argstr url file://$(pwd)/fetchurl.sh --argstr sha256 $hash --no-out-link)

cmp $outPath fetchurl.nix
cmp $outPath fetchurl.sh

rm -rf $TEST_ROOT/archive
mkdir -p $TEST_ROOT/archive
cp ./fetchurl.sh $TEST_ROOT/archive
chmod +x $TEST_ROOT/archive/fetchurl.sh
ln -s foo $TEST_ROOT/archive/symlink
nar=$TEST_ROOT/archive.nar
nix-store --dump $TEST_ROOT/archive > $nar

hash=$(nix-hash --flat --type sha256 $nar)

outPath=$(nix-build '<nix/fetchurl.nix>' --argstr url file://$nar --argstr sha256 $hash \
--arg unpack true --argstr name xyzzy --no-out-link)

echo $outPath | grep -q 'xyzzy'

test -x $outPath/fetchurl.sh
test -L $outPath/symlink

0 comments on commit dae5dc7

Please sign in to comment.