From 81e1e68eaf6c765147da964d356f704030734dd2 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Tue, 27 Apr 2021 10:56:51 +0000 Subject: lib.trivial.warnIf: init It's a common pattern in Nixpkgs to want to emit a warning in certain cases, but not actually change behaviours. This is often expressed as either if cond then lib.warn "Don't do that thing" x else x Or (if cond then lib.warn "Don't do that thing" else lib.id) x Neither of which really expresses the intent here, because it looks like 'x' is being chosen conditionally. To make this clearer, I introduce a "warnIf" function, which makes it clear that the only thing being affected by the condition is whether the warning is generated, not the value being returned. --- lib/trivial.nix | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/trivial.nix') diff --git a/lib/trivial.nix b/lib/trivial.nix index be6d0115f5b86..f6f5da5998ffb 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -297,12 +297,15 @@ rec { # 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. warn = msg: builtins.trace "warning: ${msg}"; + warnIf = cond: msg: if cond then warn msg else id; + info = msg: builtins.trace "INFO: ${msg}"; showWarnings = warnings: res: lib.fold (w: x: warn w x) res warnings; -- cgit 1.4.1