about summary refs log tree commit diff
path: root/lib/trivial.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2022-03-22 21:12:11 +0100
committerGitHub <noreply@github.com>2022-03-22 21:12:11 +0100
commita38220e61d4e692d52a331af58df6fe5ac0bc272 (patch)
treecba0bc5503e0ddba84256ca390a0b9e585880415 /lib/trivial.nix
parentc29c8895f322ec554eba7d83a0d8b29b32600d01 (diff)
parent7fae930a37a06fe08d4f85d6f41b04b37ed05cd0 (diff)
Merge pull request #164890 from ncfavier/lib-warn-throw
lib/trivial: add warnIfNot and throwIf
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r--lib/trivial.nix16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index afae4f87254f5..543680a10801f 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -347,7 +347,14 @@ rec {
 
     Type: bool -> string -> a -> a
   */
-  warnIf = cond: msg: if cond then warn msg else id;
+  warnIf = cond: msg: if cond then warn msg else x: x;
+
+  /*
+    Like warnIf, but negated (warn if the first argument is `false`).
+
+    Type: bool -> string -> a -> a
+  */
+  warnIfNot = cond: msg: if cond then x: x else warn msg;
 
   /*
     Like the `assert b; e` expression, but with a custom error message and
@@ -371,6 +378,13 @@ rec {
   */
   throwIfNot = cond: msg: if cond then x: x else throw msg;
 
+  /*
+    Like throwIfNot, but negated (throw if the first argument is `true`).
+
+    Type: bool -> string -> a -> a
+  */
+  throwIf = cond: msg: if cond then throw msg else x: x;
+
   /* Check if the elements in a list are valid values from a enum, returning the identity function, or throwing an error message otherwise.
 
      Example: