Error handling: Difference between revisions

imported>Pogobanane
m mention builtins.unsafeGetAttrPos
Axka (talk | contribs)
m Fix link "Nix manual: Assertions"
 
(4 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 69: Line 69:
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.  


Another tool that can be useful to improve error messages is <code>builtins.unsafeGetAttrPos
To find the location where variables get defined, you can use the following tools:
</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. Currently this seems to be limited to attributes defined in the same file though, is undocumented and considered bad practice.
 
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 ==