labs/tidepool/src/lib/fetchurl.nix
Ruby Iris Juric 0ae450a116 feat: add lib.fetchurl function (#27)
See comment in `tidepool/src/lib/fetchurl.nix` for details on rationale.

I've made this a function in `lib` instead of defining this as a builder, since we don't have a great mechanism at the moment for specifying the source of a package as another package. We'll need to tackle this eventually, but given that this is a bit of a special case in that it's only intended to be used to fetch sources during bootstrap, I think it's fine to just have this be a function that returns a derivation, instead of being a full builder. Open to other opinions, though.

Reviewed-on: #27
Reviewed-by: vlinkz <vlinkz@snowflakeos.org>
Co-authored-by: Ruby Iris Juric <ruby@srxl.me>
Co-committed-by: Ruby Iris Juric <ruby@srxl.me>
2025-10-01 05:20:06 +00:00

73 lines
2.2 KiB
Nix

{
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 ];
};
};
}