lib/types: Allow types to emit a deprecation warning

Previously the only way to deprecate a type was using

  theType = lib.warn "deprecated" (mkOptionType ...)

This caused the warning to be emitted when the type was evaluated, but
the error didn't include which option actually used that type.

With this commit, types can specify a deprecationMessage, which when
non-null, is printed along with the option that uses the type
This commit is contained in:
Silvan Mosberger 2020-09-04 13:39:27 +02:00
parent f3bf0f173e
commit 1d4656225d
No known key found for this signature in database
GPG Key ID: E8F1E9EAD284E17D
2 changed files with 9 additions and 2 deletions

View File

@ -457,7 +457,11 @@ rec {
# yield a value computed from the definitions # yield a value computed from the definitions
value = if opt ? apply then opt.apply res.mergedValue else res.mergedValue; value = if opt ? apply then opt.apply res.mergedValue else res.mergedValue;
in opt // warnDeprecation =
if opt.type.deprecationMessage == null then id
else warn "The type `types.${opt.type.name}' of option `${showOption loc}' defined in ${showFiles opt.declarations} is deprecated. ${opt.type.deprecationMessage}";
in warnDeprecation opt //
{ value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value; { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
inherit (res.defsFinal') highestPrio; inherit (res.defsFinal') highestPrio;
definitions = map (def: def.value) res.defsFinal; definitions = map (def: def.value) res.defsFinal;

View File

@ -91,9 +91,12 @@ rec {
# combinable with the binOp binary operation. # combinable with the binOp binary operation.
# binOp: binary operation that merge two payloads of the same type. # binOp: binary operation that merge two payloads of the same type.
functor ? defaultFunctor name functor ? defaultFunctor name
, # The deprecation message to display when this type is used by an option
# If null, the type isn't deprecated
deprecationMessage ? null
}: }:
{ _type = "option-type"; { _type = "option-type";
inherit name check merge emptyValue getSubOptions getSubModules substSubModules typeMerge functor; inherit name check merge emptyValue getSubOptions getSubModules substSubModules typeMerge functor deprecationMessage;
description = if description == null then name else description; description = if description == null then name else description;
}; };