From c92f7efef58e77544e579fa50ee9af430dc0a5a2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 24 Jul 2025 16:51:14 +0200 Subject: [PATCH] lib.types: Edit docs Co-authored-by: Valentin Gagarin --- lib/types.nix | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index c4d602452536..7bc536e52af7 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -1347,10 +1347,14 @@ rec { [`lazy`]{#sec-option-types-composed-attrsWith-lazy} : Determines whether the set of attribute names is evaluated before or after evaluating any attribute values. - `lazy = true` is a preferable choice in principle, but requires extra effort if the option value is used in aggregate (e.g. `attrNames` or `toJSON`). + `lazy = true` is a preferable choice in principle, but requires extra effort if the option value is used as a whole (e.g. `attrNames` or `toJSON`). When `lazy` is `true`, the attribute names are determined by looking at the definitions without evaluating them. - If only a `mkIf false` occurs in an attribute, this may result in an error when the attribute value is accessed. + + ::: {.warning} + With `lazy = true`, an attribute whose definitions are all `mkIf false` still counts as present, so `foo ? attr` is `true`. + Accessing `foo.attr` then throws an error, unless the element type defines `emptyValue` (e.g. `nullOr`), in which case that value is returned. + ::: When `lazy` is `false`, the attribute names are determined by evaluating the definitions to see if they are `mkIf` false, but this also causes the evaluation of regular non-`mkIf` definitions. Evaluating too much causes multiple problems: @@ -1358,12 +1362,10 @@ rec { - irrelevant errors are triggered, despite an attribute not really being used - performance is worse, if any of the values are expensive to evaluate - **Recommendation** - - If the attribute set is used _in aggregate_ (e.g. `attrNames`, `toJSON`), use the default `lazy = false;`, so that `mkIf` conditions are evaluated *before* the attribute names are returned. - - If the attribute set is used _individually_ (e.g. `config.foo`), use `lazy = true;` so that `mkIf` conditions are evaluated *after* the attribute names are returned. - This will be more robust and efficient, as the attribute values are not computed until it is needed. + Maintainers of this code recommend: + - If the attribute set is used _in aggregate_ (e.g. `attrNames`, `toJSON`), use the default `lazy = false;`, so that `mkIf` conditions are evaluated *before* the attribute names are returned. + - If attributes are accessed _in isolation_ (e.g. `config.foo`), use `lazy = true;` so that `mkIf` conditions are evaluated *after* the attribute names are returned. + This will be more robust and efficient, as the attribute values are not computed until they are needed. It is possible to combine both behaviors using `submodule` with `freeformType = attrsWith { lazy = false; ... }` instead of `attrsWith`, in which case the declared options are allowed to refer to each other, but *only* through the submodule's module arguments. See the following example.