OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 083

Overlays, override, and overrideAttrs

Adjust arguments and add patches to existing package definitions, then make the system actually consume the overlay.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSNix development environments and binary compatibility ↗
About 2 min readStart reading

️ What are Overlays?#

Overlays are a core Nixpkgs mechanism. Think of them as a layer of your own definitions applied over the official pkgs.

1. Basic syntax

self: super: {
  # 把 hello 包换成我的版本
  hello = super.hello.override {
    # 修改参数
  };
}

️ Common uses for Overlays#

1. Change build options (Override)

Some packages have certain features disabled by default at build time.

(self: super: {
  mpv = super.mpv.override {
    scripts = [ self.mpvScripts.mpris ];
  };
})

2. Apply patches (overrideAttrs)

If you have a .patch file:

(self: super: {
  sl = super.sl.overrideAttrs (oldAttrs: {
    patches = (oldAttrs.patches or []) ++ [ ./my-patch.patch ];
  });
})

3. Add a new package

You can define your own package in an Overlay and then reference it as pkgs.my-app.

Integrate Overlays into a Flake#

Define your Overlays in the outputs section.

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  outputs = { self, nixpkgs, ... }: {
    overlays.default = final: prev: {
      sl = prev.sl.overrideAttrs (old: {
        patches = (old.patches or []) ++ [ ./my-patch.patch ];
      });
    };
    nixosConfigurations.workstation = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        ./configuration.nix
        { nixpkgs.overlays = [ self.overlays.default ]; }
      ];
    };
  };
}

Why do this?#

As an Arch user, you may be used to finding PKGBUILDs in the AUR. In NixOS, Overlays are your all-purpose customization tool. You do not need to repackage software from scratch; you can simply adjust an existing package.

Summary#

  • override: Changes the arguments to a package function (usually the function arguments supplied by callPackage).
  • overrideAttrs: Changes the derivation attributes passed to mkDerivation (such as patches and postInstall), producing a new derivation.

final / prev and actual builds

self: super: is an older variable-naming convention; final: prev: is more descriptive, referring to the final package set after all overlays and the package set from the previous layer, respectively. After importing an overlay, you must still ensure that consumers actually use that package set. The original example only defined a local pkgs without connecting it to nixosSystem; here it has been changed to nixpkgs.overlays in a module. Patch changes require an actual build of the affected package: successful evaluation does not prove that a patch applies. The pinned Nixpkgs determines the package function's arguments and version.

References & further reading

  1. Nixpkgs Overlays ↗
  2. Nixpkgs Overrides ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Nix development environments and binary compatibility

  1. 01Progressing from nix-shell to devShell and direnv
  2. 02Nix development environments: languages, databases, and full-stack examples
  3. 03Dependency boundaries in Python, uv, and Matplotlib/Qt
  4. 04nix-ld: precompiled ELF binaries, loaders, and library paths
  5. 05Overlays, override, and overrideAttrs
CONTINUE READING / NEXT ARTICLEGarbage collection, store optimization, and rollback generations

Distinguish deleting generation references, reclaiming store objects, and switching to older system configurations.