From f7250f372a686599d7db607c5e5a9252e3d25886 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 24 Apr 2024 11:10:29 +0200 Subject: [PATCH 1/5] lib.warn: Use or behave like builtins.warn --- lib/trivial.nix | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index 5b7a1ee30f7a..b5c5d325eaad 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -12,6 +12,9 @@ let version versionSuffix warn; + inherit (lib) + isString + ; in { ## Simple (higher order) functions @@ -698,9 +701,18 @@ in { ``` */ 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}"; + # Since https://github.com/NixOS/nix/pull/10592 + builtins.warn or ( + 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`. From 4584ea07677102f3d93088fb7b842ef6a91ab433 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Thu, 6 Jun 2024 01:09:13 +0200 Subject: [PATCH 2/5] lib.warn: Say _evaluation_ warning, like builtins.warn --- lib/trivial.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index b5c5d325eaad..334d1029a103 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -710,8 +710,8 @@ in { # `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 + then builtins.trace "evaluation warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") + else builtins.trace "evaluation warning: ${msg}" v ); /** From f621ab9204a8615c626cd611d8467a8a24d33837 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 16 Jul 2024 15:52:17 +0200 Subject: [PATCH 3/5] lib.warn: Fix color Co-authored-by: Aleksana --- lib/trivial.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index 334d1029a103..9db1bfa66682 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -711,7 +711,7 @@ in { assert isString msg; if mustAbort then builtins.trace "evaluation warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") - else builtins.trace "evaluation warning: ${msg}" v + else builtins.trace "evaluation warning: ${msg}" v ); /** From 7d4a9a5772966bf10f31c6dc51ff7cfa1c8ce02f Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 16 Jul 2024 15:55:05 +0200 Subject: [PATCH 4/5] lib.warn: Remove color from the message itself This aligns with Nix and makes it more readable. The prefix still stands out. --- lib/trivial.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index 9db1bfa66682..4e9ec8cfbab4 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -710,8 +710,8 @@ in { # `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 "evaluation warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") - else builtins.trace "evaluation warning: ${msg}" v + then builtins.trace "evaluation warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") + else builtins.trace "evaluation warning: ${msg}" v ); /** From bc556c56864a8b593cc05c39421475e8187e5367 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 23 Jul 2024 12:48:01 +0200 Subject: [PATCH 5/5] lib.warn: Update docs - Bring up to date - Give meaning to the metavariables - Use italics for metavariables (just like the Nix manual) - Don't abbreviate - No hard feelings, Val. --- lib/trivial.nix | 68 +++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index 4e9ec8cfbab4..96929462a816 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -661,47 +661,33 @@ in { importTOML = path: builtins.fromTOML (builtins.readFile path); - ## Warnings - - # See https://github.com/NixOS/nix/issues/749. Eventually we'd like these - # to expand to Nix builtins that carry metadata so that Nix can filter out - # the INFO messages without parsing the message string. - # - # Usage: - # { - # foo = lib.warn "foo is deprecated" oldFoo; - # bar = lib.warnIf (bar == "") "Empty bar is deprecated" bar; - # } - # - # TODO: figure out a clever way to integrate location information from - # something like __unsafeGetAttrPos. - /** - 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` + `warn` *`message`* *`value`* + + Print a warning before returning the second argument. + + See [`builtins.warn`](https://nix.dev/manual/nix/latest/language/builtins.html#builtins-warn) (Nix >= 2.23). + On older versions, the Nix 2.23 behavior is emulated with [`builtins.trace`](https://nix.dev/manual/nix/latest/language/builtins.html#builtins-warn), including the [`NIX_ABORT_ON_WARN`](https://nix.dev/manual/nix/latest/command-ref/conf-file#conf-abort-on-warn) behavior, but not the `nix.conf` setting or command line option. # Inputs - `msg` + *`message`* (String) - : Warning message to print. + : Warning message to print before evaluating *`value`*. - `val` + *`value`* (any value) : Value to return as-is. # Type ``` - string -> a -> a + String -> a -> a ``` */ warn = - # Since https://github.com/NixOS/nix/pull/10592 + # Since Nix 2.23, https://github.com/NixOS/nix/pull/10592 builtins.warn or ( let mustAbort = lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"]; in @@ -715,53 +701,57 @@ in { ); /** - Like warn, but only warn when the first argument is `true`. + `warnIf` *`condition`* *`message`* *`value`* + + Like `warn`, but only warn when the first argument is `true`. # Inputs - `cond` + *`condition`* (Boolean) - : 1\. Function argument + : `true` to trigger the warning before continuing with *`value`*. - `msg` + *`message`* (String) - : 2\. Function argument + : Warning message to print before evaluating - `val` + *`value`* (any value) : Value to return as-is. # Type ``` - bool -> string -> a -> a + Bool -> String -> a -> a ``` */ warnIf = cond: msg: if cond then warn msg else x: x; /** - Like warnIf, but negated (warn if the first argument is `false`). + `warnIfNot` *`condition`* *`message`* *`value`* + + Like `warnIf`, but negated: warn if the first argument is `false`. # Inputs - `cond` + *`condition`* - : 1\. Function argument + : `false` to trigger the warning before continuing with `val`. - `msg` + *`message`* - : 2\. Function argument + : Warning message to print before evaluating *`value`*. - `val` + *`value`* : Value to return as-is. # Type ``` - bool -> string -> a -> a + Boolean -> String -> a -> a ``` */ warnIfNot = cond: msg: if cond then x: x else warn msg;