Skip to content

Improve error when can't acquire db lock#13340

Merged
tomberek merged 1 commit intoNixOS:masterfrom
mkenigs:better-error
Aug 18, 2025
Merged

Improve error when can't acquire db lock#13340
tomberek merged 1 commit intoNixOS:masterfrom
mkenigs:better-error

Conversation

@mkenigs
Copy link
Contributor

@mkenigs mkenigs commented Jun 9, 2025

Give some hints about what could be going wrong when Nix can't acquire the db lock.

Currently running Nix as a non-root user on a single-user install prints the error:

error: opening lock file '/nix/var/nix/db/big-lock': Permission denied

The error printed in this situation was indirectly changed recently by #12391,
but people have frequently run into the prior error:

error: could not set permissions on '/nix/var/nix/profiles/per-user' to 755: Operation not permitted

There seems to be a lot of confusion about the prior error, and the current error doesn't seem to give much more of a hint about how to resolve the problem. Modify the error to provide some hints at what could be going wrong, instead erroring with:

error: don't have permission to lock Nix database in '/nix/var/nix/db'.
This command may have been run as non-root in a single-user Nix installation,
or the Nix daemon may have crashed.

@github-actions github-actions bot added with-tests Issues related to testing. PRs with tests have some priority store Issues and pull requests concerning the Nix store labels Jun 9, 2025
Comment on lines 227 to 229
/* It might be more robust to check permissions earlier in this method,
but for now take the least invasive approach of providing a better error
when the database lock can't be acquired. */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure whether an explicit check earlier in the method would be less brittle (if e.g. something earlier in the method needs to be root), but that seemed like it had more potential to break something than only changing behavior when an error's already been thrown

Copy link
Member

Choose a reason for hiding this comment

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

Changed it a bit to just augment the existing error. Can you check?

Copy link
Member

@Mic92 Mic92 Jun 11, 2025

Choose a reason for hiding this comment

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


  error:
         … This command may have been run as non-root in a single-user Nix installation,
         or the Nix daemon may have crashed.

         error: opening lock file '/path/to/big-lock': Permission denied

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks good, thanks!

Add helpful context when opening the Nix database lock fails due to
permission errors. Instead of just showing "Permission denied", now
provides guidance about possible causes:
- Running as non-root in a single-user Nix installation
- Nix daemon may have crashed
if (e.errNo == EACCES || e.errNo == EPERM) {
e.addTrace({},
"This command may have been run as non-root in a single-user Nix installation,\n"
"or the Nix daemon may have crashed.");
Copy link
Member

Choose a reason for hiding this comment

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

Why would a daemon crash cause this issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had gleaned that from open issues, although maybe that's no longer the case? I just tried e.g. this reproducer and didn't get the error #3435 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do see the error if I sudo rm /nix/var/nix/daemon-socket/socket

} catch (SysError & e) {
if (e.errNo == EACCES || e.errNo == EPERM) {
e.addTrace({},
"This command may have been run as non-root in a single-user Nix installation,\n"
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by the tense ("may have been") here. Is the current command being run as non-root, or was a previous command run as non-root?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean the current command

Copy link
Member

@Mic92 Mic92 Jun 11, 2025

Choose a reason for hiding this comment

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

we could probably also check check "isNonRootuser()" to make a more concrete error.

Copy link
Member

Choose a reason for hiding this comment

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

I think in the general case where the file isn't owned by the current uid, we can add the hint that maybe you wanted to connect to a nix daemon instead.

Copy link
Contributor Author

@mkenigs mkenigs Jun 23, 2025

Choose a reason for hiding this comment

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

I guess we could say "you may want to start a Nix daemon", but the user doesn't really connect to the Nix daemon themselves so I don't think "you may want to connect to a Nix daemon instead" would be very helpful, because it's not actionable. Did you have something in mind?

@mkenigs mkenigs requested review from edolstra and roberth July 1, 2025 21:40
@mkenigs
Copy link
Contributor Author

mkenigs commented Jul 17, 2025

@edolstra could you take another look at this?

@github-project-automation github-project-automation bot moved this to Triage in Nix team Jul 23, 2025
@edolstra edolstra self-assigned this Aug 6, 2025
@edolstra edolstra removed this from Nix team Aug 6, 2025
@tomberek tomberek merged commit dc2478f into NixOS:master Aug 18, 2025
12 checks passed
@tomberek
Copy link
Contributor

optimistic merging: additional tweaks to message can be in follow up

ysndr added a commit to flox/flox that referenced this pull request Nov 6, 2025
Drops the patches for 

* NixOS/nix#12580
* NixOS/nix#12642
* NixOS/nix#13340

All three have been merged upstream
in the current release of nix (v2.31.2).
ysndr added a commit to flox/flox that referenced this pull request Nov 6, 2025
Drops the patches for 

* NixOS/nix#12580
* NixOS/nix#12642
* NixOS/nix#13340

All three have been merged upstream
in the current release of nix (v2.31.2).
ysndr added a commit to flox/flox that referenced this pull request Nov 6, 2025
Drops the patches for 

* NixOS/nix#12580
* NixOS/nix#12642
* NixOS/nix#13340

All three have been merged upstream
in the current release of nix (v2.31.2).
ysndr added a commit to flox/flox that referenced this pull request Nov 7, 2025
Drops the patches for 

* NixOS/nix#12580
* NixOS/nix#12642
* NixOS/nix#13340

All three have been merged upstream
in the current release of nix (v2.31.2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

store Issues and pull requests concerning the Nix store with-tests Issues related to testing. PRs with tests have some priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants