summary refs log tree commit diff
path: root/lib/customisation.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2019-10-22 14:37:40 +0200
committerGitHub <noreply@github.com>2019-10-22 14:37:40 +0200
commit1230fc8674439df5a90fe4c1ea740256ef9906e4 (patch)
tree52879d914eff4e4bb314046ba1b6b9b2b6b5c6e8 /lib/customisation.nix
parent872166e13650ffb39012c13d6841b56564b8fe58 (diff)
parenta4896cb4aab132c0bcc32c5df4440f6d3b7ec5de (diff)
downloadnixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.tar
nixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.tar.gz
nixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.tar.bz2
nixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.tar.lz
nixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.tar.xz
nixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.tar.zst
nixpkgs-1230fc8674439df5a90fe4c1ea740256ef9906e4.zip
Merge pull request #67809 from Infinisil/propagate-override-args
lib.makeOverridable: Propagate function arguments
Diffstat (limited to 'lib/customisation.nix')
-rw-r--r--lib/customisation.nix37
1 files changed, 23 insertions, 14 deletions
diff --git a/lib/customisation.nix b/lib/customisation.nix
index 3be36fcd719..ac234e3b8c6 100644
--- a/lib/customisation.nix
+++ b/lib/customisation.nix
@@ -66,22 +66,31 @@ rec {
   */
   makeOverridable = f: origArgs:
     let
-      ff = f origArgs;
+      result = f origArgs;
+
+      # Creates a functor with the same arguments as f
+      copyArgs = g: lib.setFunctionArgs g (lib.functionArgs f);
+      # Changes the original arguments with (potentially a function that returns) a set of new attributes
       overrideWith = newArgs: origArgs // (if lib.isFunction newArgs then newArgs origArgs else newArgs);
+
+      # Re-call the function but with different arguments
+      overrideArgs = copyArgs (newArgs: makeOverridable f (overrideWith newArgs));
+      # Change the result of the function call by applying g to it
+      overrideResult = g: makeOverridable (copyArgs (args: g (f args))) origArgs;
     in
-      if builtins.isAttrs ff then (ff // {
-        override = newArgs: makeOverridable f (overrideWith newArgs);
-        overrideDerivation = fdrv:
-          makeOverridable (args: overrideDerivation (f args) fdrv) origArgs;
-        ${if ff ? overrideAttrs then "overrideAttrs" else null} = fdrv:
-          makeOverridable (args: (f args).overrideAttrs fdrv) origArgs;
-      })
-      else if lib.isFunction ff then {
-        override = newArgs: makeOverridable f (overrideWith newArgs);
-        __functor = self: ff;
-        overrideDerivation = throw "overrideDerivation not yet supported for functors";
-      }
-      else ff;
+      if builtins.isAttrs result then
+        result // {
+          override = overrideArgs;
+          overrideDerivation = fdrv: overrideResult (x: overrideDerivation x fdrv);
+          ${if result ? overrideAttrs then "overrideAttrs" else null} = fdrv:
+            overrideResult (x: x.overrideAttrs fdrv);
+        }
+      else if lib.isFunction result then
+        # Transform the result into a functor while propagating its arguments
+        lib.setFunctionArgs result (lib.functionArgs result) // {
+          override = overrideArgs;
+        }
+      else result;
 
 
   /* Call the package function in the file `fn' with the required