lib.derivations: add warnOnInstantiate

This commit is contained in:
ash 2024-11-11 19:59:34 +00:00
parent 3d3a119dce
commit cf9805af62
2 changed files with 37 additions and 1 deletions

View File

@ -121,7 +121,7 @@ let
inherit (self.customisation) overrideDerivation makeOverridable
callPackageWith callPackagesWith extendDerivation hydraJob
makeScope makeScopeWithSplicing makeScopeWithSplicing';
inherit (self.derivations) lazyDerivation optionalDrvAttr;
inherit (self.derivations) lazyDerivation optionalDrvAttr warnOnInstantiate;
inherit (self.meta) addMetaAttrs dontDistribute setName updateName
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
hiPrioSet licensesSpdx getLicenseFromSpdxId getLicenseFromSpdxIdOr

View File

@ -4,6 +4,8 @@ let
inherit (lib)
genAttrs
isString
mapAttrs
removeAttrs
throwIfNot
;
@ -206,4 +208,38 @@ in
optionalDrvAttr =
cond:
value: if cond then value else null;
/**
Wrap a derivation such that instantiating it produces a warning.
All attributes apart from `meta`, `name`, and `type` (which are used by
`nix search`) will be wrapped in `lib.warn`.
# Inputs
`msg`
: The warning message to emit (via `lib.warn`).
`drv`
: The derivation to wrap.
# Examples
:::{.example}
## `lib.derivations.warnOnInstantiate` usage example
```nix
{
myPackage = warnOnInstantiate "myPackage has been renamed to my-package" my-package;
}
```
:::
*/
warnOnInstantiate =
msg: drv:
let
drvToWrap = removeAttrs drv [ "meta" "name" "type" ];
in
drv
// mapAttrs (_: lib.warn msg) drvToWrap;
}