Load Module depending on options

I have a module with an option that takes a path to a directory.
If the module is enabled, it loads some settings from files in that directory.
If the directory contains a settings.nix file, I want to load that file (as a module?).

I can’t use imports because that isn’t allowed to get the path from config (because of infinite recursion).

How do I load a nix file with extra settings from a path that is declared in the options?

Context

Trying to create a simple theme setup, where you supply a path to a directory, and it automatically loads a background image, color scheme etc.
The settings.nix file in that directory then applies extra settings if it is present.

Don’t conditionally import, use .enable options and then use config = mkIf config.whatever.enable { ... }; in your settings.nix file. You can read the NixOS manual and look at how existing modules are implemented.

This isn’t possible in my case, because the import is based on a given path.
I can’t create a module with enable option for each theme directory I create.

Ideally I would set

autotheme.enable = true; # autotheme is my theming module
autotheme.themePath = /path/to/theme; # some path containing theme files including a nix file

and then the module could load a settings.nix from the provided directory.

Since themePath is an option, it can’t be accessed by imports.

I think you may actually be able to do that, if that’s what you really wanted.