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

90 lines
3.4 KiB
Markdown
Raw Normal View History

2023-06-30 09:00:03 +00:00
# Downgrading or Upgrading Packages
2023-06-23 12:29:12 +00:00
2023-07-04 04:18:46 +00: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 12:29:12 +00:00
2023-07-04 04:18:46 +00:00
Here's an example of how you can add multiple nixpkgs inputs, each using a different git commit or branch:
2023-06-23 12:29:12 +00:00
```nix
{
2023-07-04 04:18:46 +00:00
description = "NixOS configuration of Ryan Yin";
2023-06-23 12:29:12 +00:00
inputs = {
2023-07-04 04:18:46 +00:00
# Default to the nixos-unstable branch
2023-06-23 12:29:12 +00:00
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
2023-07-04 04:18:46 +00:00
# Latest stable branch of nixpkgs, used for version rollback
# The current latest version is 22.11
2023-06-23 12:29:12 +00:00
nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-22.11";
2023-07-04 04:18:46 +00:00
# You can also use a specific git commit hash to lock the version
2023-06-23 12:29:12 +00:00
nixpkgs-fd40cef8d.url = "github:nixos/nixpkgs/fd40cef8d797670e203a27a91e4b8e6decf0b90c";
2023-07-04 04:18:46 +00:00
};
outputs = inputs@{ self, nixpkgs, nixpkgs-stable, nixpkgs-fd40cef8d, ... }: {
2023-06-23 12:29:12 +00:00
nixosConfigurations = {
nixos-test = nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
2023-07-04 04:18:46 +00:00
# The `specialArgs` parameter passes the non-default nixpkgs instances to other nix modules
2023-06-23 12:29:12 +00:00
specialArgs = {
2023-07-04 04:18:46 +00:00
# To use packages from nixpkgs-stable, we configure some parameters for it first
2023-06-23 12:29:12 +00:00
pkgs-stable = import nixpkgs-stable {
2023-07-04 04:18:46 +00:00
system = system; # Refer to the `system` parameter from the outer scope recursively
# To use Chrome, we need to allow the installation of non-free software
2023-06-23 12:29:12 +00:00
config.allowUnfree = true;
};
pkgs-fd40cef8d = import nixpkgs-fd40cef8d {
system = system;
config.allowUnfree = true;
};
};
2023-07-04 04:18:46 +00:00
2023-06-23 12:29:12 +00:00
modules = [
./hosts/nixos-test
2023-07-04 04:18:46 +00:00
# Omit other configurations...
2023-06-23 12:29:12 +00:00
];
};
};
};
}
```
2023-07-04 04:18:46 +00: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.
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 12:29:12 +00:00
```nix
{
pkgs,
config,
2023-07-04 04:18:46 +00:00
# Nix will search for and inject this parameter from `specialArgs` in `flake.nix`
2023-06-23 12:29:12 +00:00
pkgs-stable,
# pkgs-fd40cef8d,
...
}:
{
2023-07-04 04:18:46 +00:00
# Use packages from `pkgs-stable` instead of `pkgs`
2023-06-23 12:29:12 +00:00
home.packages = with pkgs-stable; [
firefox-wayland
2023-07-04 04:18:46 +00:00
# Chrome Wayland support was broken on the nixos-unstable branch, so we fallback to the stable branch for now
# Reference: https://github.com/swaywm/sway/issues/7562
2023-06-23 12:29:12 +00:00
google-chrome
];
programs.vscode = {
2023-07-04 04:18:46 +00:00
enable
= true;
package = pkgs-stable.vscode; # Refer to vscode from `pkgs-stable` instead of `pkgs`
2023-06-23 12:29:12 +00:00
};
}
```
2023-07-04 04:18:46 +00:00
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-06-28 09:41:46 +00:00
2023-07-03 09:56:28 +00: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 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`.