16 May 2025 |
SomeoneSerge (void) | I never acquainted with types.nix , is there some fundamental reason that either of two freeform submodules couldn't work when left and right have disjoint explicit options ?
with import <nixpkgs/lib>;
evalModules {
modules = [
{
options.foo = mkOption {
type =
types.either
(types.submodule {
freeformType = types.attrsOf types.str;
options.marker_a = mkOption {
type = types.str;
};
})
(
types.submodule {
freeformType = types.attrsOf types.str;
options.marker_b = mkOption {
type = types.str;
};
}
);
};
config.foo = {
marker_b = "bar";
};
}
];
}
{ foo = { marker_a = «error: The option `foo.marker_a' was accessed but has no value defined. Try setting
the option.»; marker_b = "bar"; }; }
| 19:49:57 |
Matt Sturgeon | I believe the issue is the left and right type's check function.
either will use whichever type fist passes check value , and iirc submodules just use something like check = isAttrs
| 19:52:25 |
h7x4 | Some of the "container types" don't really check what's inside before being evaluated at a later stage. I believe this is the case for submodules, listOf and attrsOf . As a hack, you can add those checks with lib.types.addCheck . Same scenario with either and oneOf . | 19:53:46 |
Matt Sturgeon | It's intentional that the submodule-type's check function is not very restrictive, since you can assign any module to a submodule and it is down to the submodule's configuration to evaluate and merge its own definitions internally. | 19:53:58 |
Matt Sturgeon | E.g. foo = {} is a vaild definition, as is foo = { config, ... }: { } , as is foo = ./someModule.nix | 19:54:55 |
h7x4 | nix-repl> with lib.types; (listOf int).check [ true false "hello" ]
true
nix-repl> with lib.types; (addCheck (listOf int) (builtins.all builtins.isInt)).check [ true false "hello" ]
false
| 19:57:21 |
Matt Sturgeon | The other issue with marker options (marker_a , marker_b ) is how should definitions be merged that don't include the marker? E.g.
foo = lib.mkMerge [
{ something_freeform = "hi"; }
{ marker_a = "there"; }
]
You don't know which marker is defined until all merging is done.
In a simple case like this, you could refactor this as one submodule with both markers, and then add some extra logic/conditions within the submodule and/or in the optinon's final apply function.
| 20:02:16 |
h7x4 | Also ref https://discourse.nixos.org/t/problems-with-types-oneof-and-submodules/15197 | 20:03:59 |