From 1d4656225d4f1e93ea9801c72eb0b1b0bffa245d Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 4 Sep 2020 13:39:27 +0200 Subject: [PATCH 1/5] 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 --- lib/modules.nix | 6 +++++- lib/types.nix | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index decb96ffe111..412c7f1df712 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -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; diff --git a/lib/types.nix b/lib/types.nix index 17e7a939fe3d..f999805e1a98 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -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; }; From 14095f8f480d850d146341abb72bb81d9952248c Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 4 Sep 2020 13:45:11 +0200 Subject: [PATCH 2/5] lib/types: Remove types.list, it's been deprecated long enough Has been deprecated since fd803fce606a007403ba6d05f09ed2e6a3371830 (2013-08-22) --- lib/types.nix | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index f999805e1a98..caeb688fd908 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -255,9 +255,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"; From 2bed3b2ad7e671f75580c0df7d2b644bde0181b7 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 4 Sep 2020 13:47:04 +0200 Subject: [PATCH 3/5] lib/types: Set deprecationMessage for types.string --- lib/types.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index caeb688fd908..005be5123e7e 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -225,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"; From 3b7aca47e0d2523da3158a6522e768a461b08e9f Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 4 Sep 2020 13:53:03 +0200 Subject: [PATCH 4/5] lib/types: Set deprecationMessage for types.loaOf --- lib/types.nix | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index 005be5123e7e..a8f744685f9b 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -329,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 { From a582f6adde8f4345582c80fc3ccfe0de3aa89480 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 4 Sep 2020 13:56:18 +0200 Subject: [PATCH 5/5] lib/types: Set deprecationMessage for types.optionSet --- lib/types.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/types.nix b/lib/types.nix index a8f744685f9b..d3469450963d 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -529,8 +529,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; };