From 9e6737710c4fb2613850e699178b23d54f1a3261 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 18 Dec 2020 16:42:42 +0100 Subject: Revert "Module-builtin assertions, disabling assertions and submodule assertions" --- nixos/doc/manual/development/assertions.xml | 159 +++++++--------------------- 1 file changed, 38 insertions(+), 121 deletions(-) (limited to 'nixos/doc') diff --git a/nixos/doc/manual/development/assertions.xml b/nixos/doc/manual/development/assertions.xml index 31d09f958af5..32f90cf2e7c4 100644 --- a/nixos/doc/manual/development/assertions.xml +++ b/nixos/doc/manual/development/assertions.xml @@ -3,155 +3,72 @@ xmlns:xi="http://www.w3.org/2001/XInclude" version="5.0" xml:id="sec-assertions"> - Evaluation Checks + Warnings and Assertions When configuration problems are detectable in a module, it is a good idea to - write a check for catching it early. Doing so can provide clear feedback to - the user and can prevent errors before the build. + write an assertion or warning. Doing so provides clear feedback to the user + and prevents errors after the build. Although Nix has the abort and builtins.trace functions - to perform such tasks generally, they are not ideally suited for NixOS - modules. Instead of these functions, you can declare your evaluation checks - using the NixOS module system. + 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. -
- Defining Checks +
+ Warnings - Checks can be defined using the option. - Each check needs an attribute name, under which you can define a trigger - assertion using and a - message using . - For the message, you can add - options to the module arguments and use - ${options.path.to.option} to print a context-aware string - representation of an option path. Here is an example showing how this can be - done. - - - -{ config, options, ... }: { - _module.checks.gpgSshAgent = { - check = config.programs.gnupg.agent.enableSSHSupport -> !config.programs.ssh.startAgent; - message = "If you have ${options.programs.gnupg.agent.enableSSHSupport} enabled," - + " you can't enable ${options.programs.ssh.startAgent} as well!"; - }; - - _module.checks.grafanaPassword = { - check = config.services.grafana.database.password == ""; - message = "The grafana password defined with ${options.services.grafana.database.password}" - + " will be stored as plaintext in the Nix store!"; - # This is a non-fatal warning - type = "warning"; - }; -} - - -
- -
- Ignoring Checks - - - Sometimes you can get failing checks that don't apply to your specific case - and you wish to ignore them, or at least make errors non-fatal. You can do so - for all checks defined using by - using the attribute name of the definition, which is conveniently printed - using [...] when the check is triggered. For above - example, the evaluation output when the checks are triggered looks as - follows: - - - -trace: warning: [grafanaPassword] The grafana password defined with - services.grafana.database.password will be stored as plaintext in the Nix store! -error: Failed checks: -- [gpgSshAgent] If you have programs.gnupg.agent.enableSSHSupport - enabled, you can't enable programs.ssh.startAgent as well! - - - - The [grafanaPassword] and [gpgSshAgent] - strings tell you that these were defined under the grafanaPassword - and gpgSshAgent attributes of - respectively. With this knowledge - you can adjust them to your liking: + This is an example of using warnings. + - -
-
- Checks in Submodules - - Evaluation checks can be defined within submodules in the same way. Here is an example: - - - -{ lib, ... }: { - - options.myServices = lib.mkOption { - type = lib.types.attrsOf (lib.types.submodule ({ config, options, ... }: { - options.port = lib.mkOption {}; - - config._module.checks.portConflict = { - check = config.port != 80; - message = "Port ${toString config.port} defined using" - + " ${options.port} is usually used for HTTP"; - type = "warning"; - }; - })); - }; - -} - +
+ Assertions - When this check is triggered, it shows both the submodule path along with - the check attribute within that submodule, joined by a - /. Note also how ${options.port} - correctly shows the context of the option. - - - -trace: warning: [myServices.foo/portConflict] Port 80 defined using - myServices.foo.port is usually used for HTTP - - - - Therefore to disable such a check, you can do so by changing the - option within the - myServices.foo submodule: + This example, extracted from the + + syslogd module 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. + - - - - Checks defined in submodules under types.listOf can't be - ignored, since there's no way to change previously defined list items. - - -
-- cgit v1.2.3