nixos-and-flakes-book/docs/nixos-with-flakes/downgrade-or-upgrade-packages.md

135 lines
4.4 KiB
Markdown
Raw Normal View History

2023-06-30 11:00:03 +02:00
# Downgrading or Upgrading Packages
2023-06-23 14:29:12 +02:00
When working with Flakes, you may encounter situations where you need to downgrade or
upgrade certain packages to address bugs or compatibility issues. In Flakes, package
versions and hash values are directly tied to the git commit of their flake input. To
modify the package version, you need to lock the git commit of the flake input.
2023-06-23 14:29:12 +02:00
Here's an example of how you can add multiple nixpkgs inputs, each using a different git
commit or branch:
2023-06-23 14:29:12 +02:00
2023-07-08 12:31:45 +02:00
```nix{8-13,19-20,27-44}
2023-06-23 14:29:12 +02:00
{
2023-07-04 06:18:46 +02:00
description = "NixOS configuration of Ryan Yin";
2023-06-23 14:29:12 +02:00
inputs = {
2023-07-04 06:18:46 +02:00
# Default to the nixos-unstable branch
2023-06-23 14:29:12 +02:00
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
2023-07-04 06:18:46 +02:00
# Latest stable branch of nixpkgs, used for version rollback
2023-12-05 03:39:10 +01:00
# The current latest version is 23.11
nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-23.11";
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
# You can also use a specific git commit hash to lock the version
2023-06-23 14:29:12 +02:00
nixpkgs-fd40cef8d.url = "github:nixos/nixpkgs/fd40cef8d797670e203a27a91e4b8e6decf0b90c";
2023-07-04 06:18:46 +02:00
};
2023-07-08 12:31:45 +02:00
outputs = inputs@{
self,
nixpkgs,
nixpkgs-stable,
nixpkgs-fd40cef8d,
...
}: {
2023-06-23 14:29:12 +02:00
nixosConfigurations = {
2024-02-14 09:00:16 +01:00
my-nixos = nixpkgs.lib.nixosSystem rec {
2023-06-23 14:29:12 +02:00
system = "x86_64-linux";
2023-07-08 12:31:45 +02:00
# The `specialArgs` parameter passes the
# non-default nixpkgs instances to other nix modules
2023-06-23 14:29:12 +02:00
specialArgs = {
2023-07-08 12:31:45 +02:00
# To use packages from nixpkgs-stable,
# we configure some parameters for it first
2023-06-23 14:29:12 +02:00
pkgs-stable = import nixpkgs-stable {
2023-07-08 12:31:45 +02:00
# Refer to the `system` parameter from
# the outer scope recursively
2024-03-16 05:56:21 +01:00
inherit system;
2023-07-08 12:31:45 +02:00
# To use Chrome, we need to allow the
2024-03-16 10:18:28 +01:00
# installation of non-free software.
2023-06-23 14:29:12 +02:00
config.allowUnfree = true;
};
pkgs-fd40cef8d = import nixpkgs-fd40cef8d {
2024-03-16 05:56:21 +01:00
inherit system;
2023-06-23 14:29:12 +02:00
config.allowUnfree = true;
};
};
2023-07-04 06:18:46 +02:00
2023-06-23 14:29:12 +02:00
modules = [
2024-02-14 09:00:16 +01:00
./hosts/my-nixos
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
# Omit other configurations...
2023-06-23 14:29:12 +02:00
];
};
};
};
}
```
In the above example, we have defined multiple nixpkgs inputs: `nixpkgs`,
`nixpkgs-stable`, and `nixpkgs-fd40cef8d`. Each input corresponds to a different git
commit or branch.
2023-07-04 06:18:46 +02:00
Next, you can refer to the packages from `pkgs-stable` or `pkgs-fd40cef8d` within your
submodule. Here's an example of a Home Manager submodule:
2023-06-23 14:29:12 +02:00
2023-07-08 12:31:45 +02:00
```nix{4-7,13,25}
2023-06-23 14:29:12 +02:00
{
pkgs,
config,
2023-07-08 12:31:45 +02:00
# Nix will search for and inject this parameter
# from `specialArgs` in `flake.nix`
2023-06-23 14:29:12 +02:00
pkgs-stable,
# pkgs-fd40cef8d,
...
}:
{
2023-07-04 06:18:46 +02:00
# Use packages from `pkgs-stable` instead of `pkgs`
2023-06-23 14:29:12 +02:00
home.packages = with pkgs-stable; [
firefox-wayland
2023-07-08 12:31:45 +02:00
# Chrome Wayland support was broken on the nixos-unstable branch,
# so we fallback to the stable branch for now.
2023-07-04 06:18:46 +02:00
# Reference: https://github.com/swaywm/sway/issues/7562
2023-06-23 14:29:12 +02:00
google-chrome
];
programs.vscode = {
2023-07-08 12:31:45 +02:00
enable = true;
# Refer to vscode from `pkgs-stable` instead of `pkgs`
package = pkgs-stable.vscode;
2023-06-23 14:29:12 +02:00
};
}
```
## Pinning a package version with an overlay
The above approach is perfect for application packages, but sometimes you need to replace libraries used by those packages. This is where [Overlays](../nixpkgs/overlays.md) shine! Overlays can edit or replace any attribute of a package, but for now we'll just pin a package to a different nixpkgs version. The main disadvantage of editing a dependency with an overlay is that your Nix installation will recompile all installed packages that depend on it, but your situation may require it for specific bug fixes.
```nix
# overlays/mesa.nix
{ config, pkgs, lib, pkgs-fd40cef8d, ... }:
{
nixpkgs.overlays = [
# Overlay: Use `self` and `super` to express
# the inheritance relationship
(self: super: {
mesa = pkgs-fd40cef8d.mesa;
})
];
}
```
## Applying the new configuration
By adjusting the configuration as shown above, you can deploy it using
`sudo nixos-rebuild switch`. This will downgrade your Firefox/Chrome/VSCode versions to
the ones corresponding to `nixpkgs-stable` or `nixpkgs-fd40cef8d`.
2023-07-04 06:18:46 +02:00
> According to
> [1000 instances of nixpkgs](https://discourse.nixos.org/t/1000-instances-of-nixpkgs/17347),
> it's not a good practice to use `import` in submodules or subflakes to customize
> `nixpkgs`. Each `import` creates a new instance of nixpkgs, which increases build time
> and memory usage as the configuration grows. To avoid this problem, we create all
> nixpkgs instances in `flake.nix`.