summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorYueh-Shun Li <shamrocklee@posteo.net>2023-05-29 05:51:52 +0800
committerYueh-Shun Li <shamrocklee@posteo.net>2023-11-07 01:58:04 +0000
commita16319908e7bda2a9744114396e5e32a4ff6a3fc (patch)
treeee927a4d70feb9dba6a33ac720c8f08239fb044f /lib
parent32ea236e59f3af1b96812d3365ae6f58437a898e (diff)
downloadnixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.tar
nixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.tar.gz
nixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.tar.bz2
nixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.tar.lz
nixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.tar.xz
nixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.tar.zst
nixpkgs-a16319908e7bda2a9744114396e5e32a4ff6a3fc.zip
lib.mirrorFunctionArgs: init
Co-authored-by: Silvan Mosberger <github@infinisil.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix2
-rw-r--r--lib/trivial.nix34
2 files changed, 35 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index fe737a125e6..0dac50a08ca 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -74,7 +74,7 @@ let
       importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum
       info showWarnings nixpkgsVersion version isInOldestRelease
       mod compare splitByAndCompare
-      functionArgs setFunctionArgs isFunction toFunction
+      functionArgs setFunctionArgs isFunction toFunction mirrorFunctionArgs
       toHexString toBaseDigits inPureEvalMode;
     inherit (self.fixedPoints) fix fix' converge extends composeExtensions
       composeManyExtensions makeExtensible makeExtensibleWithCustomName;
diff --git a/lib/trivial.nix b/lib/trivial.nix
index c23fc6070be..a89c1aa25b1 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -449,6 +449,40 @@ rec {
     (f ? __functor && isFunction (f.__functor f));
 
   /*
+    `mirrorFunctionArgs f g` creates a new function `g'` with the same behavior as `g` (`g' x == g x`)
+    but its function arguments mirroring `f` (`lib.functionArgs g' == lib.functionArgs f`).
+
+    Type:
+      mirrorFunctionArgs :: (a -> b) -> (a -> c) -> (a -> c)
+
+    Example:
+      addab = {a, b}: a + b
+      addab { a = 2; b = 4; }
+      => 6
+      lib.functionArgs addab
+      => { a = false; b = false; }
+      addab1 = attrs: addab attrs + 1
+      addab1 { a = 2; b = 4; }
+      => 7
+      lib.functionArgs addab1
+      => { }
+      addab1' = lib.mirrorFunctionArgs addab addab1
+      addab1' { a = 2; b = 4; }
+      => 7
+      lib.functionArgs addab1'
+      => { a = false; b = false; }
+  */
+  mirrorFunctionArgs =
+    # Function to provide the argument metadata
+    f:
+    let
+      fArgs = functionArgs f;
+    in
+    # Function to set the argument metadata to
+    g:
+    setFunctionArgs g fArgs;
+
+  /*
     Turns any non-callable values into constant functions.
     Returns callable values as is.