From 56adbf5a8af3a640b943c4a156b81e4b2aa5a08a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 11 Apr 2023 01:30:19 +0200 Subject: [PATCH] Add extraDebug, lint --- all-modules.nix | 1 + dev/tests/eval-tests.nix | 17 +++++++++++ modules/debug.nix | 9 +++++- modules/lint.nix | 61 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 modules/lint.nix diff --git a/all-modules.nix b/all-modules.nix index 83d2bd0..c57f86f 100644 --- a/all-modules.nix +++ b/all-modules.nix @@ -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 diff --git a/dev/tests/eval-tests.nix b/dev/tests/eval-tests.nix index e0dedae..c2c2158 100644 --- a/dev/tests/eval-tests.nix +++ b/dev/tests/eval-tests.nix @@ -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 == { @@ -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"; diff --git a/modules/debug.nix b/modules/debug.nix index 6436010..cacd4e8 100644 --- a/modules/debug.nix +++ b/modules/debug.nix @@ -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; @@ -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; diff --git a/modules/lint.nix b/modules/lint.nix new file mode 100644 index 0000000..43e284e --- /dev/null +++ b/modules/lint.nix @@ -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 + ); + }; +}