Skip to content

Commit

Permalink
Merge pull request #97114 from Infinisil/type-deprecation
Browse files Browse the repository at this point in the history
Better type deprecation messages
  • Loading branch information
infinisil authored Sep 7, 2020
2 parents f73b762 + a582f6a commit ed5a07c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@ rec {
# yield a value computed from the definitions
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;
inherit (res.defsFinal') highestPrio;
definitions = map (def: def.value) res.defsFinal;
Expand Down
31 changes: 16 additions & 15 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ rec {
# combinable with the binOp binary operation.
# binOp: binary operation that merge two payloads of the same type.
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";
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;
};

Expand Down Expand Up @@ -222,8 +225,10 @@ rec {

# Deprecated; should not be used because it quietly concatenates
# strings, which is usually not what you want.
string = warn "types.string is deprecated because it quietly concatenates strings"
(separatedString "");
string = separatedString "" // {
name = "string";
deprecationMessage = "See https://github.com/NixOS/nixpkgs/pull/66346 for better alternative types.";
};

attrs = mkOptionType {
name = "attrs";
Expand Down Expand Up @@ -252,9 +257,6 @@ rec {
merge = mergeEqualOption;
};

# TODO: drop this in the future:
list = builtins.trace "`types.list` has been removed; please use `types.listOf` instead" types.listOf;

listOf = elemType: mkOptionType rec {
name = "listOf";
description = "list of ${elemType.description}s";
Expand Down Expand Up @@ -327,14 +329,12 @@ rec {
};

# TODO: drop this in the future:
loaOf =
let msg =
''
`types.loaOf` has been removed and mixing lists with attribute values
is no longer possible; please use `types.attrsOf` instead.
See https://github.com/NixOS/nixpkgs/issues/1800 for the motivation.
'';
in builtins.trace msg types.attrsOf;
loaOf = elemType: types.attrsOf elemType // {
name = "loaOf";
deprecationMessage = "Mixing lists with attribute values is no longer"
+ " possible; please use `types.attrsOf` instead. See"
+ " https://github.com/NixOS/nixpkgs/issues/1800 for the motivation.";
};

# Value of given type but with no merging (i.e. `uniq list`s are not concatenated).
uniq = elemType: mkOptionType rec {
Expand Down Expand Up @@ -534,8 +534,9 @@ rec {
# declarations from the ‘options’ attribute of containing option
# declaration.
optionSet = mkOptionType {
name = builtins.trace "types.optionSet is deprecated; use types.submodule instead" "optionSet";
name = "optionSet";
description = "option set";
deprecationMessage = "Use `types.submodule' instead";
};
# Augment the given type with an additional type check function.
addCheck = elemType: check: elemType // { check = x: elemType.check x && check x; };
Expand Down

0 comments on commit ed5a07c

Please sign in to comment.