2017-09-11 18:12:50 +02:00
|
|
|
<section xmlns="http://docbook.org/ns/docbook"
|
|
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
|
|
xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
|
|
version="5.0"
|
|
|
|
xml:id="sec-assertions">
|
2018-05-02 01:57:09 +02:00
|
|
|
<title>Warnings and Assertions</title>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
<para>
|
2019-09-19 19:17:30 +02:00
|
|
|
When configuration problems are detectable in a module, it is a good idea to
|
|
|
|
write an assertion or warning. Doing so provides clear feedback to the user
|
|
|
|
and prevents errors after the build.
|
2018-05-02 01:57:09 +02:00
|
|
|
</para>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
<para>
|
2019-09-19 19:17:30 +02:00
|
|
|
Although Nix has the <literal>abort</literal> and
|
|
|
|
<literal>builtins.trace</literal>
|
|
|
|
<link xlink:href="https://nixos.org/nix/manual/#ssec-builtins">functions</link>
|
|
|
|
to perform such tasks, they are not ideally suited for NixOS modules. Instead
|
|
|
|
of these functions, you can declare your warnings and assertions using the
|
|
|
|
NixOS module system.
|
2018-05-02 01:57:09 +02:00
|
|
|
</para>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
2018-09-01 21:49:04 +02:00
|
|
|
<section xml:id="sec-assertions-warnings">
|
2018-05-02 01:57:09 +02:00
|
|
|
<title>Warnings</title>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
<para>
|
|
|
|
This is an example of using <literal>warnings</literal>.
|
|
|
|
</para>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
<![CDATA[
|
|
|
|
{ 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 [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2018-05-02 01:57:09 +02:00
|
|
|
</section>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
2018-09-01 21:49:04 +02:00
|
|
|
<section xml:id="sec-assertions-assertions">
|
2018-05-02 01:57:09 +02:00
|
|
|
<title>Assertions</title>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
<para>
|
2019-09-19 19:17:30 +02:00
|
|
|
This example, extracted from the
|
|
|
|
<link xlink:href="https://github.com/NixOS/nixpkgs/blob/release-17.09/nixos/modules/services/logging/syslogd.nix">
|
|
|
|
<literal>syslogd</literal> module </link> shows how to use
|
|
|
|
<literal>assertions</literal>. Since there can only be one active syslog
|
|
|
|
daemon at a time, an assertion is useful to prevent such a broken system
|
|
|
|
from being built.
|
2018-05-02 01:57:09 +02:00
|
|
|
</para>
|
2017-09-11 18:12:50 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
<![CDATA[
|
|
|
|
{ config, lib, ... }:
|
|
|
|
{
|
|
|
|
config = lib.mkIf config.services.syslogd.enable {
|
|
|
|
assertions =
|
|
|
|
[ { assertion = !config.services.rsyslogd.enable;
|
|
|
|
message = "rsyslogd conflicts with syslogd";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2018-05-02 01:57:09 +02:00
|
|
|
</section>
|
2017-09-11 18:12:50 +02:00
|
|
|
</section>
|