1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-22 17:01:08 +01:00
nix/doc/manual/source/development/debugging.md
Eelco Dolstra fc81840a8e dev-shell: Set mesonBuildType to debugoptimized
Previously, we got debug symbols implicitly because we were using
`separateDebugInfo = true`, which adds `-ggdb` to the compiler flags.
2025-12-15 19:09:37 +01:00

2.2 KiB

Debugging Nix

This section shows how to build and debug Nix with debug symbols enabled.

Additionally, see Testing Nix for further instructions on how to debug Nix in the context of a unit test or functional test.

Building Nix with Debug Symbols

In the development shell, mesonBuildType is set automatically to debugoptimized. This builds Nix with debug symbols, which are essential for effective debugging.

It is also possible to build without optimization for faster build:

[nix-shell]$ NIX_HARDENING_ENABLE=$(printLines $NIX_HARDENING_ENABLE | grep -v fortify)
[nix-shell]$ export mesonBuildType=debug

(The first line is needed because fortify hardening requires at least some optimization.)

Building Nix with sanitizers

Nix can be built with Address and UB sanitizers using LLVM or GCC. This is useful when debugging memory corruption issues.

[nix-shell]$ export mesonBuildType=debugoptimized
[nix-shell]$ appendToVar mesonFlags "-Dlibexpr:gc=disabled" # Disable Boehm
[nix-shell]$ appendToVar mesonFlags "-Dbindings=false" # Disable nix-perl
[nix-shell]$ appendToVar mesonFlags "-Db_sanitize=address,undefined"

Debugging the Nix Binary

Obtain your preferred debugger within the development shell:

[nix-shell]$ nix-shell -p gdb

On macOS, use lldb:

[nix-shell]$ nix-shell -p lldb

Launching the Debugger

To debug the Nix binary, run:

[nix-shell]$ gdb --args ../outputs/out/bin/nix

On macOS, use lldb:

[nix-shell]$ lldb -- ../outputs/out/bin/nix

Using the Debugger

Inside the debugger, you can set breakpoints, run the program, and inspect variables.

(gdb) break main
(gdb) run <arguments>

Refer to the GDB Documentation for comprehensive usage instructions.

On macOS, use lldb:

(lldb) breakpoint set --name main
(lldb) process launch -- <arguments>

Refer to the LLDB Tutorial for comprehensive usage instructions.