summaryrefslogtreecommitdiff
path: root/lib/modules.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-07-08 22:18:36 +0200
committerRobert Hensing <robert@roberthensing.nl>2023-08-14 10:44:56 +0200
commit0d472a62012364d064f0b75f1da491242c6ae9c6 (patch)
tree368ea6304232e38b80a6892495dffedb39e75e56 /lib/modules.nix
parentMerge pull request #248525 from helsinki-systems/upd/openssh (diff)
downloadnixpkgs-0d472a62012364d064f0b75f1da491242c6ae9c6.tar.gz
lib/modules: Report a good error when option tree has bare type
Note that this removes the possibility of declaring an option named `_type`.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix34
1 files changed, 33 insertions, 1 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 4966619f6630..9371ba4b27a4 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -630,7 +630,13 @@ let
loc = prefix ++ [name];
defns = pushedDownDefinitionsByName.${name} or [];
defns' = rawDefinitionsByName.${name} or [];
- optionDecls = filter (m: isOption m.options) decls;
+ optionDecls = filter
+ (m: m.options?_type
+ && (m.options._type == "option"
+ || throwDeclarationTypeError loc m.options._type
+ )
+ )
+ decls;
in
if length optionDecls == length decls then
let opt = fixupOptionType loc (mergeOptionDecls loc decls);
@@ -692,6 +698,32 @@ let
) unmatchedDefnsByName);
};
+ throwDeclarationTypeError = loc: actualTag:
+ let
+ name = lib.strings.escapeNixIdentifier (lib.lists.last loc);
+ path = showOption loc;
+ depth = length loc;
+
+ paragraphs = [
+ "Expected an option declaration at option path `${path}` but got an attribute set with type ${actualTag}"
+ ] ++ optional (actualTag == "option-type") ''
+ When declaring an option, you must wrap the type in a `mkOption` call. It should look somewhat like:
+ ${comment}
+ ${name} = lib.mkOption {
+ description = ...;
+ type = <the type you wrote for ${name}>;
+ ...
+ };
+ '';
+
+ # Ideally we'd know the exact syntax they used, but short of that,
+ # we can only reliably repeat the last. However, we repeat the
+ # full path in a non-misleading way here, in case they overlook
+ # the start of the message. Examples attract attention.
+ comment = optionalString (depth > 1) "\n # ${showOption loc}";
+ in
+ throw (concatStringsSep "\n\n" paragraphs);
+
/* Merge multiple option declarations into a single declaration. In
general, there should be only one declaration of each option.
The exception is the ‘options’ attribute, which specifies