Error when trying to modularize NIxOS config: Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}

So I was trying to break my large configuration.nix into smaller and more manageable config files using flakes when I get this error:

rminstrel@hp-15s-du1015tu:~/.config/nixos$ sudo nixos-rebuild switch --flake .
error:
       … while calling the 'seq' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:334:18:
          333|         options = checked options;
          334|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          335|         _module = checked (config._module);

       … while evaluating a branch condition
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:273:9:
          272|       checkUnmatched =
          273|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |         ^
          274|           let

       … in the left operand of the AND (&&) operator
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:273:72:
          272|       checkUnmatched =
          273|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |                                                                        ^
          274|           let

       … in the left operand of the AND (&&) operator
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:273:33:
          272|       checkUnmatched =
          273|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |                                 ^
          274|           let

       … while evaluating a branch condition
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:266:12:
          265|
          266|         in if declaredConfig._module.freeformType == null then declaredConfig
             |            ^
          267|           # Because all definitions that had an associated option ended in

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:254:28:
          253|           # For definitions that have an associated option
          254|           declaredConfig = mapAttrsRecursiveCond (v: ! isOption v) (_: v: v.value) options;
             |                            ^
          255|

       … while calling 'mapAttrsRecursiveCond'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/attrsets.nix:1200:5:
         1199|     f:
         1200|     set:
             |     ^
         1201|     let

       … while calling the 'mapAttrs' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/attrsets.nix:1209:5:
         1208|     in
         1209|     recurse [ ] set;
             |     ^
         1210|

       … while evaluating the attribute 'matchedOptions'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:699:15:
          698|     in {
          699|       inherit matchedOptions;
             |               ^
          700|

       … while calling the 'mapAttrs' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:690:24:
          689|
          690|       matchedOptions = mapAttrs (n: v: v.matchedOptions) resultsByName;
             |                        ^
          691|

       … while calling the 'mapAttrs' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:643:23:
          642|
          643|       resultsByName = mapAttrs (name: decls:
             |                       ^
          644|         # We're descending into attribute ‘name’.

       … while calling the 'zipAttrsWith' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:560:9:
          559|       declsByName =
          560|         zipAttrsWith
             |         ^
          561|           (n: concatLists)

       … while calling the 'map' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:562:12:
          561|           (n: concatLists)
          562|           (map
             |            ^
          563|             (module: let subtree = module.options; in

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:246:33:
          245|           ({ inherit lib options config specialArgs; } // specialArgs);
          246|         in mergeModules prefix (reverseList collected);
             |                                 ^
          247|

       … while calling 'reverseList'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/lists.nix:1117:17:
         1116|   */
         1117|   reverseList = xs:
             |                 ^
         1118|     let l = length xs; in genList (n: elemAt xs (l - n - 1)) l;

       … while calling the 'genList' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/lists.nix:1118:27:
         1117|   reverseList = xs:
         1118|     let l = length xs; in genList (n: elemAt xs (l - n - 1)) l;
             |                           ^
         1119|

       … while evaluating the second argument passed to builtins.genList

       … while calling the 'length' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/lists.nix:1118:13:
         1117|   reverseList = xs:
         1118|     let l = length xs; in genList (n: elemAt xs (l - n - 1)) l;
             |             ^
         1119|

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:241:25:
          240|       merged =
          241|         let collected = collectModules
             |                         ^
          242|           class

       … while calling anonymous lambda
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:459:37:
          458|
          459|     in modulesPath: initialModules: args:
             |                                     ^
          460|       filterModules modulesPath (collectStructuredModules unknownModule "" initialModules args);

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:460:7:
          459|     in modulesPath: initialModules: args:
          460|       filterModules modulesPath (collectStructuredModules unknownModule "" initialModules args);
             |       ^
          461|

       … while calling 'filterModules'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:427:36:
          426|       # modules recursively. It returns the final list of unique-by-key modules
          427|       filterModules = modulesPath: { disabled, modules }:
             |                                    ^
          428|         let

       … while calling the 'map' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:454:12:
          453|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);
          454|         in map (attrs: attrs.module) (genericClosure {
             |            ^
          455|           startSet = keyFilter modules;

       … while calling the 'genericClosure' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:454:39:
          453|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);
          454|         in map (attrs: attrs.module) (genericClosure {
             |                                       ^
          455|           startSet = keyFilter modules;

       … while calling the 'filter' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:455:22:
          454|         in map (attrs: attrs.module) (genericClosure {
          455|           startSet = keyFilter modules;
             |                      ^
          456|           operator = attrs: keyFilter attrs.modules;

       … while calling anonymous lambda
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:453:31:
          452|           disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled;
          453|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);
             |                               ^
          454|         in map (attrs: attrs.module) (genericClosure {

       … in the argument of the not operator
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:453:40:
          452|           disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled;
          453|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);
             |                                        ^
          454|         in map (attrs: attrs.module) (genericClosure {

       … while calling the 'elem' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:453:40:
          452|           disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled;
          453|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);
             |                                        ^
          454|         in map (attrs: attrs.module) (genericClosure {

       … while calling the 'concatMap' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:452:26:
          451|
          452|           disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled;
             |                          ^
          453|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);

       … while calling the 'concatLists' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:409:24:
          408|           collectResults = modules: {
          409|             disabled = concatLists (catAttrs "disabled" modules);
             |                        ^
          410|             inherit modules;

       … while evaluating the attribute 'disabled'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:409:13:
          408|           collectResults = modules: {
          409|             disabled = concatLists (catAttrs "disabled" modules);
             |             ^
          410|             inherit modules;

       … while calling the 'concatLists' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:409:24:
          408|           collectResults = modules: {
          409|             disabled = concatLists (catAttrs "disabled" modules);
             |                        ^
          410|             inherit modules;

       … while evaluating a branch condition
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:420:25:
          419|             modules = collectedImports.modules;
          420|             disabled = (if module.disabledModules != [] then [{ file = module._file; disabled = module.disabledModules; }] else []) ++ collectedImports.disabled;
             |                         ^
          421|           }) initialModules);

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:414:22:
          413|           let
          414|             module = checkModule (loadModule args parentFile "${parentKey}:anon-${toString n}" x);
             |                      ^
          415|             collectedImports = collectStructuredModules module._file module.key module.imports args;

       … while calling anonymous lambda
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:366:11:
          365|         then
          366|           m:
             |           ^
          367|             if m._class == null || m._class == class

       … while evaluating a branch condition
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:367:13:
          366|           m:
          367|             if m._class == null || m._class == class
             |             ^
          368|             then m

       … in the left operand of the OR (||) operator
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:367:33:
          366|           m:
          367|             if m._class == null || m._class == class
             |                                 ^
          368|             then m

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:414:35:
          413|           let
          414|             module = checkModule (loadModule args parentFile "${parentKey}:anon-${toString n}" x);
             |                                   ^
          415|             collectedImports = collectStructuredModules module._file module.key module.imports args;

       … while calling 'loadModule'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:348:53:
          347|       # Like unifyModuleSyntax, but also imports paths and calls functions if necessary
          348|       loadModule = args: fallbackFile: fallbackKey: m:
             |                                                     ^
          349|         if isFunction m then

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:361:14:
          360|           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
          361|         else unifyModuleSyntax (toString m) (toString m) (applyModuleArgsIfFunction (toString m) (import m) args);
             |              ^
          362|

       … while calling 'unifyModuleSyntax'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:468:34:
          467|      of ‘options’, ‘config’ and ‘imports’ attributes. */
          468|   unifyModuleSyntax = file: key: m:
             |                                  ^
          469|     let

       … while evaluating a branch condition
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:477:5:
          476|     in
          477|     if m ? config || m ? options then
             |     ^
          478|       let badAttrs = removeAttrs m ["_class" "_file" "key" "disabledModules" "imports" "options" "config" "meta" "freeformType"]; in

       … in the left operand of the OR (||) operator
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:477:19:
          476|     in
          477|     if m ? config || m ? options then
             |                   ^
          478|       let badAttrs = removeAttrs m ["_class" "_file" "key" "disabledModules" "imports" "options" "config" "meta" "freeformType"]; in

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:361:59:
          360|           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
          361|         else unifyModuleSyntax (toString m) (toString m) (applyModuleArgsIfFunction (toString m) (import m) args);
             |                                                           ^
          362|

       … while calling 'applyModuleArgsIfFunction'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:502:39:
          501|
          502|   applyModuleArgsIfFunction = key: f: args@{ config, ... }:
             |                                       ^
          503|     if isFunction f then applyModuleArgs key f args else f;

       … while evaluating a branch condition
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:503:5:
          502|   applyModuleArgsIfFunction = key: f: args@{ config, ... }:
          503|     if isFunction f then applyModuleArgs key f args else f;
             |     ^
          504|

       … from call site
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:503:8:
          502|   applyModuleArgsIfFunction = key: f: args@{ config, ... }:
          503|     if isFunction f then applyModuleArgs key f args else f;
             |        ^
          504|

       … while calling 'isFunction'
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/trivial.nix:1034:16:
         1033|   */
         1034|   isFunction = f: builtins.isFunction f ||
             |                ^
         1035|     (f ? __functor && isFunction (f.__functor f));

       … in the left operand of the OR (||) operator
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/trivial.nix:1034:41:
         1033|   */
         1034|   isFunction = f: builtins.isFunction f ||
             |                                         ^
         1035|     (f ? __functor && isFunction (f.__functor f));

       … while calling the 'isFunction' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/trivial.nix:1034:19:
         1033|   */
         1034|   isFunction = f: builtins.isFunction f ||
             |                   ^
         1035|     (f ? __functor && isFunction (f.__functor f));

       … while calling the 'import' builtin
         at /nix/store/s1fbk6a410xn8vcaj54iqv22agyn0ria-source/lib/modules.nix:361:99:
          360|           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
          361|         else unifyModuleSyntax (toString m) (toString m) (applyModuleArgsIfFunction (toString m) (import m) args);
             |                                                                                                   ^
          362|

       error: path '/nix/store/6cxab26wm0q3b1ry49h4bybl1vvgi9rf-source/configuration.nix' does not exist

What do I do? My NIxOS configuration is available at this Github repo and the commits 2593834d, 23b6f54f and 1ea4bca2 are broken as of recent.

That’s not the error I get.

First, your paths are off, they’re relative to the file you’re adding the imports to:

diff --git a/flake.nix b/flake.nix
index 1685ba8..0591302 100644
--- a/flake.nix
+++ b/flake.nix
@@ -37,13 +37,13 @@
       system = "x86_64-linux";
       specialArgs = { inherit inputs; };
       modules = [
-        ./system-configuration.nix
-        ./desktop-configuration.nix
-        ./flatpak-configuration.nix
-        ./hardware-configuration.nix
-        ./software-configuration.nix
-        ./style-configuration.nix
-        ./user-configuration.nix
+        ./modules/system-configuration.nix
+        ./modules/desktop-configuration.nix
+        ./modules/flatpak-configuration.nix
+        ./modules/hardware-configuration.nix
+        ./modules/software-configuration.nix
+        ./modules/style-configuration.nix
+        ./modules/user-configuration.nix
         chaotic.nixosModules.hp-15s-du1015tu
         nix-flatpak.nixosModules.nix-flatpak
         # inputs.home-manager.nixosModules.hp-15s-du1015tu

Besides that, the chaotic repo doesn’t appear to have such an output named nixosModules.hp-15s-du1015tu, so you’ll have to fix that.

This is the actual error, at the bottom:

This is from something else:

Notice all the context around it:

This part is just showing the code surrounding the part that actually failed. The ^ character is indicating the expression that actually fails to evaluate, which is not the unrelated throw expression above it.

So yes, @waffle8946 is right; the issue is that you’re using the wrong paths

Weird thing is that I actually put the paths as ./modules/<subconfig>-configuration.nix as you have given in flake.nix and it threw the same error. Welp, maybe some subconfig nix file was in the main directory because for some reason VSCodium doesn’t automatically focus to the text but rather to the file pane and that might have misplaced the files. I really couldn’t tell as it was 1:30 AM at night and my brain was shutting down atp. Anyways, that fixed my configuration this time around, so thanks for the help!