Nixpkgs: Users can't run "nix-collect-garbage -d" any more

Created on 8 Feb 2015  ·  25Comments  ·  Source: NixOS/nixpkgs

I used to be able to run nix-collect-garbage -d as a normal user, but now I get this error message when I try:

[simons@latitude:~]$ nix-collect-garbage -d
removing old generations of profile /nix/var/nix/profiles/default
error: opening lock file ‘/nix/var/nix/profiles/default.lock’: Permission denied
Died at /run/current-system/sw/bin/nix-collect-garbage line 43.

I'm running NixOS 15.05pre57108.96d6344 (Dingo).

bug

Most helpful comment

I had a similar-looking issue today whereby user xxx trying to use nix-env -i would fail miserably with:

error: opening lock file ‘/nix/var/nix/profiles/per-user/yyy/profile.lock’: Permission denied

The reason, identified by clever on IRC, was that ~xxx/.nix-profile was a symlink to ~yyy/.nix-profile, after I tried to symlink some configuration files from user yyy to user xxx. The solution involved removing that symlink, logging out and logging back in, whereby NixOS somehow created back the correct symlink.

All 25 comments

Works for me on the latest master.... Is it possible the nix daemon died or you don't have your env vars setup to use it?

@wkennington, I don't think so, I believe the system is set up properly. I guess what puzzles me is that the command aborts after the error. When I run the same command on a different machine (some version of NixOS), I get this:

[simons@work:~]$ nix-collect-garbage -d
removing old generations of profile /nix/var/nix/profiles/per-user/root/channels
error: opening lock file ‘/nix/var/nix/profiles/per-user/root/channels.lock’: Permission denied
removing old generations of profile /nix/var/nix/profiles/per-user/root/profile
error: opening lock file ‘/nix/var/nix/profiles/per-user/root/profile.lock’: Permission denied
removing old generations of profile /nix/var/nix/profiles/per-user/simons/profile
removing generation 7
removing generation 8
removing generation 9
removing generation 10
removing generation 11
removing generation 12
removing generation 13
removing generation 14
removing generation 15
removing old generations of profile /nix/var/nix/profiles/system
error: opening lock file ‘/nix/var/nix/profiles/system.lock’: Permission denied
finding garbage collector roots...
removing stale link from ‘/nix/var/nix/gcroots/auto/5ms46abnr1kj79x2dvx159df44w6xdz3’ to ‘/run/user/0/nix-build.6QJlu8/derivation’
removing stale link from ‘/nix/var/nix/gcroots/auto/lz3cajjnh63c21ga05mql0yywnp08fh1’ to ‘/run/user/1000/nix-build.vWnbGF/derivation’
removing stale link from ‘/nix/var/nix/gcroots/auto/wg7yhh6lrrdkbxcr14s421jdap1i5773’ to ‘/run/user/1000/nix-prefetch-url.Ua7BjZ/urls’
deleting garbage...

In other words, it's perfectly normal for the process to get that permission denied response, but its un-normal for it to abort the entire garbage collection process because of that.

Wild guess: different version of something (perl? nix-env?) behaves differently about failure states when removing generations ("-d"). BTW, it's probably more of a nix issue than nixpkgs/nixos.

Also noticed this today though didn't think much of it.

The issue has disappeared on my machine. I contemplated deleting the default profile, because I don't use it anyway, and so I ended up listing its contents and found some ancient package installed in there, which I probably added months ago for some temporary purposes, but then I forget to remove it again. Anyway, I removed that package from the profile -- which is now empty -- and since then garbage collection works fine, like it used to. Now what?

Cannot be easily re-produced, so there's probably no point in keeping this issue open without anyone actually working on it.

BTW, I get the same error message, but that's expected behaviour since I don't have write access to the default profile.

@edolstra, you also get the Died at /run/current-system/sw/bin/nix-collect-garbage line 43. part?

Yes.

Interesting. I don't see that error in Nix 1.8 from nixos-unstable:

[simons@latitude:~]$ nix-collect-garbage -d
removing old generations of profile /nix/var/nix/profiles/default
error: opening lock file ‘/nix/var/nix/profiles/default.lock’: Permission denied
removing old generations of profile /nix/var/nix/profiles/per-user/root/channels
error: opening lock file ‘/nix/var/nix/profiles/per-user/root/channels.lock’: Permission denied
removing old generations of profile /nix/var/nix/profiles/per-user/root/profile
error: opening lock file ‘/nix/var/nix/profiles/per-user/root/profile.lock’: Permission denied
removing old generations of profile /nix/var/nix/profiles/per-user/simons/profile
removing old generations of profile /nix/var/nix/profiles/system
error: opening lock file ‘/nix/var/nix/profiles/system.lock’: Permission denied
finding garbage collector roots...
deleting garbage...
deleting ‘/nix/store/rrn0f1g0m51yk7plbmfnw7hmqxlz2sxr-python2.7-testtools-0.9.34.drv’
deleting ‘/nix/store/wlf92r42y8kp8q7nkahd2lyb8bykggcb-ecospat_1.1.tar.gz’
[...]

still problematic?

Since then it was rewritten from perl to c++ and some issues in that were fixed already. I've never noticed this problem so I can't say. (I don't use -d, prune links manually, and 2> /dev/null.)

I still get transient permission denied errors like the ones above. Interestingly enough, the errors simply go away when I re-run the command, so it's no big deal, but, yes, it does still happen.

Works fine here on NixOS 16.03 (and master):

user $ nix-collect-garbage -d
....
deleting ‘/nix/store/39a4qg7panfzf0358wap5by647f7d2g9-tornado-4.2.1.tar.gz’
deleting ‘/nix/store/7mylnkgyrssi9rj5ccv6s7ai8kf9rfl8-xsession.drv’
deleting ‘/nix/store/trash’
deleting unused links...
note: currently hard linking saves -64.49 MiB
1569 store paths deleted, 2023.29 MiB freed
[successful exit status]

(I don't remember if I've ever had transient errors.)

I think we should close this issue for now.

I had a similar-looking issue today whereby user xxx trying to use nix-env -i would fail miserably with:

error: opening lock file ‘/nix/var/nix/profiles/per-user/yyy/profile.lock’: Permission denied

The reason, identified by clever on IRC, was that ~xxx/.nix-profile was a symlink to ~yyy/.nix-profile, after I tried to symlink some configuration files from user yyy to user xxx. The solution involved removing that symlink, logging out and logging back in, whereby NixOS somehow created back the correct symlink.

This happened to me recently, after running nix-collect-garbage -d with sudo -i.

Same @asmodehn

Same @asmodehn

still happens here (darwin) with :

deleting '/nix/store/trash'
12428 store paths deleted, 664.94 MiB freed
error: cannot unlink '/nix/store/trash/4mnbcnp5fb8495pjlzwrfy24c9yk122n-vscode/extension-ms-vscode-csharp-1.21.5/share/vscode/extensions/ms-vscode.csharp/.omnisharp/1.34.5/license.md': Permission denied

I guess it happens when nix/store/... is mutated outside of nix. vscode extensions are good candidates to replicate this error I think.

I guess it happens when nix/store/... is mutated outside of nix. vscode extensions are good candidates to replicate this error I think.

You're not supposed to ever do this.

I guess it happens when nix/store/... is mutated outside of nix. vscode extensions are good candidates to replicate this error I think.

You're not supposed to ever do this.

I did'nt do it directly I guess it was done by another process.

I'm facing similar problem after uninstalling vscode.

❯ nix-collect-garbage -d
removing old generations of profile /nix/var/nix/profiles/per-user/guilhermefilho/home-manager
removing generation 16
removing old generations of profile /nix/var/nix/profiles/per-user/guilhermefilho/profile
removing old generations of profile /nix/var/nix/profiles/per-user/guilhermefilho/channels
removing old generations of profile /nix/var/nix/profiles/system
removing generation 25
finding garbage collector roots...
removing stale link from '/nix/var/nix/gcroots/auto/007j1mr5szp3cp70chg757k8abnfq4y3' to '/private/var/folders/8x/tmvwknqx1679s05zf5zkdl3m0000gp/T/nix-build-5659-0/result'
removing stale link from '/nix/var/nix/gcroots/auto/lvwb7ia80vl56jf7pfk8kn2j4s8dycw6' to '/private/var/folders/8x/tmvwknqx1679s05zf5zkdl3m0000gp/T/nix-build-5139-0/result'
0 store paths deleted, 0.00 MiB freed
error: cannot unlink '/nix/store/trash/j5yl1h6inih9lm94bsplz8izilvgsyi3-vscode-1.45.0/Applications/Visual Studio Code.app/Contents/CodeResources': Permission denied

Any idea?

This issue comes back often... maybe reopen the issue as nix-collect-garbage having to have better error messages that offer actionable suggestions to the user?

I suspect that vscode was modified outside of nix but I'm not sure.

@fare I would reopen this one. As you said, maybe just give the user a hint on how to solve the issue without creating more problems (like running things as sudo) is enough.

I have a PR base on this project related to this issue.
This package brings a wrapper over vscode that is built to run in a nix-shell of a project.
It uses a unique data-dir per project, so everything is compartmentalized.
It gives the ability to:

  • set settings (which disable auto-updates).
  • declaratively define extensions and mutable extensions(that won't live in the nix store).
Was this page helpful?
0 / 5 - 0 ratings