Jump to content

Updating NixOS: Difference between revisions

From NixOS Wiki
Add information about nixos-rebuild boot a tip to fix a hang during update
follow commands template
Line 4: Line 4:
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.
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 ==
== Rebuilding the system after editing configuration.nix file ==
If you want to apply the configuration changes made in <code>/etc/nixos/configuration.nix</code> 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:<syntaxhighlight lang="bash">
To apply the configuration changes made in <code>/etc/nixos/configuration.nix</code> 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:<syntaxhighlight lang="console">
sudo nixos-rebuild switch
# nixos-rebuild switch
</syntaxhighlight>
</syntaxhighlight>


Line 13: Line 12:


=== Updating NixOS channels ===
=== Updating NixOS channels ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
sudo nix-channel --update
# nix-channel --update
</syntaxhighlight>
</syntaxhighlight>


=== Rebuilding the system after updating channels ===
=== 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:<syntaxhighlight lang="bash">
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:<syntaxhighlight lang="console">
sudo nixos-rebuild switch --upgrade
# nixos-rebuild switch --upgrade
</syntaxhighlight>If you want to apply configuration changes and new package updates after rebooting the system, use the following command instead:<syntaxhighlight lang="bash">
</syntaxhighlight>To apply configuration changes and new package updates only '''after''' rebooting the system, use the following command instead:<syntaxhighlight lang="console">
sudo nixos-rebuild boot --upgrade
# nixos-rebuild boot --upgrade
</syntaxhighlight>
</syntaxhighlight>


== Changing Nixpkgs version ==
=== Changing Nixpkgs version ===
https://channels.nixos.org/
To see what is the latest channel available, see https://channels.nixos.org<syntaxhighlight lang="console"># nix-channel --add https://channels.nixos.org/nixos-<version> nixos</syntaxhighlight>


== Deleting old generations ==
=== Deleting old generations ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
sudo nix-collect-garbage -d
# nix-collect-garbage -d
</syntaxhighlight>
</syntaxhighlight>


== Summary: Automating system updating and deleting old generations ==
=== Example of a system update ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
sudo nix-channel --update && sudo nixos-rebuild switch --upgrade && sudo reboot
# nix-channel --update && nixos-rebuild switch --upgrade && reboot
</syntaxhighlight>
</syntaxhighlight>


== Limiting the maximum number of running jobs ==
== 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.
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:<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
sudo nixos-rebuild switch --option max-jobs 8
# nixos-rebuild switch --option max-jobs 8
</syntaxhighlight>configuration.nix<syntaxhighlight lang="nix">
</syntaxhighlight>To make the change permanent, add the following to your configuration.nix:<syntaxhighlight lang="nix">
nix = {
nix = {
   settings = {
   settings = {

Revision as of 00:44, 5 June 2025

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.

Rebuilding the system after editing configuration.nix file

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:

# nixos-rebuild switch

Updating channels and rebuilding the system

Updating NixOS channels

# 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:

# nixos-rebuild switch --upgrade

To apply configuration changes and new package updates only after rebooting the system, use the following command instead:

# nixos-rebuild boot --upgrade

Changing Nixpkgs version

To see what is the latest channel available, see https://channels.nixos.org

# nix-channel --add https://channels.nixos.org/nixos-<version> nixos

Deleting old generations

# nix-collect-garbage -d

Example of a system update

# nix-channel --update && nixos-rebuild switch --upgrade && 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:

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

To make the change permanent, add the following to your configuration.nix:

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