Updating NixOS: Difference between revisions

Roxwize (talk | contribs)
rm numbers
Roxwize (talk | contribs)
Marked this version for translation
 
(8 intermediate revisions by 6 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>
</syntaxhighlight>


== Updating channels and rebuilding the system ==
<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>


=== Updating NixOS channels ===
<syntaxhighlight lang="console">
<syntaxhighlight lang="bash">
# nixos-rebuild switch
sudo nix-channel --update
</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>
</syntaxhighlight>


=== Rebuilding the system after updating channels ===
<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">
=== Updating channel and rebuilding the system === <!--T:12-->
sudo nixos-rebuild switch --upgrade
 
<!--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>


== Changing Nixpkgs version ==
<translate>
https://channels.nixos.org/
=== 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>


== Deleting old generations ==
<translate>=== Deleting old generations === <!--T:16--></translate>
<syntaxhighlight lang="bash">
 
sudo nix-collect-garbage -d
<syntaxhighlight lang="console">
# nix-collect-garbage -d
</syntaxhighlight>
</syntaxhighlight>


== Summary: Automating system updating and deleting old generations ==
<translate>=== Example of a system update === <!--T:17--></translate>
<syntaxhighlight lang="bash">
 
sudo nix-channel --update && sudo nixos-rebuild switch --upgrade && sudo reboot
<syntaxhighlight lang="console">
# nix-channel --update && nixos-rebuild switch && reboot
</syntaxhighlight>
 
<translate>
== For flake based configurations == <!--T:18-->
 
<!--T:19-->
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:
</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>
 
<translate>
<!--T:21-->
Finally, rebuild the system configuration to apply the changes:
</translate>
 
<syntaxhighlight lang="console">
# nixos-rebuild switch
</syntaxhighlight>
 
<translate>
== Tips and tricks == <!--T:22-->
 
=== Limiting the maximum number of running jobs === <!--T:23-->
 
<!--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 = {
  settings = {
    max-jobs = 8;
  };
};
</syntaxhighlight>
</syntaxhighlight>


[[Category:NixOS]]
[[Category:NixOS{{#translation:}}|Updating NixOS]]