summary refs log tree commit diff
path: root/lib/trivial.nix
diff options
context:
space:
mode:
authorShea Levy <shea@shealevy.com>2018-01-31 14:02:19 -0500
committerShea Levy <shea@shealevy.com>2018-01-31 14:02:19 -0500
commit943592f69850fd07dd2422da062b1c1ebc45974d (patch)
tree3376a06cd3ab25a1622c8f320573ab09b55a9470 /lib/trivial.nix
parent0d7a0d7572d35526ddf34b6d011b7b88a8904b36 (diff)
downloadnixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.tar
nixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.tar.gz
nixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.tar.bz2
nixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.tar.lz
nixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.tar.xz
nixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.tar.zst
nixpkgs-943592f69850fd07dd2422da062b1c1ebc45974d.zip
Add setFunctionArgs lib function.
Among other things, this will allow *2nix tools to output plain data
while still being composable with the traditional
callPackage/.override interfaces.
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r--lib/trivial.nix27
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index c452c7b65bc..d8d51298143 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -52,7 +52,7 @@ rec {
 
   # Pull in some builtins not included elsewhere.
   inherit (builtins)
-    pathExists readFile isBool isFunction
+    pathExists readFile isBool
     isInt add sub lessThan
     seq deepSeq genericClosure;
 
@@ -99,4 +99,29 @@ rec {
   */
   warn = msg: builtins.trace "WARNING: ${msg}";
   info = msg: builtins.trace "INFO: ${msg}";
+
+  # | Add metadata about expected function arguments to a function.
+  # The metadata should match the format given by
+  # builtins.functionArgs, i.e. a set from expected argument to a bool
+  # representing whether that argument has a default or not.
+  # setFunctionArgs : (a → b) → Map String Bool → (a → b)
+  #
+  # This function is necessary because you can't dynamically create a
+  # function of the { a, b ? foo, ... }: format, but some facilities
+  # like callPackage expect to be able to query expected arguments.
+  setFunctionArgs = f: args:
+    { # TODO: Should we add call-time "type" checking like built in?
+      __functor = self: f;
+      __functionArgs = args;
+    };
+
+  # | Extract the expected function arguments from a function.
+  # This works both with nix-native { a, b ? foo, ... }: style
+  # functions and functions with args set with 'setFunctionArgs'. It
+  # has the same return type and semantics as builtins.functionArgs.
+  # setFunctionArgs : (a → b) → Map String Bool.
+  functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
+
+  isFunction = f: builtins.isFunction f ||
+    (f ? __functor && isFunction (f.__functor f));
 }