Why is my Nix shell rebuilding every day? [SOLVED]

I’ve set up a few repos with a shell.nix. One example:

  pkgs = import
      fetchTarball {
        name = "21.05";
        url = "https://github.com/NixOS/nixpkgs/archive/7e9b0dff974c89e070da1ad85713ff3c20b0ca97.tar.gz";
        sha256 = "1ckzhh24mgz6jd1xhfgx0i9mijk6xjqxwsshnvq789xsavrmsc36";
    { };
pkgs.mkShell {
  packages = [

Even though I’ve pinned to a particular hash of nixpkgs and I activate this Nix shell almost every day this seems to rebuild every day. What could be causing that?

My configuration.nix has the following cleanup code based on the wiki:

nix = {
  allowedUsers = [ "@wheel" ];
  autoOptimiseStore = true;
  extraOptions = ''
    min-free = ${toString (100 * 1000 * 1000)}
    max-free = ${toString (1000 * 1000 * 1000)}
  gc = {
    automatic = true;
    dates = "daily";
    options = "--delete-older-than 30d";

If I understand correctly this should only garbage collect 30 day old derivations, but something is removing very recent derivations.

1 Like

The flag specifies how old profiles must be before they’ll be deleted. The derivations built via nix-shell are not in a profile.

1 Like

Cool, then all that remains is figuring out why the derivations are being deleted.

The easiest way to avoid garbage collections for nix-shells is using nix-direnv:

This also requires the following nixos options:

{ pkgs, ... }: {
  nix.extraOptions = ''
    keep-outputs = true
    keep-derivations = true
1 Like

Because, as @abathur said, they’re not in a profile. The garbage collector deletes all derivations with no gc roots. nix-shell does not create gc roots. nix-direnv, however, does. But there are quirks with how shell dependencies differ from runtime dependencies that require the extra nixos options @Mic92 mentioned.

1 Like

Fixed in configuration.nix and my home directory, thank you!

Update: Had to fix the .direnvrc file.