lol

lib/modules: Move class out of specialArgs

+39 -22
+1 -1
doc/doc-support/default.nix
··· 47 47 optionsDoc = pkgs.nixosOptionsDoc { 48 48 inherit (pkgs.lib.evalModules { 49 49 modules = [ ../../pkgs/top-level/config.nix ]; 50 - specialArgs.class = "nixpkgsConfig"; 50 + class = "nixpkgsConfig"; 51 51 }) options; 52 52 documentType = "none"; 53 53 transformOptions = opt:
+3 -3
doc/module-system/module-system.chapter.md
··· 28 28 29 29 This is in contrast to `config._module.args`, which is only available after all `imports` have been resolved. 30 30 31 - #### `specialArgs.class` {#module-system-lib-evalModules-param-specialArgs-class} 31 + #### `class` {#module-system-lib-evalModules-param-class} 32 32 33 - If the `class` attribute is set in `specialArgs`, the module system will reject modules with a different `class`. 33 + If the `class` attribute is set and non-`null`, the module system will reject `imports` with a different `class`. 34 34 35 - The `class` value should be in lower [camel case](https://en.wikipedia.org/wiki/Camel_case). 35 + The `class` value should be a string in lower [camel case](https://en.wikipedia.org/wiki/Camel_case). 36 36 37 37 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: 38 38
+19 -7
lib/modules.nix
··· 78 78 # when resolving module structure (like in imports). For everything else, 79 79 # there's _module.args. If specialArgs.modulesPath is defined it will be 80 80 # used as the base path for disabledModules. 81 - # 82 - # `specialArgs.class`: 81 + specialArgs ? {} 82 + , # `class`: 83 83 # A nominal type for modules. When set and non-null, this adds a check to 84 84 # make sure that only compatible modules are imported. 85 - specialArgs ? {} 86 - , # This would be remove in the future, Prefer _module.args option instead. 87 - args ? {} 85 + # This would be remove in the future, Prefer _module.args option instead. 86 + class ? null 87 + , args ? {} 88 88 , # This would be remove in the future, Prefer _module.check option instead. 89 89 check ? true 90 90 }: ··· 220 220 within a configuration, but can be used in module imports. 221 221 ''; 222 222 }; 223 + 224 + _module.class = mkOption { 225 + readOnly = true; 226 + internal = true; 227 + description = lib.mdDoc '' 228 + If the `class` attribute is set and non-`null`, the module system will reject `imports` with a different `class`. 229 + 230 + This option contains the expected `class` attribute of the current module evaluation. 231 + ''; 232 + }; 223 233 }; 224 234 225 235 config = { ··· 227 237 inherit extendModules; 228 238 moduleType = type; 229 239 }; 240 + _module.class = class; 230 241 _module.specialArgs = specialArgs; 231 242 }; 232 243 }; 233 244 234 245 merged = 235 246 let collected = collectModules 236 - (specialArgs.class or null) 247 + class 237 248 (specialArgs.modulesPath or "") 238 249 (regularModules ++ [ internalModule ]) 239 250 ({ inherit lib options config specialArgs; } // specialArgs); ··· 310 321 prefix ? [], 311 322 }: 312 323 evalModules (evalModulesArgs // { 324 + inherit class; 313 325 modules = regularModules ++ modules; 314 326 specialArgs = evalModulesArgs.specialArgs or {} // specialArgs; 315 327 prefix = extendArgs.prefix or evalModulesArgs.prefix or []; 316 328 }); 317 329 318 330 type = lib.types.submoduleWith { 319 - inherit modules specialArgs; 331 + inherit modules specialArgs class; 320 332 }; 321 333 322 334 result = withWarnings {
+3 -3
lib/tests/modules/class-check.nix
··· 3 3 _module.freeformType = lib.types.anything; 4 4 ok = 5 5 lib.evalModules { 6 - specialArgs.class = "nixos"; 6 + class = "nixos"; 7 7 modules = [ 8 8 ./module-class-is-nixos.nix 9 9 ]; ··· 11 11 12 12 fail = 13 13 lib.evalModules { 14 - specialArgs.class = "nixos"; 14 + class = "nixos"; 15 15 modules = [ 16 16 ./module-class-is-nixos.nix 17 17 ./module-class-is-darwin.nix ··· 20 20 21 21 fail-anon = 22 22 lib.evalModules { 23 - specialArgs.class = "nixos"; 23 + class = "nixos"; 24 24 modules = [ 25 25 ./module-class-is-nixos.nix 26 26 { _file = "foo.nix#darwinModules.default";
+8 -2
lib/types.nix
··· 696 696 , specialArgs ? {} 697 697 , shorthandOnlyDefinesConfig ? false 698 698 , description ? null 699 + , class ? null 699 700 }@attrs: 700 701 let 701 702 inherit (lib.modules) evalModules; ··· 707 708 ) defs; 708 709 709 710 base = evalModules { 710 - inherit specialArgs; 711 + inherit class specialArgs; 711 712 modules = [{ 712 713 # This is a work-around for the fact that some sub-modules, 713 714 # such as the one included in an attribute set, expects an "args" ··· 762 763 functor = defaultFunctor name // { 763 764 type = types.submoduleWith; 764 765 payload = { 765 - inherit modules specialArgs shorthandOnlyDefinesConfig description; 766 + inherit modules class specialArgs shorthandOnlyDefinesConfig description; 766 767 }; 767 768 binOp = lhs: rhs: { 769 + class = 770 + if lhs.class == null then rhs.class 771 + else if rhs.class == null then lhs.class 772 + else if lhs.class == rhs.class then lhs.class 773 + else throw "A submoduleWith option is declared multiple times with conflicting class values \"${toString lhs.class}\" and \"${toString rhs.class}\"."; 768 774 modules = lhs.modules ++ rhs.modules; 769 775 specialArgs = 770 776 let intersecting = builtins.intersectAttrs lhs.specialArgs rhs.specialArgs;
+2 -3
nixos/lib/eval-config-minimal.nix
··· 38 38 # is experimental. 39 39 lib.evalModules { 40 40 inherit prefix modules; 41 + class = "nixos"; 41 42 specialArgs = { 42 43 modulesPath = builtins.toString ../modules; 43 - } // specialArgs // { 44 - class = "nixos"; 45 - }; 44 + } // specialArgs; 46 45 }; 47 46 48 47 in
+1 -1
nixos/lib/testing/default.nix
··· 3 3 4 4 evalTest = module: lib.evalModules { 5 5 modules = testModules ++ [ module ]; 6 - specialArgs.class = "nixosTest"; 6 + class = "nixosTest"; 7 7 }; 8 8 runTest = module: (evalTest ({ config, ... }: { imports = [ module ]; result = config.test; })).config.result; 9 9
+1 -1
nixos/modules/misc/documentation.nix
··· 38 38 modules = [ { 39 39 _module.check = false; 40 40 } ] ++ docModules.eager; 41 + class = "nixos"; 41 42 specialArgs = specialArgs // { 42 - class = "nixos"; 43 43 pkgs = scrubDerivations "pkgs" pkgs; 44 44 # allow access to arbitrary options for eager modules, eg for getting 45 45 # option types from lazy modules
+1 -1
pkgs/top-level/default.nix
··· 82 82 config = config1; 83 83 }) 84 84 ]; 85 - specialArgs.class = "nixpkgsConfig"; 85 + class = "nixpkgsConfig"; 86 86 }; 87 87 88 88 # take all the rest as-is