diff options
author | Robert Hensing <robert@roberthensing.nl> | 2023-04-17 19:48:53 +0200 |
---|---|---|
committer | Robert Hensing <robert@roberthensing.nl> | 2023-05-06 18:32:58 +0200 |
commit | 8054785157119ea12e526481924d6676427904bb (patch) | |
tree | d62be0513289427454c94acc1ef86444249c21f8 /lib | |
parent | 8f02e95aff2b9cb94470ec379e2a3f55858cb03d (diff) |
lib/modules: Move class out of specialArgs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/modules.nix | 26 | ||||
-rw-r--r-- | lib/tests/modules/class-check.nix | 6 | ||||
-rw-r--r-- | lib/types.nix | 10 |
3 files changed, 30 insertions, 12 deletions
diff --git a/lib/modules.nix b/lib/modules.nix index e83d5b6d1caec..3550ebddaeac9 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -78,13 +78,13 @@ let # when resolving module structure (like in imports). For everything else, # there's _module.args. If specialArgs.modulesPath is defined it will be # used as the base path for disabledModules. - # - # `specialArgs.class`: + specialArgs ? {} + , # `class`: # A nominal type for modules. When set and non-null, this adds a check to # make sure that only compatible modules are imported. - specialArgs ? {} - , # This would be remove in the future, Prefer _module.args option instead. - args ? {} + # This would be remove in the future, Prefer _module.args option instead. + class ? null + , args ? {} , # This would be remove in the future, Prefer _module.check option instead. check ? true }: @@ -220,6 +220,16 @@ let 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 = { @@ -227,13 +237,14 @@ let inherit extendModules; moduleType = type; }; + _module.class = class; _module.specialArgs = specialArgs; }; }; merged = let collected = collectModules - (specialArgs.class or null) + class (specialArgs.modulesPath or "") (regularModules ++ [ internalModule ]) ({ inherit lib options config specialArgs; } // specialArgs); @@ -310,13 +321,14 @@ let prefix ? [], }: evalModules (evalModulesArgs // { + inherit class; modules = regularModules ++ modules; specialArgs = evalModulesArgs.specialArgs or {} // specialArgs; prefix = extendArgs.prefix or evalModulesArgs.prefix or []; }); type = lib.types.submoduleWith { - inherit modules specialArgs; + inherit modules specialArgs class; }; result = withWarnings { diff --git a/lib/tests/modules/class-check.nix b/lib/tests/modules/class-check.nix index f492c844abfbb..02d1431cc88b6 100644 --- a/lib/tests/modules/class-check.nix +++ b/lib/tests/modules/class-check.nix @@ -3,7 +3,7 @@ _module.freeformType = lib.types.anything; ok = lib.evalModules { - specialArgs.class = "nixos"; + class = "nixos"; modules = [ ./module-class-is-nixos.nix ]; @@ -11,7 +11,7 @@ fail = lib.evalModules { - specialArgs.class = "nixos"; + class = "nixos"; modules = [ ./module-class-is-nixos.nix ./module-class-is-darwin.nix @@ -20,7 +20,7 @@ fail-anon = lib.evalModules { - specialArgs.class = "nixos"; + class = "nixos"; modules = [ ./module-class-is-nixos.nix { _file = "foo.nix#darwinModules.default"; diff --git a/lib/types.nix b/lib/types.nix index 666e6502d161b..e0da18a2febb9 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -696,6 +696,7 @@ rec { , specialArgs ? {} , shorthandOnlyDefinesConfig ? false , description ? null + , class ? null }@attrs: let inherit (lib.modules) evalModules; @@ -707,7 +708,7 @@ rec { ) defs; base = evalModules { - inherit specialArgs; + inherit class specialArgs; 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" @@ -762,9 +763,14 @@ rec { functor = defaultFunctor name // { type = types.submoduleWith; payload = { - inherit modules specialArgs shorthandOnlyDefinesConfig description; + inherit modules class specialArgs shorthandOnlyDefinesConfig description; }; 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; specialArgs = let intersecting = builtins.intersectAttrs lhs.specialArgs rhs.specialArgs; |