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/default.nix | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/default.nix') diff --git a/lib/default.nix b/lib/default.nix index 50320669e2804..ccae0bbc3ab41 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -66,8 +66,9 @@ let stringLength sub substring tail trace; inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max - importJSON importTOML warn info showWarnings nixpkgsVersion version mod compare - splitByAndCompare functionArgs setFunctionArgs isFunction toHexString toBaseDigits; + importJSON importTOML warn warnIf info showWarnings nixpkgsVersion version + mod compare splitByAndCompare functionArgs setFunctionArgs isFunction + toHexString toBaseDigits; inherit (self.fixedPoints) fix fix' converge extends composeExtensions composeManyExtensions makeExtensible makeExtensibleWithCustomName; inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath -- cgit 1.4.1