lib/options: Throw error for options without a type

Makes all options rendered in the manual throw an error if they don't
have a type specified.

This is a follow-up to #76184

Co-Authored-By: Silvan Mosberger <contact@infinisil.com>
This commit is contained in:
Janne Heß 2021-06-16 12:27:47 +02:00 committed by Silvan Mosberger
parent b2d803ca57
commit 0c766a100e
4 changed files with 18 additions and 8 deletions

View file

@ -231,7 +231,7 @@ rec {
then true then true
else opt.visible or true; else opt.visible or true;
readOnly = opt.readOnly or false; readOnly = opt.readOnly or false;
type = opt.type.description or null; type = opt.type.description or "unspecified";
} }
// optionalAttrs (opt ? example) { example = scrubOptionValue opt.example; } // optionalAttrs (opt ? example) { example = scrubOptionValue opt.example; }
// optionalAttrs (opt ? default) { default = scrubOptionValue opt.default; } // optionalAttrs (opt ? default) { default = scrubOptionValue opt.default; }

View file

@ -27,9 +27,10 @@ The function `mkOption` accepts the following arguments.
`type` `type`
: The type of the option (see [](#sec-option-types)). It may be : The type of the option (see [](#sec-option-types)). This
omitted, but that's not advisable since it may lead to errors that argument is mandatory for nixpkgs modules. Setting this is highly
are hard to diagnose. recommended for the sake of documentation and type checking. In case it is
not set, a fallback type with unspecified behavior is used.
`default` `default`

View file

@ -38,9 +38,11 @@ options = {
<listitem> <listitem>
<para> <para>
The type of the option (see The type of the option (see
<xref linkend="sec-option-types" />). It may be omitted, but <xref linkend="sec-option-types" />). This argument is
thats not advisable since it may lead to errors that are hard mandatory for nixpkgs modules. Setting this is highly
to diagnose. recommended for the sake of documentation and type checking.
In case it is not set, a fallback type with unspecified
behavior is used.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View file

@ -66,14 +66,21 @@ for (k, v) in overrides.items():
elif ov is not None or cur.get(ok, None) is None: elif ov is not None or cur.get(ok, None) is None:
cur[ok] = ov cur[ok] = ov
severity = "error" if warningsAreErrors else "warning"
# check that every option has a description # check that every option has a description
hasWarnings = False hasWarnings = False
for (k, v) in options.items(): for (k, v) in options.items():
if v.value.get('description', None) is None: if v.value.get('description', None) is None:
severity = "error" if warningsAreErrors else "warning"
hasWarnings = True hasWarnings = True
print(f"\x1b[1;31m{severity}: option {v.name} has no description\x1b[0m", file=sys.stderr) print(f"\x1b[1;31m{severity}: option {v.name} has no description\x1b[0m", file=sys.stderr)
v.value['description'] = "This option has no description." v.value['description'] = "This option has no description."
if v.value.get('type', "unspecified") == "unspecified":
hasWarnings = True
print(
f"\x1b[1;31m{severity}: option {v.name} has no type. Please specify a valid type, see " +
"https://nixos.org/manual/nixos/stable/index.html#sec-option-types\x1b[0m", file=sys.stderr)
if hasWarnings and warningsAreErrors: if hasWarnings and warningsAreErrors:
print( print(
"\x1b[1;31m" + "\x1b[1;31m" +