nixos,nixpkgs: Add module classes

This allows modules that declare their class to be checked.
While that's not most user modules, frameworks can take advantage
of this by setting declaring the module class for their users.
That way, the mistake of importing a module into the wrong hierarchy
can be reported more clearly in some cases.
This commit is contained in:
Robert Hensing 2023-04-10 17:55:34 +02:00
parent 84b1b01702
commit 79703eef08
6 changed files with 14 additions and 3 deletions

View File

@ -45,7 +45,10 @@ let
# NB: This file describes the Nixpkgs manual, which happens to use module
# docs infra originally developed for NixOS.
optionsDoc = pkgs.nixosOptionsDoc {
inherit (pkgs.lib.evalModules { modules = [ ../../pkgs/top-level/config.nix ]; }) options;
inherit (pkgs.lib.evalModules {
modules = [ ../../pkgs/top-level/config.nix ];
specialArgs.class = "nixpkgsConfig";
}) options;
documentType = "none";
transformOptions = opt:
opt // {

View File

@ -33,6 +33,7 @@ let
];
specialArgs = {
inherit config pkgs utils;
class = "nixos";
};
};
docs = import "${nixosPath}/doc/manual" {

View File

@ -40,7 +40,9 @@ let
inherit prefix modules;
specialArgs = {
modulesPath = builtins.toString ../modules;
} // specialArgs;
} // specialArgs // {
class = "nixos";
};
};
in

View File

@ -1,7 +1,10 @@
{ lib }:
let
evalTest = module: lib.evalModules { modules = testModules ++ [ module ]; };
evalTest = module: lib.evalModules {
modules = testModules ++ [ module ];
specialArgs.class = "nixosTest";
};
runTest = module: (evalTest ({ config, ... }: { imports = [ module ]; result = config.test; })).config.result;
testModules = [

View File

@ -39,6 +39,7 @@ let
_module.check = false;
} ] ++ docModules.eager;
specialArgs = specialArgs // {
class = "nixos";
pkgs = scrubDerivations "pkgs" pkgs;
# allow access to arbitrary options for eager modules, eg for getting
# option types from lazy modules

View File

@ -82,6 +82,7 @@ in let
config = config1;
})
];
specialArgs.class = "nixpkgsConfig";
};
# take all the rest as-is