Updating NixOS: Difference between revisions

Golbinex (talk | contribs)
Add information about nixos-rebuild boot a tip to fix a hang during update
Roxwize (talk | contribs)
Marked this version for translation
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
== Introduction ==
<languages/>
[[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.
<translate>
== Introduction == <!--T:1-->


All commands below are executed in a Terminal application (Shell: Bash).
<!--T:2-->
== Rebuilding the system after editing configuration.nix file ==
[[<tvar name="1">NixOS</tvar>|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.
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">
 
sudo nixos-rebuild switch
<!--T:3-->
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 == <!--T:4-->
 
<!--T:5-->
To apply the configuration changes made in <code>/etc/nixos/configuration.nix</code> without updating the channels, [[<tvar name="1">Nixpkgs</tvar>|Nixpkgs]] and package versions. This is typically used when you've edited the system configuration, and you just want to apply those changes:
</translate>
 
<syntaxhighlight lang="console">
# nixos-rebuild switch
</syntaxhighlight>
 
<translate>
== For non-flake configurations == <!--T:6-->
 
<!--T:7-->
What follows is a short set of instructions. Further details can be found in the [<tvar name="1">https://nixos.org/manual/nixos/stable/#sec-upgrading</tvar> NixOS Manual.]
 
=== Updating NixOS channels === <!--T:8-->
</translate>
 
<syntaxhighlight lang="console">
# nix-channel --update
</syntaxhighlight>
 
<translate>
<!--T:9-->
For more information on channels, see the main [[<tvar name="1">Channel branches</tvar>|Channel branches]] page.
 
=== Rebuilding the system after updating channels === <!--T:10-->
 
<!--T:11-->
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:
</translate>
 
<syntaxhighlight lang="console">
# nixos-rebuild switch
</syntaxhighlight>To apply configuration changes and new package updates only '''after''' rebooting the system, use the following command instead:<syntaxhighlight lang="console">
# nixos-rebuild boot
</syntaxhighlight>
 
<translate>
=== Updating channel and rebuilding the system === <!--T:12-->
 
<!--T:13-->
The below command is a shortcut equivalent to running '''nix-channel --update nixos; nixos-rebuild switch''' previously described:
</translate>
 
<syntaxhighlight lang="console">
# nixos-rebuild switch --upgrade
</syntaxhighlight>
</syntaxhighlight>


== Updating channels and rebuilding the system ==
<translate>
=== Changing Nixpkgs version === <!--T:14-->
 
<!--T:15-->
To see what is the latest channel available, see <tvar name="1">https://channels.nixos.org/</tvar>.
</translate>
 
<syntaxhighlight lang="console"># nix-channel --add https://channels.nixos.org/nixos-<version> nixos</syntaxhighlight>
 
<translate>=== Deleting old generations === <!--T:16--></translate>


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


=== Rebuilding the system after updating channels ===
<translate>=== Example of a system update === <!--T:17--></translate>
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">
 
sudo nixos-rebuild switch --upgrade
<syntaxhighlight lang="console">
</syntaxhighlight>If you want to apply configuration changes and new package updates after rebooting the system, use the following command instead:<syntaxhighlight lang="bash">
# nix-channel --update && nixos-rebuild switch && reboot
sudo nixos-rebuild boot --upgrade
</syntaxhighlight>
</syntaxhighlight>


== Changing Nixpkgs version ==
<translate>
https://channels.nixos.org/
== For flake based configurations == <!--T:18-->


== Deleting old generations ==
<!--T:19-->
<syntaxhighlight lang="bash">
Because [[<tvar name="1">Flakes</tvar>|Flakes]] do not use channels and instead rely on explicitly defined inputs, updating a configuration involves modifying the system’s <code>flake.nix</code> to reference the desired versions of inputs. For example:
sudo nix-collect-garbage -d
</translate>
 
{{file|flake.nix|nix|
<nowiki>
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; # update version
    ...
  };
  ...
}
</nowiki>
}}
 
<translate>
<!--T:20-->
Once the input URLs have been updated, refresh the flake lockfile with:
</translate>
 
<syntaxhighlight lang="console">
# nix flake update
</syntaxhighlight>
</syntaxhighlight>


== Summary: Automating system updating and deleting old generations ==
<translate>
<syntaxhighlight lang="bash">
<!--T:21-->
sudo nix-channel --update && sudo nixos-rebuild switch --upgrade && sudo reboot
Finally, rebuild the system configuration to apply the changes:
</translate>
 
<syntaxhighlight lang="console">
# nixos-rebuild switch
</syntaxhighlight>
</syntaxhighlight>


== Limiting the maximum number of running jobs ==
<translate>
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.
== Tips and tricks == <!--T:22-->


Command:<syntaxhighlight lang="bash">
=== Limiting the maximum number of running jobs === <!--T:23-->
sudo nixos-rebuild switch --option max-jobs 8
 
</syntaxhighlight>configuration.nix<syntaxhighlight lang="nix">
<!--T:24-->
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:
</translate>
 
<syntaxhighlight lang="console">
# nixos-rebuild switch --option max-jobs 8
</syntaxhighlight>To make the change permanent, add the following to your configuration.nix:<syntaxhighlight lang="nix">
nix = {
nix = {
   settings = {
   settings = {
Line 49: Line 136:
};
};
</syntaxhighlight>
</syntaxhighlight>
[[Category:NixOS]]
 
[[Category:NixOS{{#translation:}}|Updating NixOS]]