Home Explore Blog CI



nixpkgs

2nd chunk of `doc/using/overrides.chapter.md`
8d0ae8c8a25bae18c8eb307824d53ed3f86eccdbdcf9641200000001000008ca
If only a one-argument function is written, the argument has the meaning of `previousAttrs`.

Function arguments can be omitted entirely if there is no need to access `previousAttrs` or `finalAttrs`.

```nix
{
  helloWithDebug = pkgs.hello.overrideAttrs {
    separateDebugInfo = true;
  };
}
```

In the above example, the `separateDebugInfo` attribute is overridden to be true, thus building debug info for `helloWithDebug`.

::: {.note}
Note that `separateDebugInfo` is processed only by the `stdenv.mkDerivation` function, not the generated, raw Nix derivation. Thus, using `overrideDerivation` will not work in this case, as it overrides only the attributes of the final derivation. It is for this reason that `overrideAttrs` should be preferred in (almost) all cases to `overrideDerivation`, i.e. to allow using `stdenv.mkDerivation` to process input arguments, as well as the fact that it is easier to use (you can use the same attribute names you see in your Nix code, instead of the ones generated (e.g. `buildInputs` vs `nativeBuildInputs`), and it involves less typing).
:::

## <pkg>.overrideDerivation {#sec-pkg-overrideDerivation}

::: {.warning}
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.

Title: More on Overriding Packages: overrideAttrs and overrideDerivation
Summary
This section provides further details on `overrideAttrs`, showing how a single-argument function is treated as `previousAttrs` and how arguments can be omitted. It includes an example of setting `separateDebugInfo` to true. It also explains why `overrideAttrs` is generally preferred over `overrideDerivation` due to how `stdenv.mkDerivation` processes arguments. Finally, it discusses the usage and caveats of `overrideDerivation`, noting its limitations and potential performance issues, recommending it only for ad-hoc customization outside of Nixpkgs.