How to update every package on NixOS?

Finally figured out how to set up my systems a couple months ago, and have been using them happily ever since, but realized that it's time to update stuff. My NixOS setup involves declarative package management using Home Manager, but completely forgot the basics and wasn't sure if Home Manager would complicate things as the repo only mentions the home-manager tool which I don't have.


Solution 1:

TL;DR

  1. Are you on the latest channel? (See how to check and set below.)

  2. sudo nix-channel --update

  3. a. (declarative) sudo nixos-rebuild switch
    b. (ad-hoc) nix-env -u '*'

Step 0. Check and set the latest channel

Chapter 4. Upgrading NixOS of the latest NixOS manual will always show the name and the link of the latest channel. To check the current one you are subscribed to, issue

sudo nixos-channel --list

The Nix channels wiki page has a good summary on the basic commands. For example, I was subscribed to channel 20.09 so I needed to update it to 21.11 by doing

$ sudo nix-channel --list
nixos https://nixos.org/channels/nixos-20.09

$ sudo nix-channel --remove nixos
$ sudo nix-channel --add nixos https://nixos.org/channels/nixos-21.11 nixos

blabla

It's in the NixOS manual but it is not spelled out directly and one has to read through the entire II.6 Package Management section; even though my setup is declarative, the crucial part was under II.6.2. Ad-Hoc Package Management (i.e., nix-channel --update). This NixOS discourse thread also helped a lot.

Please note that I'm not trying to disparage the NixOS manual; it is quite excellent, especially with all the latest changes, but for a casual user or for one who is looking for solutions only for common scenarios, it can be less useful. There is also the NixOS Wiki entry but it is still not spelled out in the same order as need below.

Home Manager does not complicate this process (at least, when used declaratively, see my setup), and running 2.a below will take care of everything.