Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extraDebug, lint #134

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions all-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
./modules/flake.nix
./modules/formatter.nix
./modules/legacyPackages.nix
./modules/lint.nix
./modules/moduleWithSystem.nix
./modules/nixosConfigurations.nix
./modules/nixosModules.nix
Expand Down
17 changes: 17 additions & 0 deletions dev/tests/eval-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ rec {
config = { };
};

lints = mkFlake
{ inputs.self = { }; }
{
lint.messages = [ "a top level message" ];
systems = [ "x86_64-linux" "aarch64-darwin" ];
debug = true;
perSystem = {
lint.messages = [ "a per-system message" ];
};
};

runTests = ok:

assert empty == {
Expand Down Expand Up @@ -148,6 +159,12 @@ rec {

assert flakeModulesDisable.test123 == "option123";

assert lints.debug.lint.messages == [
"a top level message"
"in perSystem.aarch64-darwin: a per-system message"
"in perSystem.x86_64-linux: a per-system message"
];

ok;

result = runTests "ok";
Expand Down
9 changes: 8 additions & 1 deletion modules/debug.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let
removeAttrs
;

mkDebugConfig = { config, options, extendModules }: config // {
mkDebugConfig = { config, options, extendModules }: config // config.extraDebug // {
inherit config;
inherit (config) _module;
inherit options;
Expand Down Expand Up @@ -52,6 +52,13 @@ in
See [Expore and debug option values](../debug.html) for more examples.
'';
};
extraDebug = mkOption {
type = types.attrsOf types.raw;
default = { };
description = ''
Extra values to return the flake `debug` attribute (if that has been enabled).
'';
};
perSystem = mkPerSystemOption
({ options, config, extendModules, ... }: {
_file = ./formatter.nix;
Expand Down
61 changes: 61 additions & 0 deletions modules/lint.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{ config, flake-parts-lib, lib, ... }:
let
inherit (flake-parts-lib)
mkPerSystemOption
;
in
{
options = {
lint = {
messages = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Diagnostic messages that a flake author may or may not care about.

For example, a module might detect that it's used in a weird way, but
not be sure whether that's a mistake or not. Emitting a warning would
be too much, but with this option, the author can still find the
detected problem, by enabling [`debug`](#opt-debug) and querying
the `debug.lint.messages` flake attribute in `nix repl`.

This feature is not gated by an enable option, as performance does not
suffer from an unevaluated option.

There's also no option to upgrade to warnings, because that would make
evaluation dependent on rather many options, even if the caller only
needs one specific unrelated thing from the flake.
A more complex interface could attach the warnings to specific flake
attribute paths, but that's not implemented for now.
'';
};
};
perSystem = mkPerSystemOption
({ ... }: {
options.lint.messages = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Diagnostic messages that a flake author may or may not care about.

These messages are added to the `debug.lint.messages` flake attribute,
when [`debug`](#opt-debug) is enabled.
'';
};
});
};
config = {
extraDebug.lint.toplevel.messages = config.lint.messages;
extraDebug.lint.messages =
config.lint.messages ++
lib.concatLists (
lib.mapAttrsToList
(sysName: sys:
map
(msg: "in perSystem.${sysName}: ${msg}")
sys.lint.messages
)
config.allSystems
);
};
}