nix icon indicating copy to clipboard operation
nix copied to clipboard

Cannot delete store path with no roots

Open asymmetric opened this issue 3 years ago • 6 comments

Describe the bug

Can't delete a store path, even if it isn't reachable by any root.

Steps To Reproduce

❯ nix store delete /nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source
0 store paths deleted, 0.00 MiB freed
error: Cannot delete path '/nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source' since it is still alive. To find out why, use: nix-store --query --roots
❯ nix-store -q --roots /nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source
{temp:3862} -> /nix/store/mqvmj5rijym5nkj0p05nddb3jaf5s85k-centrifuge-chain-2.0.0-2df8df-vendor.tar.gz.drv
{temp:3862} -> /nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source
{temp:3862} -> /nix/store/9qy3ksajzqkdm77nc3wkcc2kix3w7z83-centrifuge-chain-2.0.0-2df8df.drv
❯ rm -f `/nix/var/nix/temproots/3862
❯ nix-store -q --roots /nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source

i.e. no output

❯ nix store delete /nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source
0 store paths deleted, 0.00 MiB freed
error: Cannot delete path '/nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source' since it is still alive. To find out why, use: nix-store --query --roots
❯ nix store delete --ignore-liveness /nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source
0 store paths deleted, 0.00 MiB freed
error: Cannot delete path '/nix/store/r3n40nbgq8x9kac2lg3b1daw9g8kxp4q-centrifuge-chain-2.0.0-2df8df-source' since it is still alive. To find out why, use: nix-store --query --roots

Expected behavior

I should be able to remove the store path.

nix-env --version output

nix-env (Nix) 2.6.0

Additional context

Question posted on Discourse.

asymmetric avatar Feb 21 '22 22:02 asymmetric

I also encountered the same issue, and it's caused by nix checking all /proc/*/environ files for references to that path... which just happens to include invocations of nix store delete. I only managed to delete paths using sudo nix store delete --ignore-liveness.

IMHO the liveness check should exclude its own PID.

manveru avatar Feb 26 '22 23:02 manveru

also seems to be a duplicate of https://github.com/NixOS/nix/issues/6135

manveru avatar Feb 26 '22 23:02 manveru

same issue here

> ./result/bin/nix store delete /nix/store/51cgjmc4ql9r8w8953y865fhbg63cxq9-zlib-1.2.11-static --ignore-liveness
0 store paths deleted, 0.00 MiB freed
error: you are not allowed to ignore liveness
> ./result/bin/nix store delete /nix/store/51cgjmc4ql9r8w8953y865fhbg63cxq9-zlib-1.2.11-static
0 store paths deleted, 0.00 MiB freed
error: Cannot delete path '/nix/store/51cgjmc4ql9r8w8953y865fhbg63cxq9-zlib-1.2.11-static' since it is still alive. To find out why, use: nix-store --query --roots
> nix-store --query --roots /nix/store/51cgjmc4ql9r8w8953y865fhbg63cxq9-zlib-1.2.11-static

Artturin avatar Mar 18 '22 23:03 Artturin

Sometimes it is better to do:

nix-store --query --referrers /nix/store/k43kmk5gnd2k1bi2k94ps5rp0ajwrqp1-source | xargs nix-store --delete

as it is not a root, but a drv blocking deletion (nix.conf: keep-derivations?)

tomberek avatar May 12 '22 23:05 tomberek

Sometimes it is better to do:

nix-store --query --referrers /nix/store/k43kmk5gnd2k1bi2k94ps5rp0ajwrqp1-source | xargs nix-store --delete

as it is not a root, but a drv blocking deletion (nix.conf: keep-derivations?)

Followed in my case by:

nix-store --delete /nix/store/k43kmk5gnd2k1bi2k94ps5rp0ajwrqp1-source

so not only the drv store path gets deleted.

573 avatar Jun 24 '22 05:06 573

I can reproduce this on Darwin (which rules out /proc checking), with Nix 2.10.3:

  • --roots is blank
  • --referrers is also blank
  • I can delete other paths just fine
  • GC removes the path just fine

If there are any other details I can add to aid debugging, please let me know.

winterqt avatar Aug 04 '22 19:08 winterqt

Sometimes it is better to do:

nix-store --query --referrers /nix/store/k43kmk5gnd2k1bi2k94ps5rp0ajwrqp1-source | xargs nix-store --delete

as it is not a root, but a drv blocking deletion (nix.conf: keep-derivations?)

This did the trick for me, though the other referrers weren't .drv's but just other normal store paths.

Should the error message of nix store --delete be changed to this?:

error: Cannot delete path '...' since it is still alive. To find out why, use:
  - nix-store --query --roots '...'
  - nix-store --query --referrers '...'

DavHau avatar Oct 18 '22 08:10 DavHau

Hi!

I got the same issue, but I can't find a proper way to remove it from the store:

image

drupol avatar Nov 26 '22 10:11 drupol

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-store-cannot-remove-a-package/20770/11

nixos-discourse avatar Nov 26 '22 13:11 nixos-discourse

I've also come across this, in my case it was extra puzzling because the path in question (a .drv) didn't even have any referrers as such, it was because of keep-derivations (on by default) meaning that I would have to also include the .drv's output paths in the delete request too for it to work.

This issue (and its variants) having kicked around for multiple years, I really think the "quick win" would be to simply change the error message @ https://github.com/NixOS/nix/blob/b5b6055bbf00fa809633e88dab1e8eaec3ae9f8d/src/libstore/gc.cc#L787 to be less incorrect. When it gets to that point in the code what it actually means is:

Cannot delete path foo because it has referrers or output paths that weren't included in the delete request.

If we were really feeling fancy we could conditionally include the "or output paths" part only if keep-derivations were set and/or direct people to find these via --referrers or --outputs.

risicle avatar Mar 19 '23 00:03 risicle

Sometimes it is better to do:

nix-store --query --referrers /nix/store/k43kmk5gnd2k1bi2k94ps5rp0ajwrqp1-source | xargs nix-store --delete

as it is not a root, but a drv blocking deletion (nix.conf: keep-derivations?)

I had to use --referrers-closure to be able to delete. Thank you for the trick!

symphorien avatar Mar 20 '23 19:03 symphorien

I had to use --referrers-closure to be able to delete. Thank you for the trick!

Thanks! The only thing that worked that easily!

nix-store --query --referrers-closure /nix/store/k43kmk5gnd2k1bi2k94ps5rp0ajwrqp1-source | xargs nix-store --delete

I guess it removes entire system derivation but in my case it's fine (I guess???)

ca5ua1 avatar Mar 21 '23 14:03 ca5ua1

My story in nutshell:

I got broken Nextcloud instance after instilling NC app (app resulted broken) on existent instance. I tried to deal with it via nextcloud-occ and got it working. Then tried to reinstall it via NixOS config. At the end of the night Nextcloud (in maintance mode) tried to update app that doesn't exist but the thing is that for unknown reason it was referencing for previous system derivation???? Nextcloud[1956]: {"reqId":"wZHJOcrgc8FK0nT5k5fB","level":3,"time":"2023-03-21T00:20:09+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"Cannot declare class OCA\\Recognize\\AppInfo\\Application, because the name is already in use at /nix/store/l20b0hdn36rqfx1igzwpwyibbmrpk12n-source-patched/lib/AppInfo/Application.php#25","userAgent":"--","version":"25.0.4.1","data":{"app":"PHP"}}

# nix-store --query  --referrers-closure /nix/store/l20b0hdn36rqfx1igzwpwyibbmrpk12n-source-patched/
/nix/store/l20b0hdn36rqfx1igzwpwyibbmrpk12n-source-patched
/nix/store/mmjhhcbk2cw7w9vfr0cfl9x0mmkh98c9-nix-apps
/nix/store/nj253ndrjmi63d06mf9r2vi606giaf2v-unit-script-nextcloud-setup-start
/nix/store/pr75md91blwfycqivlzdfnhm9gwx50s3-unit-nextcloud-setup.service
/nix/store/7z42i3cpm63ssg5kcig3ba9vvbyby8a1-system-units
/nix/store/hkrdlswphc272kg9k09zq550glpyv46p-etc
/nix/store/s0wazf7fidnvdp9abi8bbsxb0wsifqsq-nixos-system-Servicer01-22.11.2510.103fe0800b9

ca5ua1 avatar Mar 21 '23 14:03 ca5ua1

On a side note, my guess would be that if you also have keep-outputs enabled, the two would form reference loops that would only be manually removable by identifying the loop and specifying all the participants in the delete request together. I have not tried this.

risicle avatar Mar 21 '23 19:03 risicle

I also ran into this problem. For me it was triggered because (I think) my disk was full and Nix wrote a large number of empty .drv files. The error I kept getting was:

error: error parsing derivation ‘/nix/store/XXX.drv’: expected string ‘Derive([’

I was able to delete each of them manually using the command from https://github.com/NixOS/nix/issues/6141#issuecomment-1477969199, but it seemed there was no end to the number of .drv.

To delete all empty .drv:

find /nix/store -maxdepth 1 -name '*.drv' -type f -empty | xargs nix-store --query --referrers-closure | xargs nix-store --delete

bobvanderlinden avatar May 11 '23 11:05 bobvanderlinden

https://github.com/NixOS/nixpkgs/issues/266858

KCynk avatar Nov 11 '23 13:11 KCynk

Nix team discussion notes:

  • nix store delete should always print at least one root/reference is preventing GC (i.e. cannot delete X because root Y is keeping it alive).
  • See #4850 which does something like that (but probably in a too-verbose way).

edolstra avatar Jan 05 '24 13:01 edolstra

I came across a similar issue. In my case, the paths were of the pattern /nix/store/...-system-path. It was after a system rebooting that these paths became recyclable.

The paths led me to discover the redundant system-paths were not system-paths themselves though; they were other paths. nix-store --query --referrers was able to show referrers for those paths, which included system-paths. However, nix-store --query --referrers showed nothing but the system-paths as referrers to system-paths.

herberteuler avatar Apr 03 '24 15:04 herberteuler