Error handling: Difference between revisions
imported>Pogobanane mention nixos modules definitionsWithLocations |
m Fix link "Nix manual: Assertions" |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 8: | Line 8: | ||
lib.warn "This is a sample warning message." | lib.warn "This is a sample warning message." | ||
{ | { | ||
config.warnings = | config.warnings = ( | ||
# Some NixOS module: throw error, if services.foo.bar == true | # Some NixOS module: throw error, if services.foo.bar == true | ||
lib.optionals config.services.foo.bar "This is also a sample warning message, but invoked differently." | |||
); | |||
} | } | ||
</syntaxHighlight> | </syntaxHighlight> | ||
Line 19: | Line 19: | ||
The nix language has a construct to help with printing messages. | The nix language has a construct to help with printing messages. | ||
* '''assert''': throw an error (see [https://nixos.org/manual/nix/stable/language/ | * '''assert''': throw an error (see [https://nixos.org/manual/nix/stable/language/syntax.html?highlight=assert#assertions Nix manual: Assertions]) | ||
The nix language also comes with some related [https://nixos.org/manual/nix/stable/language/builtins.html builtin functions]: | The nix language also comes with some related [https://nixos.org/manual/nix/stable/language/builtins.html builtin functions]: | ||
Line 74: | Line 74: | ||
</code> ([https://github.com/NixOS/nix/blob/b17c4290cf61d8a0386817b87231762c175097c5/tests/lang/eval-okay-getattrpos.nix example]) which returns the line and column of where an attribute is defined. It is undocumented and considered bad practice. | </code> ([https://github.com/NixOS/nix/blob/b17c4290cf61d8a0386817b87231762c175097c5/tests/lang/eval-okay-getattrpos.nix example]) which returns the line and column of where an attribute is defined. It is undocumented and considered bad practice. | ||
For NixOS options unsafeGetAttrPos doesn't work, but the module system itself records that information: to find the location of <code>config.networking.hostName</code>, use <code>:p options.networking.hostName. | (soon to come [https://github.com/NixOS/nixpkgs/pull/249243 github PR]): For NixOS options unsafeGetAttrPos doesn't work, but the module system itself records that information: to find the location of <code>config.networking.hostName</code>, use <code>:p options.networking.hostName.declarationPositions</code>. | ||
== References == | == References == |