lol

lib/modules: Check against importing things with a _type

+20 -1
+8 -1
lib/modules.nix
··· 362 362 363 363 # Like unifyModuleSyntax, but also imports paths and calls functions if necessary 364 364 loadModule = args: fallbackFile: fallbackKey: m: 365 - if isFunction m || isAttrs m then 365 + if isFunction m then 366 366 unifyModuleSyntax fallbackFile fallbackKey (applyModuleArgsIfFunction fallbackKey m args) 367 + else if isAttrs m then 368 + if m._type or "module" == "module" then 369 + unifyModuleSyntax fallbackFile fallbackKey (applyModuleArgsIfFunction fallbackKey m args) 370 + else if m._type == "if" || m._type == "override" then 371 + loadModule args fallbackFile fallbackKey { config = m; } 372 + else 373 + throw "Could not load a value as a module, because it is of type ${lib.strings.escapeNixString m._type}${lib.optionalString (fallbackFile != null) ", in file ${toString fallbackFile}."}" 367 374 else if isList m then 368 375 let defs = [{ file = fallbackFile; value = m; }]; in 369 376 throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
+4
lib/tests/modules.sh
··· 365 365 checkConfigError 'The module .*/module-class-is-darwin.nix was imported into nixos instead of darwin.' config.fail.config ./class-check.nix 366 366 checkConfigError 'The module foo.nix#darwinModules.default was imported into nixos instead of darwin.' config.fail-anon.config ./class-check.nix 367 367 368 + # _type check 369 + checkConfigError 'Could not load a value as a module, because it is of type "flake", in file .*/module-imports-_type-check.nix' config.ok.config ./module-imports-_type-check.nix 370 + checkConfigOutput '^true$' "$@" config.enable ./declare-enable.nix ./define-enable-with-top-level-mkIf.nix 371 + 368 372 # doRename works when `warnings` does not exist. 369 373 checkConfigOutput '^1234$' config.c.d.e ./doRename-basic.nix 370 374 # doRename adds a warning.
+5
lib/tests/modules/define-enable-with-top-level-mkIf.nix
··· 1 + { lib, ... }: 2 + # I think this might occur more realistically in a submodule 3 + { 4 + imports = [ (lib.mkIf true { enable = true; }) ]; 5 + }
+3
lib/tests/modules/module-imports-_type-check.nix
··· 1 + { 2 + imports = [ { _type = "flake"; } ]; 3 + }