Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc/manual: Add 'Debugging Nix' section #11637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

michaelvanstraten
Copy link

This PR adds a new 'Debugging Nix' section to the Nix manual. It provides instructions on how to build Nix with debug symbols and how to debug the Nix binary using debuggers like lldb.

Motivation

Currently, it's hard to get started with launching a debuggable instance of Nix. Building Nix with debug symbols requires knowledge of internal environment variables like mesonBuildType, which are not well-documented.

Context

Please see #11502.

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

This commit adds a new 'Debugging Nix' section to the Nix manual. It provides instructions on how to build Nix with debug symbols and how to debug the Nix binary using debuggers like `lldb`.

Fixes NixOS#11502
`debug` before configuring the build:

```console
[nix-shell]$ export mesonBuildType=debug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this is needed for the Meson build, but our regular build has debug symbols by default.

Copy link
Author

@michaelvanstraten michaelvanstraten Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edolstra is talking about the old autotools buildsystem that we are migrating away from. Do we want debugoptimized instead of debug here?

Suggested change
[nix-shell]$ export mesonBuildType=debug
[nix-shell]$ export mesonBuildType=debugoptimized

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimized makes sense to me if the debugger is this able to nicely link back into the source code.

To debug the Nix binary you just built:

```console
[nix-shell]$ lldb -- ./subprojects/nix/nix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I use gdb, and if you're in a nix develop shell, you can just do

gdb --args nix ...

i.e. no need to give a full path to the nix binary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So meson actually installs it to /home/joerg/git/nix/out/bin which is not in PATH. But in that case we would also need to run ninjaInstallPhase. I think it makes sense that both build system choose different locations for our own sanity while we still have two and not put both in PATH. I actually prefer being explicit about the executable location, because it's less error prone for users following the tutorial. Let's say one person has exited the devshell to get a debugger and no longer has the PATH from it set, in that case they might run the debugger against the global installed nix, which is not good.


```console
[nix-shell]$ mesonConfigurePhase
[nix-shell]$ ninjaBuildPhase
Copy link
Member

@Mic92 Mic92 Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get a proper nix installation, we might want to install it instead? The extra overhead is negligible.

Suggested change
[nix-shell]$ ninjaBuildPhase
[nix-shell]$ ninjaInstallPhase

### Installing a Debugger

Install your preferred debugger within the development shell. For example, to
install `lldb`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to suggest gdb on Linux still by default? At least for me it used to be more stable. But I am happily convinced if this has changed. And on macOS I believe we should not suggest to install lldb with nix-shell because it's not code-signed, meaning it won't be able to use the debugger api without disabling apples security protection.

Copy link
Author

@michaelvanstraten michaelvanstraten Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Linux I can see why we would want to recommend gdb, it's just the case that I am on Mac an that's the only thing I was familiar with.

I haven't experienced needing to disable security protection on macOS to work with lldb in nix-shell.

@michaelvanstraten
Copy link
Author

michaelvanstraten commented Oct 6, 2024

I haven't figured out how to both debug the nix-daemon and nix when the debugging user is not the owner of /nix.

We could use the --store option to point to a local store but that would make debugging harder.

Is there a way to point nix to a different socket in terms of the daemon?

@Mic92
Copy link
Member

Mic92 commented Oct 6, 2024

I haven't figured out how to both debug the nix-daemon and nix when the debugging user is not the owner of /nix.

We could use the --store option to point to a local store but that would make debugging harder.

Is there a way to point nix to a different socket in terms of the daemon?

if you run as root than nix won't use the nix-daemon but will have similar privileges. This might be helpful for debugging. Otherwise, you would need to run two debugger sessions.
One that runs the nix-daemon as root and a second one with the actual command.
Debuggers are not very good at tracking multiple processes.

@michaelvanstraten
Copy link
Author

if you run as root than nix won't use the nix-daemon but will have similar privileges.

Then the problem becomes that you can't reproduce the IPC interactions between the two.

@Mic92
Copy link
Member

Mic92 commented Oct 6, 2024

Usually you won't to need a debugger on both processes at the same time. You can just have the debugger on one process and than maybe verbose logging or strace on the other instance.

@michaelvanstraten
Copy link
Author

Usually you won't to need a debugger on both processes at the same time. You can just have the debugger on one process and than maybe verbose logging or strace on the other instance.

I recently had the problem that the nix-daemon wasn't able to access an ssh key needed to access a remote builder. This was do to the fact that the key was password protected and the subprocess was not piped back to the nix client.

@tomberek tomberek added the idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants