core/pkgs/build-support/fetchurl/default.nix

269 lines
6.9 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +00:00
{
lib,
buildPackages ? {
inherit stdenvNoCC;
},
stdenvNoCC,
curl, # Note that `curl' may be `null', in case of the native stdenvNoCC.
cacert ? null,
}:
2024-05-02 00:46:19 +00:00
let
mirrors = import ./mirrors.nix;
# Write the list of mirrors to a file that we can reuse between
# fetchurl instantiations, instead of passing the mirrors to
# fetchurl instantiations via environment variables. This makes the
# resulting store derivations (.drv files) much smaller, which in
# turn makes nix-env/nix-instantiate faster.
2024-06-30 08:16:52 +00:00
mirrorsFile = buildPackages.stdenvNoCC.mkDerivation (
{
2024-05-02 00:46:19 +00:00
name = "mirrors-list";
strictDeps = true;
builder = ./write-mirror-list.sh;
preferLocalBuild = true;
2024-06-30 08:16:52 +00:00
}
// mirrors
);
2024-05-02 00:46:19 +00:00
# Names of the master sites that are mirrored (i.e., "sourceforge",
# "gnu", etc.).
sites = builtins.attrNames mirrors;
2024-06-30 08:16:52 +00:00
impureEnvVars =
lib.fetchers.proxyImpureEnvVars
++ [
# This variable allows the user to pass additional options to curl
"NIX_CURL_FLAGS"
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# This variable allows the user to override hashedMirrors from the
# command-line.
"NIX_HASHED_MIRRORS"
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# This variable allows overriding the timeout for connecting to
# the hashed mirrors.
"NIX_CONNECT_TIMEOUT"
]
++ (map (site: "NIX_MIRRORS_${site}") sites);
2024-05-02 00:46:19 +00:00
in
2024-06-30 08:16:52 +00:00
{
# URL to fetch.
url ? "",
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Alternatively, a list of URLs specifying alternative download
2024-05-02 00:46:19 +00:00
# locations. They are tried in order.
2024-06-30 08:16:52 +00:00
urls ? [ ],
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Additional curl options needed for the download to succeed.
2024-05-02 00:46:19 +00:00
# Warning: Each space (no matter the escaping) will start a new argument.
# If you wish to pass arguments with spaces, use `curlOptsList`
2024-06-30 08:16:52 +00:00
curlOpts ? "",
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Additional curl options needed for the download to succeed.
curlOptsList ? [ ],
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Name of the file. If empty, use the basename of `url' (or of the
2024-05-02 00:46:19 +00:00
# first element of `urls').
2024-06-30 08:16:52 +00:00
name ? "",
2024-05-02 00:46:19 +00:00
# for versioned downloads optionally take pname + version.
2024-06-30 08:16:52 +00:00
pname ? "",
version ? "",
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# SRI hash.
hash ? "",
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Legacy ways of specifying the hash.
outputHash ? "",
outputHashAlgo ? "",
sha1 ? "",
sha256 ? "",
sha512 ? "",
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
recursiveHash ? false,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Shell code to build a netrc file for BASIC auth
netrcPhase ? null,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Impure env vars (https://nixos.org/nix/manual/#sec-advanced-attributes)
2024-05-02 00:46:19 +00:00
# needed for netrcPhase
2024-06-30 08:16:52 +00:00
netrcImpureEnvVars ? [ ],
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Shell code executed after the file has been fetched
2024-05-02 00:46:19 +00:00
# successfully. This can do things like check or transform the file.
2024-06-30 08:16:52 +00:00
postFetch ? "",
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Whether to download to a temporary path rather than $out. Useful
2024-05-02 00:46:19 +00:00
# in conjunction with postFetch. The location of the temporary file
# is communicated to postFetch via $downloadedFile.
2024-06-30 08:16:52 +00:00
downloadToTemp ? false,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# If true, set executable bit on downloaded file
executable ? false,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# If set, don't download the file, but write a list of all possible
2024-05-02 00:46:19 +00:00
# URLs (resulting from resolving mirror:// URLs) to $out.
2024-06-30 08:16:52 +00:00
showURLs ? false,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Meta information, if any.
meta ? { },
2024-05-02 00:46:19 +00:00
# Passthru information, if any.
2024-06-30 08:16:52 +00:00
passthru ? { },
2024-05-02 00:46:19 +00:00
# Doing the download on a remote machine just duplicates network
# traffic, so don't do that by default
2024-06-30 08:16:52 +00:00
preferLocalBuild ? true,
2024-05-02 00:46:19 +00:00
# Additional packages needed as part of a fetch
2024-06-30 08:16:52 +00:00
nativeBuildInputs ? [ ],
2024-05-02 00:46:19 +00:00
}:
let
urls_ =
2024-06-30 08:16:52 +00:00
if urls != [ ] && url == "" then
(if lib.isList urls then urls else throw "`urls` is not a list")
else if urls == [ ] && url != "" then
(if lib.isString url then [ url ] else throw "`url` is not a string")
else
throw "fetchurl requires either `url` or `urls` to be set";
2024-05-02 00:46:19 +00:00
hash_ =
2024-06-30 08:16:52 +00:00
if
with lib.lists;
length (
filter (s: s != "") [
hash
outputHash
sha1
sha256
sha512
]
) > 1
then
throw "multiple hashes passed to fetchurl"
else
if hash != "" then
{
outputHashAlgo = null;
outputHash = hash;
}
2024-05-02 00:46:19 +00:00
else if outputHash != "" then
2024-06-30 08:16:52 +00:00
if outputHashAlgo != "" then
{ inherit outputHashAlgo outputHash; }
else
throw "fetchurl was passed outputHash without outputHashAlgo"
else if sha512 != "" then
{
outputHashAlgo = "sha512";
outputHash = sha512;
}
else if sha256 != "" then
{
outputHashAlgo = "sha256";
outputHash = sha256;
}
else if sha1 != "" then
{
outputHashAlgo = "sha1";
outputHash = sha1;
}
else if cacert != null then
{
outputHashAlgo = "sha256";
outputHash = "";
}
else
throw "fetchurl requires a hash for fixed-output derivation: ${lib.concatStringsSep ", " urls_}";
2024-05-02 00:46:19 +00:00
in
2024-06-30 08:16:52 +00:00
assert
(lib.isList curlOpts)
-> lib.warn ''
fetchurl for ${toString (builtins.head urls_)}: curlOpts is a list (${
lib.generators.toPretty { multiline = false; } curlOpts
}), which is not supported anymore.
2024-05-02 00:46:19 +00:00
- If you wish to get the same effect as before, for elements with spaces (even if escaped) to expand to multiple curl arguments, use a string argument instead:
curlOpts = ${lib.strings.escapeNixString (toString curlOpts)};
- If you wish for each list element to be passed as a separate curl argument, allowing arguments to contain spaces, use curlOptsList instead:
curlOptsList = [ ${lib.concatMapStringsSep " " lib.strings.escapeNixString curlOpts} ];'' true;
2024-06-30 08:16:52 +00:00
stdenvNoCC.mkDerivation (
(
if (pname != "" && version != "") then
{ inherit pname version; }
else
{
name =
if showURLs then
"urls"
else if name != "" then
name
else
baseNameOf (toString (builtins.head urls_));
}
)
// {
builder = ./builder.sh;
nativeBuildInputs = [ curl ] ++ nativeBuildInputs;
urls = urls_;
# If set, prefer the content-addressable mirrors
# (http://tarballs.nixos.org) over the original URLs.
preferHashedMirrors = true;
# New-style output content requirements.
inherit (hash_) outputHashAlgo outputHash;
SSL_CERT_FILE =
if
(
hash_.outputHash == ""
|| hash_.outputHash == lib.fakeSha256
|| hash_.outputHash == lib.fakeSha512
|| hash_.outputHash == lib.fakeHash
)
then
"${cacert}/etc/ssl/certs/ca-bundle.crt"
else
"/no-cert-file.crt";
outputHashMode = if (recursiveHash || executable) then "recursive" else "flat";
inherit curlOpts;
curlOptsList = lib.escapeShellArgs curlOptsList;
inherit
showURLs
mirrorsFile
postFetch
downloadToTemp
executable
;
impureEnvVars = impureEnvVars ++ netrcImpureEnvVars;
nixpkgsVersion = lib.trivial.release;
inherit preferLocalBuild;
postHook =
if netrcPhase == null then
null
else
''
${netrcPhase}
curlOpts="$curlOpts --netrc-file $PWD/netrc"
'';
inherit meta;
passthru = {
inherit url;
} // passthru;
}
)