Unverified Commit 9ca21a51 authored by Michael Daniels's avatar Michael Daniels Committed by GitHub
Browse files

nix-prefetch: fix compatibility with extendMkDerivation-based fetchers (#470230)

parents 439c83c4 030e2797
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
--- a/lib/overlay.nix	2025-12-12 14:17:10.585262065 -0500
+++ b/lib/overlay.nix	2025-12-12 14:17:31.567341843 -0500
@@ -35,9 +35,17 @@
     ];
   };
 
-  curlFetcher = fetcher: setFunctionArgs (args: fetcher (args // {
-    curlOpts = (args.curlOpts or "") + " --no-insecure --cacert ${cacert}/etc/ssl/certs/ca-bundle.crt ";
-  })) (functionArgs fetcher);
+  # Handle both attrset and function arguments for extendMkDerivation compatibility
+  curlFetcher = fetcher: setFunctionArgs (args:
+    let
+      modifyArgs = a: a // {
+        curlOpts = (a.curlOpts or "") + " --no-insecure --cacert ${cacert}/etc/ssl/certs/ca-bundle.crt ";
+      };
+    in
+      if isFunction args
+      then fetcher (final: modifyArgs (args final))
+      else fetcher (modifyArgs args)
+  ) (functionArgs fetcher);
 
   unsafeFetcher = name: reason: throw "The fetcher ${name} is deemed unsafe: ${reason}.";
 
+37 −0
Original line number Diff line number Diff line
--- a/lib/prelude.nix
+++ b/lib/prelude.nix
@@ -108,9 +108,24 @@
 
   primitiveFetchers = listFetchers builtinsOverlay true ++ [ "fetchurlBoot" ];
 
+  # Helper to merge args, handling both attrsets and functions (for extendMkDerivation)
+  mergeArgsWithRequired = requiredArgs: args:
+    if isFunction args
+    then final: requiredArgs // args final
+    else requiredArgs // args;
+
+  # Helper to safely intersect args with oldArgs, handling function args
+  safeIntersectArgs = args: oldArgs:
+    if isFunction args
+    then {}  # When args is a function, we can't know the keys at evaluation time
+    else builtins.intersectAttrs args oldArgs;
+
   markFetcher = { type, name, fetcher }:
     let
-      customFetcher = args: markFetcherDrv { inherit type name fetcher args; drv = fetcher (requiredFetcherArgs // args); };
+      customFetcher = args: markFetcherDrv {
+        inherit type name fetcher args;
+        drv = fetcher (mergeArgsWithRequired requiredFetcherArgs args);
+      };
 
       # The required fetcher arguments are assumed to be of type string,
       # because requiring a complex value, e.g. a derivation attrset, is very unlikely,
@@ -132,7 +147,7 @@
               if !(elem origPassthru.__fetcher.name primitiveFetchers) then functionArgs origPassthru.__fetcher
               else throw "Fetcher ${name} is build on top of the primitive fetcher ${origPassthru.__fetcher.name}, which is not supported."
             else {};
-          newArgs = oldArgs // functionArgs fetcher // mapAttrs (_: _: true) (builtins.intersectAttrs args oldArgs);
+          newArgs = oldArgs // functionArgs fetcher // mapAttrs (_: _: true) (safeIntersectArgs args oldArgs);
         in {
           passthru = origPassthru // {
             __fetcher = setFunctionArgs fetcher newArgs // { inherit type name args; drv = drvOverriden; };
+5 −0
Original line number Diff line number Diff line
@@ -47,6 +47,11 @@ stdenv.mkDerivation (finalAttrs: {
      url = "https://github.com/msteen/nix-prefetch/commit/508237f48f7e2d8496ce54f38abbe57f44d0cbca.patch";
      hash = "sha256-9SYPcRFZaVyNjMUVdXbef5eGvLp/kr379eU9lG5GgE0=";
    })
    # Fix compatibility with extendMkDerivation-based fetchers (fetchzip, fetchgit, etc.)
    # The curlFetcher and markFetcher functions assumed fetcher arguments are always
    # attribute sets, but extendMkDerivation can pass functions for the finalAttrs pattern.
    ./fix-extendMkDerivation-overlay.patch
    ./fix-extendMkDerivation-prelude.patch
  ];

  postPatch = ''