Conditionally import module if it exists?

I’m trying to figure out how to conditionally import a module if it exists in the path, but I think this might cause some weird recursion issue? The use case is that I want a mostly standard config across machines published to a git repo, but a very small local config that is gitignored, with tweaks and potentially some private info that I don’t want it git. Here is my awful attempt:

imports = lib.optionals pathExists "config.local.nix" [


error: infinite recursion encountered, at /nix/store/.../modules.nix 

Thoughts or suggestions on how to achieve a local config? Thank you!

conditional imports will often cause infinite recursion issues.

Generally, I would use specialArgs to make the slightly different machine unique. He’s an example using home-manager+flakes:

The quick, hacky solution, would just be to create the file with empty contents for machines which don’t need it. echo "{ }" > /etc/nixos/config.local.nix, and make the import mandatory.

1 Like

Back when I wasn’t using, I was using something like this to conditionally import secrets:

imports = [ … ] ++ lib.optional (builtins.pathExists ./secrets.nix) ./secrets.nix;

oh yea, parenthesis in this situation is probably very important

1 Like

This is also usually safe to use, as the condition can’t be altered by any module.

That’s why I used builtins over any other predicate from lib or pkgs.

1 Like