Home Explore Blog Models CI



nixpkgs

3rd chunk of `doc/using/overrides.chapter.md`
09368d4bf026dc983992e222e0a6e0359ab3692085ffdeba0000000100000b3e
You should prefer `overrideAttrs` in almost all cases, see its documentation for the reasons why. `overrideDerivation` is not deprecated and will continue to work, but is less nice to use and does not have as many abilities as `overrideAttrs`.
:::

::: {.warning}
Do not use this function in Nixpkgs as it evaluates a derivation before modifying it, which breaks package abstraction. In addition, this evaluation-per-function application incurs a performance penalty, which can become a problem if many overrides are used. It is only intended for ad-hoc customisation, such as in `~/.config/nixpkgs/config.nix`.
:::

The function `overrideDerivation` creates a new derivation based on an existing one by overriding the original's attributes with the attribute set produced by the specified function. This function is available on all derivations defined using the `makeOverridable` function. Most standard derivation-producing functions, such as `stdenv.mkDerivation`, are defined using this function, which means most packages in the Nixpkgs expression, `pkgs`, have this function.

Example usage:

```nix
{
  mySed = pkgs.gnused.overrideDerivation (oldAttrs: {
    name = "sed-4.2.2-pre";
    src = fetchurl {
      url = "ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2";
      hash = "sha256-MxBJRcM2rYzQYwJ5XKxhXTQByvSg5jZc5cSHEZoB2IY=";
    };
    patches = [ ];
  });
}
```

In the above example, the `name`, `src`, and `patches` of the derivation will be overridden, while all other attributes will be retained from the original derivation.

The argument `oldAttrs` is used to refer to the attribute set of the original derivation.

::: {.note}
A package's attributes are evaluated *before* being modified by the `overrideDerivation` function. For example, the `name` attribute reference in `url = "mirror://gnu/hello/${name}.tar.gz";` is filled-in *before* the `overrideDerivation` function modifies the attribute set. This means that overriding the `name` attribute, in this example, *will not* change the value of the `url` attribute. Instead, we need to override both the `name` *and* `url` attributes.
:::

## lib.makeOverridable {#sec-lib-makeOverridable}

The function `lib.makeOverridable` is used to make the result of a function easily customizable. This utility only makes sense for functions that accept an argument set and return an attribute set.

Example usage:

```nix
{
  f =
    { a, b }:
    {
      result = a + b;
    };
  c = lib.makeOverridable f {
    a = 1;
    b = 2;
  };
}
```

The variable `c` is the value of the `f` function applied with some default arguments. Hence the value of `c.result` is `3`, in this example.

The variable `c` however also has some additional functions, like
[c.override](#sec-pkg-override) which can be used to override the
default arguments. In this example the value of
`(c.override { a = 4; }).result` is 6.

Title: Using `overrideDerivation` and `lib.makeOverridable`
Summary
This chunk continues the explanation of `overrideDerivation`, providing an example of its usage to override attributes like `name`, `src`, and `patches` of a derivation, while retaining others. It clarifies that `oldAttrs` refers to the original derivation's attribute set. A critical note highlights that `overrideDerivation` evaluates a package's attributes *before* modification; therefore, overriding one attribute (e.g., `name`) will not automatically update dependent attributes (e.g., `url`) if they were already computed, requiring both to be overridden explicitly. The text then introduces `lib.makeOverridable`, a utility function designed to make the results of functions easily customizable, particularly for those that accept an argument set and return an attribute set. An example demonstrates how `makeOverridable` can apply default arguments to a function and how the resulting variable (e.g., `c`) gains additional functions like `.override` to further customize its arguments.