1<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-assertions">
2 <title>Warnings and Assertions</title>
3 <para>
4 When configuration problems are detectable in a module, it is a good
5 idea to write an assertion or warning. Doing so provides clear
6 feedback to the user and prevents errors after the build.
7 </para>
8 <para>
9 Although Nix has the <literal>abort</literal> and
10 <literal>builtins.trace</literal>
11 <link xlink:href="https://nixos.org/nix/manual/#ssec-builtins">functions</link>
12 to perform such tasks, they are not ideally suited for NixOS
13 modules. Instead of these functions, you can declare your warnings
14 and assertions using the NixOS module system.
15 </para>
16 <section xml:id="sec-assertions-warnings">
17 <title>Warnings</title>
18 <para>
19 This is an example of using <literal>warnings</literal>.
20 </para>
21 <programlisting language="bash">
22{ config, lib, ... }:
23{
24 config = lib.mkIf config.services.foo.enable {
25 warnings =
26 if config.services.foo.bar
27 then [ ''You have enabled the bar feature of the foo service.
28 This is known to cause some specific problems in certain situations.
29 '' ]
30 else [];
31 }
32}
33</programlisting>
34 </section>
35 <section xml:id="sec-assertions-assetions">
36 <title>Assertions</title>
37 <para>
38 This example, extracted from the
39 <link xlink:href="https://github.com/NixOS/nixpkgs/blob/release-17.09/nixos/modules/services/logging/syslogd.nix"><literal>syslogd</literal>
40 module</link> shows how to use <literal>assertions</literal>.
41 Since there can only be one active syslog daemon at a time, an
42 assertion is useful to prevent such a broken system from being
43 built.
44 </para>
45 <programlisting language="bash">
46{ config, lib, ... }:
47{
48 config = lib.mkIf config.services.syslogd.enable {
49 assertions =
50 [ { assertion = !config.services.rsyslogd.enable;
51 message = "rsyslogd conflicts with syslogd";
52 }
53 ];
54 }
55}
56</programlisting>
57 </section>
58</section>