Infinite recursion of `config` in a home manager module

Hello,

I’m writing a helper home manager module using a flake.
Here is minimal code that shows the issue:

# flake.nix
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  };
  outputs = {...}: {
    homeManagerModule = import ./module.nix;
  };
}
# module.nix
{ lib, config, ... }: {
  imports = [
    ./example-module.nix
  ];
  options.myModule.enable = lib.mkEnableOption "myModule";
  config.lib.myLib = {
    mkModule = cfg: lib.mkIf config.myModule.enable cfg;
  };
}
# example-module.nix
{config, ...}:
config.lib.myLib.mkModule {
  config.programs.alacritty.enable = true;
}

In this module, I expect to write lots of sub-modules with the pattern

lib.mkIf config.myModule.enable {...}

So I isolated it into a function. But, when calling mkModule, I’m getting infinite recursion errors with config:

error:
       … while evaluating a branch condition
         at /nix/store/rhn4fkh7hqjlqs9j6naaq623qpl62yz0-source/lib/lists.nix:125:9:
          124|       fold' = n:
          125|         if n == len
             |         ^
          126|         then nul

       … while calling the 'length' builtin
         at /nix/store/rhn4fkh7hqjlqs9j6naaq623qpl62yz0-source/lib/lists.nix:123:13:
          122|     let
          123|       len = length list;
             |             ^
          124|       fold' = n:

       … while evaluating the module argument `config' in "/nix/store/nks2pia0n6s2kxwssfm4ildp4yirpc57-source/example-module.nix":

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: infinite recursion encountered
       at /nix/store/rhn4fkh7hqjlqs9j6naaq623qpl62yz0-source/lib/modules.nix:245:34:
          244|           (regularModules ++ [ internalModule ])
          245|           ({ inherit lib options config specialArgs; } // specialArgs);
             |                                  ^
          246|         in mergeModules prefix (reverseList collected);

How can I solve this? On a related note, is this how I would make global helper functions?
Thanks in advance

The problem is that you can’t use a value from your config at a level that is at or higher than the level in which it is defined. So this would work:

config.programs = config.lib.myLib.mkModule {
  alacritty.enable = true;
};

because programs is not a prefix of myModule.enable. But you can’t depend on myModule.enable at the top level of your config; it needs to be nested within at least one level (and that level can’t be myModule).