Assign config declared in different module of a nixosModule flake

I’m trying to write a nixosModule flake with different options set in different files to be imported in imports. I’m not sure if what I’m going for is simply not possible or not the ideal way of doing in nixos modules.

Let’s say I have a tree like

./
├── subdir/
│   └── submodule.nix
└── flake.nix

in subdir/submodule I declared

{config, ...}: {
    options.mymodule.submodule = {
        enable = lib.mkEnableOption "submodule";
    };
    config = lib.mkIf config.mymodule.submodule.enable {
        # config here ....
    };
}

in flake.nix I’m trying to set the submodule option in it’s config

{
  outputs = {...}: {
    nixosModules.default =
      { config, lib, ... }: {
        options.mymodule = {
              enable = lib.mkEnableOption "module";
         };
         config = lib.mkIf config.mymodule.enable {
              # apparently this does not work
              config.mymodule.submodule = true;
              # ... other configuration done here
          };
          imports = [
                ./subdir/submodule.nix
         ];
      };
    };
}

I know I could probably rearrange into something without create nested config but the idea is to compose options and reference the ones declared in other files so it can be more elegant. I guess I’m trying resemble something like a general purpose language module system but this may impossible here. Is it?

Right now I’m only getting errors like error: The option 'config' does not exist.

Change it to mymodule.submodule = true; because the leading config. is already present on the previous line. See NixOS Manual for more info.

Also FYI, it’s a little confusing to call these things “submodules” since submodule is already an actual type in the module system.

The build worked! For some reason I didn’t think that the config.mymodule.submodule would be assigned to the already top level config.

Thank you for the help and the FYI!