Using a function in a module config

i’m currently writing a module for my home-manager where i use a function to realize configurations where the name of an attribute is used in a function (think users.users..x).

my module currently looks like this:

{ config, pkgs, lib, ... }:
with lib;
  cfg = config.custom;
  funsCfg = { name, config, ... }: {
    options = {
      package = mkOption {
        type = types.package;
        default = pkgs.${name};
    config = {
      home.packages = [ config.package ];
  options.custom = {
    enable = mkEnableOption "enable it";
    funs = mkOption {
      default = { };
      type = with types; attrsOf (submodule funsCfg);
  config = mkIf cfg.enable (mkMerge
    (attrValues cfg.apps)

the problem is that the config line triggers an infinite recursion. Even when options.custom.enable is not set to true.

I’m not sure if it is your case but once @roberth told me:

I would recommend lazyAttrsOf, which supports more definitions without causing infinite recursion. The only reason to use attrsOf is mkIf, but mkIf does not really add anything to an attrsOf definition, as it is no lazier than optionalAttrs would be in this case, iirc. It is not used as often as attrsOf though, because in many cases it isn’t necessary, and even if it is, people just give up because the infinite recursion is hard to debug.

Could you post the trace from --show-trace?