From ca050c53b2016a41d2a3107799615020b2094f7d Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Thu, 18 Dec 2025 08:03:43 +0800 Subject: [PATCH] fetchurl: provide fetchurl.resolveUrl --- pkgs/build-support/fetchurl/default.nix | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pkgs/build-support/fetchurl/default.nix b/pkgs/build-support/fetchurl/default.nix index 3e16efd619a2..95fac7219d52 100644 --- a/pkgs/build-support/fetchurl/default.nix +++ b/pkgs/build-support/fetchurl/default.nix @@ -35,6 +35,18 @@ let # "gnu", etc.). sites = builtins.attrNames mirrors; + resolveUrl = + url: + let + mirrorSplit = lib.match "mirror://([[:alpha:]]+)/(.+)" url; + mirrorName = lib.head mirrorSplit; + mirrorList = mirrors."${mirrorName}" or (throw "unknown mirror:// site ${mirrorName}"); + in + if mirrorSplit == null || mirrorName == null then + [ url ] + else + map (mirror: mirror + lib.elemAt mirrorSplit 1) mirrorList; + impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ @@ -223,16 +235,7 @@ lib.extendMkDerivation { finalHashHasColon = lib.hasInfix ":" finalAttrs.hash; finalHashColonMatch = lib.match "([^:]+)[:](.*)" finalAttrs.hash; - resolvedUrl = - let - mirrorSplit = lib.match "mirror://([[:alpha:]]+)/(.+)" url; - mirrorName = lib.head mirrorSplit; - mirrorList = mirrors."${mirrorName}" or (throw "unknown mirror:// site ${mirrorName}"); - in - if mirrorSplit == null || mirrorName == null then - url - else - "${lib.head mirrorList}${lib.elemAt mirrorSplit 1}"; + resolvedUrl = lib.head (resolveUrl url); in derivationArgs @@ -339,3 +342,6 @@ lib.extendMkDerivation { # No ellipsis inheritFunctionArgs = false; } +// { + inherit resolveUrl; +}