[Colmena/Nix] How to fix infinite recursion error

I am trying to create a function for my colmena config.
the function should be taking in a hostName and reuse it dynamically to generate the configuration file.

below is my function

  mkColmenaConfig = hostName: {
    user ? "nixos",
    host,
    buildOnTarget ? false,
    tags ? [],
    system ? "x86_64-linux",
    extraModules ? [],
    hostModule,
  }: {
    deployment = {
      targetHost = host;
      targetPort = 22;
      targetUser = user;
      buildOnTarget = buildOnTarget;
      tags = tags;
    };
    nixpkgs.system = system;

    specialArgs = {
      hostName = hostName;
    };

    imports =
      [
        sops-nix.nixosModules.sops
        disko.nixosModules.disko
        hostModule
      ]
      ++ extraModules;
  };

below is my configuration file (referred as hostModule in the above code, hostModule is also a path)

{
  modulesPath,
  lib,
  pkgs,
  hostName,
  ...
}: {
  imports =
    [
      (modulesPath + "/installer/scan/not-detected.nix")
      (modulesPath + "/profiles/qemu-guest.nix")
      (import ./disko-config.nix {device = "/dev/sda";})
    ]
    ++ lib.optional (builtins.pathExists ./hardware-configuration-${hostName}.nix) ./hardware-configuration-${hostName}.nix;

  boot.loader.grub = {
    # no need to set devices, disko will add all devices that have a EF02 partition to the list already
    # devices = [ ];
    efiSupport = true;
    efiInstallAsRemovable = true;
  };
  services.openssh.enable = true;

  networking.hostName = hostName;

  time.timeZone = "America/Denver";

  environment.systemPackages = map lib.lowPrio [
    pkgs.curl
    pkgs.gitMinimal
  ];

  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINDkA9QW9+SBK4dXpIj9nR9k49wuPdjlMwLvSacM9ExM zhongjie.x.shen@gmail.com"
  ];

  system.stateVersion = "24.05";
}

my function calls is

      colmena = {
        meta = {
          nixpkgs = import nixpkgs {
            system = "x86_64-linux";
          };
          specialArgs = {
            inputs = inputs;
          };
        };

        defaults = {pkgs, ...}: {
          environment.systemPackages = [
            pkgs.curl
          ];
        };
        demo = mkColmenaConfig "demo" {
          host = "192.168.50.203";
          user = "root";
          tags = ["homelab"];
          hostModule = ./hosts/k3s/configuration.nix;
        };
      };

however with this config I am getting infinite recusion error.

❯ colmena apply --on @homelab
[INFO ] Using flake: git+file:///home/zshen/personal/server-config
[INFO ] Enumerating nodes...
error:
       … while evaluating attribute 'demo'

       … while evaluating the attribute 'config.deployment'
         at /nix/store/fwhfa9pbx8vdi8nd5pcys665baz6xdxf-source/lib/modules.nix:358:9:
          357|         options = checked options;
          358|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          359|         _module = checked (config._module);

       … while calling the 'seq' builtin
         at /nix/store/fwhfa9pbx8vdi8nd5pcys665baz6xdxf-source/lib/modules.nix:358:18:
          357|         options = checked options;
          358|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          359|         _module = checked (config._module);

       … while evaluating the module argument `hostName' in "/nix/store/1yz4wgcbznk6bacpwzlqb09h08bkiks3-source/hosts/k3s/configuration.nix":

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

       error: infinite recursion encountered
       at /nix/store/fwhfa9pbx8vdi8nd5pcys665baz6xdxf-source/lib/modules.nix:651:66:
          650|       extraArgs = mapAttrs (
          651|         name: _: addErrorContext (context name) (args.${name} or config._module.args.${name})
             |                                                                  ^
          652|       ) (functionArgs f);
[ERROR] -----
[ERROR] Operation failed with error: Child process exited with error code: 1
Hint: Backtrace available - Use `RUST_BACKTRACE=1` environment variable to display a backtrace

any one would have any insight on how to fix this?

my repo is here if anyone is interested in the full picture.

Probably it’s the fact that you’re defining specialArgs in a module that imports another module that depends on something from specialArgs.

Can you curry the host module to look like this at the start:

hostName:
{
  modulesPath,
  lib,
  pkgs,
  ...
}: {

and then import it by passing hostName explicitly?

    imports =
      [
        sops-nix.nixosModules.sops
        disko.nixosModules.disko
        (nixpkgs.lib.modules.importApply hostModule hostName)
      ]
      ++ extraModules;