Nix-store cannot remove a package

I have permanent problems with removing packages. nix-store --delete --ignore-liveness says it’s still alive, whereas nix-store -q --query --roots shows nothing. This problem appears pretty frequently.

jk@phonelab-desktop:/nix/store$ sudo /home/jk/.nix-profile/bin/nix-store  --delete --ignore-liveness /nix/store/1jqhzqi0x92ljxgwyzm80v0j6z5fcj8n-catgirl-static-aarch64-unknown-linux-musl-2.1
finding garbage collector roots...
0 store paths deleted, 0.00 MiB freed
error: Cannot delete path '/nix/store/1jqhzqi0x92ljxgwyzm80v0j6z5fcj8n-catgirl-static-aarch64-unknown-linux-musl-2.1' since it is still alive. To find out why, use: nix-store --query --roots
jk@phonelab-desktop:/nix/store$ sudo /home/jk/.nix-profile/bin/nix-store  -q --query --roots /nix/store/1jqhzqi0x92ljxgwyzm80v0j6z5fcj8n-catgirl-static-aarch64-unknown-linux-musl-2.1
/proc/1913703/environ -> /nix/store/1jqhzqi0x92ljxgwyzm80v0j6z5fcj8n-catgirl-static-aarch64-unknown-linux-musl-2.1
/proc/1913720/environ -> /nix/store/1jqhzqi0x92ljxgwyzm80v0j6z5fcj8n-catgirl-static-aarch64-unknown-linux-musl-2.1

Two processes have a reference to the store path in their environment in some way that it considers might potentially get used.

Those two listed files contain the (NUL-separated) process environments. Worth looking at what those processes are and what’s in their environment.

(you may find cat /proc/nnn/environ | tr '\0' '\n' helpful)

If this is the same user who asked in the discord chat recently, then this PIDs are ephemeral and change on each query, at the same time, those processes can’t be introspected, as they are gone after the query.

The processes numbers are related with the command. They don’t exist after executing the last command.

sudo /home/jk/.nix-profile/bin/nix-store ...

BTW if the query is issued under a regular user, it shows nothing.

Oh, then they’re related to the running command that mentioned those paths on the command line, perhaps via some shell-hook integration?

You might need to dump the full dependency topology out to a file, and search through it after the fact to avoid this. But I predict that there’s nothing (else) there, and your issue is something like the above, that inherently makes the store path live while you’re trying to delete it, because you’ve mentioned it on the command line.

As an aside, is a regular gc not sufficient for you?

No. It deletes a lot of stuff, so after that it takes a long time to rebuild.

to rebuild what? development projects? if you use nix-direnv there’s an option to prevent garbage collection deleting the project’s downloaded derivations

I build the SpectrumOS, which contains a lot of packages. gc is a poor option, as it deletes a lot of packages, so rebuidling them takes long time. As a workaround of this bug instead of that I delete and reinstall the Nix store.

I have the same exact issue. I’m not sure how to debug this further. What kind of shell hook could be causing this?

This post had the solution:

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