summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2022-03-24 17:01:47 +0100
committerGitHub <noreply@github.com>2022-03-24 17:01:47 +0100
commitce5a33e62bea14024af8bfad23b54c8848a89d9e (patch)
tree6b85cec1750e456d6f852cb3e0a07b4694414b1f /lib
parent6ea8d5ee71793e236a19af3b5686a1ccdb0af3da (diff)
parent6c469679f6dea7f0cefafcac7e9d95b98bf8ff55 (diff)
downloadnixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.tar
nixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.tar.gz
nixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.tar.bz2
nixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.tar.lz
nixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.tar.xz
nixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.tar.zst
nixpkgs-ce5a33e62bea14024af8bfad23b54c8848a89d9e.zip
Merge pull request #164660 from ncfavier/tests-restrict-arguments
nixos/testing: restrict arguments to makeTest
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix5
-rw-r--r--lib/modules.nix6
-rw-r--r--lib/trivial.nix19
3 files changed, 25 insertions, 5 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 6f7930c5343..0e94e821bea 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -68,7 +68,8 @@ let
       bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max
       importJSON importTOML warn warnIf throwIfNot checkListOfEnum
       info showWarnings nixpkgsVersion version isInOldestRelease
-      mod compare splitByAndCompare functionArgs setFunctionArgs isFunction
+      mod compare splitByAndCompare
+      functionArgs setFunctionArgs isFunction toFunction
       toHexString toBaseDigits;
     inherit (self.fixedPoints) fix fix' converge extends composeExtensions
       composeManyExtensions makeExtensible makeExtensibleWithCustomName;
@@ -113,7 +114,7 @@ let
       commitIdFromGitRepo cleanSourceWith pathHasContext
       canCleanSource pathIsRegularFile pathIsGitRepo;
     inherit (self.modules) evalModules setDefaultModuleLocation
-      unifyModuleSyntax applyIfFunction mergeModules
+      unifyModuleSyntax applyModuleArgsIfFunction mergeModules
       mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions
       pushDownProperties dischargeProperties filterOverrides
       sortProperties fixupOptionType mkIf mkAssert mkMerge mkOverride
diff --git a/lib/modules.nix b/lib/modules.nix
index 9bb8bfbbdf1..35c93d22baf 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -282,11 +282,11 @@ rec {
       # Like unifyModuleSyntax, but also imports paths and calls functions if necessary
       loadModule = args: fallbackFile: fallbackKey: m:
         if isFunction m || isAttrs m then
-          unifyModuleSyntax fallbackFile fallbackKey (applyIfFunction fallbackKey m args)
+          unifyModuleSyntax fallbackFile fallbackKey (applyModuleArgsIfFunction fallbackKey m args)
         else if isList m then
           let defs = [{ file = fallbackFile; value = m; }]; in
           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
-        else unifyModuleSyntax (toString m) (toString m) (applyIfFunction (toString m) (import m) args);
+        else unifyModuleSyntax (toString m) (toString m) (applyModuleArgsIfFunction (toString m) (import m) args);
 
       /*
       Collects all modules recursively into the form
@@ -383,7 +383,7 @@ rec {
         config = addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]));
       };
 
-  applyIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then
+  applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then
     let
       # Module arguments are resolved in a strict manner when attribute set
       # deconstruction is used.  As the arguments are now defined with the
diff --git a/lib/trivial.nix b/lib/trivial.nix
index 543680a1080..18616a189c2 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -441,6 +441,25 @@ rec {
   isFunction = f: builtins.isFunction f ||
     (f ? __functor && isFunction (f.__functor f));
 
+  /*
+    Turns any non-callable values into constant functions.
+    Returns callable values as is.
+
+    Example:
+
+      nix-repl> lib.toFunction 1 2
+      1
+
+      nix-repl> lib.toFunction (x: x + 1) 2
+      3
+  */
+  toFunction =
+    # Any value
+    v:
+    if isFunction v
+    then v
+    else k: v;
+
   /* Convert the given positive integer to a string of its hexadecimal
      representation. For example: