How do .drv files persist in the nix store even without any referers?

I am new to Nix and NixOS and was toying around in a test project to learn more.

$ ls  default.nix  main.c

$ cat default.nix 
with import <nixpkgs> {};
derivation {
  name = "hello-world";
  builder = "${bash}/bin/bash";
  args = [ ./ ];
  inherit coreutils gcc findutils patchelf;
  src = ./main.c;
  system = builtins.currentSystem;

The builder script is very simple - it just creates an executable in the output directory.

My confusion is that when I run nix-store --query --referrers on the .drv file instantiated upon running nix-build, I get nothing, indicating that the .drv file has no referrers. My question is: how does this file persist upon evoking the garbage collector, how does the GC know not to remove it?
My understanding was that this is done by reference counting from other store paths, but it appears not to be the case here. Does the GC maintain a soft symlink to this file like it does to the result directory generated when nix-build is run?

1 Like