Refer to submodule option within itself

Hello,
I am writing a custom service, which, as one of its options, will accept a list of submodules. The problem is that I need to be able to refer to submodule options within other of its options in order to be able to provide sensible defaults.

So something like this:

submodule = types.submodule { 
  options = {
    foo = mkOption { <- reference this
      type = types.str; 
      default = "val"; 
    };

    bar =  mkOption {
      type = types.str;
      default = "${something.foo (?)}var" <- here
  };
};
...
options.services.someservice = {
  configs = mkOption {
    type = types.listOf submodule;
    default = [ ];
  };
};

Is this possible?

1 Like

Yup:

types.submodule ({ config, ... }: { 
  options = {
    foo = mkOption { <- reference this
      type = types.str; 
      default = "val"; 
    };

    bar =  mkOption {
      type = types.str;
      default = "${config.foo}var" <- here
  };
})

:slight_smile:

2 Likes

Thank you! Is there any site where I can see a detailed documentation of stuff like this?

Unfortunately there’s no good reference docs for the module system. They should be here, but it’s very bare-bones right now. There’s the official module system tutorials on nix.dev that probably covers this somewhere though.

1 Like