summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2021-08-25 18:35:16 +0200
committerRobert Hensing <robert@roberthensing.nl>2021-10-13 22:43:06 +0200
commitc9b7cc79e6ce6b38debb28ab0d06051a4fb5b336 (patch)
treed5c9357f4e77edf293581f337d8f2acde0069702 /lib
parentcc3b147ed182a6cae239348ef094158815da14ae (diff)
downloadnixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.tar
nixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.tar.gz
nixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.tar.bz2
nixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.tar.lz
nixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.tar.xz
nixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.tar.zst
nixpkgs-c9b7cc79e6ce6b38debb28ab0d06051a4fb5b336.zip
lib.warn: Add NIX_ABORT_ON_WARN for call traces
Diffstat (limited to 'lib')
-rw-r--r--lib/trivial.nix21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index 7956ba4bde6..a389c7cdfac 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -303,7 +303,26 @@ rec {
   # TODO: figure out a clever way to integrate location information from
   # something like __unsafeGetAttrPos.
 
-  warn = msg: builtins.trace "warning: ${msg}";
+  /*
+    Print a warning before returning the second argument. This function behaves
+    like `builtins.trace`, but requires a string message and formats it as a
+    warning, including the `warning: ` prefix.
+
+    To get a call stack trace and abort evaluation, set the environment variable
+    `NIX_ABORT_ON_WARN=true` and set the Nix options `--option pure-eval false --show-trace`
+
+    Type: string -> a -> a
+  */
+  warn =
+    if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"]
+    then msg: builtins.trace "warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.")
+    else msg: builtins.trace "warning: ${msg}";
+
+  /*
+    Like warn, but only warn when the first argument is `true`.
+
+    Type: bool -> string -> a -> a
+  */
   warnIf = cond: msg: if cond then warn msg else id;
 
   info = msg: builtins.trace "INFO: ${msg}";