Can you import in a module based on config?

I’m trying to do roughly this in a module:

{config, ...}: {
  options.foo = {
    # ...
  };
  imports = someFunc config.foo;
}

but that gives me an infinite recursion error. I don’t understand how that’s possible since I don’t refer to config.foo in someFunc.

Is this possible to do?

No this is impossible because of how the module system works. The imports line in any module cannot have any dependence on dynamic properties of the module system like config, options, or pkgs. The reason is described here, https://github.com/NixOS/nixpkgs/blob/edcd3d30564ca6d4c1f2442c4149fa908228243e/lib/modules.nix#L166. There are a few arguments that are allowed to be used in imports known as specialArgs, one example of this is modulesPath which lets you reference the path where the default modules available in nixos are stored.

You can find out more about the internals of the module system works by reading the comments in https://github.com/NixOS/nixpkgs/blob/44414a98a9911dc6667e58a8deae06d7f1f18b48/lib/modules.nix. I don’t know if there is any proper docs for the module system itself, there are some for NixOS which is based on the module system.

1 Like

Too bad that it’s this way. I’m switching from imperatively generating configs from functions that I put in imports to declaratively storing the function arguments, and this means that where before, merging was automatic and correct, now I have to manually merge generated configs :frowning:

EDIT: Actually, that seems to be what mkMerge is for, yey :slight_smile: