lib.warn: Use or behave like builtins.warn

This commit is contained in:
Robert Hensing 2024-04-24 11:10:29 +02:00
parent 76e6d8b1a8
commit f7250f372a

View File

@ -12,6 +12,9 @@ let
version version
versionSuffix versionSuffix
warn; warn;
inherit (lib)
isString
;
in { in {
## Simple (higher order) functions ## Simple (higher order) functions
@ -698,9 +701,18 @@ in {
``` ```
*/ */
warn = warn =
if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"] # Since https://github.com/NixOS/nix/pull/10592
then msg: builtins.trace "warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") builtins.warn or (
else msg: builtins.trace "warning: ${msg}"; let mustAbort = lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"];
in
# Do not eta reduce v, so that we have the same strictness as `builtins.warn`.
msg: v:
# `builtins.warn` requires a string message, so we enforce that in our implementation, so that callers aren't accidentally incompatible with newer Nix versions.
assert isString msg;
if mustAbort
then builtins.trace "warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.")
else builtins.trace "warning: ${msg}" v
);
/** /**
Like warn, but only warn when the first argument is `true`. Like warn, but only warn when the first argument is `true`.