1<chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xml:id="chap-overlays">
4
5<title>Overlays</title>
6
7<para>This chapter describes how to extend and change Nixpkgs packages using
8overlays. Overlays are used to add layers in the fix-point used by Nixpkgs
9to compose the set of all packages.</para>
10
11<para>Nixpkgs can be configured with a list of overlays, which are
12applied in order. This means that the order of the overlays can be significant
13if multiple layers override the same package.</para>
14
15<!--============================================================-->
16
17<section xml:id="sec-overlays-install">
18<title>Installing overlays</title>
19
20<para>The list of overlays is determined as follows.</para>
21
22<para>If the <varname>overlays</varname> argument is not provided explicitly, we look for overlays in a path. The path
23is determined as follows:
24
25<orderedlist>
26
27 <listitem>
28 <para>First, if an <varname>overlays</varname> argument to the nixpkgs function itself is given,
29 then that is used.</para>
30
31 <para>This can be passed explicitly when importing nipxkgs, for example
32 <literal>import <nixpkgs> { overlays = [ overlay1 overlay2 ]; }</literal>.</para>
33 </listitem>
34
35 <listitem>
36 <para>Otherwise, if the Nix path entry <literal><nixpkgs-overlays></literal> exists, we look for overlays
37 at that path, as described below.</para>
38
39 <para>See the section on <literal>NIX_PATH</literal> in the Nix manual for more details on how to
40 set a value for <literal><nixpkgs-overlays>.</literal></para>
41 </listitem>
42
43 <listitem>
44 <para>If one of <filename>~/.config/nixpkgs/overlays.nix</filename> and
45 <filename>~/.config/nixpkgs/overlays/</filename> exists, then we look for overlays at that path, as
46 described below. It is an error if both exist.</para>
47 </listitem>
48
49</orderedlist>
50</para>
51
52<para>If we are looking for overlays at a path, then there are two cases:
53<itemizedlist>
54 <listitem>
55 <para>If the path is a file, then the file is imported as a Nix expression and used as the list of
56 overlays.</para>
57 </listitem>
58
59 <listitem>
60 <para>If the path is a directory, then we take the content of the directory, order it
61 lexicographically, and attempt to interpret each as an overlay by:
62 <itemizedlist>
63 <listitem>
64 <para>Importing the file, if it is a <literal>.nix</literal> file.</para>
65 </listitem>
66 <listitem>
67 <para>Importing a top-level <filename>default.nix</filename> file, if it is a directory.</para>
68 </listitem>
69 </itemizedlist>
70 </para>
71 </listitem>
72</itemizedlist>
73</para>
74
75<para>On a NixOS system the value of the <literal>nixpkgs.overlays</literal> option, if present,
76is passed to the system Nixpkgs directly as an argument. Note that this does not affect the overlays for
77non-NixOS operations (e.g. <literal>nix-env</literal>), which are looked up independently.</para>
78
79<para>The <filename>overlays.nix</filename> option therefore provides a convenient way to use the same
80overlays for a NixOS system configuration and user configuration: the same file can be used
81as <filename>overlays.nix</filename> and imported as the value of <literal>nixpkgs.overlays</literal>.</para>
82
83</section>
84
85<!--============================================================-->
86
87<section xml:id="sec-overlays-definition">
88<title>Defining overlays</title>
89
90<para>Overlays are Nix functions which accept two arguments,
91conventionally called <varname>self</varname> and <varname>super</varname>,
92and return a set of packages. For example, the following is a valid overlay.</para>
93
94<programlisting>
95self: super:
96
97{
98 boost = super.boost.override {
99 python = self.python3;
100 };
101 rr = super.callPackage ./pkgs/rr {
102 stdenv = self.stdenv_32bit;
103 };
104}
105</programlisting>
106
107<para>The first argument (<varname>self</varname>) corresponds to the final package
108set. You should use this set for the dependencies of all packages specified in your
109overlay. For example, all the dependencies of <varname>rr</varname> in the example above come
110from <varname>self</varname>, as well as the overridden dependencies used in the
111<varname>boost</varname> override.</para>
112
113<para>The second argument (<varname>super</varname>)
114corresponds to the result of the evaluation of the previous stages of
115Nixpkgs. It does not contain any of the packages added by the current
116overlay, nor any of the following overlays. This set should be used either
117to refer to packages you wish to override, or to access functions defined
118in Nixpkgs. For example, the original recipe of <varname>boost</varname>
119in the above example, comes from <varname>super</varname>, as well as the
120<varname>callPackage</varname> function.</para>
121
122<para>The value returned by this function should be a set similar to
123<filename>pkgs/top-level/all-packages.nix</filename>, containing
124overridden and/or new packages.</para>
125
126<para>Overlays are similar to other methods for customizing Nixpkgs, in particular
127the <literal>packageOverrides</literal> attribute described in <xref linkend="sec-modify-via-packageOverrides"/>.
128Indeed, <literal>packageOverrides</literal> acts as an overlay with only the
129<varname>super</varname> argument. It is therefore appropriate for basic use,
130but overlays are more powerful and easier to distribute.</para>
131
132</section>
133
134</chapter>