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

205 lines
6.3 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
{ lib, buildPackages ? { inherit stdenvNoCC; }, stdenvNoCC
, curl # Note that `curl' may be `null', in case of the native stdenvNoCC.
, cacert ? null }:
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-05-13 21:24:10 +00:00
mirrorsFile = buildPackages.stdenvNoCC.mkDerivation ({
name = "mirrors-list";
strictDeps = true;
builder = ./write-mirror-list.sh;
preferLocalBuild = true;
} // 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;
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
# This variable allows the user to pass additional options to curl
"NIX_CURL_FLAGS"
# This variable allows the user to override hashedMirrors from the
# command-line.
"NIX_HASHED_MIRRORS"
# This variable allows overriding the timeout for connecting to
# the hashed mirrors.
"NIX_CONNECT_TIMEOUT"
] ++ (map (site: "NIX_MIRRORS_${site}") sites);
2024-05-13 21:24:10 +00:00
in { # URL to fetch.
url ? ""
2024-05-02 00:46:19 +00:00
, # Alternatively, a list of URLs specifying alternative download
2024-05-13 21:24:10 +00:00
# locations. They are tried in order.
urls ? [ ]
2024-05-02 00:46:19 +00:00
, # Additional curl options needed for the download to succeed.
2024-05-13 21:24:10 +00:00
# Warning: Each space (no matter the escaping) will start a new argument.
# If you wish to pass arguments with spaces, use `curlOptsList`
curlOpts ? ""
2024-05-02 00:46:19 +00:00
, # Additional curl options needed for the download to succeed.
2024-05-13 21:24:10 +00:00
curlOptsList ? [ ]
2024-05-02 00:46:19 +00:00
, # Name of the file. If empty, use the basename of `url' (or of the
2024-05-13 21:24:10 +00:00
# first element of `urls').
name ? ""
2024-05-02 00:46:19 +00:00
# for versioned downloads optionally take pname + version.
2024-05-13 21:24:10 +00:00
, pname ? "", version ? ""
2024-05-02 00:46:19 +00:00
, # SRI hash.
2024-05-13 21:24:10 +00:00
hash ? ""
2024-05-02 00:46:19 +00:00
, # Legacy ways of specifying the hash.
2024-05-13 21:24:10 +00:00
outputHash ? "", outputHashAlgo ? "", sha1 ? "", sha256 ? "", sha512 ? ""
2024-05-02 00:46:19 +00:00
, recursiveHash ? false
, # Shell code to build a netrc file for BASIC auth
2024-05-13 21:24:10 +00:00
netrcPhase ? null
2024-05-02 00:46:19 +00:00
, # Impure env vars (https://nixos.org/nix/manual/#sec-advanced-attributes)
2024-05-13 21:24:10 +00:00
# needed for netrcPhase
netrcImpureEnvVars ? [ ]
2024-05-02 00:46:19 +00:00
, # Shell code executed after the file has been fetched
2024-05-13 21:24:10 +00:00
# successfully. This can do things like check or transform the file.
postFetch ? ""
2024-05-02 00:46:19 +00:00
, # Whether to download to a temporary path rather than $out. Useful
2024-05-13 21:24:10 +00:00
# in conjunction with postFetch. The location of the temporary file
# is communicated to postFetch via $downloadedFile.
downloadToTemp ? false
2024-05-02 00:46:19 +00:00
, # If true, set executable bit on downloaded file
2024-05-13 21:24:10 +00:00
executable ? false
2024-05-02 00:46:19 +00:00
, # If set, don't download the file, but write a list of all possible
2024-05-13 21:24:10 +00:00
# URLs (resulting from resolving mirror:// URLs) to $out.
showURLs ? false
2024-05-02 00:46:19 +00:00
, # Meta information, if any.
2024-05-13 21:24:10 +00:00
meta ? { }
2024-05-02 00:46:19 +00:00
# Passthru information, if any.
2024-05-13 21:24:10 +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
, preferLocalBuild ? true
# Additional packages needed as part of a fetch
2024-05-13 21:24:10 +00:00
, nativeBuildInputs ? [ ] }:
2024-05-02 00:46:19 +00:00
let
2024-05-13 21:24:10 +00:00
urls_ = 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";
hash_ = if with lib.lists;
length (filter (s: s != "") [ hash outputHash sha1 sha256 sha512 ]) > 1 then
throw "multiple hashes passed to fetchurl"
2024-05-02 00:46:19 +00:00
else
2024-05-13 21:24:10 +00:00
if hash != "" then {
outputHashAlgo = null;
outputHash = hash;
} else if outputHash != "" then
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_
}";
in 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.
- 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;
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_));
}) // {
2024-05-02 00:46:19 +00:00
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;
2024-05-13 21:24:10 +00:00
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";
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
outputHashMode =
if (recursiveHash || executable) then "recursive" else "flat";
2024-05-02 00:46:19 +00:00
inherit curlOpts;
curlOptsList = lib.escapeShellArgs curlOptsList;
inherit showURLs mirrorsFile postFetch downloadToTemp executable;
impureEnvVars = impureEnvVars ++ netrcImpureEnvVars;
nixpkgsVersion = lib.trivial.release;
inherit preferLocalBuild;
2024-05-13 21:24:10 +00:00
postHook = if netrcPhase == null then
null
else ''
2024-05-02 00:46:19 +00:00
${netrcPhase}
curlOpts="$curlOpts --netrc-file $PWD/netrc"
'';
inherit meta;
passthru = { inherit url; } // passthru;
})