query
On this page

warn

lib.trivial.warn

Primop
Docs pulled from | This Revision | 10 minutes ago

Takes 2 arguments

e1, e2


warn message value

Print a warning before returning the second argument.

See builtins.warn (Nix >= 2.23). On older versions, the Nix 2.23 behavior is emulated with builtins.trace, including the NIX_ABORT_ON_WARN behavior, but not the nix.conf setting or command line option.

Inputs

message (String)

Warning message to print before evaluating value.

value (any value)

Value to return as-is.

Type

String -> a -> a

Noogle detected

Aliases

Implementation

This function is implemented in c++ and is part of the native nix runtime.

Implementation

The following is the current implementation of this function.

warn =
    # 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
      # 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 "evaluation warning: ${msg}" (
          abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors."
        )
      else
        builtins.trace "evaluation warning: ${msg}" v
    );