2021-07-03 18:10:18 +02:00
|
|
|
# Upgrading NixOS {#sec-upgrading}
|
|
|
|
|
|
|
|
The best way to keep your NixOS installation up to date is to use one of
|
|
|
|
the NixOS *channels*. A channel is a Nix mechanism for distributing Nix
|
|
|
|
expressions and associated binaries. The NixOS channels are updated
|
|
|
|
automatically from NixOS's Git repository after certain tests have
|
|
|
|
passed and all packages have been built. These channels are:
|
|
|
|
|
2023-11-29 18:17:21 +01:00
|
|
|
- *Stable channels*, such as [`nixos-23.11`](https://channels.nixos.org/nixos-23.11).
|
2021-07-03 18:10:18 +02:00
|
|
|
These only get conservative bug fixes and package upgrades. For
|
|
|
|
instance, a channel update may cause the Linux kernel on your system
|
|
|
|
to be upgraded from 4.19.34 to 4.19.38 (a minor bug fix), but not
|
|
|
|
from 4.19.x to 4.20.x (a major change that has the potential to break things).
|
|
|
|
Stable channels are generally maintained until the next stable
|
|
|
|
branch is created.
|
|
|
|
|
2023-05-30 16:29:55 +02:00
|
|
|
- The *unstable channel*, [`nixos-unstable`](https://channels.nixos.org/nixos-unstable).
|
2021-07-03 18:10:18 +02:00
|
|
|
This corresponds to NixOS's main development branch, and may thus see
|
|
|
|
radical changes between channel updates. It's not recommended for
|
|
|
|
production systems.
|
|
|
|
|
2023-11-29 18:17:21 +01:00
|
|
|
- *Small channels*, such as [`nixos-23.11-small`](https://channels.nixos.org/nixos-23.11-small)
|
2023-05-30 16:29:55 +02:00
|
|
|
or [`nixos-unstable-small`](https://channels.nixos.org/nixos-unstable-small).
|
2021-07-03 18:10:18 +02:00
|
|
|
These are identical to the stable and unstable channels described above,
|
|
|
|
except that they contain fewer binary packages. This means they get updated
|
|
|
|
faster than the regular channels (for instance, when a critical security patch
|
|
|
|
is committed to NixOS's source tree), but may require more packages to be
|
|
|
|
built from source than usual. They're mostly intended for server environments
|
|
|
|
and as such contain few GUI applications.
|
|
|
|
|
2023-05-30 16:29:55 +02:00
|
|
|
To see what channels are available, go to <https://channels.nixos.org>.
|
2021-07-03 18:10:18 +02:00
|
|
|
(Note that the URIs of the various channels redirect to a directory that
|
|
|
|
contains the channel's latest version and includes ISO images and
|
|
|
|
VirtualBox appliances.) Please note that during the release process,
|
|
|
|
channels that are not yet released will be present here as well. See the
|
|
|
|
Getting NixOS page <https://nixos.org/nixos/download.html> to find the
|
|
|
|
newest supported stable release.
|
|
|
|
|
|
|
|
When you first install NixOS, you're automatically subscribed to the
|
|
|
|
NixOS channel that corresponds to your installation source. For
|
2023-11-29 18:17:21 +01:00
|
|
|
instance, if you installed from a 23.11 ISO, you will be subscribed to
|
|
|
|
the `nixos-23.11` channel. To see which NixOS channel you're subscribed
|
2021-07-03 18:10:18 +02:00
|
|
|
to, run the following as root:
|
|
|
|
|
|
|
|
```ShellSession
|
|
|
|
# nix-channel --list | grep nixos
|
2023-05-30 16:29:55 +02:00
|
|
|
nixos https://channels.nixos.org/nixos-unstable
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
To switch to a different NixOS channel, do
|
|
|
|
|
|
|
|
```ShellSession
|
2023-05-30 16:29:55 +02:00
|
|
|
# nix-channel --add https://channels.nixos.org/channel-name nixos
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
(Be sure to include the `nixos` parameter at the end.) For instance, to
|
2023-11-29 18:17:21 +01:00
|
|
|
use the NixOS 23.11 stable channel:
|
2021-07-03 18:10:18 +02:00
|
|
|
|
|
|
|
```ShellSession
|
2023-11-29 18:17:21 +01:00
|
|
|
# nix-channel --add https://channels.nixos.org/nixos-23.11 nixos
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
If you have a server, you may want to use the "small" channel instead:
|
|
|
|
|
|
|
|
```ShellSession
|
2023-11-29 18:17:21 +01:00
|
|
|
# nix-channel --add https://channels.nixos.org/nixos-23.11-small nixos
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
And if you want to live on the bleeding edge:
|
|
|
|
|
|
|
|
```ShellSession
|
2023-05-30 16:29:55 +02:00
|
|
|
# nix-channel --add https://channels.nixos.org/nixos-unstable nixos
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
You can then upgrade NixOS to the latest version in your chosen channel
|
|
|
|
by running
|
|
|
|
|
|
|
|
```ShellSession
|
|
|
|
# nixos-rebuild switch --upgrade
|
|
|
|
```
|
|
|
|
|
|
|
|
which is equivalent to the more verbose `nix-channel --update nixos; nixos-rebuild switch`.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
Channels are set per user. This means that running `nix-channel --add`
|
|
|
|
as a non root user (or without sudo) will not affect
|
|
|
|
configuration in `/etc/nixos/configuration.nix`
|
|
|
|
:::
|
|
|
|
|
|
|
|
::: {.warning}
|
|
|
|
It is generally safe to switch back and forth between channels. The only
|
|
|
|
exception is that a newer NixOS may also have a newer Nix version, which
|
|
|
|
may involve an upgrade of Nix's database schema. This cannot be undone
|
|
|
|
easily, so in that case you will not be able to go back to your original
|
|
|
|
channel.
|
|
|
|
:::
|
|
|
|
|
|
|
|
## Automatic Upgrades {#sec-upgrading-automatic}
|
|
|
|
|
|
|
|
You can keep a NixOS system up-to-date automatically by adding the
|
|
|
|
following to `configuration.nix`:
|
|
|
|
|
|
|
|
```nix
|
2024-03-27 19:10:27 +01:00
|
|
|
{
|
|
|
|
system.autoUpgrade.enable = true;
|
|
|
|
system.autoUpgrade.allowReboot = true;
|
|
|
|
}
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
This enables a periodically executed systemd service named
|
|
|
|
`nixos-upgrade.service`. If the `allowReboot` option is `false`, it runs
|
|
|
|
`nixos-rebuild switch --upgrade` to upgrade NixOS to the latest version
|
|
|
|
in the current channel. (To see when the service runs, see `systemctl list-timers`.)
|
|
|
|
If `allowReboot` is `true`, then the system will automatically reboot if
|
|
|
|
the new generation contains a different kernel, initrd or kernel
|
|
|
|
modules. You can also specify a channel explicitly, e.g.
|
|
|
|
|
|
|
|
```nix
|
2024-03-27 19:10:27 +01:00
|
|
|
{
|
|
|
|
system.autoUpgrade.channel = "https://channels.nixos.org/nixos-23.11";
|
|
|
|
}
|
2021-07-03 18:10:18 +02:00
|
|
|
```
|