How to set config options from within a config module

I want to figure out a good way to manage modules enabling other modules from my config. For example, if I have my hyprland module enabled I also want to enable wofi. Of course, I can just enable them both manually, but I’d rather have a system where one module can conditionally enable another.

Consider the following two files which are a very basic version of what my modules are:

# foo.nix example file
{ lib, config, ... }: {
  imports = [ ];

  options = {
    foo = {
      enable = lib.mkEnableOption "enable foo";
    };
  };

  config = lib.mkIf config.foo.enable (lib.mkMerge [
    {
      option_one.enable = true;
    }
    (lib.mkIf (config.programs.random_program.enable == true) {
      option_two.enable = true;
      # config.bar.enable = true;
    })
  ]);
}
# bar.nix example file
{ lib, config, ... }: {
  imports = [ ];

  options = {
    bar = {
      enable = lib.mkEnableOption "enable bar";
    };
  };

  config = lib.mkIf config.bar.enable {
    option_three.enable = true;
  };
}

These two files create two config options: config.foo.enable and config.bar.enable. When either option is enabled, the configuration of each respective option is applied. Would there be a way to enable one option when the other is enabled?

One idea I have would be to specify the default value of an option such as:

options = {
  bar = {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = config.foo.enable;
    };
  };
};

However, it makes more sense for me to work this the other way. Rather than have the dependence of bar on foo be defined in the bar module, it would make more sense for me to define it in the foo module so it behaves more like dependencies.

Is there an established convention on this or any other ideas on how I can get this implemented? Thanks in advance

No need to overcomplicate.

  config = {
    option_one.enable = lib.mkDefault config.foo.enable;
    option_two.enable = lib.mkDefault (config.foo.enable && config.programs.random_program.enable);
  };

This is definitely helpful, but I think I might have not explained my question as well as I could have. In each of my modules, I’ve created options such as the config.foo.enable and config.bar.enable. Then, from home.nix I’m able to just say foo.enable = true to enable all of the config that is done in that module.

What I’m hoping to figure out is a way to tie the value of bar.enable to foo.enable such that when I enable foo, bar is also enabled.

The goal of this is to implement some sort of dependency system within my config such that, for example, when I enable hyprland, dunst is also enabled. So, from my home.nix file, I can just say hyprland.enable = true, and implicitly enable dunst.enable because I’ve set it up as a “dependency” of sorts of hyprland.

I’m wondering if there’s a way I can configure this from within the hyprland module so that I can essentially just make a little list of the dependencies inside the hyprland module rather than altering the modules of all the individual dependencies.

I don’t see how my answer doesn’t address what you asked. option_one.enable and option_two.enable clearly depend on config.foo.enable, you can put it in whatever file or module you want.

Yeah, you’re very right. I don’t know why I didn’t recognize what was going on, but yeah on second look this is exactly what I was looking for. I really was over-complicating things for myself. Thank you!