Skip to content

Commit cf9805a

Browse files
committed
lib.derivations: add warnOnInstantiate
1 parent 3d3a119 commit cf9805a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ let
121121
inherit (self.customisation) overrideDerivation makeOverridable
122122
callPackageWith callPackagesWith extendDerivation hydraJob
123123
makeScope makeScopeWithSplicing makeScopeWithSplicing';
124-
inherit (self.derivations) lazyDerivation optionalDrvAttr;
124+
inherit (self.derivations) lazyDerivation optionalDrvAttr warnOnInstantiate;
125125
inherit (self.meta) addMetaAttrs dontDistribute setName updateName
126126
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
127127
hiPrioSet licensesSpdx getLicenseFromSpdxId getLicenseFromSpdxIdOr

lib/derivations.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ let
44
inherit (lib)
55
genAttrs
66
isString
7+
mapAttrs
8+
removeAttrs
79
throwIfNot
810
;
911

@@ -206,4 +208,38 @@ in
206208
optionalDrvAttr =
207209
cond:
208210
value: if cond then value else null;
211+
212+
/**
213+
Wrap a derivation such that instantiating it produces a warning.
214+
215+
All attributes apart from `meta`, `name`, and `type` (which are used by
216+
`nix search`) will be wrapped in `lib.warn`.
217+
218+
# Inputs
219+
220+
`msg`
221+
: The warning message to emit (via `lib.warn`).
222+
223+
`drv`
224+
: The derivation to wrap.
225+
226+
# Examples
227+
:::{.example}
228+
## `lib.derivations.warnOnInstantiate` usage example
229+
230+
```nix
231+
{
232+
myPackage = warnOnInstantiate "myPackage has been renamed to my-package" my-package;
233+
}
234+
```
235+
236+
:::
237+
*/
238+
warnOnInstantiate =
239+
msg: drv:
240+
let
241+
drvToWrap = removeAttrs drv [ "meta" "name" "type" ];
242+
in
243+
drv
244+
// mapAttrs (_: lib.warn msg) drvToWrap;
209245
}

0 commit comments

Comments
 (0)