@@ -7,1016 +7,11 @@
77 The nixpkgs repository has several utility functions to manipulate Nix
88 expressions.
99 </para>
10− <section xml:id="sec-overrides">
11− <title>Overriding</title>
1210
13− <para>
14− Sometimes one wants to override parts of <literal>nixpkgs</literal>, e.g.
15− derivation attributes, the results of derivations or even the whole package
16− set.
17− </para>
18−
19− <section xml:id="sec-pkg-override">
20− <title><pkg>.override</title>
21−
22− <para>
23− The function <varname>override</varname> is usually available for all the
24− derivations in the nixpkgs expression (<varname>pkgs</varname>).
25− </para>
26−
27− <para>
28− It is used to override the arguments passed to a function.
29− </para>
30−
31− <para>
32− Example usages:
33−<programlisting>pkgs.foo.override { arg1 = val1; arg2 = val2; ... }</programlisting>
34−<programlisting>
35−import pkgs.path { overlays = [ (self: super: {
36− foo = super.foo.override { barSupport = true ; };
37− })]};
38−</programlisting>
39−<programlisting>
40−mypkg = pkgs.callPackage ./mypkg.nix {
41− mydep = pkgs.mydep.override { ... };
42− }
43−</programlisting>
44− </para>
45−
46− <para>
47− In the first example, <varname>pkgs.foo</varname> is the result of a
48− function call with some default arguments, usually a derivation. Using
49− <varname>pkgs.foo.override</varname> will call the same function with the
50− given new arguments.
51− </para>
52− </section>
53−
54− <section xml:id="sec-pkg-overrideAttrs">
55− <title><pkg>.overrideAttrs</title>
56−
57− <para>
58− The function <varname>overrideAttrs</varname> allows overriding the
59− attribute set passed to a <varname>stdenv.mkDerivation</varname> call,
60− producing a new derivation based on the original one. This function is
61− available on all derivations produced by the
62− <varname>stdenv.mkDerivation</varname> function, which is most packages in
63− the nixpkgs expression <varname>pkgs</varname>.
64− </para>
65−
66− <para>
67− Example usage:
68−<programlisting>
69−helloWithDebug = pkgs.hello.overrideAttrs (oldAttrs: rec {
70− separateDebugInfo = true;
71−});
72−</programlisting>
73− </para>
74−
75− <para>
76− In the above example, the <varname>separateDebugInfo</varname> attribute is
77− overridden to be true, thus building debug info for
78− <varname>helloWithDebug</varname>, while all other attributes will be
79− retained from the original <varname>hello</varname> package.
80− </para>
81−
82− <para>
83− The argument <varname>oldAttrs</varname> is conventionally used to refer to
84− the attr set originally passed to <varname>stdenv.mkDerivation</varname>.
85− </para>
86−
87− <note>
88− <para>
89− Note that <varname>separateDebugInfo</varname> is processed only by the
90− <varname>stdenv.mkDerivation</varname> function, not the generated, raw
91− Nix derivation. Thus, using <varname>overrideDerivation</varname> will not
92− work in this case, as it overrides only the attributes of the final
93− derivation. It is for this reason that <varname>overrideAttrs</varname>
94− should be preferred in (almost) all cases to
95− <varname>overrideDerivation</varname>, i.e. to allow using
96− <varname>sdenv.mkDerivation</varname> to process input arguments, as well
97− as the fact that it is easier to use (you can use the same attribute names
98− you see in your Nix code, instead of the ones generated (e.g.
99− <varname>buildInputs</varname> vs <varname>nativeBuildInputs</varname>,
100− and involves less typing.
101− </para>
102− </note>
103− </section>
104−
105− <section xml:id="sec-pkg-overrideDerivation">
106− <title><pkg>.overrideDerivation</title>
107−
108− <warning>
109− <para>
110− You should prefer <varname>overrideAttrs</varname> in almost all cases,
111− see its documentation for the reasons why.
112− <varname>overrideDerivation</varname> is not deprecated and will continue
113− to work, but is less nice to use and does not have as many abilities as
114− <varname>overrideAttrs</varname>.
115− </para>
116− </warning>
117−
118− <warning>
119− <para>
120− Do not use this function in Nixpkgs as it evaluates a Derivation before
121− modifying it, which breaks package abstraction and removes error-checking
122− of function arguments. In addition, this evaluation-per-function
123− application incurs a performance penalty, which can become a problem if
124− many overrides are used. It is only intended for ad-hoc customisation,
125− such as in <filename>~/.config/nixpkgs/config.nix</filename>.
126− </para>
127− </warning>
128−
129− <para>
130− The function <varname>overrideDerivation</varname> creates a new derivation
131− based on an existing one by overriding the original's attributes with the
132− attribute set produced by the specified function. This function is
133− available on all derivations defined using the
134− <varname>makeOverridable</varname> function. Most standard
135− derivation-producing functions, such as
136− <varname>stdenv.mkDerivation</varname>, are defined using this function,
137− which means most packages in the nixpkgs expression,
138− <varname>pkgs</varname>, have this function.
139− </para>
140−
141− <para>
142− Example usage:
143−<programlisting>
144−mySed = pkgs.gnused.overrideDerivation (oldAttrs: {
145− name = "sed-4.2.2-pre";
146− src = fetchurl {
147− url = ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2;
148− sha256 = "11nq06d131y4wmf3drm0yk502d2xc6n5qy82cg88rb9nqd2lj41k";
149− };
150− patches = [];
151−});
152−</programlisting>
153− </para>
154−
155− <para>
156− In the above example, the <varname>name</varname>, <varname>src</varname>,
157− and <varname>patches</varname> of the derivation will be overridden, while
158− all other attributes will be retained from the original derivation.
159− </para>
160−
161− <para>
162− The argument <varname>oldAttrs</varname> is used to refer to the attribute
163− set of the original derivation.
164− </para>
165−
166− <note>
167− <para>
168− A package's attributes are evaluated *before* being modified by the
169− <varname>overrideDerivation</varname> function. For example, the
170− <varname>name</varname> attribute reference in <varname>url =
171− "mirror://gnu/hello/${name}.tar.gz";</varname> is filled-in *before* the
172− <varname>overrideDerivation</varname> function modifies the attribute set.
173− This means that overriding the <varname>name</varname> attribute, in this
174− example, *will not* change the value of the <varname>url</varname>
175− attribute. Instead, we need to override both the <varname>name</varname>
176− *and* <varname>url</varname> attributes.
177− </para>
178− </note>
179− </section>
180−
181− <section xml:id="sec-lib-makeOverridable">
182− <title>lib.makeOverridable</title>
183−
184− <para>
185− The function <varname>lib.makeOverridable</varname> is used to make the
186− result of a function easily customizable. This utility only makes sense for
187− functions that accept an argument set and return an attribute set.
188− </para>
189−
190− <para>
191− Example usage:
192−<programlisting>
193−f = { a, b }: { result = a+b; };
194−c = lib.makeOverridable f { a = 1; b = 2; };
195−</programlisting>
196− </para>
197−
198− <para>
199− The variable <varname>c</varname> is the value of the <varname>f</varname>
200− function applied with some default arguments. Hence the value of
201− <varname>c.result</varname> is <literal>3</literal>, in this example.
202− </para>
203−
204− <para>
205− The variable <varname>c</varname> however also has some additional
206− functions, like <link linkend="sec-pkg-override">c.override</link> which
207− can be used to override the default arguments. In this example the value of
208− <varname>(c.override { a = 4; }).result</varname> is 6.
209− </para>
210− </section>
211− </section>
212− <section xml:id="sec-generators">
213− <title>Generators</title>
214−
215− <para>
216− Generators are functions that create file formats from nix data structures,
217− e. g. for configuration files. There are generators available for:
218− <literal>INI</literal>, <literal>JSON</literal> and <literal>YAML</literal>
219− </para>
220−
221− <para>
222− All generators follow a similar call interface: <code>generatorName
223− configFunctions data</code>, where <literal>configFunctions</literal> is an
224− attrset of user-defined functions that format nested parts of the content.
225− They each have common defaults, so often they do not need to be set
226− manually. An example is <code>mkSectionName ? (name: libStr.escape [ "[" "]"
227− ] name)</code> from the <literal>INI</literal> generator. It receives the
228− name of a section and sanitizes it. The default
229− <literal>mkSectionName</literal> escapes <literal>[</literal> and
230− <literal>]</literal> with a backslash.
231− </para>
232−
233− <para>
234− Generators can be fine-tuned to produce exactly the file format required by
235− your application/service. One example is an INI-file format which uses
236− <literal>: </literal> as separator, the strings
237− <literal>"yes"</literal>/<literal>"no"</literal> as boolean values and
238− requires all string values to be quoted:
239− </para>
240−
241−<programlisting>
242−with lib;
243−let
244− customToINI = generators.toINI {
245− # specifies how to format a key/value pair
246− mkKeyValue = generators.mkKeyValueDefault {
247− # specifies the generated string for a subset of nix values
248− mkValueString = v:
249− if v == true then ''"yes"''
250− else if v == false then ''"no"''
251− else if isString v then ''"${v}"''
252− # and delegats all other values to the default generator
253− else generators.mkValueStringDefault {} v;
254− } ":";
255− };
256−
257−# the INI file can now be given as plain old nix values
258−in customToINI {
259− main = {
260− pushinfo = true;
261− autopush = false;
262− host = "localhost";
263− port = 42;
264− };
265− mergetool = {
266− merge = "diff3";
267− };
268−}
269−</programlisting>
270−
271− <para>
272− This will produce the following INI file as nix string:
273− </para>
274−
275−<programlisting>
276−[main]
277−autopush:"no"
278−host:"localhost"
279−port:42
280−pushinfo:"yes"
281−str\:ange:"very::strange"
282−
283−[mergetool]
284−merge:"diff3"
285−</programlisting>
286−
287− <note>
288− <para>
289− Nix store paths can be converted to strings by enclosing a derivation
290− attribute like so: <code>"${drv}"</code>.
291− </para>
292− </note>
293−
294− <para>
295− Detailed documentation for each generator can be found in
296− <literal>lib/generators.nix</literal>.
297− </para>
298− </section>
299− <section xml:id="sec-debug">
300− <title>Debugging Nix Expressions</title>
301−
302− <para>
303− Nix is a unityped, dynamic language, this means every value can potentially
304− appear anywhere. Since it is also non-strict, evaluation order and what
305− ultimately is evaluated might surprise you. Therefore it is important to be
306− able to debug nix expressions.
307− </para>
308−
309− <para>
310− In the <literal>lib/debug.nix</literal> file you will find a number of
311− functions that help (pretty-)printing values while evaluation is runnnig.
312− You can even specify how deep these values should be printed recursively,
313− and transform them on the fly. Please consult the docstrings in
314− <literal>lib/debug.nix</literal> for usage information.
315− </para>
316− </section>
317− <section xml:id="sec-fhs-environments">
318− <title>buildFHSUserEnv</title>
319−
320− <para>
321− <function>buildFHSUserEnv</function> provides a way to build and run
322− FHS-compatible lightweight sandboxes. It creates an isolated root with bound
323− <filename>/nix/store</filename>, so its footprint in terms of disk space
324− needed is quite small. This allows one to run software which is hard or
325− unfeasible to patch for NixOS -- 3rd-party source trees with FHS
326− assumptions, games distributed as tarballs, software with integrity checking
327− and/or external self-updated binaries. It uses Linux namespaces feature to
328− create temporary lightweight environments which are destroyed after all
329− child processes exit, without root user rights requirement. Accepted
330− arguments are:
331− </para>
332−
333− <variablelist>
334− <varlistentry>
335− <term>
336− <literal>name</literal>
337− </term>
338− <listitem>
339− <para>
340− Environment name.
341− </para>
342− </listitem>
343− </varlistentry>
344− <varlistentry>
345− <term>
346− <literal>targetPkgs</literal>
347− </term>
348− <listitem>
349− <para>
350− Packages to be installed for the main host's architecture (i.e. x86_64 on
351− x86_64 installations). Along with libraries binaries are also installed.
352− </para>
353− </listitem>
354− </varlistentry>
355− <varlistentry>
356− <term>
357− <literal>multiPkgs</literal>
358− </term>
359− <listitem>
360− <para>
361− Packages to be installed for all architectures supported by a host (i.e.
362− i686 and x86_64 on x86_64 installations). Only libraries are installed by
363− default.
364− </para>
365− </listitem>
366− </varlistentry>
367− <varlistentry>
368− <term>
369− <literal>extraBuildCommands</literal>
370− </term>
371− <listitem>
372− <para>
373− Additional commands to be executed for finalizing the directory
374− structure.
375− </para>
376− </listitem>
377− </varlistentry>
378− <varlistentry>
379− <term>
380− <literal>extraBuildCommandsMulti</literal>
381− </term>
382− <listitem>
383− <para>
384− Like <literal>extraBuildCommands</literal>, but executed only on multilib
385− architectures.
386− </para>
387− </listitem>
388− </varlistentry>
389− <varlistentry>
390− <term>
391− <literal>extraOutputsToInstall</literal>
392− </term>
393− <listitem>
394− <para>
395− Additional derivation outputs to be linked for both target and
396− multi-architecture packages.
397− </para>
398− </listitem>
399− </varlistentry>
400− <varlistentry>
401− <term>
402− <literal>extraInstallCommands</literal>
403− </term>
404− <listitem>
405− <para>
406− Additional commands to be executed for finalizing the derivation with
407− runner script.
408− </para>
409− </listitem>
410− </varlistentry>
411− <varlistentry>
412− <term>
413− <literal>runScript</literal>
414− </term>
415− <listitem>
416− <para>
417− A command that would be executed inside the sandbox and passed all the
418− command line arguments. It defaults to <literal>bash</literal>.
419− </para>
420− </listitem>
421− </varlistentry>
422− </variablelist>
423−
424− <para>
425− One can create a simple environment using a <literal>shell.nix</literal>
426− like that:
427− </para>
428−
429−<programlisting><![CDATA[
430−{ pkgs ? import <nixpkgs> {} }:
431−
432−(pkgs.buildFHSUserEnv {
433− name = "simple-x11-env";
434− targetPkgs = pkgs: (with pkgs;
435− [ udev
436− alsaLib
437− ]) ++ (with pkgs.xorg;
438− [ libX11
439− libXcursor
440− libXrandr
441− ]);
442− multiPkgs = pkgs: (with pkgs;
443− [ udev
444− alsaLib
445− ]);
446− runScript = "bash";
447−}).env
448−]]></programlisting>
449−
450− <para>
451− Running <literal>nix-shell</literal> would then drop you into a shell with
452− these libraries and binaries available. You can use this to run
453− closed-source applications which expect FHS structure without hassles:
454− simply change <literal>runScript</literal> to the application path, e.g.
455− <filename>./bin/start.sh</filename> -- relative paths are supported.
456− </para>
457− </section>
458− <xi:include href="shell.section.xml" />
459− <section xml:id="sec-pkgs-dockerTools">
460− <title>pkgs.dockerTools</title>
461−
462− <para>
463− <varname>pkgs.dockerTools</varname> is a set of functions for creating and
464− manipulating Docker images according to the
465− <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#docker-image-specification-v120">
466− Docker Image Specification v1.2.0 </link>. Docker itself is not used to
467− perform any of the operations done by these functions.
468− </para>
469−
470− <warning>
471− <para>
472− The <varname>dockerTools</varname> API is unstable and may be subject to
473− backwards-incompatible changes in the future.
474− </para>
475− </warning>
476−
477− <section xml:id="ssec-pkgs-dockerTools-buildImage">
478− <title>buildImage</title>
479−
480− <para>
481− This function is analogous to the <command>docker build</command> command,
482− in that can used to build a Docker-compatible repository tarball containing
483− a single image with one or multiple layers. As such, the result is suitable
484− for being loaded in Docker with <command>docker load</command>.
485− </para>
486−
487− <para>
488− The parameters of <varname>buildImage</varname> with relative example
489− values are described below:
490− </para>
491−
492− <example xml:id='ex-dockerTools-buildImage'>
493− <title>Docker build</title>
494−<programlisting>
495−buildImage {
496− name = "redis"; <co xml:id='ex-dockerTools-buildImage-1' />
497− tag = "latest"; <co xml:id='ex-dockerTools-buildImage-2' />
498−
499− fromImage = someBaseImage; <co xml:id='ex-dockerTools-buildImage-3' />
500− fromImageName = null; <co xml:id='ex-dockerTools-buildImage-4' />
501− fromImageTag = "latest"; <co xml:id='ex-dockerTools-buildImage-5' />
502−
503− contents = pkgs.redis; <co xml:id='ex-dockerTools-buildImage-6' />
504− runAsRoot = '' <co xml:id='ex-dockerTools-buildImage-runAsRoot' />
505− #!${stdenv.shell}
506− mkdir -p /data
507− '';
508−
509− config = { <co xml:id='ex-dockerTools-buildImage-8' />
510− Cmd = [ "/bin/redis-server" ];
511− WorkingDir = "/data";
512− Volumes = {
513− "/data" = {};
514− };
515− };
516−}
517−</programlisting>
518− </example>
519−
520− <para>
521− The above example will build a Docker image <literal>redis/latest</literal>
522− from the given base image. Loading and running this image in Docker results
523− in <literal>redis-server</literal> being started automatically.
524− </para>
525−
526− <calloutlist>
527− <callout arearefs='ex-dockerTools-buildImage-1'>
528− <para>
529− <varname>name</varname> specifies the name of the resulting image. This
530− is the only required argument for <varname>buildImage</varname>.
531− </para>
532− </callout>
533− <callout arearefs='ex-dockerTools-buildImage-2'>
534− <para>
535− <varname>tag</varname> specifies the tag of the resulting image. By
536− default it's <literal>null</literal>, which indicates that the nix output
537− hash will be used as tag.
538− </para>
539− </callout>
540− <callout arearefs='ex-dockerTools-buildImage-3'>
541− <para>
542− <varname>fromImage</varname> is the repository tarball containing the
543− base image. It must be a valid Docker image, such as exported by
544− <command>docker save</command>. By default it's <literal>null</literal>,
545− which can be seen as equivalent to <literal>FROM scratch</literal> of a
546− <filename>Dockerfile</filename>.
547− </para>
548− </callout>
549− <callout arearefs='ex-dockerTools-buildImage-4'>
550− <para>
551− <varname>fromImageName</varname> can be used to further specify the base
552− image within the repository, in case it contains multiple images. By
553− default it's <literal>null</literal>, in which case
554− <varname>buildImage</varname> will peek the first image available in the
555− repository.
556− </para>
557− </callout>
558− <callout arearefs='ex-dockerTools-buildImage-5'>
559− <para>
560− <varname>fromImageTag</varname> can be used to further specify the tag of
561− the base image within the repository, in case an image contains multiple
562− tags. By default it's <literal>null</literal>, in which case
563− <varname>buildImage</varname> will peek the first tag available for the
564− base image.
565− </para>
566− </callout>
567− <callout arearefs='ex-dockerTools-buildImage-6'>
568− <para>
569− <varname>contents</varname> is a derivation that will be copied in the
570− new layer of the resulting image. This can be similarly seen as
571− <command>ADD contents/ /</command> in a <filename>Dockerfile</filename>.
572− By default it's <literal>null</literal>.
573− </para>
574− </callout>
575− <callout arearefs='ex-dockerTools-buildImage-runAsRoot'>
576− <para>
577− <varname>runAsRoot</varname> is a bash script that will run as root in an
578− environment that overlays the existing layers of the base image with the
579− new resulting layer, including the previously copied
580− <varname>contents</varname> derivation. This can be similarly seen as
581− <command>RUN ...</command> in a <filename>Dockerfile</filename>.
582− <note>
583− <para>
584− Using this parameter requires the <literal>kvm</literal> device to be
585− available.
586− </para>
587− </note>
588− </para>
589− </callout>
590− <callout arearefs='ex-dockerTools-buildImage-8'>
591− <para>
592− <varname>config</varname> is used to specify the configuration of the
593− containers that will be started off the built image in Docker. The
594− available options are listed in the
595− <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions">
596− Docker Image Specification v1.2.0 </link>.
597− </para>
598− </callout>
599− </calloutlist>
600−
601− <para>
602− After the new layer has been created, its closure (to which
603− <varname>contents</varname>, <varname>config</varname> and
604− <varname>runAsRoot</varname> contribute) will be copied in the layer
605− itself. Only new dependencies that are not already in the existing layers
606− will be copied.
607− </para>
608−
609− <para>
610− At the end of the process, only one new single layer will be produced and
611− added to the resulting image.
612− </para>
613−
614− <para>
615− The resulting repository will only list the single image
616− <varname>image/tag</varname>. In the case of
617− <xref linkend='ex-dockerTools-buildImage'/> it would be
618− <varname>redis/latest</varname>.
619− </para>
620−
621− <para>
622− It is possible to inspect the arguments with which an image was built using
623− its <varname>buildArgs</varname> attribute.
624− </para>
625−
626− <note>
627− <para>
628− If you see errors similar to <literal>getProtocolByName: does not exist
629− (no such protocol name: tcp)</literal> you may need to add
630− <literal>pkgs.iana-etc</literal> to <varname>contents</varname>.
631− </para>
632− </note>
633−
634− <note>
635− <para>
636− If you see errors similar to <literal>Error_Protocol ("certificate has
637− unknown CA",True,UnknownCa)</literal> you may need to add
638− <literal>pkgs.cacert</literal> to <varname>contents</varname>.
639− </para>
640− </note>
641−
642− <example xml:id="example-pkgs-dockerTools-buildImage-creation-date">
643− <title>Impurely Defining a Docker Layer's Creation Date</title>
644− <para>
645− By default <function>buildImage</function> will use a static
646− date of one second past the UNIX Epoch. This allows
647− <function>buildImage</function> to produce binary reproducible
648− images. When listing images with <command>docker list
649− images</command>, the newly created images will be listed like
650− this:
651− </para>
652− <screen><![CDATA[
653−$ docker image list
654−REPOSITORY TAG IMAGE ID CREATED SIZE
655−hello latest 08c791c7846e 48 years ago 25.2MB
656−]]></screen>
657− <para>
658− You can break binary reproducibility but have a sorted,
659− meaningful <literal>CREATED</literal> column by setting
660− <literal>created</literal> to <literal>now</literal>.
661− </para>
662− <programlisting><![CDATA[
663−pkgs.dockerTools.buildImage {
664− name = "hello";
665− tag = "latest";
666− created = "now";
667− contents = pkgs.hello;
668−
669− config.Cmd = [ "/bin/hello" ];
670−}
671−]]></programlisting>
672− <para>
673− and now the Docker CLI will display a reasonable date and
674− sort the images as expected:
675− <screen><![CDATA[
676−$ docker image list
677−REPOSITORY TAG IMAGE ID CREATED SIZE
678−hello latest de2bf4786de6 About a minute ago 25.2MB
679−]]></screen>
680− however, the produced images will not be binary reproducible.
681− </para>
682− </example>
683− </section>
684−
685− <section xml:id="ssec-pkgs-dockerTools-buildLayeredImage">
686− <title>buildLayeredImage</title>
687−
688− <para>
689− Create a Docker image with many of the store paths being on their own layer
690− to improve sharing between images.
691− </para>
692−
693− <variablelist>
694− <varlistentry>
695− <term>
696− <varname>name</varname>
697− </term>
698− <listitem>
699− <para>
700− The name of the resulting image.
701− </para>
702− </listitem>
703− </varlistentry>
704− <varlistentry>
705− <term>
706− <varname>tag</varname> <emphasis>optional</emphasis>
707− </term>
708− <listitem>
709− <para>
710− Tag of the generated image.
711− </para>
712− <para>
713− <emphasis>Default:</emphasis> the output path's hash
714− </para>
715− </listitem>
716− </varlistentry>
717− <varlistentry>
718− <term>
719− <varname>contents</varname> <emphasis>optional</emphasis>
720− </term>
721− <listitem>
722− <para>
723− Top level paths in the container. Either a single derivation, or a list
724− of derivations.
725− </para>
726− <para>
727− <emphasis>Default:</emphasis> <literal>[]</literal>
728− </para>
729− </listitem>
730− </varlistentry>
731− <varlistentry>
732− <term>
733− <varname>config</varname> <emphasis>optional</emphasis>
734− </term>
735− <listitem>
736− <para>
737− Run-time configuration of the container. A full list of the options are
738− available at in the
739− <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions">
740− Docker Image Specification v1.2.0 </link>.
741− </para>
742− <para>
743− <emphasis>Default:</emphasis> <literal>{}</literal>
744− </para>
745− </listitem>
746− </varlistentry>
747− <varlistentry>
748− <term>
749− <varname>created</varname> <emphasis>optional</emphasis>
750− </term>
751− <listitem>
752− <para>
753− Date and time the layers were created. Follows the same
754− <literal>now</literal> exception supported by
755− <literal>buildImage</literal>.
756− </para>
757− <para>
758− <emphasis>Default:</emphasis> <literal>1970-01-01T00:00:01Z</literal>
759− </para>
760− </listitem>
761− </varlistentry>
762− <varlistentry>
763− <term>
764− <varname>maxLayers</varname> <emphasis>optional</emphasis>
765− </term>
766− <listitem>
767− <para>
768− Maximum number of layers to create.
769− </para>
770− <para>
771− <emphasis>Default:</emphasis> <literal>24</literal>
772− </para>
773− </listitem>
774− </varlistentry>
775− </variablelist>
776−
777− <section xml:id="dockerTools-buildLayeredImage-arg-contents">
778− <title>Behavior of <varname>contents</varname> in the final image</title>
779−
780− <para>
781− Each path directly listed in <varname>contents</varname> will have a
782− symlink in the root of the image.
783− </para>
784−
785− <para>
786− For example:
787−<programlisting><![CDATA[
788−pkgs.dockerTools.buildLayeredImage {
789− name = "hello";
790− contents = [ pkgs.hello ];
791−}
792−]]></programlisting>
793− will create symlinks for all the paths in the <literal>hello</literal>
794− package:
795−<screen><![CDATA[
796−/bin/hello -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/bin/hello
797−/share/info/hello.info -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/info/hello.info
798−/share/locale/bg/LC_MESSAGES/hello.mo -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/locale/bg/LC_MESSAGES/hello.mo
799−]]></screen>
800− </para>
801− </section>
802−
803− <section xml:id="dockerTools-buildLayeredImage-arg-config">
804− <title>Automatic inclusion of <varname>config</varname> references</title>
805−
806− <para>
807− The closure of <varname>config</varname> is automatically included in the
808− closure of the final image.
809− </para>
810−
811− <para>
812− This allows you to make very simple Docker images with very little code.
813− This container will start up and run <command>hello</command>:
814−<programlisting><![CDATA[
815−pkgs.dockerTools.buildLayeredImage {
816− name = "hello";
817− config.Cmd = [ "${pkgs.hello}/bin/hello" ];
818−}
819−]]></programlisting>
820− </para>
821− </section>
822−
823− <section xml:id="dockerTools-buildLayeredImage-arg-maxLayers">
824− <title>Adjusting <varname>maxLayers</varname></title>
825−
826− <para>
827− Increasing the <varname>maxLayers</varname> increases the number of layers
828− which have a chance to be shared between different images.
829− </para>
830−
831− <para>
832− Modern Docker installations support up to 128 layers, however older
833− versions support as few as 42.
834− </para>
835−
836− <para>
837− If the produced image will not be extended by other Docker builds, it is
838− safe to set <varname>maxLayers</varname> to <literal>128</literal>.
839− However it will be impossible to extend the image further.
840− </para>
841−
842− <para>
843− The first (<literal>maxLayers-2</literal>) most "popular" paths will have
844− their own individual layers, then layer #<literal>maxLayers-1</literal>
845− will contain all the remaining "unpopular" paths, and finally layer
846− #<literal>maxLayers</literal> will contain the Image configuration.
847− </para>
848−
849− <para>
850− Docker's Layers are not inherently ordered, they are content-addressable
851− and are not explicitly layered until they are composed in to an Image.
852− </para>
853− </section>
854− </section>
855−
856− <section xml:id="ssec-pkgs-dockerTools-fetchFromRegistry">
857− <title>pullImage</title>
858−
859− <para>
860− This function is analogous to the <command>docker pull</command> command,
861− in that can be used to pull a Docker image from a Docker registry. By
862− default <link xlink:href="https://hub.docker.com/">Docker Hub</link> is
863− used to pull images.
864− </para>
865−
866− <para>
867− Its parameters are described in the example below:
868− </para>
869−
870− <example xml:id='ex-dockerTools-pullImage'>
871− <title>Docker pull</title>
872−<programlisting>
873−pullImage {
874− imageName = "nixos/nix"; <co xml:id='ex-dockerTools-pullImage-1' />
875− imageDigest = "sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b"; <co xml:id='ex-dockerTools-pullImage-2' />
876− finalImageTag = "1.11"; <co xml:id='ex-dockerTools-pullImage-3' />
877− sha256 = "0mqjy3zq2v6rrhizgb9nvhczl87lcfphq9601wcprdika2jz7qh8"; <co xml:id='ex-dockerTools-pullImage-4' />
878− os = "linux"; <co xml:id='ex-dockerTools-pullImage-5' />
879− arch = "x86_64"; <co xml:id='ex-dockerTools-pullImage-6' />
880−}
881−</programlisting>
882− </example>
883−
884− <calloutlist>
885− <callout arearefs='ex-dockerTools-pullImage-1'>
886− <para>
887− <varname>imageName</varname> specifies the name of the image to be
888− downloaded, which can also include the registry namespace (e.g.
889− <literal>nixos</literal>). This argument is required.
890− </para>
891− </callout>
892− <callout arearefs='ex-dockerTools-pullImage-2'>
893− <para>
894− <varname>imageDigest</varname> specifies the digest of the image to be
895− downloaded. Skopeo can be used to get the digest of an image, with its
896− <varname>inspect</varname> subcommand. Since a given
897− <varname>imageName</varname> may transparently refer to a manifest list
898− of images which support multiple architectures and/or operating systems,
899− supply the `--override-os` and `--override-arch` arguments to specify
900− exactly which image you want. By default it will match the OS and
901− architecture of the host the command is run on.
902−<programlisting>
903−$ nix-shell --packages skopeo jq --command "skopeo --override-os linux --override-arch x86_64 inspect docker://docker.io/nixos/nix:1.11 | jq -r '.Digest'"
904−sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b
905−</programlisting>
906− This argument is required.
907− </para>
908− </callout>
909− <callout arearefs='ex-dockerTools-pullImage-3'>
910− <para>
911− <varname>finalImageTag</varname>, if specified, this is the tag of the
912− image to be created. Note it is never used to fetch the image since we
913− prefer to rely on the immutable digest ID. By default it's
914− <literal>latest</literal>.
915− </para>
916− </callout>
917− <callout arearefs='ex-dockerTools-pullImage-4'>
918− <para>
919− <varname>sha256</varname> is the checksum of the whole fetched image.
920− This argument is required.
921− </para>
922− </callout>
923− <callout arearefs='ex-dockerTools-pullImage-5'>
924− <para>
925− <varname>os</varname>, if specified, is the operating system of the
926− fetched image. By default it's <literal>linux</literal>.
927− </para>
928− </callout>
929− <callout arearefs='ex-dockerTools-pullImage-6'>
930− <para>
931− <varname>arch</varname>, if specified, is the cpu architecture of the
932− fetched image. By default it's <literal>x86_64</literal>.
933− </para>
934− </callout>
935− </calloutlist>
936− </section>
937−
938− <section xml:id="ssec-pkgs-dockerTools-exportImage">
939− <title>exportImage</title>
940−
941− <para>
942− This function is analogous to the <command>docker export</command> command,
943− in that can used to flatten a Docker image that contains multiple layers.
944− It is in fact the result of the merge of all the layers of the image. As
945− such, the result is suitable for being imported in Docker with
946− <command>docker import</command>.
947− </para>
948−
949− <note>
950− <para>
951− Using this function requires the <literal>kvm</literal> device to be
952− available.
953− </para>
954− </note>
955−
956− <para>
957− The parameters of <varname>exportImage</varname> are the following:
958− </para>
959−
960− <example xml:id='ex-dockerTools-exportImage'>
961− <title>Docker export</title>
962−<programlisting>
963−exportImage {
964− fromImage = someLayeredImage;
965− fromImageName = null;
966− fromImageTag = null;
967−
968− name = someLayeredImage.name;
969−}
970− </programlisting>
971− </example>
972−
973− <para>
974− The parameters relative to the base image have the same synopsis as
975− described in <xref linkend='ssec-pkgs-dockerTools-buildImage'/>, except
976− that <varname>fromImage</varname> is the only required argument in this
977− case.
978− </para>
979−
980− <para>
981− The <varname>name</varname> argument is the name of the derivation output,
982− which defaults to <varname>fromImage.name</varname>.
983− </para>
984− </section>
985−
986− <section xml:id="ssec-pkgs-dockerTools-shadowSetup">
987− <title>shadowSetup</title>
988−
989− <para>
990− This constant string is a helper for setting up the base files for managing
991− users and groups, only if such files don't exist already. It is suitable
992− for being used in a <varname>runAsRoot</varname>
993− <xref linkend='ex-dockerTools-buildImage-runAsRoot'/> script for cases like
994− in the example below:
995− </para>
996−
997− <example xml:id='ex-dockerTools-shadowSetup'>
998− <title>Shadow base files</title>
999−<programlisting>
1000−buildImage {
1001− name = "shadow-basic";
1002−
1003− runAsRoot = ''
1004− #!${stdenv.shell}
1005− ${shadowSetup}
1006− groupadd -r redis
1007− useradd -r -g redis redis
1008− mkdir /data
1009− chown redis:redis /data
1010− '';
1011−}
1012−</programlisting>
1013− </example>
1014−
1015− <para>
1016− Creating base files like <literal>/etc/passwd</literal> or
1017− <literal>/etc/login.defs</literal> are necessary for shadow-utils to
1018− manipulate users and groups.
1019− </para>
1020− </section>
1021− </section>
11+ <xi:include href="functions/overrides.xml" />
12+ <xi:include href="functions/generators.xml" />
13+ <xi:include href="functions/debug.xml" />
14+ <xi:include href="functions/fhs-environments.xml" />
15+ <xi:include href="functions/shell.xml" />
16+ <xi:include href="functions/dockertools.xml" />
102217</chapter>