Error handling: Difference between revisions

imported>Pogobanane
add examples
imported>Carschandler
m The previous example for config.warnings was returning a list inside of a list, which doesn't work properly
 
(5 intermediate revisions by one other user 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 67: Line 67:
<!-- put debugging into a heading for search engine optimisation -->
<!-- put debugging into a heading for search engine optimisation -->


As mentioned above, 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 ==