1# Overriding {#chap-overrides}
2
3Sometimes one wants to override parts of `nixpkgs`, e.g. derivation attributes, the results of derivations.
4
5These functions are used to make changes to packages, returning only single packages. [Overlays](#chap-overlays), on the other hand, can be used to combine the overridden packages across the entire package set of Nixpkgs.
6
7## <pkg>.override {#sec-pkg-override}
8
9The function `override` is usually available for all the derivations in the nixpkgs expression (`pkgs`).
10
11It is used to override the arguments passed to a function.
12
13Example usages:
14
15```nix
16pkgs.foo.override { arg1 = val1; arg2 = val2; ... }
17```
18
19It's also possible to access the previous arguments.
20
21```nix
22pkgs.foo.override (previous: { arg1 = previous.arg1; ... })
23```
24
25<!-- TODO: move below programlisting to a new section about extending and overlays and reference it -->
26
27```nix
28import pkgs.path { overlays = [ (self: super: {
29 foo = super.foo.override { barSupport = true ; };
30 })]};
31```
32
33```nix
34mypkg = pkgs.callPackage ./mypkg.nix {
35 mydep = pkgs.mydep.override { ... };
36 }
37```
38
39In the first example, `pkgs.foo` is the result of a function call with some default arguments, usually a derivation. Using `pkgs.foo.override` will call the same function with the given new arguments.
40
41## <pkg>.overrideAttrs {#sec-pkg-overrideAttrs}
42
43The function `overrideAttrs` allows overriding the attribute set passed to a `stdenv.mkDerivation` call, producing a new derivation based on the original one. This function is available on all derivations produced by the `stdenv.mkDerivation` function, which is most packages in the nixpkgs expression `pkgs`.
44
45Example usages:
46
47```nix
48helloBar = pkgs.hello.overrideAttrs (finalAttrs: previousAttrs: {
49 pname = previousAttrs.pname + "-bar";
50});
51```
52
53In the above example, "-bar" is appended to the pname attribute, while all other attributes will be retained from the original `hello` package.
54
55The argument `previousAttrs` is conventionally used to refer to the attr set originally passed to `stdenv.mkDerivation`.
56
57The argument `finalAttrs` refers to the final attributes passed to `mkDerivation`, plus the `finalPackage` attribute which is equal to the result of `mkDerivation` or subsequent `overrideAttrs` calls.
58
59If only a one-argument function is written, the argument has the meaning of `previousAttrs`.
60
61Function arguments can be omitted entirely if there is no need to access `previousAttrs` or `finalAttrs`.
62
63```nix
64helloWithDebug = pkgs.hello.overrideAttrs {
65 separateDebugInfo = true;
66};
67```
68
69In the above example, the `separateDebugInfo` attribute is overridden to be true, thus building debug info for `helloWithDebug`.
70
71::: {.note}
72Note 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).
73:::
74
75## <pkg>.overrideDerivation {#sec-pkg-overrideDerivation}
76
77::: {.warning}
78You 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`.
79:::
80
81::: {.warning}
82Do 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`.
83:::
84
85The 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.
86
87Example usage:
88
89```nix
90mySed = pkgs.gnused.overrideDerivation (oldAttrs: {
91 name = "sed-4.2.2-pre";
92 src = fetchurl {
93 url = "ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2";
94 hash = "sha256-MxBJRcM2rYzQYwJ5XKxhXTQByvSg5jZc5cSHEZoB2IY=";
95 };
96 patches = [];
97});
98```
99
100In 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.
101
102The argument `oldAttrs` is used to refer to the attribute set of the original derivation.
103
104::: {.note}
105A 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.
106:::
107
108## lib.makeOverridable {#sec-lib-makeOverridable}
109
110The 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.
111
112Example usage:
113
114```nix
115f = { a, b }: { result = a+b; };
116c = lib.makeOverridable f { a = 1; b = 2; };
117```
118
119The 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.
120
121The variable `c` however also has some additional functions, like
122[c.override](#sec-pkg-override) which can be used to override the
123default arguments. In this example the value of
124`(c.override { a = 4; }).result` is 6.