I am trying to create a date submodule. Here is a snippet of of it.
options = {
day = lib.mkOption {
description = "The day.";
type = types.nullOr (types.numbers.between 1 31);
default = null;
};
month = lib.mkOption {
description = "The month.";
type = types.nullOr (types.numbers.between 1 12);
default = null;
};
year = lib.mkOption {
description = "The year.";
type = types.nullOr types.numbers.positive;
default = null;
};
...
And I am using it as a submodule as shown below.
options = {
nixcv = lib.mkOption {
description = "Your CV.";
type = types.submodule {
options = {
startDate = lib.mkOption {
description = "The start date.";
type = types.submoduleWith {
modules = [./date.nix];
};
};
endDate = lib.mkOption {
description = "The start date.";
type = types.submoduleWith {
modules = [./date.nix];
};
};
};
};
};
};
And a basic config shown below.
{
nixcv = {
startDate = {
year = 2021;
month = 11;
day = 4;
};
endDate = {
year = 2023;
month = 12;
day = 14;
};
};
}
Under a certain condition, if a month is not set, I want to throw a meaningful error to the user. Within the date.nix
module, I could easily throw "month was not set"
but that gives no context for where the problem is in the config. It would be really nice if i could say nixcv.startDate.month is not set etc...
.
So far I found that I can access the parent of the date module’s name using config._module.arg.name
. Alternatively, if I pass options
to the submodule, and inspect options._module.args.loc
it returns [ "nixcv" "endDate" "_module" "args" ]
which I could filter and concat to get what I want.
Is this the best way to access all the parent config names of a submodule or is there a better way?