From cff98124230b79ed6c3cb32d720c8900f7a2785b Mon Sep 17 00:00:00 2001 From: Ruby Iris Juric Date: Wed, 1 Oct 2025 10:00:59 +1000 Subject: [PATCH] feat: add lib.fetchurl function --- tidepool/src/lib/default.nix | 1 + tidepool/src/lib/fetchurl.nix | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tidepool/src/lib/fetchurl.nix diff --git a/tidepool/src/lib/default.nix b/tidepool/src/lib/default.nix index bf0af85..b0a79ec 100644 --- a/tidepool/src/lib/default.nix +++ b/tidepool/src/lib/default.nix @@ -1,5 +1,6 @@ { includes = [ + ./fetchurl.nix ./options.nix ./packages.nix ./platforms.nix diff --git a/tidepool/src/lib/fetchurl.nix b/tidepool/src/lib/fetchurl.nix new file mode 100644 index 0000000..43b4381 --- /dev/null +++ b/tidepool/src/lib/fetchurl.nix @@ -0,0 +1,73 @@ +{ + config = { + # This function is a vendored version of . 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 ]; + }; + }; +}