Error handling: Difference between revisions

imported>Pogobanane
mNo edit summary
Axka (talk | contribs)
m Fix link "Nix manual: Assertions"
 
(5 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.")
       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/constructs.html?highlight=assert#assertions Nix manual: Assertions])
* '''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 68: Line 68:


To summarise debugging approaches discussed in this article, you can use <code>break</code> to debug nix code, <code>breakpointHook</code> to debug nix builds and interactive tools to debug NixOS tests.  
To summarise debugging approaches discussed in this article, you can use <code>break</code> to debug nix code, <code>breakpointHook</code> to debug nix builds and interactive tools to debug NixOS tests.  
To find the location where variables get defined, you can use the following tools:
For bare nix code, use <code>builtins.unsafeGetAttrPos
</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.
(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 ==