Updating NixOS

Revision as of 17:37, 25 May 2025 by Golbinex (talk | contribs) (Add information about nixos-rebuild boot a tip to fix a hang during update)

Introduction

NixOS stands out due to its declarative configuration and atomic updates, which ensure that system updates are predictable, reversible, and don’t risk breaking the setup. This approach guarantees consistency across versions, allowing any changes to be easily rolled back. NixOS also offers flexibility, multi-version support, and advanced dependency management, making it an excellent choice for developers and system administrators.

As part of this process, only repository channels are updated or removed during updates. The system requires an internet connection to download the latest changes, and users cannot directly modify the system. For optimal stability, security, and access to new features, regular updates — ideally once a week — are recommended.

All commands below are executed in a Terminal application (Shell: Bash).

Rebuilding the system after editing configuration.nix file

If you want to apply the configuration changes made in /etc/nixos/configuration.nix without updating the channels, Nixpkgs and package versions. This is typically used when you've edited the system configuration, and you just want to apply those changes:

sudo nixos-rebuild switch

Updating channels and rebuilding the system

Updating NixOS channels

sudo nix-channel --update

Rebuilding the system after updating channels

If you want to not only apply your configuration changes but also update the packages and system environment to the latest versions available from the Nixpkgs repository. This is typically used when you want to ensure you are using the latest versions of your software and system services:

sudo nixos-rebuild switch --upgrade

If you want to apply configuration changes and new package updates after rebooting the system, use the following command instead:

sudo nixos-rebuild boot --upgrade

Changing Nixpkgs version

https://channels.nixos.org/

Deleting old generations

sudo nix-collect-garbage -d

Summary: Automating system updating and deleting old generations

sudo nix-channel --update && sudo nixos-rebuild switch --upgrade && sudo reboot

Limiting the maximum number of running jobs

Sometimes, the update process may hang when the system CPU has a high number of cores. You can limit the maximum number of running jobs.

Command:

sudo nixos-rebuild switch --option max-jobs 8

configuration.nix

nix = {
  settings = {
    max-jobs = 8;
  };
};