lib/options: Warn about options without type#127066
lib/options: Warn about options without type#127066dasJ wants to merge 2 commits intoNixOS:masterfrom
Conversation
|
The relevant documentation should be updated since it says that the option type may be omitted. https://nixos.org/manual/nixos/unstable/index.html#sec-option-declarations EDIT: https://github.com/lourkeur/nixpkgs/tree/doc/option-type |
|
To prevent options without a type for all options, not just ones rendered in documentation, you should add the warning here instead: Line 685 in a3274dc |
5e97ba3 to
a742176
Compare
|
@infinisil I tried that but there are actually a lot of internal options that have no type. Going for all options in the manual is a good start in my opinion without having to touch a lot of options. |
|
It turns out that this PR didn't work at all like this, I could never make it produce any warnings even with an option that obviously doesn't have a type. I force pushed a fix based on my previous suggested, but making sure to only throw a warning for options that aren't internal and are visible. This now gives a couple warnings: And some more depending on which modules are included: |
|
Oh wait, just realized that the force push didn't work, looks like I can't push to this branch. Pushed it to my own branch: https://github.com/Infinisil/nixpkgs/tree/feat/warn-no-type |
|
@infinisil Do you want to open a PR yourself or do you want me to pick the commit? |
|
Feel free to pick the commit :) |
48b2ac7 to
4d71487
Compare
|
After talking to @domenkozar we replaced the error message with something that is actionable and more clear |
ed52ff1 to
2767b73
Compare
|
Hmm, I'm having second thoughts. With this we're essentially telling people to always specify a type, but at least one of the main reasons we want to do this is that the fallback type of An alternative to fix this problem is to change the default type to something that doesn't have bad behavior, like |
|
I guess another reason for wanting good types is documentation and actual type checking (duh!). Though the cost of requiring every option definition having a type is significant. I suspect there to be many many instances of people declaring their own options without types, me including. |
|
It's the same logic (I think) as with the description which throws a warning as well without the dangers of changing the default type. |
|
The PR in its current state throws warnings for user-defined options without a type. E.g. with a { lib, config, ... }: {
options.foo = lib.mkOption {
default = "test";
};
config = {
fileSystems."/".device = "/nodev";
boot.loader.grub.device = "nodev";
environment.etc.foo.text = config.foo;
};
}Evaluating it: |
|
Yeah I wasn't arguing that, but I think we could make the warning an opt-out when people get annoyed by it |
|
My point was that not setting a I think if we just throw warnings for undeclared types in nixpkgs (or rather, for all modules that are rendered into the manual, which can include user modules if For the suggestion of only giving a warning for rendered options, here's a patch that makes it work, notably assuming that diff --git a/lib/options.nix b/lib/options.nix
index 5d52f065af0..c7970d38e73 100644
--- a/lib/options.nix
+++ b/lib/options.nix
@@ -185,7 +185,13 @@ rec {
then true
else opt.visible or true;
readOnly = opt.readOnly or false;
- type = opt.type.description or null;
+ type =
+ if opt.type.name == "unspecified"
+ then lib.warn
+ ("Option `${showOption loc}' defined in ${showFiles opt.declarations} has no type.\n"
+ +" Please specify a valid types from <nixpkgs/lib/types.nix> or use types.raw if there is no appropriate type.")
+ null
+ else opt.type.description or null;
}
// optionalAttrs (opt ? example) { example = scrubOptionValue opt.example; }
// optionalAttrs (opt ? default) { default = scrubOptionValue opt.default; }This notably does mention
|
ec3f749 to
55dce12
Compare
This is a follow-up to NixOS#76184 Co-Authored-By: Silvan Mosberger <contact@infinisil.com>
55dce12 to
feadddc
Compare
|
I created #162271 as an updated version of this, along with some improvements |
This is a follow-up to #76184
Closes #76184
Closes #141730
Closes #141760
Motivation for this change
Should push people to actually add types to all options.
Things done
sandboxinnix.confon non-NixOS linux)nix-shell -p nixpkgs-review --run "nixpkgs-review wip"./result/bin/)