summary refs log tree commit diff
path: root/lib/trivial.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2021-12-22 13:05:55 +0100
committerRobert Hensing <robert@roberthensing.nl>2021-12-22 13:13:50 +0100
commitf2c5c706f47024fd477655fc34e883ce15172484 (patch)
tree167d3c3fa974291e9f24ead0d1ba473cdbfb316f /lib/trivial.nix
parentcf73196411928e1dfc6784a8a1c67a467533af4c (diff)
downloadnixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.tar
nixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.tar.gz
nixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.tar.bz2
nixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.tar.lz
nixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.tar.xz
nixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.tar.zst
nixpkgs-f2c5c706f47024fd477655fc34e883ce15172484.zip
lib.throwIfNot: init
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r--lib/trivial.nix22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index 33b553ac419..c961d3aa730 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -325,6 +325,28 @@ rec {
   */
   warnIf = cond: msg: if cond then warn msg else id;
 
+  /*
+    Like the `assert b; e` expression, but with a custom error message and
+    without the semicolon.
+
+    If true, return the identity function, `r: r`.
+
+    If false, throw the error message.
+
+    Calls can be juxtaposed using function application, as `(r: r) a = a`, so
+    `(r: r) (r: r) a = a`, and so forth.
+
+    Type: bool -> string -> a -> a
+
+    Example:
+
+        throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list."
+        lib.foldr (x: throwIfNot (lib.isFunction x) "All overlays passed to nixpkgs must be functions.") (r: r) overlays
+        pkgs
+
+  */
+  throwIfNot = cond: msg: if cond then x: x else throw msg;
+
   info = msg: builtins.trace "INFO: ${msg}";
 
   showWarnings = warnings: res: lib.foldr (w: x: warn w x) res warnings;