2023-06-30 11:00:03 +02:00
|
|
|
# Overlays
|
2023-06-23 17:18:01 +02:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
In the previous section, we learned about overriding derivations by
|
|
|
|
`pkgs.xxx.override { ... }` or
|
|
|
|
`pkgs.xxx.overrideAttrs (finalAttrs: previousAttrs: { ... });`. However, this approach
|
|
|
|
will generate a new derivation and doesn't modify the original derivation in `pkgs`
|
|
|
|
instance. If the derivation you want to override is also used by other Nix packages, they
|
|
|
|
will still use the unmodified derivation.
|
2024-02-12 10:40:36 +01:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
To globally modify derivations in the default nixpkgs instance, Nix provides a feature
|
|
|
|
called "overlays".
|
2023-06-23 16:19:10 +02:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
In traditional Nix environments, overlays can be configured globally using the
|
|
|
|
`~/.config/nixpkgs/overlays.nix` or `~/.config/nixpkgs/overlays/*.nix` files. However,
|
|
|
|
with Flakes feature, to ensure system reproducibility, overlays cannot rely on
|
|
|
|
configurations outside of the Git repository.
|
2023-06-23 16:19:10 +02:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
When using `flake.nix` to configure NixOS, both Home Manager and NixOS provide the
|
|
|
|
`nixpkgs.overlays` option to define overlays. You can refer to the following documentation
|
|
|
|
for more details:
|
2023-06-23 16:19:10 +02:00
|
|
|
|
2024-01-11 04:40:44 +01:00
|
|
|
- [Home Manager docs - `nixpkgs.overlays`](https://nix-community.github.io/home-manager/options.xhtml#opt-nixpkgs.overlays)
|
2023-07-04 06:18:46 +02:00
|
|
|
- [Nixpkgs source code - `nixpkgs.overlays`](https://github.com/NixOS/nixpkgs/blob/30d7dd7e7f2cba9c105a6906ae2c9ed419e02f17/nixos/modules/misc/nixpkgs.nix#L169)
|
2023-06-23 16:19:10 +02:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
Let's take a look at an example module that loads overlays. This module can be used as a
|
|
|
|
Home Manager module or a NixOS module, as the definitions are the same:
|
2023-06-23 16:19:10 +02:00
|
|
|
|
|
|
|
```nix
|
2024-02-12 10:25:48 +01:00
|
|
|
# ./overlays/default.nix
|
2023-06-23 16:19:10 +02:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
{
|
|
|
|
nixpkgs.overlays = [
|
2023-10-09 17:21:03 +02:00
|
|
|
# Overlay 1: Use `self` and `super` to express
|
|
|
|
# the inheritance relationship
|
2023-06-23 16:19:10 +02:00
|
|
|
(self: super: {
|
2023-07-04 06:18:46 +02:00
|
|
|
google-chrome = super.google-chrome.override {
|
|
|
|
commandLineArgs =
|
|
|
|
"--proxy-server='https=127.0.0.1:3128;http=127.0.0.1:3128'";
|
|
|
|
};
|
2023-06-23 16:19:10 +02:00
|
|
|
})
|
|
|
|
|
2023-10-09 17:21:03 +02:00
|
|
|
# Overlay 2: Use `final` and `prev` to express
|
|
|
|
# the relationship between the new and the old
|
2023-06-23 16:19:10 +02:00
|
|
|
(final: prev: {
|
|
|
|
steam = prev.steam.override {
|
2023-07-04 06:18:46 +02:00
|
|
|
extraPkgs = pkgs: with pkgs; [
|
|
|
|
keyutils
|
|
|
|
libkrb5
|
|
|
|
libpng
|
|
|
|
libpulseaudio
|
|
|
|
libvorbis
|
|
|
|
stdenv.cc.cc.lib
|
|
|
|
xorg.libXcursor
|
|
|
|
xorg.libXi
|
|
|
|
xorg.libXinerama
|
|
|
|
xorg.libXScrnSaver
|
|
|
|
];
|
2023-06-23 16:19:10 +02:00
|
|
|
extraProfile = "export GDK_SCALE=2";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
2023-07-04 06:18:46 +02:00
|
|
|
# Overlay 3: Define overlays in other files
|
2024-02-12 10:48:19 +01:00
|
|
|
# The content of ./overlays/overlay3/default.nix is the same as above:
|
2023-07-04 06:18:46 +02:00
|
|
|
# `(final: prev: { xxx = prev.xxx.override { ... }; })`
|
2024-02-12 10:25:48 +01:00
|
|
|
(import ./overlay3)
|
2023-06-23 16:19:10 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-03-16 11:07:01 +01:00
|
|
|
In the above example, we define three overlays.
|
2023-07-04 06:18:46 +02:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
1. Overlay 1 modifies the `google-chrome` derivation by adding a command-line argument for
|
|
|
|
a proxy server.
|
|
|
|
2. Overlay 2 modifies the `steam` derivation by adding extra packages and environment
|
|
|
|
variables.
|
2024-02-12 10:47:54 +01:00
|
|
|
3. Overlay 3 is defined in a separate file `./overlays/overlay3/default.nix`.
|
2023-06-23 16:19:10 +02:00
|
|
|
|
2024-02-12 10:25:48 +01:00
|
|
|
One example of importing the above configuration as a NixOS module is as follows:
|
2023-06-23 16:19:10 +02:00
|
|
|
|
|
|
|
```nix
|
2024-02-12 10:25:48 +01:00
|
|
|
# ./flake.nix
|
2023-06-23 16:19:10 +02:00
|
|
|
{
|
|
|
|
inputs = {
|
2023-07-04 06:18:46 +02:00
|
|
|
# ...
|
2023-06-23 16:19:10 +02:00
|
|
|
};
|
|
|
|
|
2024-02-12 10:25:48 +01:00
|
|
|
outputs = inputs@{ nixpkgs, ... }: {
|
|
|
|
nixosConfigurations = {
|
2024-02-14 09:00:16 +01:00
|
|
|
my-nixos = nixpkgs.lib.nixosSystem {
|
2024-02-12 10:25:48 +01:00
|
|
|
system = "x86_64-linux";
|
|
|
|
modules = [
|
|
|
|
./configuration.nix
|
2024-03-16 11:07:01 +01:00
|
|
|
|
2024-02-12 10:32:00 +01:00
|
|
|
# import the module that contains overlays
|
2024-02-12 10:25:48 +01:00
|
|
|
(import ./overlays)
|
|
|
|
];
|
2023-06-23 16:19:10 +02:00
|
|
|
};
|
|
|
|
};
|
2024-02-12 10:25:48 +01:00
|
|
|
};
|
2023-06-23 16:19:10 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-02-12 10:25:48 +01:00
|
|
|
This is just an example. Please write your own overlays according to your needs.
|
|
|
|
|
2024-02-12 10:32:48 +01:00
|
|
|
## Multiple nixpkgs Instances with different Overlays
|
2024-02-12 10:25:48 +01:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
The `nixpkgs.overlays = [...];` mentioned above directly modifies the global nixpkgs
|
|
|
|
instance `pkgs`. If your overlays make changes to some low-level packages, it might impact
|
|
|
|
other modules. One downside is an increase in local compilation (due to cache
|
|
|
|
invalidation), and there might also be functionality issues with the affected packages.
|
2024-02-12 10:25:48 +01:00
|
|
|
|
2024-03-16 12:29:05 +01:00
|
|
|
If you wish to utilize overlays only in a specific location without affecting the default
|
|
|
|
nixpkgs instance, you can instantiate a new nixpkgs instance and apply your overlays to
|
|
|
|
it. We will discuss how to do this in the next section
|
|
|
|
[The Ingenious Uses of Multiple nixpkgs Instances](./multiple-nixpkgs.md).
|
2024-02-12 10:25:48 +01:00
|
|
|
|
|
|
|
## References
|
2023-07-04 06:18:46 +02:00
|
|
|
|
2024-02-12 10:25:48 +01:00
|
|
|
- [Chapter 3. Overlays - nixpkgs Manual](https://nixos.org/manual/nixpkgs/stable/#chap-overlays)
|