Error handling: Difference between revisions

imported>Pogobanane
m mention builtins.unsafeGetAttrPos
imported>Pogobanane
mention nixos modules definitionsWithLocations
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.
 
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.definitionsWithLocations</code>.


== References ==
== References ==