How to make nixos-build not upgrade my packages, just rebuild

I don’t have enough disk space. Once the new packages are downloaded during the refactoring process, it will fail. Now I modified /etc/nixos/hardware-configuration.nix and added a new larger partition. But I could not successfully rebuild the system so that /etc/fstab could never be updated.

I didn’t use the --upgrade option in the command, but it still pulls new packages remotely. This may be because I have updated before, but it failed (maybe the channel has been updated?).

This sounds stupid, but I found it not easy to solve.

I would go like this:

  1. Find the exact nixpgs commit your nixos is build upon with nixos-version. The hash is in the name.
  2. Pull nixpkgs and checkout that revision.
  3. Direct nixos-rebuild to use your checkout by setting NIX_PATH or using the -I flag and the value nixpkgs=/full/path/to/checkout.

That would allow to rebuild from the same packege set, and minimise the new packages.

1 Like

I would use flakes, since they solve exactly this problem :slight_smile:

1 Like

Yes, this is because you already used --upgrade, this made your channels advance.

There are ways to rollback channels. Check the man page of nix-channel.

In the long term it might be beneficial to use one of the various pinning techniques.

Also a nix-collect-garbage (--delete-older-than …d with an appropriate value for ) might mitigate the problem by getting rid of earlier generations of your system.

1 Like

To flesh out a little bit what others have said:

nixos-rebuild switch --upgrade isn’t atomic. It first does a nix-channel --update and then a nixos-rebuild switch. If the latter fails, your channel will still be updated, so subsequent nixos-rebuild switches will be based on the newer version.

nix-channel --rollback allows you to undo the effects of the channel update, though I don’t remember the exact form of the command.

1 Like

Even if you start using a pinning strategy after resolving this, it might also be helpful to set min-free and max-free in the future to prevent running out of disk space inadvertently: Storage optimization - NixOS Wiki

That page also details instructions on how to move the nix store to a bigger partition that will not fail if you’re running out of disk space :slight_smile:

1 Like