···11+# Warnings and Assertions {#sec-assertions}
22+33+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.
44+55+Although Nix has the `abort` and `builtins.trace` [functions](https://nixos.org/nix/manual/#ssec-builtins) 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.
66+77+## Warnings {#sec-assertions-warnings}
88+99+This is an example of using `warnings`.
1010+1111+```nix
1212+{ config, lib, ... }:
1313+{
1414+ config = lib.mkIf config.services.foo.enable {
1515+ warnings =
1616+ if config.services.foo.bar
1717+ then [ ''You have enabled the bar feature of the foo service.
1818+ This is known to cause some specific problems in certain situations.
1919+ '' ]
2020+ else [];
2121+ }
2222+}
2323+```
2424+2525+## Assertions {#sec-assertions-assetions}
2626+2727+This example, extracted from the [`syslogd` module](https://github.com/NixOS/nixpkgs/blob/release-17.09/nixos/modules/services/logging/syslogd.nix) shows how to use `assertions`. 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.
2828+2929+```nix
3030+{ config, lib, ... }:
3131+{
3232+ config = lib.mkIf config.services.syslogd.enable {
3333+ assertions =
3434+ [ { assertion = !config.services.rsyslogd.enable;
3535+ message = "rsyslogd conflicts with syslogd";
3636+ }
3737+ ];
3838+ }
3939+}
4040+```
-74
nixos/doc/manual/development/assertions.xml
···11-<section xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xmlns:xi="http://www.w3.org/2001/XInclude"
44- version="5.0"
55- xml:id="sec-assertions">
66- <title>Warnings and Assertions</title>
77-88- <para>
99- When configuration problems are detectable in a module, it is a good idea to
1010- write an assertion or warning. Doing so provides clear feedback to the user
1111- and prevents errors after the build.
1212- </para>
1313-1414- <para>
1515- Although Nix has the <literal>abort</literal> and
1616- <literal>builtins.trace</literal>
1717- <link xlink:href="https://nixos.org/nix/manual/#ssec-builtins">functions</link>
1818- to perform such tasks, they are not ideally suited for NixOS modules. Instead
1919- of these functions, you can declare your warnings and assertions using the
2020- NixOS module system.
2121- </para>
2222-2323- <section xml:id="sec-assertions-warnings">
2424- <title>Warnings</title>
2525-2626- <para>
2727- This is an example of using <literal>warnings</literal>.
2828- </para>
2929-3030-<programlisting>
3131-<![CDATA[
3232-{ config, lib, ... }:
3333-{
3434- config = lib.mkIf config.services.foo.enable {
3535- warnings =
3636- if config.services.foo.bar
3737- then [ ''You have enabled the bar feature of the foo service.
3838- This is known to cause some specific problems in certain situations.
3939- '' ]
4040- else [];
4141- }
4242-}
4343-]]>
4444-</programlisting>
4545- </section>
4646-4747- <section xml:id="sec-assertions-assertions">
4848- <title>Assertions</title>
4949-5050- <para>
5151- This example, extracted from the
5252- <link xlink:href="https://github.com/NixOS/nixpkgs/blob/release-17.09/nixos/modules/services/logging/syslogd.nix">
5353- <literal>syslogd</literal> module </link> shows how to use
5454- <literal>assertions</literal>. Since there can only be one active syslog
5555- daemon at a time, an assertion is useful to prevent such a broken system
5656- from being built.
5757- </para>
5858-5959-<programlisting>
6060-<![CDATA[
6161-{ config, lib, ... }:
6262-{
6363- config = lib.mkIf config.services.syslogd.enable {
6464- assertions =
6565- [ { assertion = !config.services.rsyslogd.enable;
6666- message = "rsyslogd conflicts with syslogd";
6767- }
6868- ];
6969- }
7070-}
7171-]]>
7272-</programlisting>
7373- </section>
7474-</section>
···11+<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-assertions">
22+ <title>Warnings and Assertions</title>
33+ <para>
44+ When configuration problems are detectable in a module, it is a good
55+ idea to write an assertion or warning. Doing so provides clear
66+ feedback to the user and prevents errors after the build.
77+ </para>
88+ <para>
99+ Although Nix has the <literal>abort</literal> and
1010+ <literal>builtins.trace</literal>
1111+ <link xlink:href="https://nixos.org/nix/manual/#ssec-builtins">functions</link>
1212+ to perform such tasks, they are not ideally suited for NixOS
1313+ modules. Instead of these functions, you can declare your warnings
1414+ and assertions using the NixOS module system.
1515+ </para>
1616+ <section xml:id="sec-assertions-warnings">
1717+ <title>Warnings</title>
1818+ <para>
1919+ This is an example of using <literal>warnings</literal>.
2020+ </para>
2121+ <programlisting language="bash">
2222+{ config, lib, ... }:
2323+{
2424+ config = lib.mkIf config.services.foo.enable {
2525+ warnings =
2626+ if config.services.foo.bar
2727+ then [ ''You have enabled the bar feature of the foo service.
2828+ This is known to cause some specific problems in certain situations.
2929+ '' ]
3030+ else [];
3131+ }
3232+}
3333+</programlisting>
3434+ </section>
3535+ <section xml:id="sec-assertions-assetions">
3636+ <title>Assertions</title>
3737+ <para>
3838+ This example, extracted from the
3939+ <link xlink:href="https://github.com/NixOS/nixpkgs/blob/release-17.09/nixos/modules/services/logging/syslogd.nix"><literal>syslogd</literal>
4040+ module</link> shows how to use <literal>assertions</literal>.
4141+ Since there can only be one active syslog daemon at a time, an
4242+ assertion is useful to prevent such a broken system from being
4343+ built.
4444+ </para>
4545+ <programlisting language="bash">
4646+{ config, lib, ... }:
4747+{
4848+ config = lib.mkIf config.services.syslogd.enable {
4949+ assertions =
5050+ [ { assertion = !config.services.rsyslogd.enable;
5151+ message = "rsyslogd conflicts with syslogd";
5252+ }
5353+ ];
5454+ }
5555+}
5656+</programlisting>
5757+ </section>
5858+</section>