Skip to content
47 changes: 26 additions & 21 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1573,19 +1573,18 @@ let
x:
throw "The option `${showOption optionName}' can no longer be used since it's been removed. ${replacementInstructions}";
});
config.assertions =
config.assertions = setAttrByPath (optionName ++ [ "removed" ]) (
let
opt = getAttrFromPath optionName options;
in
[
{
assertion = !opt.isDefined;
message = ''
The option definition `${showOption optionName}' in ${showFiles opt.files} no longer has any effect; please remove it.
${replacementInstructions}
'';
}
];
{
assertion = !opt.isDefined;
message = ''
The option definition `${showOption optionName}' in ${showFiles opt.files} no longer has any effect; please remove it.
${replacementInstructions}
'';
}
);
};

/**
Expand Down Expand Up @@ -1703,15 +1702,20 @@ let
);

config = {
warnings = filter (x: x != "") (
warnings = foldl' recursiveUpdate { } (
map (
f:
let
val = getAttrFromPath f config;
opt = getAttrFromPath f options;
in
optionalString (val != "_mkMergedOptionModule")
"The option `${showOption f}' defined in ${showFiles opt.files} has been changed to `${showOption to}' that has a different type. Please read `${showOption to}' documentation and update your configuration accordingly."
path:
setAttrByPath (path ++ [ "mergedOption" ]) {
condition = (getAttrFromPath path config) != "_mkMergedOptionModule";
message =
let
opt = getAttrFromPath path options;
in
''
The option `${showOption path}' defined in ${showFiles opt.files} has been changed to `${showOption to}' that has a different type.
Please read `${showOption to}' documentation and update your configuration accordingly.
'';
}
) from
);
}
Expand Down Expand Up @@ -1917,9 +1921,10 @@ let
);
config = mkIf condition (mkMerge [
(optionalAttrs (options ? warnings) {
warnings =
optional (warn && fromOpt.isDefined)
"The option `${showOption from}' defined in ${showFiles fromOpt.files} has been renamed to `${showOption to}'.";
warnings = setAttrByPath (from ++ [ "aliased" ]) {
condition = warn && fromOpt.isDefined;
message = "The option `${showOption from}' defined in ${showFiles fromOpt.files} has been renamed to `${showOption to}'.";
};
})
(
if withPriority then
Expand Down
27 changes: 25 additions & 2 deletions lib/tests/modules/doRename-warnings.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,35 @@
})
];
options = {
warnings = lib.mkOption { type = lib.types.listOf lib.types.str; };
warnings = lib.mkOption {
type =
let
checkedWarningItemType =
let
check = x: x ? condition && x ? message;
in
lib.types.addCheck (lib.types.attrsOf lib.types.anything) check;

nestedWarningAttrsType =
let
nestedWarningItemType = lib.types.either checkedWarningItemType (
lib.types.attrsOf nestedWarningItemType
);
in
nestedWarningItemType;
in
lib.types.submodule { freeformType = nestedWarningAttrsType; };
};

c.d.e = lib.mkOption { };
result = lib.mkOption { };
};
config = {
a.b = 1234;
result = lib.concatStringsSep "%" config.warnings;
result =
let
warning = config.warnings.a.b.aliased;
in
lib.optionalString warning.condition warning.message;
};
}
27 changes: 11 additions & 16 deletions nixos/doc/manual/development/assertions.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ This is an example of using `warnings`.
{ config, lib, ... }:
{
config = lib.mkIf config.services.foo.enable {
warnings =
if config.services.foo.bar then
[
''
You have enabled the bar feature of the foo service.
This is known to cause some specific problems in certain situations.
''
]
else
[ ];
warnings.services.foo.beCarefulWithBar = {
condition = config.services.foo.bar;
message = ''
You have enabled the bar feature of the foo service.
This is known to cause some specific problems in certain situations.
'';
};
};
}
```
Expand All @@ -34,12 +31,10 @@ This example, extracted from the [`syslogd` module](https://github.com/NixOS/nix
{ config, lib, ... }:
{
config = lib.mkIf config.services.syslogd.enable {
assertions = [
{
assertion = !config.services.rsyslogd.enable;
message = "rsyslogd conflicts with syslogd";
}
];
assertions.services.syslogd.rsyslogdConflict = {
assertion = !config.services.rsyslogd.enable;
message = "rsyslogd conflicts with syslogd";
};
};
}
```
Loading
Loading