Root downloads the same packages again

Hello,

I recently noticed that when I run nix shell nixpkgs/release-23.05#... as a normal user and then again as a root, root downloads the package (or something else) again.

Why?

When I run it again as the normal user, obviously nothing is downloaded since the package was downloaded 5 min ago…

Thank you.

1 Like

Each user has their own cache of the flake.

I don’t think that’s true? Flakes are stored in the nix store, which is shared among users.

I looked into home dir:

du -sh ~/.local/share/nix ~/.cache/nix

48K     /home/mikey/.local/share/nix
1.9M    /home/mikey/.cache/nix

and Nix directories don’t store it since the download is around 37M.

37M is around the right size for a nixpkgs tarball, so it is probably nixpkgs. What is stored per-user is the freshness of a tarball (see tarball-ttl in man nix.conf). So if it’s been over tarball-ttl seconds (default one hour) since root has updated the nixpkgs tarball, it will download it again no matter how recently another user has downloaded it. It’s just that if it’s the same tarball, it’ll be able to recognize that the hashes are the same and it won’t have to add anything to the store before using it.

Note that this only happens because the nix “registry” isn’t pinned by default, so nix shell nixpkgs/release-23.05#... is checking the internet for the latest release-23.05 commit. If you pin your registry, it won’t have to download anything.

Also, please don’t use release-23.05. Use nixos-23.05. The former has not undergone hydra caching and testing, while the latter has.

1 Like

That makes a lot of sense. Thank you. I just wonder if there is a way to ask Github (assuming it’s downloaded from Github) for hash of the tarball - so it doesn’t need to be downloaded just to check that hashes are already in the nix store.

I’m assuming that means that commands likes nix shell and nix run don’t use a flake.lock and therefore they use the most current software (the latest commit). Which is cool since I consider those commands as ad-hoc / testing and if I’m more serious I’d use NixOS repo with flake.lock or nix develop ....

If I’m wrong, please point me to documentation about pining the registry. :wink:

I had no idea! I probably have “release” everywhere including all my flakes. Thank you for letting me know.