at 21.11-pre 13 kB view raw
1<chapter xmlns="http://docbook.org/ns/docbook" 2 xmlns:xlink="http://www.w3.org/1999/xlink" 3 xml:id="chap-overlays"> 4 <title>Overlays</title> 5 <para> 6 This chapter describes how to extend and change Nixpkgs using overlays. Overlays are used to add layers in the fixed-point used by Nixpkgs to compose the set of all packages. 7 </para> 8 <para> 9 Nixpkgs can be configured with a list of overlays, which are applied in order. This means that the order of the overlays can be significant if multiple layers override the same package. 10 </para> 11<!--============================================================--> 12 <section xml:id="sec-overlays-install"> 13 <title>Installing overlays</title> 14 15 <para> 16 The list of overlays can be set either explicitly in a Nix expression, or through <literal>&lt;nixpkgs-overlays></literal> or user configuration files. 17 </para> 18 19 <section xml:id="sec-overlays-argument"> 20 <title>Set overlays in NixOS or Nix expressions</title> 21 22 <para> 23 On a NixOS system the value of the <literal>nixpkgs.overlays</literal> option, if present, is passed to the system Nixpkgs directly as an argument. Note that this does not affect the overlays for non-NixOS operations (e.g. <literal>nix-env</literal>), which are <link xlink:href="#sec-overlays-lookup">looked</link> up independently. 24 </para> 25 26 <para> 27 The list of overlays can be passed explicitly when importing nixpkgs, for example <literal>import &lt;nixpkgs> { overlays = [ overlay1 overlay2 ]; }</literal>. 28 </para> 29 30 <para> 31 NOTE: DO NOT USE THIS in nixpkgs. Further overlays can be added by calling the <literal>pkgs.extend</literal> or <literal>pkgs.appendOverlays</literal>, although it is often preferable to avoid these functions, because they recompute the Nixpkgs fixpoint, which is somewhat expensive to do. 32 </para> 33 </section> 34 35 <section xml:id="sec-overlays-lookup"> 36 <title>Install overlays via configuration lookup</title> 37 38 <para> 39 The list of overlays is determined as follows. 40 </para> 41 42 <para> 43 <orderedlist> 44 <listitem> 45 <para> 46 First, if an <link xlink:href="#sec-overlays-argument"><varname>overlays</varname> argument</link> to the Nixpkgs function itself is given, then that is used and no path lookup will be performed. 47 </para> 48 </listitem> 49 <listitem> 50 <para> 51 Otherwise, if the Nix path entry <literal>&lt;nixpkgs-overlays></literal> exists, we look for overlays at that path, as described below. 52 </para> 53 <para> 54 See the section on <literal>NIX_PATH</literal> in the Nix manual for more details on how to set a value for <literal>&lt;nixpkgs-overlays>.</literal> 55 </para> 56 </listitem> 57 <listitem> 58 <para> 59 If one of <filename>~/.config/nixpkgs/overlays.nix</filename> and <filename>~/.config/nixpkgs/overlays/</filename> exists, then we look for overlays at that path, as described below. It is an error if both exist. 60 </para> 61 </listitem> 62 </orderedlist> 63 </para> 64 65 <para> 66 If we are looking for overlays at a path, then there are two cases: 67 <itemizedlist> 68 <listitem> 69 <para> 70 If the path is a file, then the file is imported as a Nix expression and used as the list of overlays. 71 </para> 72 </listitem> 73 <listitem> 74 <para> 75 If the path is a directory, then we take the content of the directory, order it lexicographically, and attempt to interpret each as an overlay by: 76 <itemizedlist> 77 <listitem> 78 <para> 79 Importing the file, if it is a <literal>.nix</literal> file. 80 </para> 81 </listitem> 82 <listitem> 83 <para> 84 Importing a top-level <filename>default.nix</filename> file, if it is a directory. 85 </para> 86 </listitem> 87 </itemizedlist> 88 </para> 89 </listitem> 90 </itemizedlist> 91 </para> 92 93 <para> 94 Because overlays that are set in NixOS configuration do not affect non-NixOS operations such as <literal>nix-env</literal>, the <filename>overlays.nix</filename> option provides a convenient way to use the same overlays for a NixOS system configuration and user configuration: the same file can be used as <filename>overlays.nix</filename> and imported as the value of <literal>nixpkgs.overlays</literal>. 95 </para> 96 97<!-- TODO: Example of sharing overlays between NixOS configuration 98 and configuration lookup. Also reference the example 99 from the sec-overlays-argument paragraph about NixOS. 100 --> 101 </section> 102 </section> 103<!--============================================================--> 104 <section xml:id="sec-overlays-definition"> 105 <title>Defining overlays</title> 106 107 <para> 108 Overlays are Nix functions which accept two arguments, conventionally called <varname>self</varname> and <varname>super</varname>, and return a set of packages. For example, the following is a valid overlay. 109 </para> 110 111<programlisting> 112self: super: 113 114{ 115 boost = super.boost.override { 116 python = self.python3; 117 }; 118 rr = super.callPackage ./pkgs/rr { 119 stdenv = self.stdenv_32bit; 120 }; 121} 122</programlisting> 123 124 <para> 125 The first argument (<varname>self</varname>) corresponds to the final package set. You should use this set for the dependencies of all packages specified in your overlay. For example, all the dependencies of <varname>rr</varname> in the example above come from <varname>self</varname>, as well as the overridden dependencies used in the <varname>boost</varname> override. 126 </para> 127 128 <para> 129 The second argument (<varname>super</varname>) corresponds to the result of the evaluation of the previous stages of Nixpkgs. It does not contain any of the packages added by the current overlay, nor any of the following overlays. This set should be used either to refer to packages you wish to override, or to access functions defined in Nixpkgs. For example, the original recipe of <varname>boost</varname> in the above example, comes from <varname>super</varname>, as well as the <varname>callPackage</varname> function. 130 </para> 131 132 <para> 133 The value returned by this function should be a set similar to <filename>pkgs/top-level/all-packages.nix</filename>, containing overridden and/or new packages. 134 </para> 135 136 <para> 137 Overlays are similar to other methods for customizing Nixpkgs, in particular the <literal>packageOverrides</literal> attribute described in <xref linkend="sec-modify-via-packageOverrides"/>. Indeed, <literal>packageOverrides</literal> acts as an overlay with only the <varname>super</varname> argument. It is therefore appropriate for basic use, but overlays are more powerful and easier to distribute. 138 </para> 139 </section> 140 <section xml:id="sec-overlays-alternatives"> 141 <title>Using overlays to configure alternatives</title> 142 143 <para> 144 Certain software packages have different implementations of the same interface. Other distributions have functionality to switch between these. For example, Debian provides <link 145 xlink:href="https://wiki.debian.org/DebianAlternatives">DebianAlternatives</link>. Nixpkgs has what we call <literal>alternatives</literal>, which are configured through overlays. 146 </para> 147 148 <section xml:id="sec-overlays-alternatives-blas-lapack"> 149 <title>BLAS/LAPACK</title> 150 151 <para> 152 In Nixpkgs, we have multiple implementations of the BLAS/LAPACK numerical linear algebra interfaces. They are: 153 </para> 154 155 <itemizedlist> 156 <listitem> 157 <para> 158 <link xlink:href="https://www.openblas.net/">OpenBLAS</link> 159 </para> 160 <para> 161 The Nixpkgs attribute is <literal>openblas</literal> for ILP64 (integer width = 64 bits) and <literal>openblasCompat</literal> for LP64 (integer width = 32 bits). <literal>openblasCompat</literal> is the default. 162 </para> 163 </listitem> 164 <listitem> 165 <para> 166 <link xlink:href="http://www.netlib.org/lapack/">LAPACK reference</link> (also provides BLAS) 167 </para> 168 <para> 169 The Nixpkgs attribute is <literal>lapack-reference</literal>. 170 </para> 171 </listitem> 172 <listitem> 173 <para> 174 <link 175 xlink:href="https://software.intel.com/en-us/mkl">Intel MKL</link> (only works on the x86_64 architecture, unfree) 176 </para> 177 <para> 178 The Nixpkgs attribute is <literal>mkl</literal>. 179 </para> 180 </listitem> 181 <listitem> 182 <para> 183 <link 184 xlink:href="https://github.com/flame/blis">BLIS</link> 185 </para> 186 <para> 187 BLIS, available through the attribute <literal>blis</literal>, is a framework for linear algebra kernels. In addition, it implements the BLAS interface. 188 </para> 189 </listitem> 190 <listitem> 191 <para> 192 <link 193 xlink:href="https://developer.amd.com/amd-aocl/blas-library/">AMD BLIS/LIBFLAME</link> (optimized for modern AMD x86_64 CPUs) 194 </para> 195 <para> 196 The AMD fork of the BLIS library, with attribute <literal>amd-blis</literal>, extends BLIS with optimizations for modern AMD CPUs. The changes are usually submitted to the upstream BLIS project after some time. However, AMD BLIS typically provides some performance improvements on AMD Zen CPUs. The complementary AMD LIBFLAME library, with attribute <literal>amd-libflame</literal>, provides a LAPACK implementation. 197 </para> 198 </listitem> 199 </itemizedlist> 200 201 <para> 202 Introduced in <link 203 xlink:href="https://github.com/NixOS/nixpkgs/pull/83888">PR #83888</link>, we are able to override the <literal>blas</literal> and <literal>lapack</literal> packages to use different implementations, through the <literal>blasProvider</literal> and <literal>lapackProvider</literal> argument. This can be used to select a different provider. BLAS providers will have symlinks in <literal>$out/lib/libblas.so.3</literal> and <literal>$out/lib/libcblas.so.3</literal> to their respective BLAS libraries. Likewise, LAPACK providers will have symlinks in <literal>$out/lib/liblapack.so.3</literal> and <literal>$out/lib/liblapacke.so.3</literal> to their respective LAPACK libraries. For example, Intel MKL is both a BLAS and LAPACK provider. An overlay can be created to use Intel MKL that looks like: 204 </para> 205 206<programlisting> 207self: super: 208 209{ 210 blas = super.blas.override { 211 blasProvider = self.mkl; 212 }; 213 214 lapack = super.lapack.override { 215 lapackProvider = self.mkl; 216 }; 217} 218</programlisting> 219 220 <para> 221 This overlay uses Intel’s MKL library for both BLAS and LAPACK interfaces. Note that the same can be accomplished at runtime using <literal>LD_LIBRARY_PATH</literal> of <literal>libblas.so.3</literal> and <literal>liblapack.so.3</literal>. For instance: 222 </para> 223 224<screen> 225<prompt>$ </prompt>LD_LIBRARY_PATH=$(nix-build -A mkl)/lib:$LD_LIBRARY_PATH nix-shell -p octave --run octave 226</screen> 227 228 <para> 229 Intel MKL requires an <literal>openmp</literal> implementation when running with multiple processors. By default, <literal>mkl</literal> will use Intel’s <literal>iomp</literal> implementation if no other is specified, but this is a runtime-only dependency and binary compatible with the LLVM implementation. To use that one instead, Intel recommends users set it with <literal>LD_PRELOAD</literal>. Note that <literal>mkl</literal> is only available on <literal>x86_64-linux</literal> and <literal>x86_64-darwin</literal>. Moreover, Hydra is not building and distributing pre-compiled binaries using it. 230 </para> 231 232 <para> 233 For BLAS/LAPACK switching to work correctly, all packages must depend on <literal>blas</literal> or <literal>lapack</literal>. This ensures that only one BLAS/LAPACK library is used at one time. There are two versions of BLAS/LAPACK currently in the wild, <literal>LP64</literal> (integer size = 32 bits) and <literal>ILP64</literal> (integer size = 64 bits). Some software needs special flags or patches to work with <literal>ILP64</literal>. You can check if <literal>ILP64</literal> is used in Nixpkgs with <varname>blas.isILP64</varname> and <varname>lapack.isILP64</varname>. Some software does NOT work with <literal>ILP64</literal>, and derivations need to specify an assertion to prevent this. You can prevent <literal>ILP64</literal> from being used with the following: 234 </para> 235 236<programlisting> 237{ stdenv, blas, lapack, ... }: 238 239assert (!blas.isILP64) &amp;&amp; (!lapack.isILP64); 240 241stdenv.mkDerivation { 242 ... 243} 244</programlisting> 245 </section> 246 247 <section xml:id="sec-overlays-alternatives-mpi"> 248 <title>Switching the MPI implementation</title> 249 250 <para> 251 All programs that are built with <link xlink:href="https://en.wikipedia.org/wiki/Message_Passing_Interface">MPI</link> support use the generic attribute <varname>mpi</varname> as an input. At the moment Nixpkgs natively provides two different MPI implementations: 252 <itemizedlist> 253 <listitem> 254 <para> 255 <link xlink:href="https://www.open-mpi.org/">Open MPI</link> (default), attribute name <varname>openmpi</varname> 256 </para> 257 </listitem> 258 <listitem> 259 <para> 260 <link xlink:href="https://www.mpich.org/">MPICH</link>, attribute name <varname>mpich</varname> 261 </para> 262 </listitem> 263 </itemizedlist> 264 </para> 265 266 <para> 267 To provide MPI enabled applications that use <literal>MPICH</literal>, instead of the default <literal>Open MPI</literal>, simply use the following overlay: 268 </para> 269 270<programlisting> 271self: super: 272 273{ 274 mpi = self.mpich; 275} 276 </programlisting> 277 </section> 278 </section> 279</chapter>