nixpkgs/doc/stdenv/multiple-output.xml

263 lines
14 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter [
<!ENTITY ndash "&#x2013;"> <!-- @vcunat likes to use this one ;-) -->
]>
<chapter xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:id="chap-multiple-output">
2018-05-02 00:54:21 +01:00
<title>Multiple-output packages</title>
2018-09-01 20:20:57 +01:00
<section xml:id="sec-multiple-outputs-introduction">
2018-05-02 00:54:21 +01:00
<title>Introduction</title>
2018-05-02 00:54:21 +01:00
<para>
2019-09-18 21:12:54 +01:00
The Nix language allows a derivation to produce multiple outputs, which is similar to what is utilized by other Linux distribution packaging systems. The outputs reside in separate Nix store paths, so they can be mostly handled independently of each other, including passing to build inputs, garbage collection or binary substitution. The exception is that building from source always produces all the outputs.
2018-05-02 00:54:21 +01:00
</para>
2018-05-02 00:54:21 +01:00
<para>
2019-09-18 21:12:54 +01:00
The main motivation is to save disk space by reducing runtime closure sizes; consequently also sizes of substituted binaries get reduced. Splitting can be used to have more granular runtime dependencies, for example the typical reduction is to split away development-only files, as those are typically not needed during runtime. As a result, closure sizes of many packages can get reduced to a half or even much less.
2018-05-02 00:54:21 +01:00
</para>
<note>
<para>
2019-09-18 21:12:54 +01:00
The reduction effects could be instead achieved by building the parts in completely separate derivations. That would often additionally reduce build-time closures, but it tends to be much harder to write such derivations, as build systems typically assume all parts are being built at once. This compromise approach of single source package producing multiple binary packages is also utilized often by rpm and deb.
2018-05-02 00:54:21 +01:00
</para>
</note>
<para>
A number of attributes can be used to work with a derivation with multiple outputs. The attribute <varname>outputs</varname> is a list of strings, which are the names of the outputs. For each of these names, an identically named attribute is created, corresponding to that output. The attribute <varname>meta.outputsToInstall</varname> is used to determine the default set of outputs to install when using the derivation name unqualified.
</para>
2018-05-02 00:54:21 +01:00
</section>
2018-09-01 20:20:57 +01:00
<section xml:id="sec-multiple-outputs-installing">
2018-05-02 00:54:21 +01:00
<title>Installing a split package</title>
<para>
When installing a package with multiple outputs, the package's <varname>meta.outputsToInstall</varname> attribute determines which outputs are actually installed. <varname>meta.outputsToInstall</varname> is a list whose <link xlink:href="https://github.com/NixOS/nixpkgs/blob/f1680774340d5443a1409c3421ced84ac1163ba9/pkgs/stdenv/generic/make-derivation.nix#L310-L320">default installs binaries and the associated man pages</link>. The following sections describe ways to install different outputs.
2018-05-02 00:54:21 +01:00
</para>
<section xml:id="sec-multiple-outputs-installing-nixos">
<title>Selecting outputs to install via NixOS</title>
<para>
NixOS provides two ways to select the outputs to install for packages listed in <varname>environment.systemPackages</varname>:
</para>
<itemizedlist>
<listitem>
<para>
The configuration option <varname>environment.extraOutputsToInstall</varname> is appended to each package's <varname>meta.outputsToInstall</varname> attribute to determine the outputs to install. It can for example be used to install <literal>info</literal> documentation or debug symbols for all packages.
</para>
</listitem>
<listitem>
<para>
The outputs can be listed as packages in <varname>environment.systemPackages</varname>. For example, the <literal>"out"</literal> and <literal>"info"</literal> outputs for the <varname>coreutils</varname> package can be installed by including <varname>coreutils</varname> and <varname>coreutils.info</varname> in <varname>environment.systemPackages</varname>.
</para>
</listitem>
</itemizedlist>
</section>
<section xml:id="sec-multiple-outputs-installing-nix-env">
<title>Selecting outputs to install via <command>nix-env</command></title>
<para>
<command>nix-env</command> lacks an easy way to select the outputs to install. When installing a package, <command>nix-env</command> always installs the outputs listed in <varname>meta.outputsToInstall</varname>, even when the user explicitly selects an output.
</para>
<warning>
2018-05-02 00:54:21 +01:00
<para>
<command>nix-env</command> silenty disregards the outputs selected by the user, and instead installs the outputs from <varname>meta.outputsToInstall</varname>. For example,
2018-05-02 00:54:21 +01:00
</para>
<programlisting>$ nix-env -iA nixpkgs.coreutils.info</programlisting>
2018-05-02 00:54:21 +01:00
<para>
installs the <literal>"out"</literal> output (<varname>coreutils.meta.outputsToInstall</varname> is <literal>[ "out" ]</literal>) instead of the requested <literal>"info"</literal>.
2018-05-02 00:54:21 +01:00
</para>
</warning>
<para>
The only recourse to select an output with <command>nix-env</command> is to override the package's <varname>meta.outputsToInstall</varname>, using the functions described in <xref linkend="chap-overrides" />. For example, the following overlay adds the <literal>"info"</literal> output for the <varname>coreutils</varname> package:
</para>
<programlisting>self: super:
{
coreutils = super.coreutils.overrideAttrs (oldAttrs: {
meta = oldAttrs.meta // { outputsToInstall = oldAttrs.meta.outputsToInstall or [ "out" ] ++ [ "info" ]; };
});
}
</programlisting>
</section>
2018-05-02 00:54:21 +01:00
</section>
2018-09-01 20:20:57 +01:00
<section xml:id="sec-multiple-outputs-using-split-packages">
2018-05-02 00:54:21 +01:00
<title>Using a split package</title>
<para>
2019-09-18 21:12:54 +01:00
In the Nix language the individual outputs can be reached explicitly as attributes, e.g. <varname>coreutils.info</varname>, but the typical case is just using packages as build inputs.
2018-05-02 00:54:21 +01:00
</para>
<para>
2019-09-18 21:12:54 +01:00
When a multiple-output derivation gets into a build input of another derivation, the <varname>dev</varname> output is added if it exists, otherwise the first output is added. In addition to that, <varname>propagatedBuildOutputs</varname> of that package which by default contain <varname>$outputBin</varname> and <varname>$outputLib</varname> are also added. (See <xref linkend="multiple-output-file-type-groups" />.)
2018-05-02 00:54:21 +01:00
</para>
<para>
2019-09-18 21:12:54 +01:00
In some cases it may be desirable to combine different outputs under a single store path. A function <literal>symlinkJoin</literal> can be used to do this. (Note that it may negate some closure size benefits of using a multiple-output package.)
</para>
2018-05-02 00:54:21 +01:00
</section>
2018-09-01 20:20:57 +01:00
<section xml:id="sec-multiple-outputs-">
2018-05-02 00:54:21 +01:00
<title>Writing a split derivation</title>
2018-05-02 00:54:21 +01:00
<para>
Here you find how to write a derivation that produces multiple outputs.
</para>
2018-05-02 00:54:21 +01:00
<para>
2019-09-18 21:12:54 +01:00
In nixpkgs there is a framework supporting multiple-output derivations. It tries to cover most cases by default behavior. You can find the source separated in &lt;<filename>nixpkgs/pkgs/build-support/setup-hooks/multiple-outputs.sh</filename>&gt;; it's relatively well-readable. The whole machinery is triggered by defining the <varname>outputs</varname> attribute to contain the list of desired output names (strings).
2018-05-02 00:54:21 +01:00
</para>
2018-05-02 00:54:21 +01:00
<programlisting>outputs = [ "bin" "dev" "out" "doc" ];</programlisting>
<para>
2019-09-18 21:12:54 +01:00
Often such a single line is enough. For each output an equally named environment variable is passed to the builder and contains the path in nix store for that output. Typically you also want to have the main <varname>out</varname> output, as it catches any files that didn't get elsewhere.
2018-05-02 00:54:21 +01:00
</para>
<note>
<para>
2019-09-18 21:12:54 +01:00
There is a special handling of the <varname>debug</varname> output, described at <xref linkend="stdenv-separateDebugInfo" />.
2018-05-02 00:54:21 +01:00
</para>
</note>
<section xml:id="multiple-output-file-binaries-first-convention">
2018-05-02 00:54:21 +01:00
<title><quote>Binaries first</quote></title>
<para>
2019-09-18 21:12:54 +01:00
A commonly adopted convention in <literal>nixpkgs</literal> is that executables provided by the package are contained within its first output. This convention allows the dependent packages to reference the executables provided by packages in a uniform manner. For instance, provided with the knowledge that the <literal>perl</literal> package contains a <literal>perl</literal> executable it can be referenced as <literal>${pkgs.perl}/bin/perl</literal> within a Nix derivation that needs to execute a Perl script.
2018-05-02 00:54:21 +01:00
</para>
<para>
2019-09-18 21:12:54 +01:00
The <literal>glibc</literal> package is a deliberate single exception to the <quote>binaries first</quote> convention. The <literal>glibc</literal> has <literal>libs</literal> as its first output allowing the libraries provided by <literal>glibc</literal> to be referenced directly (e.g. <literal>${stdenv.glibc}/lib/ld-linux-x86-64.so.2</literal>). The executables provided by <literal>glibc</literal> can be accessed via its <literal>bin</literal> attribute (e.g. <literal>${stdenv.glibc.bin}/bin/ldd</literal>).
2018-05-02 00:54:21 +01:00
</para>
<para>
The reason for why <literal>glibc</literal> deviates from the convention is because referencing a library provided by <literal>glibc</literal> is a very common operation among Nix packages. For instance, third-party executables packaged by Nix are typically patched and relinked with the relevant version of <literal>glibc</literal> libraries from Nix packages (please see the documentation on <link xlink:href="https://github.com/NixOS/patchelf/blob/master/README">patchelf</link> for more details).
2018-05-02 00:54:21 +01:00
</para>
</section>
<section xml:id="multiple-output-file-type-groups">
2018-05-02 00:54:21 +01:00
<title>File type groups</title>
2018-05-02 00:54:21 +01:00
<para>
2019-09-18 21:12:54 +01:00
The support code currently recognizes some particular kinds of outputs and either instructs the build system of the package to put files into their desired outputs or it moves the files during the fixup phase. Each group of file types has an <varname>outputFoo</varname> variable specifying the output name where they should go. If that variable isn't defined by the derivation writer, it is guessed &ndash; a default output name is defined, falling back to other possibilities if the output isn't defined.
2018-05-02 00:54:21 +01:00
</para>
<variablelist>
<varlistentry>
<term>
<varname> $outputDev</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is for development-only files. These include C(++) headers, pkg-config, cmake and aclocal files. They go to <varname>dev</varname> or <varname>out</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputBin</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is meant for user-facing binaries, typically residing in bin/. They go to <varname>bin</varname> or <varname>out</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputLib</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is meant for libraries, typically residing in <filename>lib/</filename> and <filename>libexec/</filename>. They go to <varname>lib</varname> or <varname>out</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputDoc</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is for user documentation, typically residing in <filename>share/doc/</filename>. It goes to <varname>doc</varname> or <varname>out</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputDevdoc</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is for <emphasis>developer</emphasis> documentation. Currently we count gtk-doc and devhelp books in there. It goes to <varname>devdoc</varname> or is removed (!) by default. This is because e.g. gtk-doc tends to be rather large and completely unused by nixpkgs users.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputMan</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is for man pages (except for section 3). They go to <varname>man</varname> or <varname>$outputBin</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputDevman</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is for section 3 man pages. They go to <varname>devman</varname> or <varname>$outputMan</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname> $outputInfo</varname>
2018-05-02 00:54:21 +01:00
</term>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
is for info pages. They go to <varname>info</varname> or <varname>$outputBin</varname> by default.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</varlistentry>
</variablelist>
</section>
2018-09-01 20:20:57 +01:00
<section xml:id="sec-multiple-outputs-caveats">
2018-05-02 00:54:21 +01:00
<title>Common caveats</title>
2018-05-02 00:54:21 +01:00
<itemizedlist>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
Some configure scripts don't like some of the parameters passed by default by the framework, e.g. <literal>--docdir=/foo/bar</literal>. You can disable this by setting <literal>setOutputFlags = false;</literal>.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
The outputs of a single derivation can retain references to each other, but note that circular references are not allowed. (And each strongly-connected component would act as a single output anyway.)
2018-05-02 00:54:21 +01:00
</para>
</listitem>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
Most of split packages contain their core functionality in libraries. These libraries tend to refer to various kind of data that typically gets into <varname>out</varname>, e.g. locale strings, so there is often no advantage in separating the libraries into <varname>lib</varname>, as keeping them in <varname>out</varname> is easier.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
<listitem>
<para>
2019-09-18 21:12:54 +01:00
Some packages have hidden assumptions on install paths, which complicates splitting.
2018-05-02 00:54:21 +01:00
</para>
</listitem>
</itemizedlist>
</section>
</section>
<!--Writing a split derivation-->
</chapter>