lib/modules: Move class out of specialArgs

This commit is contained in:
Robert Hensing 2023-04-17 19:48:53 +02:00
parent 8f02e95aff
commit 8054785157
9 changed files with 39 additions and 22 deletions

View File

@ -47,7 +47,7 @@ let
optionsDoc = pkgs.nixosOptionsDoc { optionsDoc = pkgs.nixosOptionsDoc {
inherit (pkgs.lib.evalModules { inherit (pkgs.lib.evalModules {
modules = [ ../../pkgs/top-level/config.nix ]; modules = [ ../../pkgs/top-level/config.nix ];
specialArgs.class = "nixpkgsConfig"; class = "nixpkgsConfig";
}) options; }) options;
documentType = "none"; documentType = "none";
transformOptions = opt: transformOptions = opt:

View File

@ -28,11 +28,11 @@ An attribute set of module arguments that can be used in `imports`.
This is in contrast to `config._module.args`, which is only available after all `imports` have been resolved. This is in contrast to `config._module.args`, which is only available after all `imports` have been resolved.
#### `specialArgs.class` {#module-system-lib-evalModules-param-specialArgs-class} #### `class` {#module-system-lib-evalModules-param-class}
If the `class` attribute is set in `specialArgs`, the module system will reject modules with a different `class`. If the `class` attribute is set and non-`null`, the module system will reject `imports` with a different `class`.
The `class` value should be in lower [camel case](https://en.wikipedia.org/wiki/Camel_case). The `class` value should be a string in lower [camel case](https://en.wikipedia.org/wiki/Camel_case).
If applicable, the `class` should match the "prefix" of the attributes used in (experimental) [flakes](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#description). Some examples are: If applicable, the `class` should match the "prefix" of the attributes used in (experimental) [flakes](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#description). Some examples are:

View File

@ -78,13 +78,13 @@ let
# when resolving module structure (like in imports). For everything else, # when resolving module structure (like in imports). For everything else,
# there's _module.args. If specialArgs.modulesPath is defined it will be # there's _module.args. If specialArgs.modulesPath is defined it will be
# used as the base path for disabledModules. # used as the base path for disabledModules.
# specialArgs ? {}
# `specialArgs.class`: , # `class`:
# A nominal type for modules. When set and non-null, this adds a check to # A nominal type for modules. When set and non-null, this adds a check to
# make sure that only compatible modules are imported. # make sure that only compatible modules are imported.
specialArgs ? {} # This would be remove in the future, Prefer _module.args option instead.
, # This would be remove in the future, Prefer _module.args option instead. class ? null
args ? {} , args ? {}
, # This would be remove in the future, Prefer _module.check option instead. , # This would be remove in the future, Prefer _module.check option instead.
check ? true check ? true
}: }:
@ -220,6 +220,16 @@ let
within a configuration, but can be used in module imports. within a configuration, but can be used in module imports.
''; '';
}; };
_module.class = mkOption {
readOnly = true;
internal = true;
description = lib.mdDoc ''
If the `class` attribute is set and non-`null`, the module system will reject `imports` with a different `class`.
This option contains the expected `class` attribute of the current module evaluation.
'';
};
}; };
config = { config = {
@ -227,13 +237,14 @@ let
inherit extendModules; inherit extendModules;
moduleType = type; moduleType = type;
}; };
_module.class = class;
_module.specialArgs = specialArgs; _module.specialArgs = specialArgs;
}; };
}; };
merged = merged =
let collected = collectModules let collected = collectModules
(specialArgs.class or null) class
(specialArgs.modulesPath or "") (specialArgs.modulesPath or "")
(regularModules ++ [ internalModule ]) (regularModules ++ [ internalModule ])
({ inherit lib options config specialArgs; } // specialArgs); ({ inherit lib options config specialArgs; } // specialArgs);
@ -310,13 +321,14 @@ let
prefix ? [], prefix ? [],
}: }:
evalModules (evalModulesArgs // { evalModules (evalModulesArgs // {
inherit class;
modules = regularModules ++ modules; modules = regularModules ++ modules;
specialArgs = evalModulesArgs.specialArgs or {} // specialArgs; specialArgs = evalModulesArgs.specialArgs or {} // specialArgs;
prefix = extendArgs.prefix or evalModulesArgs.prefix or []; prefix = extendArgs.prefix or evalModulesArgs.prefix or [];
}); });
type = lib.types.submoduleWith { type = lib.types.submoduleWith {
inherit modules specialArgs; inherit modules specialArgs class;
}; };
result = withWarnings { result = withWarnings {

View File

@ -3,7 +3,7 @@
_module.freeformType = lib.types.anything; _module.freeformType = lib.types.anything;
ok = ok =
lib.evalModules { lib.evalModules {
specialArgs.class = "nixos"; class = "nixos";
modules = [ modules = [
./module-class-is-nixos.nix ./module-class-is-nixos.nix
]; ];
@ -11,7 +11,7 @@
fail = fail =
lib.evalModules { lib.evalModules {
specialArgs.class = "nixos"; class = "nixos";
modules = [ modules = [
./module-class-is-nixos.nix ./module-class-is-nixos.nix
./module-class-is-darwin.nix ./module-class-is-darwin.nix
@ -20,7 +20,7 @@
fail-anon = fail-anon =
lib.evalModules { lib.evalModules {
specialArgs.class = "nixos"; class = "nixos";
modules = [ modules = [
./module-class-is-nixos.nix ./module-class-is-nixos.nix
{ _file = "foo.nix#darwinModules.default"; { _file = "foo.nix#darwinModules.default";

View File

@ -696,6 +696,7 @@ rec {
, specialArgs ? {} , specialArgs ? {}
, shorthandOnlyDefinesConfig ? false , shorthandOnlyDefinesConfig ? false
, description ? null , description ? null
, class ? null
}@attrs: }@attrs:
let let
inherit (lib.modules) evalModules; inherit (lib.modules) evalModules;
@ -707,7 +708,7 @@ rec {
) defs; ) defs;
base = evalModules { base = evalModules {
inherit specialArgs; inherit class specialArgs;
modules = [{ modules = [{
# This is a work-around for the fact that some sub-modules, # This is a work-around for the fact that some sub-modules,
# such as the one included in an attribute set, expects an "args" # such as the one included in an attribute set, expects an "args"
@ -762,9 +763,14 @@ rec {
functor = defaultFunctor name // { functor = defaultFunctor name // {
type = types.submoduleWith; type = types.submoduleWith;
payload = { payload = {
inherit modules specialArgs shorthandOnlyDefinesConfig description; inherit modules class specialArgs shorthandOnlyDefinesConfig description;
}; };
binOp = lhs: rhs: { binOp = lhs: rhs: {
class =
if lhs.class == null then rhs.class
else if rhs.class == null then lhs.class
else if lhs.class == rhs.class then lhs.class
else throw "A submoduleWith option is declared multiple times with conflicting class values \"${toString lhs.class}\" and \"${toString rhs.class}\".";
modules = lhs.modules ++ rhs.modules; modules = lhs.modules ++ rhs.modules;
specialArgs = specialArgs =
let intersecting = builtins.intersectAttrs lhs.specialArgs rhs.specialArgs; let intersecting = builtins.intersectAttrs lhs.specialArgs rhs.specialArgs;

View File

@ -38,11 +38,10 @@ let
# is experimental. # is experimental.
lib.evalModules { lib.evalModules {
inherit prefix modules; inherit prefix modules;
class = "nixos";
specialArgs = { specialArgs = {
modulesPath = builtins.toString ../modules; modulesPath = builtins.toString ../modules;
} // specialArgs // { } // specialArgs;
class = "nixos";
};
}; };
in in

View File

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

View File

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

View File

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