Commit ca050c53 authored by Yueh-Shun Li's avatar Yueh-Shun Li
Browse files

fetchurl: provide fetchurl.resolveUrl

parent 7c4673bb
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -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;
}