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