How to move `nixosModules` to a separate file?

I’m trying to move some stuff out of flake.nix for readability. What I’ve tried:

  • Change nixosModules = { foo = import modules/foo.nix; […] }; to nixosModules = import ./modules;
  • Copy { foo = import modules/foo.nix; […] } verbatim into modules/default.nix and substituting modules with ..

The problem is that now nix flake check complains about the module structure:

error: In module /nix/store/[…]-source/modules/default.nix, you’re trying to define a value of type lambda rather than an attribute set for the option foo!

So it looks like I’d have to massively complicate things, adding arguments to both the flake.nix import and to modules/default.nix. Is there a simpler way, more similar to how imports works?

Seems as if your CI setup was the fault. The following change made nix flake check succeed.

I also introduced some smaller change to keep some context for the modules that improve module errors as well as enable deduplication.

diff --git a/.ci/full-configuration.nix b/.ci/full-configuration.nix
index 6c3f8a79f2..2700255486 100644
--- a/.ci/full-configuration.nix
+++ b/.ci/full-configuration.nix
@@ -5,7 +5,7 @@
   ...
 }:
 {
-  imports = lib.lists.filter (path: lib.hasSuffix ".nix" path) (
+  imports = lib.lists.filter (path: lib.hasSuffix ".nix" path && !lib.hasSuffix "default.nix" path) (
     lib.filesystem.listFilesRecursive ../modules
   );
   config = {
diff --git a/flake.nix b/flake.nix
index bbf9ece52c..8da3f3b0c7 100644
--- a/flake.nix
+++ b/flake.nix
@@ -45,24 +45,7 @@
       checks.${system} = import ./tests { inherit pkgs; };
       devShells.${system}.default = import ./shell.nix { inherit pkgs; };
       formatter.${system} = ((import treefmt-nix).evalModule pkgs ./treefmt.nix).config.build.wrapper;
-      nixosModules = {
-        android-backup-service = import ./modules/android-backup-service.nix;
-        defaults = import ./modules/defaults.nix;
-        deployer = import ./modules/deployer.nix;
-        dev = import ./modules/dev.nix;
-        external-webcam = import ./modules/external-webcam.nix;
-        gaming = import ./modules/gaming.nix;
-        gui = import ./modules/gui.nix;
-        home-only = import ./modules/home-only.nix;
-        pijul = import ./modules/pijul.nix;
-        photography = import ./modules/photography.nix;
-        prometheus-exporter = import ./modules/prometheus-exporter.nix;
-        ssh-client = import ./modules/ssh-client.nix;
-        ssh-server = import ./modules/ssh-server.nix;
-        thunderbird = import ./modules/thunderbird.nix;
-        tor = import ./modules/tor.nix;
-        xdg = import ./modules/xdg.nix;
-      };
+      nixosModules = import ./modules;
       nixosConfigurations = {
         ci-full-stable = nixpkgs-stable.lib.nixosSystem {
           inherit system;
diff --git a/modules/default.nix b/modules/default.nix
new file mode 100644
index 0000000000..f502e841b1
--- /dev/null
+++ b/modules/default.nix
@@ -0,0 +1,18 @@
+{
+  android-backup-service = ./android-backup-service.nix;
+  defaults = ./defaults.nix;
+  deployer = ./deployer.nix;
+  dev = ./dev.nix;
+  external-webcam = ./external-webcam.nix;
+  gaming = ./gaming.nix;
+  gui = ./gui.nix;
+  home-only = ./home-only.nix;
+  pijul = ./pijul.nix;
+  photography = ./photography.nix;
+  prometheus-exporter = ./prometheus-exporter.nix;
+  ssh-client = ./ssh-client.nix;
+  ssh-server = ./ssh-server.nix;
+  thunderbird = ./thunderbird.nix;
+  tor = ./tor.nix;
+  xdg = ./xdg.nix;
+}
1 Like

Thank you! I did exclude default.nix (forgot it in the description), but I didn’t know I had to get rid of the imports in modules/default.nix.

as a reference for what @NobbZ has shown you i’ll include this article by @fzakaria - Import but don’t import your NixOS modules | Farid Zakaria’s Blog

1 Like

Removing the imports was simple cleaning, it was not necessary at all. Excluding default.nix in the CI module was the important part.

1 Like