Check flake.nixosModules

Hello,

In a flake.nix, let say that I write in the outputs:

nixosModules.default = { config, lib, pkgs, ... }: 
{
   config = {
      services.nginx.virtualHosts."${unexistingVariable}" = {}
   }  
}

I am writing this code by hand I did not check it, but, how would I test that the nixosModules.default compiles?

How to make it yield an error ? Apart from using a nixos-rebuild.

I am looking for nix-instanciate ot nix eval command probably.

Thanks

You would need to add this to a config. Then nixos-rebuild <subcommand>.
For testing you can use a smaller config that’d eval faster.

Modules do not make sense to evaluate in isolation; they have to be passed to lib.evalModules (lib.nixosSystem).

You could also write a test for your module and add that to checks - then nix flake check would also run your test, and as part of the test run the module would need to be evaluated.

I’d recommend using a nested flake for that, so you don’t need to include nixpkgs in your inputs, assuming you only provide nixosModules.

Thank you, I’ll do that!