feat: add lib.fetchurl function

This commit is contained in:
Ruby Iris Juric 2025-10-01 10:00:59 +10:00
parent ab85ed8550
commit cff9812423
Signed by: srxl
SSH key fingerprint: SHA256:zgspAKSFKA6vB30vPBY6QSa/osCDgrr8YASW+mNz13M
2 changed files with 74 additions and 0 deletions

View file

@ -1,5 +1,6 @@
{
includes = [
./fetchurl.nix
./options.nix
./packages.nix
./platforms.nix

View file

@ -0,0 +1,73 @@
{
config = {
# This function is a vendored version of <nix/fetchurl.nix>. It uses the (largely undocumented) builtin:fetchurl
# builder for derivations to give us a way to download files (eg. source tarballs) in the early bootstrap phase,
# before we have access to tools like curl. It functions almost identically to the builtins.fetchurl function,
# except it downloads files during the derivation's build, instead of during Nix evaluation, preventing evaluation
# from being blocked by downloads.
#
# Most packages should avoid using this function, and instead prefer using builders to download sources, such as
# (TODO: create these).
#
# Original source: https://git.lix.systems/lix-project/lix/src/commit/6599be1a9f76f0fba2a6905ea85235aeb7b7eae9/lix/libexpr/fetchurl.nix
lib.fetchurl =
{
# URL of file to download
url,
# SRI hash of downloaded file
hash ? "",
# Legacy, base32-encoded hash specifications
md5 ? "",
sha1 ? "",
sha256 ? "",
sha512 ? "",
outputHash ?
if hash != "" then
hash
else if sha512 != "" then
sha512
else if sha1 != "" then
sha1
else if md5 != "" then
md5
else
sha256,
outputHashAlgo ?
if hash != "" then
""
else if sha512 != "" then
"sha512"
else if sha1 != "" then
"sha1"
else if md5 != "" then
"md5"
else
"sha256",
name ? baseNameOf url,
# Make the downloaded file executable (ie. chmod 755)
executable ? false,
# If the downloaded file is an archive, extract it before adding to the store
# NOTE: Only appears to unpack .xz archives?
unpack ? false,
}:
builtins.derivation {
inherit
name
url
executable
unpack
;
builder = "builtin:fetchurl";
system = "builtin";
preferLocalBuild = true;
inherit outputHashAlgo outputHash;
outputHashMode = if unpack || executable then "recursive" else "flat";
urls = [ url ];
};
};
}