1<chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xml:id="chap-stdenv">
4 <title>The Standard Environment</title>
5 <para>
6 The standard build environment in the Nix Packages collection provides an
7 environment for building Unix packages that does a lot of common build tasks
8 automatically. In fact, for Unix packages that use the standard
9 <literal>./configure; make; make install</literal> build interface, you
10 don’t need to write a build script at all; the standard environment does
11 everything automatically. If <literal>stdenv</literal> doesn’t do what you
12 need automatically, you can easily customise or override the various build
13 phases.
14 </para>
15 <section xml:id="sec-using-stdenv">
16 <title>Using <literal>stdenv</literal></title>
17
18 <para>
19 To build a package with the standard environment, you use the function
20 <varname>stdenv.mkDerivation</varname>, instead of the primitive built-in
21 function <varname>derivation</varname>, e.g.
22<programlisting>
23stdenv.mkDerivation {
24 name = "libfoo-1.2.3";
25 src = fetchurl {
26 url = http://example.org/libfoo-1.2.3.tar.bz2;
27 sha256 = "0x2g1jqygyr5wiwg4ma1nd7w4ydpy82z9gkcv8vh2v8dn3y58v5m";
28 };
29}</programlisting>
30 (<varname>stdenv</varname> needs to be in scope, so if you write this in a
31 separate Nix expression from <filename>pkgs/all-packages.nix</filename>, you
32 need to pass it as a function argument.) Specifying a
33 <varname>name</varname> and a <varname>src</varname> is the absolute minimum
34 you need to do. Many packages have dependencies that are not provided in the
35 standard environment. It’s usually sufficient to specify those
36 dependencies in the <varname>buildInputs</varname> attribute:
37<programlisting>
38stdenv.mkDerivation {
39 name = "libfoo-1.2.3";
40 ...
41 buildInputs = [libbar perl ncurses];
42}</programlisting>
43 This attribute ensures that the <filename>bin</filename> subdirectories of
44 these packages appear in the <envar>PATH</envar> environment variable during
45 the build, that their <filename>include</filename> subdirectories are
46 searched by the C compiler, and so on. (See
47 <xref linkend="ssec-setup-hooks"/> for details.)
48 </para>
49
50 <para>
51 Often it is necessary to override or modify some aspect of the build. To
52 make this easier, the standard environment breaks the package build into a
53 number of <emphasis>phases</emphasis>, all of which can be overridden or
54 modified individually: unpacking the sources, applying patches, configuring,
55 building, and installing. (There are some others; see
56 <xref linkend="sec-stdenv-phases"/>.) For instance, a package that doesn’t
57 supply a makefile but instead has to be compiled “manually” could be
58 handled like this:
59<programlisting>
60stdenv.mkDerivation {
61 name = "fnord-4.5";
62 ...
63 buildPhase = ''
64 gcc foo.c -o foo
65 '';
66 installPhase = ''
67 mkdir -p $out/bin
68 cp foo $out/bin
69 '';
70}</programlisting>
71 (Note the use of <literal>''</literal>-style string literals, which are very
72 convenient for large multi-line script fragments because they don’t need
73 escaping of <literal>"</literal> and <literal>\</literal>, and because
74 indentation is intelligently removed.)
75 </para>
76
77 <para>
78 There are many other attributes to customise the build. These are listed in
79 <xref linkend="ssec-stdenv-attributes"/>.
80 </para>
81
82 <para>
83 While the standard environment provides a generic builder, you can still
84 supply your own build script:
85<programlisting>
86stdenv.mkDerivation {
87 name = "libfoo-1.2.3";
88 ...
89 builder = ./builder.sh;
90}</programlisting>
91 where the builder can do anything it wants, but typically starts with
92<programlisting>
93source $stdenv/setup
94</programlisting>
95 to let <literal>stdenv</literal> set up the environment (e.g., process the
96 <varname>buildInputs</varname>). If you want, you can still use
97 <literal>stdenv</literal>’s generic builder:
98<programlisting>
99source $stdenv/setup
100
101buildPhase() {
102 echo "... this is my custom build phase ..."
103 gcc foo.c -o foo
104}
105
106installPhase() {
107 mkdir -p $out/bin
108 cp foo $out/bin
109}
110
111genericBuild
112</programlisting>
113 </para>
114 </section>
115 <section xml:id="sec-tools-of-stdenv">
116 <title>Tools provided by <literal>stdenv</literal></title>
117
118 <para>
119 The standard environment provides the following packages:
120 <itemizedlist>
121 <listitem>
122 <para>
123 The GNU C Compiler, configured with C and C++ support.
124 </para>
125 </listitem>
126 <listitem>
127 <para>
128 GNU coreutils (contains a few dozen standard Unix commands).
129 </para>
130 </listitem>
131 <listitem>
132 <para>
133 GNU findutils (contains <command>find</command>).
134 </para>
135 </listitem>
136 <listitem>
137 <para>
138 GNU diffutils (contains <command>diff</command>, <command>cmp</command>).
139 </para>
140 </listitem>
141 <listitem>
142 <para>
143 GNU <command>sed</command>.
144 </para>
145 </listitem>
146 <listitem>
147 <para>
148 GNU <command>grep</command>.
149 </para>
150 </listitem>
151 <listitem>
152 <para>
153 GNU <command>awk</command>.
154 </para>
155 </listitem>
156 <listitem>
157 <para>
158 GNU <command>tar</command>.
159 </para>
160 </listitem>
161 <listitem>
162 <para>
163 <command>gzip</command>, <command>bzip2</command> and
164 <command>xz</command>.
165 </para>
166 </listitem>
167 <listitem>
168 <para>
169 GNU Make. It has been patched to provide <quote>nested</quote> output
170 that can be fed into the <command>nix-log2xml</command> command and
171 <command>log2html</command> stylesheet to create a structured, readable
172 output of the build steps performed by Make.
173 </para>
174 </listitem>
175 <listitem>
176 <para>
177 Bash. This is the shell used for all builders in the Nix Packages
178 collection. Not using <command>/bin/sh</command> removes a large source
179 of portability problems.
180 </para>
181 </listitem>
182 <listitem>
183 <para>
184 The <command>patch</command> command.
185 </para>
186 </listitem>
187 </itemizedlist>
188 </para>
189
190 <para>
191 On Linux, <literal>stdenv</literal> also includes the
192 <command>patchelf</command> utility.
193 </para>
194 </section>
195 <section xml:id="ssec-stdenv-dependencies">
196 <title>Specifying dependencies</title>
197
198 <para>
199 As described in the Nix manual, almost any <filename>*.drv</filename> store
200 path in a derivation's attribute set will induce a dependency on that
201 derivation. <varname>mkDerivation</varname>, however, takes a few attributes
202 intended to, between them, include all the dependencies of a package. This
203 is done both for structure and consistency, but also so that certain other
204 setup can take place. For example, certain dependencies need their bin
205 directories added to the <envar>PATH</envar>. That is built-in, but other
206 setup is done via a pluggable mechanism that works in conjunction with these
207 dependency attributes. See <xref linkend="ssec-setup-hooks"/> for details.
208 </para>
209
210 <para>
211 Dependencies can be broken down along three axes: their host and target
212 platforms relative to the new derivation's, and whether they are propagated.
213 The platform distinctions are motivated by cross compilation; see
214 <xref linkend="chap-cross"/> for exactly what each platform means.
215 <footnote xml:id="footnote-stdenv-ignored-build-platform">
216 <para>
217 The build platform is ignored because it is a mere implementation detail
218 of the package satisfying the dependency: As a general programming
219 principle, dependencies are always <emphasis>specified</emphasis> as
220 interfaces, not concrete implementation.
221 </para>
222 </footnote>
223 But even if one is not cross compiling, the platforms imply whether or not
224 the dependency is needed at run-time or build-time, a concept that makes
225 perfect sense outside of cross compilation. For now, the run-time/build-time
226 distinction is just a hint for mental clarity, but in the future it perhaps
227 could be enforced.
228 </para>
229
230 <para>
231 The extension of <envar>PATH</envar> with dependencies, alluded to above,
232 proceeds according to the relative platforms alone. The process is carried
233 out only for dependencies whose host platform matches the new derivation's
234 build platform–i.e. which run on the platform where the new derivation
235 will be built.
236 <footnote xml:id="footnote-stdenv-native-dependencies-in-path">
237 <para>
238 Currently, that means for native builds all dependencies are put on the
239 <envar>PATH</envar>. But in the future that may not be the case for sake
240 of matching cross: the platforms would be assumed to be unique for native
241 and cross builds alike, so only the <varname>depsBuild*</varname> and
242 <varname>nativeBuildDependencies</varname> dependencies would affect the
243 <envar>PATH</envar>.
244 </para>
245 </footnote>
246 For each dependency <replaceable>dep</replaceable> of those dependencies,
247 <filename><replaceable>dep</replaceable>/bin</filename>, if present, is
248 added to the <envar>PATH</envar> environment variable.
249 </para>
250
251 <para>
252 The dependency is propagated when it forces some of its other-transitive
253 (non-immediate) downstream dependencies to also take it on as an immediate
254 dependency. Nix itself already takes a package's transitive dependencies
255 into account, but this propagation ensures nixpkgs-specific infrastructure
256 like setup hooks (mentioned above) also are run as if the propagated
257 dependency.
258 </para>
259
260 <para>
261 It is important to note dependencies are not necessary propagated as the
262 same sort of dependency that they were before, but rather as the
263 corresponding sort so that the platform rules still line up. The exact rules
264 for dependency propagation can be given by assigning each sort of dependency
265 two integers based one how it's host and target platforms are offset from
266 the depending derivation's platforms. Those offsets are given are given
267 below in the descriptions of each dependency list attribute.
268 Algorithmically, we traverse propagated inputs, accumulating every
269 propagated dep's propagated deps and adjusting them to account for the
270 "shift in perspective" described by the current dep's platform offsets. This
271 results in sort a transitive closure of the dependency relation, with the
272 offsets being approximately summed when two dependency links are combined.
273 We also prune transitive deps whose combined offsets go out-of-bounds, which
274 can be viewed as a filter over that transitive closure removing dependencies
275 that are blatantly absurd.
276 </para>
277
278 <para>
279 We can define the process precisely with
280 <link xlink:href="https://en.wikipedia.org/wiki/Natural_deduction">Natural
281 Deduction</link> using the inference rules. This probably seems a bit
282 obtuse, but so is the bash code that actually implements it!
283 <footnote xml:id="footnote-stdenv-find-inputs-location">
284 <para>
285 The <function>findInputs</function> function, currently residing in
286 <filename>pkgs/stdenv/generic/setup.sh</filename>, implements the
287 propagation logic.
288 </para>
289 </footnote>
290 They're confusing in very different ways so...hopefully if something doesn't
291 make sense in one presentation, it does in the other!
292<programlisting>
293let mapOffset(h, t, i) = i + (if i <= 0 then h else t - 1)
294
295propagated-dep(h0, t0, A, B)
296propagated-dep(h1, t1, B, C)
297h0 + h1 in {-1, 0, 1}
298h0 + t1 in {-1, 0, 1}
299-------------------------------------- Transitive property
300propagated-dep(mapOffset(h0, t0, h1),
301 mapOffset(h0, t0, t1),
302 A, C)</programlisting>
303<programlisting>
304let mapOffset(h, t, i) = i + (if i <= 0 then h else t - 1)
305
306dep(h0, _, A, B)
307propagated-dep(h1, t1, B, C)
308h0 + h1 in {-1, 0, 1}
309h0 + t1 in {-1, 0, -1}
310-------------------------------------- Take immediate deps' propagated deps
311propagated-dep(mapOffset(h0, t0, h1),
312 mapOffset(h0, t0, t1),
313 A, C)</programlisting>
314<programlisting>
315propagated-dep(h, t, A, B)
316-------------------------------------- Propagated deps count as deps
317dep(h, t, A, B)</programlisting>
318 Some explanation of this monstrosity is in order. In the common case, the
319 target offset of a dependency is the successor to the target offset:
320 <literal>t = h + 1</literal>. That means that:
321<programlisting>
322let f(h, t, i) = i + (if i <= 0 then h else t - 1)
323let f(h, h + 1, i) = i + (if i <= 0 then h else (h + 1) - 1)
324let f(h, h + 1, i) = i + (if i <= 0 then h else h)
325let f(h, h + 1, i) = i + h
326 </programlisting>
327 This is where the "sum-like" comes from above: We can just sum all the host
328 offset to get the host offset of the transitive dependency. The target
329 offset is the transitive dep is simply the host offset + 1, just as it was
330 with the dependencies composed to make this transitive one; it can be
331 ignored as it doesn't add any new information.
332 </para>
333
334 <para>
335 Because of the bounds checks, the uncommon cases are <literal>h =
336 t</literal> and <literal>h + 2 = t</literal>. In the former case, the
337 motivation for <function>mapOffset</function> is that since its host and
338 target platforms are the same, no transitive dep of it should be able to
339 "discover" an offset greater than its reduced target offsets.
340 <function>mapOffset</function> effectively "squashes" all its transitive
341 dependencies' offsets so that none will ever be greater than the target
342 offset of the original <literal>h = t</literal> package. In the other case,
343 <literal>h + 1</literal> is skipped over between the host and target
344 offsets. Instead of squashing the offsets, we need to "rip" them apart so no
345 transitive dependencies' offset is that one.
346 </para>
347
348 <para>
349 Overall, the unifying theme here is that propagation shouldn't be
350 introducing transitive dependencies involving platforms the needing package
351 is unaware of. The offset bounds checking and definition of
352 <function>mapOffset</function> together ensure that this is the case.
353 Discovering a new offset is discovering a new platform, and since those
354 platforms weren't in the derivation "spec" of the needing package, they
355 cannot be relevant. From a capability perspective, we can imagine that the
356 host and target platforms of a package are the capabilities a package
357 requires, and the depending package must provide the capability to the
358 dependency.
359 </para>
360
361 <variablelist>
362 <title>Variables specifying dependencies</title>
363 <varlistentry>
364 <term>
365 <varname>depsBuildBuild</varname>
366 </term>
367 <listitem>
368 <para>
369 A list of dependencies whose host and target platforms are the new
370 derivation's build platform. This means a <literal>-1</literal> host and
371 <literal>-1</literal> target offset from the new derivation's platforms.
372 They are programs/libraries used at build time that furthermore produce
373 programs/libraries also used at build time. If the dependency doesn't
374 care about the target platform (i.e. isn't a compiler or similar tool),
375 put it in <varname>nativeBuildInputs</varname>instead. The most common
376 use for this <literal>buildPackages.stdenv.cc</literal>, the default C
377 compiler for this role. That example crops up more than one might think
378 in old commonly used C libraries.
379 </para>
380 <para>
381 Since these packages are able to be run at build time, that are always
382 added to the <envar>PATH</envar>, as described above. But since these
383 packages are only guaranteed to be able to run then, they shouldn't
384 persist as run-time dependencies. This isn't currently enforced, but
385 could be in the future.
386 </para>
387 </listitem>
388 </varlistentry>
389 <varlistentry>
390 <term>
391 <varname>nativeBuildInputs</varname>
392 </term>
393 <listitem>
394 <para>
395 A list of dependencies whose host platform is the new derivation's build
396 platform, and target platform is the new derivation's host platform. This
397 means a <literal>-1</literal> host offset and <literal>0</literal> target
398 offset from the new derivation's platforms. They are programs/libraries
399 used at build time that, if they are a compiler or similar tool, produce
400 code to run at run time—i.e. tools used to build the new derivation. If
401 the dependency doesn't care about the target platform (i.e. isn't a
402 compiler or similar tool), put it here, rather than in
403 <varname>depsBuildBuild</varname> or <varname>depsBuildTarget</varname>.
404 This would be called <varname>depsBuildHost</varname> but for historical
405 continuity.
406 </para>
407 <para>
408 Since these packages are able to be run at build time, that are added to
409 the <envar>PATH</envar>, as described above. But since these packages
410 only are guaranteed to be able to run then, they shouldn't persist as
411 run-time dependencies. This isn't currently enforced, but could be in the
412 future.
413 </para>
414 </listitem>
415 </varlistentry>
416 <varlistentry>
417 <term>
418 <varname>depsBuildTarget</varname>
419 </term>
420 <listitem>
421 <para>
422 A list of dependencies whose host platform is the new derivation's build
423 platform, and target platform is the new derivation's target platform.
424 This means a <literal>-1</literal> host offset and <literal>1</literal>
425 target offset from the new derivation's platforms. They are programs used
426 at build time that produce code to run at run with code produced by the
427 depending package. Most commonly, these would tools used to build the
428 runtime or standard library the currently-being-built compiler will
429 inject into any code it compiles. In many cases, the currently-being
430 built compiler is itself employed for that task, but when that compiler
431 won't run (i.e. its build and host platform differ) this is not possible.
432 Other times, the compiler relies on some other tool, like binutils, that
433 is always built separately so the dependency is unconditional.
434 </para>
435 <para>
436 This is a somewhat confusing dependency to wrap ones head around, and for
437 good reason. As the only one where the platform offsets are not adjacent
438 integers, it requires thinking of a bootstrapping stage
439 <emphasis>two</emphasis> away from the current one. It and it's use-case
440 go hand in hand and are both considered poor form: try not to need this
441 sort dependency, and try not avoid building standard libraries / runtimes
442 in the same derivation as the compiler produces code using them. Instead
443 strive to build those like a normal library, using the newly-built
444 compiler just as a normal library would. In short, do not use this
445 attribute unless you are packaging a compiler and are sure it is needed.
446 </para>
447 <para>
448 Since these packages are able to be run at build time, that are added to
449 the <envar>PATH</envar>, as described above. But since these packages
450 only are guaranteed to be able to run then, they shouldn't persist as
451 run-time dependencies. This isn't currently enforced, but could be in the
452 future.
453 </para>
454 </listitem>
455 </varlistentry>
456 <varlistentry>
457 <term>
458 <varname>depsHostHost</varname>
459 </term>
460 <listitem>
461 <para>
462 A list of dependencies whose host and target platforms match the new
463 derivation's host platform. This means a both <literal>0</literal> host
464 offset and <literal>0</literal> target offset from the new derivation's
465 host platform. These are packages used at run-time to generate code also
466 used at run-time. In practice, that would usually be tools used by
467 compilers for metaprogramming/macro systems, or libraries used by the
468 macros/metaprogramming code itself. It's always preferable to use a
469 <varname>depsBuildBuild</varname> dependency in the derivation being
470 built than a <varname>depsHostHost</varname> on the tool doing the
471 building for this purpose.
472 </para>
473 </listitem>
474 </varlistentry>
475 <varlistentry>
476 <term>
477 <varname>buildInputs</varname>
478 </term>
479 <listitem>
480 <para>
481 A list of dependencies whose host platform and target platform match the
482 new derivation's. This means a <literal>0</literal> host offset and
483 <literal>1</literal> target offset from the new derivation's host
484 platform. This would be called <varname>depsHostTarget</varname> but for
485 historical continuity. If the dependency doesn't care about the target
486 platform (i.e. isn't a compiler or similar tool), put it here, rather
487 than in <varname>depsBuildBuild</varname>.
488 </para>
489 <para>
490 These often are programs/libraries used by the new derivation at
491 <emphasis>run</emphasis>-time, but that isn't always the case. For
492 example, the machine code in a statically linked library is only used at
493 run time, but the derivation containing the library is only needed at
494 build time. Even in the dynamic case, the library may also be needed at
495 build time to appease the linker.
496 </para>
497 </listitem>
498 </varlistentry>
499 <varlistentry>
500 <term>
501 <varname>depsTargetTarget</varname>
502 </term>
503 <listitem>
504 <para>
505 A list of dependencies whose host platform matches the new derivation's
506 target platform. This means a <literal>1</literal> offset from the new
507 derivation's platforms. These are packages that run on the target
508 platform, e.g. the standard library or run-time deps of standard library
509 that a compiler insists on knowing about. It's poor form in almost all
510 cases for a package to depend on another from a future stage [future
511 stage corresponding to positive offset]. Do not use this attribute unless
512 you are packaging a compiler and are sure it is needed.
513 </para>
514 </listitem>
515 </varlistentry>
516 <varlistentry>
517 <term>
518 <varname>depsBuildBuildPropagated</varname>
519 </term>
520 <listitem>
521 <para>
522 The propagated equivalent of <varname>depsBuildBuild</varname>. This
523 perhaps never ought to be used, but it is included for consistency [see
524 below for the others].
525 </para>
526 </listitem>
527 </varlistentry>
528 <varlistentry>
529 <term>
530 <varname>propagatedNativeBuildInputs</varname>
531 </term>
532 <listitem>
533 <para>
534 The propagated equivalent of <varname>nativeBuildInputs</varname>. This
535 would be called <varname>depsBuildHostPropagated</varname> but for
536 historical continuity. For example, if package <varname>Y</varname> has
537 <literal>propagatedNativeBuildInputs = [X]</literal>, and package
538 <varname>Z</varname> has <literal>buildInputs = [Y]</literal>, then
539 package <varname>Z</varname> will be built as if it included package
540 <varname>X</varname> in its <varname>nativeBuildInputs</varname>. If
541 instead, package <varname>Z</varname> has <literal>nativeBuildInputs =
542 [Y]</literal>, then <varname>Z</varname> will be built as if it included
543 <varname>X</varname> in the <varname>depsBuildBuild</varname> of package
544 <varname>Z</varname>, because of the sum of the two <literal>-1</literal>
545 host offsets.
546 </para>
547 </listitem>
548 </varlistentry>
549 <varlistentry>
550 <term>
551 <varname>depsBuildTargetPropagated</varname>
552 </term>
553 <listitem>
554 <para>
555 The propagated equivalent of <varname>depsBuildTarget</varname>. This is
556 prefixed for the same reason of alerting potential users.
557 </para>
558 </listitem>
559 </varlistentry>
560 <varlistentry>
561 <term>
562 <varname>depsHostHostPropagated</varname>
563 </term>
564 <listitem>
565 <para>
566 The propagated equivalent of <varname>depsHostHost</varname>.
567 </para>
568 </listitem>
569 </varlistentry>
570 <varlistentry>
571 <term>
572 <varname>propagatedBuildInputs</varname>
573 </term>
574 <listitem>
575 <para>
576 The propagated equivalent of <varname>buildInputs</varname>. This would
577 be called <varname>depsHostTargetPropagated</varname> but for historical
578 continuity.
579 </para>
580 </listitem>
581 </varlistentry>
582 <varlistentry>
583 <term>
584 <varname>depsTargetTarget</varname>
585 </term>
586 <listitem>
587 <para>
588 The propagated equivalent of <varname>depsTargetTarget</varname>. This is
589 prefixed for the same reason of alerting potential users.
590 </para>
591 </listitem>
592 </varlistentry>
593 </variablelist>
594 </section>
595 <section xml:id="ssec-stdenv-attributes">
596 <title>Attributes</title>
597
598 <variablelist>
599 <title>Variables affecting <literal>stdenv</literal> initialisation</title>
600 <varlistentry>
601 <term>
602 <varname>NIX_DEBUG</varname>
603 </term>
604 <listitem>
605 <para>
606 A natural number indicating how much information to log. If set to 1 or
607 higher, <literal>stdenv</literal> will print moderate debug information
608 during the build. In particular, the <command>gcc</command> and
609 <command>ld</command> wrapper scripts will print out the complete command
610 line passed to the wrapped tools. If set to 6 or higher, the
611 <literal>stdenv</literal> setup script will be run with <literal>set
612 -x</literal> tracing. If set to 7 or higher, the <command>gcc</command>
613 and <command>ld</command> wrapper scripts will also be run with
614 <literal>set -x</literal> tracing.
615 </para>
616 </listitem>
617 </varlistentry>
618 </variablelist>
619
620 <variablelist>
621 <title>Variables affecting build properties</title>
622 <varlistentry>
623 <term>
624 <varname>enableParallelBuilding</varname>
625 </term>
626 <listitem>
627 <para>
628 If set to <literal>true</literal>, <literal>stdenv</literal> will pass
629 specific flags to <literal>make</literal> and other build tools to enable
630 parallel building with up to <literal>build-cores</literal> workers.
631 </para>
632 <para>
633 Unless set to <literal>false</literal>, some build systems with good
634 support for parallel building including <literal>cmake</literal>,
635 <literal>meson</literal>, and <literal>qmake</literal> will set it to
636 <literal>true</literal>.
637 </para>
638 </listitem>
639 </varlistentry>
640 <varlistentry>
641 <term>
642 <varname>preferLocalBuild</varname>
643 </term>
644 <listitem>
645 <para>
646 If set, specifies that the package is so lightweight in terms of build
647 operations (e.g. write a text file from a Nix string to the store) that
648 there's no need to look for it in binary caches -- it's faster to just
649 build it locally. It also tells Hydra and other facilities that this
650 package doesn't need to be exported in binary caches (noone would use it,
651 after all).
652 </para>
653 </listitem>
654 </varlistentry>
655 </variablelist>
656
657 <variablelist>
658 <title>Special variables</title>
659 <varlistentry>
660 <term>
661 <varname>passthru</varname>
662 </term>
663 <listitem>
664 <para>
665 This is an attribute set which can be filled with arbitrary values. For
666 example:
667<programlisting>
668passthru = {
669 foo = "bar";
670 baz = {
671 value1 = 4;
672 value2 = 5;
673 };
674}
675</programlisting>
676 </para>
677 <para>
678 Values inside it are not passed to the builder, so you can change them
679 without triggering a rebuild. However, they can be accessed outside of a
680 derivation directly, as if they were set inside a derivation itself, e.g.
681 <literal>hello.baz.value1</literal>. We don't specify any usage or schema
682 of <literal>passthru</literal> - it is meant for values that would be
683 useful outside the derivation in other parts of a Nix expression (e.g. in
684 other derivations). An example would be to convey some specific
685 dependency of your derivation which contains a program with plugins
686 support. Later, others who make derivations with plugins can use
687 passed-through dependency to ensure that their plugin would be
688 binary-compatible with built program.
689 </para>
690 </listitem>
691 </varlistentry>
692 </variablelist>
693 </section>
694 <section xml:id="sec-stdenv-phases">
695 <title>Phases</title>
696
697 <para>
698 The generic builder has a number of <emphasis>phases</emphasis>. Package
699 builds are split into phases to make it easier to override specific parts of
700 the build (e.g., unpacking the sources or installing the binaries).
701 Furthermore, it allows a nicer presentation of build logs in the Nix build
702 farm.
703 </para>
704
705 <para>
706 Each phase can be overridden in its entirety either by setting the
707 environment variable <varname><replaceable>name</replaceable>Phase</varname>
708 to a string containing some shell commands to be executed, or by redefining
709 the shell function <varname><replaceable>name</replaceable>Phase</varname>.
710 The former is convenient to override a phase from the derivation, while the
711 latter is convenient from a build script. However, typically one only wants
712 to <emphasis>add</emphasis> some commands to a phase, e.g. by defining
713 <literal>postInstall</literal> or <literal>preFixup</literal>, as skipping
714 some of the default actions may have unexpected consequences.
715 </para>
716
717 <section xml:id="ssec-controlling-phases">
718 <title>Controlling phases</title>
719
720 <para>
721 There are a number of variables that control what phases are executed and
722 in what order:
723 <variablelist>
724 <title>Variables affecting phase control</title>
725 <varlistentry>
726 <term>
727 <varname>phases</varname>
728 </term>
729 <listitem>
730 <para>
731 Specifies the phases. You can change the order in which phases are
732 executed, or add new phases, by setting this variable. If it’s not
733 set, the default value is used, which is <literal>$prePhases
734 unpackPhase patchPhase $preConfigurePhases configurePhase
735 $preBuildPhases buildPhase checkPhase $preInstallPhases installPhase
736 fixupPhase $preDistPhases distPhase $postPhases</literal>.
737 </para>
738 <para>
739 Usually, if you just want to add a few phases, it’s more convenient
740 to set one of the variables below (such as
741 <varname>preInstallPhases</varname>), as you then don’t specify all
742 the normal phases.
743 </para>
744 </listitem>
745 </varlistentry>
746 <varlistentry>
747 <term>
748 <varname>prePhases</varname>
749 </term>
750 <listitem>
751 <para>
752 Additional phases executed before any of the default phases.
753 </para>
754 </listitem>
755 </varlistentry>
756 <varlistentry>
757 <term>
758 <varname>preConfigurePhases</varname>
759 </term>
760 <listitem>
761 <para>
762 Additional phases executed just before the configure phase.
763 </para>
764 </listitem>
765 </varlistentry>
766 <varlistentry>
767 <term>
768 <varname>preBuildPhases</varname>
769 </term>
770 <listitem>
771 <para>
772 Additional phases executed just before the build phase.
773 </para>
774 </listitem>
775 </varlistentry>
776 <varlistentry>
777 <term>
778 <varname>preInstallPhases</varname>
779 </term>
780 <listitem>
781 <para>
782 Additional phases executed just before the install phase.
783 </para>
784 </listitem>
785 </varlistentry>
786 <varlistentry>
787 <term>
788 <varname>preFixupPhases</varname>
789 </term>
790 <listitem>
791 <para>
792 Additional phases executed just before the fixup phase.
793 </para>
794 </listitem>
795 </varlistentry>
796 <varlistentry>
797 <term>
798 <varname>preDistPhases</varname>
799 </term>
800 <listitem>
801 <para>
802 Additional phases executed just before the distribution phase.
803 </para>
804 </listitem>
805 </varlistentry>
806 <varlistentry>
807 <term>
808 <varname>postPhases</varname>
809 </term>
810 <listitem>
811 <para>
812 Additional phases executed after any of the default phases.
813 </para>
814 </listitem>
815 </varlistentry>
816 </variablelist>
817 </para>
818 </section>
819
820 <section xml:id="ssec-unpack-phase">
821 <title>The unpack phase</title>
822
823 <para>
824 The unpack phase is responsible for unpacking the source code of the
825 package. The default implementation of <function>unpackPhase</function>
826 unpacks the source files listed in the <envar>src</envar> environment
827 variable to the current directory. It supports the following files by
828 default:
829 <variablelist>
830 <varlistentry>
831 <term>
832 Tar files
833 </term>
834 <listitem>
835 <para>
836 These can optionally be compressed using <command>gzip</command>
837 (<filename>.tar.gz</filename>, <filename>.tgz</filename> or
838 <filename>.tar.Z</filename>), <command>bzip2</command>
839 (<filename>.tar.bz2</filename>, <filename>.tbz2</filename> or
840 <filename>.tbz</filename>) or <command>xz</command>
841 (<filename>.tar.xz</filename>, <filename>.tar.lzma</filename> or
842 <filename>.txz</filename>).
843 </para>
844 </listitem>
845 </varlistentry>
846 <varlistentry>
847 <term>
848 Zip files
849 </term>
850 <listitem>
851 <para>
852 Zip files are unpacked using <command>unzip</command>. However,
853 <command>unzip</command> is not in the standard environment, so you
854 should add it to <varname>buildInputs</varname> yourself.
855 </para>
856 </listitem>
857 </varlistentry>
858 <varlistentry>
859 <term>
860 Directories in the Nix store
861 </term>
862 <listitem>
863 <para>
864 These are simply copied to the current directory. The hash part of the
865 file name is stripped, e.g.
866 <filename>/nix/store/1wydxgby13cz...-my-sources</filename> would be
867 copied to <filename>my-sources</filename>.
868 </para>
869 </listitem>
870 </varlistentry>
871 </variablelist>
872 Additional file types can be supported by setting the
873 <varname>unpackCmd</varname> variable (see below).
874 </para>
875
876 <para></para>
877
878 <variablelist>
879 <title>Variables controlling the unpack phase</title>
880 <varlistentry>
881 <term>
882 <varname>srcs</varname> / <varname>src</varname>
883 </term>
884 <listitem>
885 <para>
886 The list of source files or directories to be unpacked or copied. One of
887 these must be set.
888 </para>
889 </listitem>
890 </varlistentry>
891 <varlistentry>
892 <term>
893 <varname>sourceRoot</varname>
894 </term>
895 <listitem>
896 <para>
897 After running <function>unpackPhase</function>, the generic builder
898 changes the current directory to the directory created by unpacking the
899 sources. If there are multiple source directories, you should set
900 <varname>sourceRoot</varname> to the name of the intended directory.
901 </para>
902 </listitem>
903 </varlistentry>
904 <varlistentry>
905 <term>
906 <varname>setSourceRoot</varname>
907 </term>
908 <listitem>
909 <para>
910 Alternatively to setting <varname>sourceRoot</varname>, you can set
911 <varname>setSourceRoot</varname> to a shell command to be evaluated by
912 the unpack phase after the sources have been unpacked. This command must
913 set <varname>sourceRoot</varname>.
914 </para>
915 </listitem>
916 </varlistentry>
917 <varlistentry>
918 <term>
919 <varname>preUnpack</varname>
920 </term>
921 <listitem>
922 <para>
923 Hook executed at the start of the unpack phase.
924 </para>
925 </listitem>
926 </varlistentry>
927 <varlistentry>
928 <term>
929 <varname>postUnpack</varname>
930 </term>
931 <listitem>
932 <para>
933 Hook executed at the end of the unpack phase.
934 </para>
935 </listitem>
936 </varlistentry>
937 <varlistentry>
938 <term>
939 <varname>dontMakeSourcesWritable</varname>
940 </term>
941 <listitem>
942 <para>
943 If set to <literal>1</literal>, the unpacked sources are
944 <emphasis>not</emphasis> made writable. By default, they are made
945 writable to prevent problems with read-only sources. For example, copied
946 store directories would be read-only without this.
947 </para>
948 </listitem>
949 </varlistentry>
950 <varlistentry>
951 <term>
952 <varname>unpackCmd</varname>
953 </term>
954 <listitem>
955 <para>
956 The unpack phase evaluates the string <literal>$unpackCmd</literal> for
957 any unrecognised file. The path to the current source file is contained
958 in the <varname>curSrc</varname> variable.
959 </para>
960 </listitem>
961 </varlistentry>
962 </variablelist>
963 </section>
964
965 <section xml:id="ssec-patch-phase">
966 <title>The patch phase</title>
967
968 <para>
969 The patch phase applies the list of patches defined in the
970 <varname>patches</varname> variable.
971 </para>
972
973 <variablelist>
974 <title>Variables controlling the patch phase</title>
975 <varlistentry>
976 <term>
977 <varname>patches</varname>
978 </term>
979 <listitem>
980 <para>
981 The list of patches. They must be in the format accepted by the
982 <command>patch</command> command, and may optionally be compressed using
983 <command>gzip</command> (<filename>.gz</filename>),
984 <command>bzip2</command> (<filename>.bz2</filename>) or
985 <command>xz</command> (<filename>.xz</filename>).
986 </para>
987 </listitem>
988 </varlistentry>
989 <varlistentry>
990 <term>
991 <varname>patchFlags</varname>
992 </term>
993 <listitem>
994 <para>
995 Flags to be passed to <command>patch</command>. If not set, the argument
996 <option>-p1</option> is used, which causes the leading directory
997 component to be stripped from the file names in each patch.
998 </para>
999 </listitem>
1000 </varlistentry>
1001 <varlistentry>
1002 <term>
1003 <varname>prePatch</varname>
1004 </term>
1005 <listitem>
1006 <para>
1007 Hook executed at the start of the patch phase.
1008 </para>
1009 </listitem>
1010 </varlistentry>
1011 <varlistentry>
1012 <term>
1013 <varname>postPatch</varname>
1014 </term>
1015 <listitem>
1016 <para>
1017 Hook executed at the end of the patch phase.
1018 </para>
1019 </listitem>
1020 </varlistentry>
1021 </variablelist>
1022 </section>
1023
1024 <section xml:id="ssec-configure-phase">
1025 <title>The configure phase</title>
1026
1027 <para>
1028 The configure phase prepares the source tree for building. The default
1029 <function>configurePhase</function> runs <filename>./configure</filename>
1030 (typically an Autoconf-generated script) if it exists.
1031 </para>
1032
1033 <variablelist>
1034 <title>Variables controlling the configure phase</title>
1035 <varlistentry>
1036 <term>
1037 <varname>configureScript</varname>
1038 </term>
1039 <listitem>
1040 <para>
1041 The name of the configure script. It defaults to
1042 <filename>./configure</filename> if it exists; otherwise, the configure
1043 phase is skipped. This can actually be a command (like <literal>perl
1044 ./Configure.pl</literal>).
1045 </para>
1046 </listitem>
1047 </varlistentry>
1048 <varlistentry>
1049 <term>
1050 <varname>configureFlags</varname>
1051 </term>
1052 <listitem>
1053 <para>
1054 A list of strings passed as additional arguments to the configure
1055 script.
1056 </para>
1057 </listitem>
1058 </varlistentry>
1059 <varlistentry>
1060 <term>
1061 <varname>configureFlagsArray</varname>
1062 </term>
1063 <listitem>
1064 <para>
1065 A shell array containing additional arguments passed to the configure
1066 script. You must use this instead of <varname>configureFlags</varname>
1067 if the arguments contain spaces.
1068 </para>
1069 </listitem>
1070 </varlistentry>
1071 <varlistentry>
1072 <term>
1073 <varname>dontAddPrefix</varname>
1074 </term>
1075 <listitem>
1076 <para>
1077 By default, the flag <literal>--prefix=$prefix</literal> is added to the
1078 configure flags. If this is undesirable, set this variable to true.
1079 </para>
1080 </listitem>
1081 </varlistentry>
1082 <varlistentry>
1083 <term>
1084 <varname>prefix</varname>
1085 </term>
1086 <listitem>
1087 <para>
1088 The prefix under which the package must be installed, passed via the
1089 <option>--prefix</option> option to the configure script. It defaults to
1090 <option>$out</option>.
1091 </para>
1092 </listitem>
1093 </varlistentry>
1094 <varlistentry>
1095 <term>
1096 <varname>dontAddDisableDepTrack</varname>
1097 </term>
1098 <listitem>
1099 <para>
1100 By default, the flag <literal>--disable-dependency-tracking</literal> is
1101 added to the configure flags to speed up Automake-based builds. If this
1102 is undesirable, set this variable to true.
1103 </para>
1104 </listitem>
1105 </varlistentry>
1106 <varlistentry>
1107 <term>
1108 <varname>dontFixLibtool</varname>
1109 </term>
1110 <listitem>
1111 <para>
1112 By default, the configure phase applies some special hackery to all
1113 files called <filename>ltmain.sh</filename> before running the configure
1114 script in order to improve the purity of Libtool-based packages
1115 <footnote xml:id="footnote-stdenv-sys-lib-search-path">
1116 <para>
1117 It clears the
1118 <varname>sys_lib_<replaceable>*</replaceable>search_path</varname>
1119 variables in the Libtool script to prevent Libtool from using
1120 libraries in <filename>/usr/lib</filename> and such.
1121 </para>
1122 </footnote>
1123 . If this is undesirable, set this variable to true.
1124 </para>
1125 </listitem>
1126 </varlistentry>
1127 <varlistentry>
1128 <term>
1129 <varname>dontDisableStatic</varname>
1130 </term>
1131 <listitem>
1132 <para>
1133 By default, when the configure script has
1134 <option>--enable-static</option>, the option
1135 <option>--disable-static</option> is added to the configure flags.
1136 </para>
1137 <para>
1138 If this is undesirable, set this variable to true.
1139 </para>
1140 </listitem>
1141 </varlistentry>
1142 <varlistentry>
1143 <term>
1144 <varname>configurePlatforms</varname>
1145 </term>
1146 <listitem>
1147 <para>
1148 By default, when cross compiling, the configure script has
1149 <option>--build=...</option> and <option>--host=...</option> passed.
1150 Packages can instead pass <literal>[ "build" "host" "target" ]</literal>
1151 or a subset to control exactly which platform flags are passed.
1152 Compilers and other tools should use this to also pass the target
1153 platform, for example.
1154 <footnote xml:id="footnote-stdenv-build-time-guessing-impurity">
1155 <para>
1156 Eventually these will be passed when in native builds too, to improve
1157 determinism: build-time guessing, as is done today, is a risk of
1158 impurity.
1159 </para>
1160 </footnote>
1161 </para>
1162 </listitem>
1163 </varlistentry>
1164 <varlistentry>
1165 <term>
1166 <varname>preConfigure</varname>
1167 </term>
1168 <listitem>
1169 <para>
1170 Hook executed at the start of the configure phase.
1171 </para>
1172 </listitem>
1173 </varlistentry>
1174 <varlistentry>
1175 <term>
1176 <varname>postConfigure</varname>
1177 </term>
1178 <listitem>
1179 <para>
1180 Hook executed at the end of the configure phase.
1181 </para>
1182 </listitem>
1183 </varlistentry>
1184 </variablelist>
1185 </section>
1186
1187 <section xml:id="build-phase">
1188 <title>The build phase</title>
1189
1190 <para>
1191 The build phase is responsible for actually building the package (e.g.
1192 compiling it). The default <function>buildPhase</function> simply calls
1193 <command>make</command> if a file named <filename>Makefile</filename>,
1194 <filename>makefile</filename> or <filename>GNUmakefile</filename> exists in
1195 the current directory (or the <varname>makefile</varname> is explicitly
1196 set); otherwise it does nothing.
1197 </para>
1198
1199 <variablelist>
1200 <title>Variables controlling the build phase</title>
1201 <varlistentry>
1202 <term>
1203 <varname>dontBuild</varname>
1204 </term>
1205 <listitem>
1206 <para>
1207 Set to true to skip the build phase.
1208 </para>
1209 </listitem>
1210 </varlistentry>
1211 <varlistentry>
1212 <term>
1213 <varname>makefile</varname>
1214 </term>
1215 <listitem>
1216 <para>
1217 The file name of the Makefile.
1218 </para>
1219 </listitem>
1220 </varlistentry>
1221 <varlistentry>
1222 <term>
1223 <varname>checkInputs</varname>
1224 </term>
1225 <listitem>
1226 <para>
1227 A list of dependencies used by the phase. This gets included in
1228 <varname>buildInputs</varname> when <varname>doCheck</varname> is set.
1229 </para>
1230 </listitem>
1231 </varlistentry>
1232 <varlistentry>
1233 <term>
1234 <varname>makeFlags</varname>
1235 </term>
1236 <listitem>
1237 <para>
1238 A list of strings passed as additional flags to <command>make</command>.
1239 These flags are also used by the default install and check phase. For
1240 setting make flags specific to the build phase, use
1241 <varname>buildFlags</varname> (see below).
1242<programlisting>
1243makeFlags = [ "PREFIX=$(out)" ];
1244</programlisting>
1245 <note>
1246 <para>
1247 The flags are quoted in bash, but environment variables can be
1248 specified by using the make syntax.
1249 </para>
1250 </note>
1251 </para>
1252 </listitem>
1253 </varlistentry>
1254 <varlistentry>
1255 <term>
1256 <varname>makeFlagsArray</varname>
1257 </term>
1258 <listitem>
1259 <para>
1260 A shell array containing additional arguments passed to
1261 <command>make</command>. You must use this instead of
1262 <varname>makeFlags</varname> if the arguments contain spaces, e.g.
1263<programlisting>
1264makeFlagsArray=(CFLAGS="-O0 -g" LDFLAGS="-lfoo -lbar")
1265</programlisting>
1266 Note that shell arrays cannot be passed through environment variables,
1267 so you cannot set <varname>makeFlagsArray</varname> in a derivation
1268 attribute (because those are passed through environment variables): you
1269 have to define them in shell code.
1270 </para>
1271 </listitem>
1272 </varlistentry>
1273 <varlistentry>
1274 <term>
1275 <varname>buildFlags</varname> / <varname>buildFlagsArray</varname>
1276 </term>
1277 <listitem>
1278 <para>
1279 A list of strings passed as additional flags to <command>make</command>.
1280 Like <varname>makeFlags</varname> and <varname>makeFlagsArray</varname>,
1281 but only used by the build phase.
1282 </para>
1283 </listitem>
1284 </varlistentry>
1285 <varlistentry>
1286 <term>
1287 <varname>preBuild</varname>
1288 </term>
1289 <listitem>
1290 <para>
1291 Hook executed at the start of the build phase.
1292 </para>
1293 </listitem>
1294 </varlistentry>
1295 <varlistentry>
1296 <term>
1297 <varname>postBuild</varname>
1298 </term>
1299 <listitem>
1300 <para>
1301 Hook executed at the end of the build phase.
1302 </para>
1303 </listitem>
1304 </varlistentry>
1305 </variablelist>
1306
1307 <para>
1308 You can set flags for <command>make</command> through the
1309 <varname>makeFlags</varname> variable.
1310 </para>
1311
1312 <para>
1313 Before and after running <command>make</command>, the hooks
1314 <varname>preBuild</varname> and <varname>postBuild</varname> are called,
1315 respectively.
1316 </para>
1317 </section>
1318
1319 <section xml:id="ssec-check-phase">
1320 <title>The check phase</title>
1321
1322 <para>
1323 The check phase checks whether the package was built correctly by running
1324 its test suite. The default <function>checkPhase</function> calls
1325 <command>make check</command>, but only if the <varname>doCheck</varname>
1326 variable is enabled.
1327 </para>
1328
1329 <variablelist>
1330 <title>Variables controlling the check phase</title>
1331 <varlistentry>
1332 <term>
1333 <varname>doCheck</varname>
1334 </term>
1335 <listitem>
1336 <para>
1337 Controls whether the check phase is executed. By default it is skipped,
1338 but if <varname>doCheck</varname> is set to true, the check phase is
1339 usually executed. Thus you should set
1340<programlisting>doCheck = true;</programlisting>
1341 in the derivation to enable checks. The exception is cross compilation.
1342 Cross compiled builds never run tests, no matter how
1343 <varname>doCheck</varname> is set, as the newly-built program won't run
1344 on the platform used to build it.
1345 </para>
1346 </listitem>
1347 </varlistentry>
1348 <varlistentry>
1349 <term>
1350 <varname>makeFlags</varname> / <varname>makeFlagsArray</varname> / <varname>makefile</varname>
1351 </term>
1352 <listitem>
1353 <para>
1354 See the build phase for details.
1355 </para>
1356 </listitem>
1357 </varlistentry>
1358 <varlistentry>
1359 <term>
1360 <varname>checkTarget</varname>
1361 </term>
1362 <listitem>
1363 <para>
1364 The make target that runs the tests. Defaults to
1365 <literal>check</literal>.
1366 </para>
1367 </listitem>
1368 </varlistentry>
1369 <varlistentry>
1370 <term>
1371 <varname>checkFlags</varname> / <varname>checkFlagsArray</varname>
1372 </term>
1373 <listitem>
1374 <para>
1375 A list of strings passed as additional flags to <command>make</command>.
1376 Like <varname>makeFlags</varname> and <varname>makeFlagsArray</varname>,
1377 but only used by the check phase.
1378 </para>
1379 </listitem>
1380 </varlistentry>
1381 <varlistentry>
1382 <term>
1383 <varname>preCheck</varname>
1384 </term>
1385 <listitem>
1386 <para>
1387 Hook executed at the start of the check phase.
1388 </para>
1389 </listitem>
1390 </varlistentry>
1391 <varlistentry>
1392 <term>
1393 <varname>postCheck</varname>
1394 </term>
1395 <listitem>
1396 <para>
1397 Hook executed at the end of the check phase.
1398 </para>
1399 </listitem>
1400 </varlistentry>
1401 </variablelist>
1402 </section>
1403
1404 <section xml:id="ssec-install-phase">
1405 <title>The install phase</title>
1406
1407 <para>
1408 The install phase is responsible for installing the package in the Nix
1409 store under <envar>out</envar>. The default
1410 <function>installPhase</function> creates the directory
1411 <literal>$out</literal> and calls <command>make install</command>.
1412 </para>
1413
1414 <variablelist>
1415 <title>Variables controlling the install phase</title>
1416 <varlistentry>
1417 <term>
1418 <varname>makeFlags</varname> / <varname>makeFlagsArray</varname> / <varname>makefile</varname>
1419 </term>
1420 <listitem>
1421 <para>
1422 See the build phase for details.
1423 </para>
1424 </listitem>
1425 </varlistentry>
1426 <varlistentry>
1427 <term>
1428 <varname>installTargets</varname>
1429 </term>
1430 <listitem>
1431 <para>
1432 The make targets that perform the installation. Defaults to
1433 <literal>install</literal>. Example:
1434<programlisting>
1435installTargets = "install-bin install-doc";</programlisting>
1436 </para>
1437 </listitem>
1438 </varlistentry>
1439 <varlistentry>
1440 <term>
1441 <varname>installFlags</varname> / <varname>installFlagsArray</varname>
1442 </term>
1443 <listitem>
1444 <para>
1445 A list of strings passed as additional flags to <command>make</command>.
1446 Like <varname>makeFlags</varname> and <varname>makeFlagsArray</varname>,
1447 but only used by the install phase.
1448 </para>
1449 </listitem>
1450 </varlistentry>
1451 <varlistentry>
1452 <term>
1453 <varname>preInstall</varname>
1454 </term>
1455 <listitem>
1456 <para>
1457 Hook executed at the start of the install phase.
1458 </para>
1459 </listitem>
1460 </varlistentry>
1461 <varlistentry>
1462 <term>
1463 <varname>postInstall</varname>
1464 </term>
1465 <listitem>
1466 <para>
1467 Hook executed at the end of the install phase.
1468 </para>
1469 </listitem>
1470 </varlistentry>
1471 </variablelist>
1472 </section>
1473
1474 <section xml:id="ssec-fixup-phase">
1475 <title>The fixup phase</title>
1476
1477 <para>
1478 The fixup phase performs some (Nix-specific) post-processing actions on the
1479 files installed under <filename>$out</filename> by the install phase. The
1480 default <function>fixupPhase</function> does the following:
1481 <itemizedlist>
1482 <listitem>
1483 <para>
1484 It moves the <filename>man/</filename>, <filename>doc/</filename> and
1485 <filename>info/</filename> subdirectories of <envar>$out</envar> to
1486 <filename>share/</filename>.
1487 </para>
1488 </listitem>
1489 <listitem>
1490 <para>
1491 It strips libraries and executables of debug information.
1492 </para>
1493 </listitem>
1494 <listitem>
1495 <para>
1496 On Linux, it applies the <command>patchelf</command> command to ELF
1497 executables and libraries to remove unused directories from the
1498 <literal>RPATH</literal> in order to prevent unnecessary runtime
1499 dependencies.
1500 </para>
1501 </listitem>
1502 <listitem>
1503 <para>
1504 It rewrites the interpreter paths of shell scripts to paths found in
1505 <envar>PATH</envar>. E.g., <filename>/usr/bin/perl</filename> will be
1506 rewritten to
1507 <filename>/nix/store/<replaceable>some-perl</replaceable>/bin/perl</filename>
1508 found in <envar>PATH</envar>.
1509 </para>
1510 </listitem>
1511 </itemizedlist>
1512 </para>
1513
1514 <variablelist>
1515 <title>Variables controlling the fixup phase</title>
1516 <varlistentry>
1517 <term>
1518 <varname>dontStrip</varname>
1519 </term>
1520 <listitem>
1521 <para>
1522 If set, libraries and executables are not stripped. By default, they
1523 are.
1524 </para>
1525 </listitem>
1526 </varlistentry>
1527 <varlistentry>
1528 <term>
1529 <varname>dontStripHost</varname>
1530 </term>
1531 <listitem>
1532 <para>
1533 Like <varname>dontStripHost</varname>, but only affects the
1534 <command>strip</command> command targetting the package's host platform.
1535 Useful when supporting cross compilation, but otherwise feel free to
1536 ignore.
1537 </para>
1538 </listitem>
1539 </varlistentry>
1540 <varlistentry>
1541 <term>
1542 <varname>dontStripTarget</varname>
1543 </term>
1544 <listitem>
1545 <para>
1546 Like <varname>dontStripHost</varname>, but only affects the
1547 <command>strip</command> command targetting the packages' target
1548 platform. Useful when supporting cross compilation, but otherwise feel
1549 free to ignore.
1550 </para>
1551 </listitem>
1552 </varlistentry>
1553 <varlistentry>
1554 <term>
1555 <varname>dontMoveSbin</varname>
1556 </term>
1557 <listitem>
1558 <para>
1559 If set, files in <filename>$out/sbin</filename> are not moved to
1560 <filename>$out/bin</filename>. By default, they are.
1561 </para>
1562 </listitem>
1563 </varlistentry>
1564 <varlistentry>
1565 <term>
1566 <varname>stripAllList</varname>
1567 </term>
1568 <listitem>
1569 <para>
1570 List of directories to search for libraries and executables from which
1571 <emphasis>all</emphasis> symbols should be stripped. By default, it’s
1572 empty. Stripping all symbols is risky, since it may remove not just
1573 debug symbols but also ELF information necessary for normal execution.
1574 </para>
1575 </listitem>
1576 </varlistentry>
1577 <varlistentry>
1578 <term>
1579 <varname>stripAllFlags</varname>
1580 </term>
1581 <listitem>
1582 <para>
1583 Flags passed to the <command>strip</command> command applied to the
1584 files in the directories listed in <varname>stripAllList</varname>.
1585 Defaults to <option>-s</option> (i.e. <option>--strip-all</option>).
1586 </para>
1587 </listitem>
1588 </varlistentry>
1589 <varlistentry>
1590 <term>
1591 <varname>stripDebugList</varname>
1592 </term>
1593 <listitem>
1594 <para>
1595 List of directories to search for libraries and executables from which
1596 only debugging-related symbols should be stripped. It defaults to
1597 <literal>lib bin sbin</literal>.
1598 </para>
1599 </listitem>
1600 </varlistentry>
1601 <varlistentry>
1602 <term>
1603 <varname>stripDebugFlags</varname>
1604 </term>
1605 <listitem>
1606 <para>
1607 Flags passed to the <command>strip</command> command applied to the
1608 files in the directories listed in <varname>stripDebugList</varname>.
1609 Defaults to <option>-S</option> (i.e. <option>--strip-debug</option>).
1610 </para>
1611 </listitem>
1612 </varlistentry>
1613 <varlistentry>
1614 <term>
1615 <varname>dontPatchELF</varname>
1616 </term>
1617 <listitem>
1618 <para>
1619 If set, the <command>patchelf</command> command is not used to remove
1620 unnecessary <literal>RPATH</literal> entries. Only applies to Linux.
1621 </para>
1622 </listitem>
1623 </varlistentry>
1624 <varlistentry>
1625 <term>
1626 <varname>dontPatchShebangs</varname>
1627 </term>
1628 <listitem>
1629 <para>
1630 If set, scripts starting with <literal>#!</literal> do not have their
1631 interpreter paths rewritten to paths in the Nix store.
1632 </para>
1633 </listitem>
1634 </varlistentry>
1635 <varlistentry>
1636 <term>
1637 <varname>forceShare</varname>
1638 </term>
1639 <listitem>
1640 <para>
1641 The list of directories that must be moved from
1642 <filename>$out</filename> to <filename>$out/share</filename>. Defaults
1643 to <literal>man doc info</literal>.
1644 </para>
1645 </listitem>
1646 </varlistentry>
1647 <varlistentry>
1648 <term>
1649 <varname>setupHook</varname>
1650 </term>
1651 <listitem>
1652 <para>
1653 A package can export a <link
1654 linkend="ssec-setup-hooks">setup
1655 hook</link> by setting this variable. The setup hook, if defined, is
1656 copied to <filename>$out/nix-support/setup-hook</filename>. Environment
1657 variables are then substituted in it using
1658 <function
1659 linkend="fun-substituteAll">substituteAll</function>.
1660 </para>
1661 </listitem>
1662 </varlistentry>
1663 <varlistentry>
1664 <term>
1665 <varname>preFixup</varname>
1666 </term>
1667 <listitem>
1668 <para>
1669 Hook executed at the start of the fixup phase.
1670 </para>
1671 </listitem>
1672 </varlistentry>
1673 <varlistentry>
1674 <term>
1675 <varname>postFixup</varname>
1676 </term>
1677 <listitem>
1678 <para>
1679 Hook executed at the end of the fixup phase.
1680 </para>
1681 </listitem>
1682 </varlistentry>
1683 <varlistentry xml:id="stdenv-separateDebugInfo">
1684 <term>
1685 <varname>separateDebugInfo</varname>
1686 </term>
1687 <listitem>
1688 <para>
1689 If set to <literal>true</literal>, the standard environment will enable
1690 debug information in C/C++ builds. After installation, the debug
1691 information will be separated from the executables and stored in the
1692 output named <literal>debug</literal>. (This output is enabled
1693 automatically; you don’t need to set the <varname>outputs</varname>
1694 attribute explicitly.) To be precise, the debug information is stored in
1695 <filename><replaceable>debug</replaceable>/lib/debug/.build-id/<replaceable>XX</replaceable>/<replaceable>YYYY…</replaceable></filename>,
1696 where <replaceable>XXYYYY…</replaceable> is the <replaceable>build
1697 ID</replaceable> of the binary — a SHA-1 hash of the contents of the
1698 binary. Debuggers like GDB use the build ID to look up the separated
1699 debug information.
1700 </para>
1701 <para>
1702 For example, with GDB, you can add
1703<programlisting>
1704set debug-file-directory ~/.nix-profile/lib/debug
1705</programlisting>
1706 to <filename>~/.gdbinit</filename>. GDB will then be able to find debug
1707 information installed via <literal>nix-env -i</literal>.
1708 </para>
1709 </listitem>
1710 </varlistentry>
1711 </variablelist>
1712 </section>
1713
1714 <section xml:id="ssec-installCheck-phase">
1715 <title>The installCheck phase</title>
1716
1717 <para>
1718 The installCheck phase checks whether the package was installed correctly
1719 by running its test suite against the installed directories. The default
1720 <function>installCheck</function> calls <command>make
1721 installcheck</command>.
1722 </para>
1723
1724 <variablelist>
1725 <title>Variables controlling the installCheck phase</title>
1726 <varlistentry>
1727 <term>
1728 <varname>doInstallCheck</varname>
1729 </term>
1730 <listitem>
1731 <para>
1732 Controls whether the installCheck phase is executed. By default it is
1733 skipped, but if <varname>doInstallCheck</varname> is set to true, the
1734 installCheck phase is usually executed. Thus you should set
1735<programlisting>doInstallCheck = true;</programlisting>
1736 in the derivation to enable install checks. The exception is cross
1737 compilation. Cross compiled builds never run tests, no matter how
1738 <varname>doInstallCheck</varname> is set, as the newly-built program
1739 won't run on the platform used to build it.
1740 </para>
1741 </listitem>
1742 </varlistentry>
1743 <varlistentry>
1744 <term>
1745 <varname>installCheckTarget</varname>
1746 </term>
1747 <listitem>
1748 <para>
1749 The make target that runs the install tests. Defaults to
1750 <literal>installcheck</literal>.
1751 </para>
1752 </listitem>
1753 </varlistentry>
1754 <varlistentry>
1755 <term>
1756 <varname>installCheckFlags</varname> / <varname>installCheckFlagsArray</varname>
1757 </term>
1758 <listitem>
1759 <para>
1760 A list of strings passed as additional flags to <command>make</command>.
1761 Like <varname>makeFlags</varname> and <varname>makeFlagsArray</varname>,
1762 but only used by the installCheck phase.
1763 </para>
1764 </listitem>
1765 </varlistentry>
1766 <varlistentry>
1767 <term>
1768 <varname>installCheckInputs</varname>
1769 </term>
1770 <listitem>
1771 <para>
1772 A list of dependencies used by the phase. This gets included in
1773 <varname>buildInputs</varname> when <varname>doInstallCheck</varname> is
1774 set.
1775 </para>
1776 </listitem>
1777 </varlistentry>
1778 <varlistentry>
1779 <term>
1780 <varname>preInstallCheck</varname>
1781 </term>
1782 <listitem>
1783 <para>
1784 Hook executed at the start of the installCheck phase.
1785 </para>
1786 </listitem>
1787 </varlistentry>
1788 <varlistentry>
1789 <term>
1790 <varname>postInstallCheck</varname>
1791 </term>
1792 <listitem>
1793 <para>
1794 Hook executed at the end of the installCheck phase.
1795 </para>
1796 </listitem>
1797 </varlistentry>
1798 </variablelist>
1799 </section>
1800
1801 <section xml:id="ssec-distribution-phase">
1802 <title>The distribution phase</title>
1803
1804 <para>
1805 The distribution phase is intended to produce a source distribution of the
1806 package. The default <function>distPhase</function> first calls
1807 <command>make dist</command>, then it copies the resulting source tarballs
1808 to <filename>$out/tarballs/</filename>. This phase is only executed if the
1809 attribute <varname>doDist</varname> is set.
1810 </para>
1811
1812 <variablelist>
1813 <title>Variables controlling the distribution phase</title>
1814 <varlistentry>
1815 <term>
1816 <varname>distTarget</varname>
1817 </term>
1818 <listitem>
1819 <para>
1820 The make target that produces the distribution. Defaults to
1821 <literal>dist</literal>.
1822 </para>
1823 </listitem>
1824 </varlistentry>
1825 <varlistentry>
1826 <term>
1827 <varname>distFlags</varname> / <varname>distFlagsArray</varname>
1828 </term>
1829 <listitem>
1830 <para>
1831 Additional flags passed to <command>make</command>.
1832 </para>
1833 </listitem>
1834 </varlistentry>
1835 <varlistentry>
1836 <term>
1837 <varname>tarballs</varname>
1838 </term>
1839 <listitem>
1840 <para>
1841 The names of the source distribution files to be copied to
1842 <filename>$out/tarballs/</filename>. It can contain shell wildcards. The
1843 default is <filename>*.tar.gz</filename>.
1844 </para>
1845 </listitem>
1846 </varlistentry>
1847 <varlistentry>
1848 <term>
1849 <varname>dontCopyDist</varname>
1850 </term>
1851 <listitem>
1852 <para>
1853 If set, no files are copied to <filename>$out/tarballs/</filename>.
1854 </para>
1855 </listitem>
1856 </varlistentry>
1857 <varlistentry>
1858 <term>
1859 <varname>preDist</varname>
1860 </term>
1861 <listitem>
1862 <para>
1863 Hook executed at the start of the distribution phase.
1864 </para>
1865 </listitem>
1866 </varlistentry>
1867 <varlistentry>
1868 <term>
1869 <varname>postDist</varname>
1870 </term>
1871 <listitem>
1872 <para>
1873 Hook executed at the end of the distribution phase.
1874 </para>
1875 </listitem>
1876 </varlistentry>
1877 </variablelist>
1878 </section>
1879 </section>
1880 <section xml:id="ssec-stdenv-functions">
1881 <title>Shell functions</title>
1882
1883 <para>
1884 The standard environment provides a number of useful functions.
1885 </para>
1886
1887 <variablelist>
1888 <varlistentry xml:id='fun-makeWrapper'>
1889 <term>
1890 <function>makeWrapper</function> <replaceable>executable</replaceable> <replaceable>wrapperfile</replaceable> <replaceable>args</replaceable>
1891 </term>
1892 <listitem>
1893 <para>
1894 Constructs a wrapper for a program with various possible arguments. For
1895 example:
1896<programlisting>
1897# adds `FOOBAR=baz` to `$out/bin/foo`’s environment
1898makeWrapper $out/bin/foo $wrapperfile --set FOOBAR baz
1899
1900# prefixes the binary paths of `hello` and `git`
1901# Be advised that paths often should be patched in directly
1902# (via string replacements or in `configurePhase`).
1903makeWrapper $out/bin/foo $wrapperfile --prefix PATH : ${lib.makeBinPath [ hello git ]}
1904</programlisting>
1905 There’s many more kinds of arguments, they are documented in
1906 <literal>nixpkgs/pkgs/build-support/setup-hooks/make-wrapper.sh</literal>.
1907 </para>
1908 <para>
1909 <literal>wrapProgram</literal> is a convenience function you probably
1910 want to use most of the time.
1911 </para>
1912 </listitem>
1913 </varlistentry>
1914 <varlistentry xml:id='fun-substitute'>
1915 <term>
1916 <function>substitute</function> <replaceable>infile</replaceable> <replaceable>outfile</replaceable> <replaceable>subs</replaceable>
1917 </term>
1918 <listitem>
1919 <para>
1920 Performs string substitution on the contents of
1921 <replaceable>infile</replaceable>, writing the result to
1922 <replaceable>outfile</replaceable>. The substitutions in
1923 <replaceable>subs</replaceable> are of the following form:
1924 <variablelist>
1925 <varlistentry>
1926 <term>
1927 <option>--replace</option> <replaceable>s1</replaceable> <replaceable>s2</replaceable>
1928 </term>
1929 <listitem>
1930 <para>
1931 Replace every occurrence of the string <replaceable>s1</replaceable>
1932 by <replaceable>s2</replaceable>.
1933 </para>
1934 </listitem>
1935 </varlistentry>
1936 <varlistentry>
1937 <term>
1938 <option>--subst-var</option> <replaceable>varName</replaceable>
1939 </term>
1940 <listitem>
1941 <para>
1942 Replace every occurrence of
1943 <literal>@<replaceable>varName</replaceable>@</literal> by the
1944 contents of the environment variable
1945 <replaceable>varName</replaceable>. This is useful for generating
1946 files from templates, using
1947 <literal>@<replaceable>...</replaceable>@</literal> in the template
1948 as placeholders.
1949 </para>
1950 </listitem>
1951 </varlistentry>
1952 <varlistentry>
1953 <term>
1954 <option>--subst-var-by</option> <replaceable>varName</replaceable> <replaceable>s</replaceable>
1955 </term>
1956 <listitem>
1957 <para>
1958 Replace every occurrence of
1959 <literal>@<replaceable>varName</replaceable>@</literal> by the string
1960 <replaceable>s</replaceable>.
1961 </para>
1962 </listitem>
1963 </varlistentry>
1964 </variablelist>
1965 </para>
1966 <para>
1967 Example:
1968<programlisting>
1969substitute ./foo.in ./foo.out \
1970 --replace /usr/bin/bar $bar/bin/bar \
1971 --replace "a string containing spaces" "some other text" \
1972 --subst-var someVar
1973</programlisting>
1974 </para>
1975 <para>
1976 <function>substitute</function> is implemented using the
1977 <command
1978 xlink:href="http://replace.richardlloyd.org.uk/">replace</command>
1979 command. Unlike with the <command>sed</command> command, you don’t have
1980 to worry about escaping special characters. It supports performing
1981 substitutions on binary files (such as executables), though there
1982 you’ll probably want to make sure that the replacement string is as
1983 long as the replaced string.
1984 </para>
1985 </listitem>
1986 </varlistentry>
1987 <varlistentry xml:id='fun-substituteInPlace'>
1988 <term>
1989 <function>substituteInPlace</function> <replaceable>file</replaceable> <replaceable>subs</replaceable>
1990 </term>
1991 <listitem>
1992 <para>
1993 Like <function>substitute</function>, but performs the substitutions in
1994 place on the file <replaceable>file</replaceable>.
1995 </para>
1996 </listitem>
1997 </varlistentry>
1998 <varlistentry xml:id='fun-substituteAll'>
1999 <term>
2000 <function>substituteAll</function> <replaceable>infile</replaceable> <replaceable>outfile</replaceable>
2001 </term>
2002 <listitem>
2003 <para>
2004 Replaces every occurrence of
2005 <literal>@<replaceable>varName</replaceable>@</literal>, where
2006 <replaceable>varName</replaceable> is any environment variable, in
2007 <replaceable>infile</replaceable>, writing the result to
2008 <replaceable>outfile</replaceable>. For instance, if
2009 <replaceable>infile</replaceable> has the contents
2010<programlisting>
2011#! @bash@/bin/sh
2012PATH=@coreutils@/bin
2013echo @foo@
2014</programlisting>
2015 and the environment contains
2016 <literal>bash=/nix/store/bmwp0q28cf21...-bash-3.2-p39</literal> and
2017 <literal>coreutils=/nix/store/68afga4khv0w...-coreutils-6.12</literal>,
2018 but does not contain the variable <varname>foo</varname>, then the output
2019 will be
2020<programlisting>
2021#! /nix/store/bmwp0q28cf21...-bash-3.2-p39/bin/sh
2022PATH=/nix/store/68afga4khv0w...-coreutils-6.12/bin
2023echo @foo@
2024</programlisting>
2025 That is, no substitution is performed for undefined variables.
2026 </para>
2027 <para>
2028 Environment variables that start with an uppercase letter or an
2029 underscore are filtered out, to prevent global variables (like
2030 <literal>HOME</literal>) or private variables (like
2031 <literal>__ETC_PROFILE_DONE</literal>) from accidentally getting
2032 substituted. The variables also have to be valid bash “names”, as
2033 defined in the bash manpage (alphanumeric or <literal>_</literal>, must
2034 not start with a number).
2035 </para>
2036 </listitem>
2037 </varlistentry>
2038 <varlistentry xml:id='fun-substituteAllInPlace'>
2039 <term>
2040 <function>substituteAllInPlace</function> <replaceable>file</replaceable>
2041 </term>
2042 <listitem>
2043 <para>
2044 Like <function>substituteAll</function>, but performs the substitutions
2045 in place on the file <replaceable>file</replaceable>.
2046 </para>
2047 </listitem>
2048 </varlistentry>
2049 <varlistentry xml:id='fun-stripHash'>
2050 <term>
2051 <function>stripHash</function> <replaceable>path</replaceable>
2052 </term>
2053 <listitem>
2054 <para>
2055 Strips the directory and hash part of a store path, outputting the name
2056 part to <literal>stdout</literal>. For example:
2057<programlisting>
2058# prints coreutils-8.24
2059stripHash "/nix/store/9s9r019176g7cvn2nvcw41gsp862y6b4-coreutils-8.24"
2060</programlisting>
2061 If you wish to store the result in another variable, then the following
2062 idiom may be useful:
2063<programlisting>
2064name="/nix/store/9s9r019176g7cvn2nvcw41gsp862y6b4-coreutils-8.24"
2065someVar=$(stripHash $name)
2066</programlisting>
2067 </para>
2068 </listitem>
2069 </varlistentry>
2070 <varlistentry xml:id='fun-wrapProgram'>
2071 <term>
2072 <function>wrapProgram</function> <replaceable>executable</replaceable> <replaceable>makeWrapperArgs</replaceable>
2073 </term>
2074 <listitem>
2075 <para>
2076 Convenience function for <literal>makeWrapper</literal> that
2077 automatically creates a sane wrapper file It takes all the same arguments
2078 as <literal>makeWrapper</literal>, except for <literal>--argv0</literal>.
2079 </para>
2080 <para>
2081 It cannot be applied multiple times, since it will overwrite the wrapper
2082 file.
2083 </para>
2084 </listitem>
2085 </varlistentry>
2086 </variablelist>
2087 </section>
2088 <section xml:id="ssec-setup-hooks">
2089 <title>Package setup hooks</title>
2090
2091 <para>
2092 Nix itself considers a build-time dependency merely something that should
2093 previously be built and accessible at build time—packages themselves are
2094 on their own to perform any additional setup. In most cases, that is fine,
2095 and the downstream derivation can deal with it's own dependencies. But for a
2096 few common tasks, that would result in almost every package doing the same
2097 sort of setup work---depending not on the package itself, but entirely on
2098 which dependencies were used.
2099 </para>
2100
2101 <para>
2102 In order to alleviate this burden, the <firstterm>setup
2103 hook></firstterm>mechanism was written, where any package can include a
2104 shell script that [by convention rather than enforcement by Nix], any
2105 downstream reverse-dependency will source as part of its build process. That
2106 allows the downstream dependency to merely specify its dependencies, and
2107 lets those dependencies effectively initialize themselves. No boilerplate
2108 mirroring the list of dependencies is needed.
2109 </para>
2110
2111 <para>
2112 The Setup hook mechanism is a bit of a sledgehammer though: a powerful
2113 feature with a broad and indiscriminate area of effect. The combination of
2114 its power and implicit use may be expedient, but isn't without costs. Nix
2115 itself is unchanged, but the spirit of adding dependencies being effect-free
2116 is violated even if the letter isn't. For example, if a derivation path is
2117 mentioned more than once, Nix itself doesn't care and simply makes sure the
2118 dependency derivation is already built just the same—depending is just
2119 needing something to exist, and needing is idempotent. However, a dependency
2120 specified twice will have its setup hook run twice, and that could easily
2121 change the build environment (though a well-written setup hook will
2122 therefore strive to be idempotent so this is in fact not observable). More
2123 broadly, setup hooks are anti-modular in that multiple dependencies, whether
2124 the same or different, should not interfere and yet their setup hooks may
2125 well do so.
2126 </para>
2127
2128 <para>
2129 The most typical use of the setup hook is actually to add other hooks which
2130 are then run (i.e. after all the setup hooks) on each dependency. For
2131 example, the C compiler wrapper's setup hook feeds itself flags for each
2132 dependency that contains relevant libaries and headers. This is done by
2133 defining a bash function, and appending its name to one of
2134 <envar>envBuildBuildHooks</envar>`, <envar>envBuildHostHooks</envar>`,
2135 <envar>envBuildTargetHooks</envar>`, <envar>envHostHostHooks</envar>`,
2136 <envar>envHostTargetHooks</envar>`, or <envar>envTargetTargetHooks</envar>`.
2137 These 6 bash variables correspond to the 6 sorts of dependencies by platform
2138 (there's 12 total but we ignore the propagated/non-propagated axis).
2139 </para>
2140
2141 <para>
2142 Packages adding a hook should not hard code a specific hook, but rather
2143 choose a variable <emphasis>relative</emphasis> to how they are included.
2144 Returning to the C compiler wrapper example, if it itself is an
2145 <literal>n</literal> dependency, then it only wants to accumulate flags from
2146 <literal>n + 1</literal> dependencies, as only those ones match the
2147 compiler's target platform. The <envar>hostOffset</envar> variable is
2148 defined with the current dependency's host offset
2149 <envar>targetOffset</envar> with its target offset, before it's setup hook
2150 is sourced. Additionally, since most environment hooks don't care about the
2151 target platform, That means the setup hook can append to the right bash
2152 array by doing something like
2153<programlisting language="bash">
2154addEnvHooks "$hostOffset" myBashFunction
2155 </programlisting>
2156 </para>
2157
2158 <para>
2159 The <emphasis>existence</emphasis> of setups hooks has long been documented
2160 and packages inside Nixpkgs are free to use these mechanism. Other packages,
2161 however, should not rely on these mechanisms not changing between Nixpkgs
2162 versions. Because of the existing issues with this system, there's little
2163 benefit from mandating it be stable for any period of time.
2164 </para>
2165
2166 <para>
2167 Here are some packages that provide a setup hook. Since the mechanism is
2168 modular, this probably isn't an exhaustive list. Then again, since the
2169 mechanism is only to be used as a last resort, it might be.
2170 <variablelist>
2171 <varlistentry>
2172 <term>
2173 Bintools Wrapper
2174 </term>
2175 <listitem>
2176 <para>
2177 Bintools Wrapper wraps the binary utilities for a bunch of miscellaneous
2178 purposes. These are GNU Binutils when targetting Linux, and a mix of
2179 cctools and GNU binutils for Darwin. [The "Bintools" name is supposed to
2180 be a compromise between "Binutils" and "cctools" not denoting any
2181 specific implementation.] Specifically, the underlying bintools package,
2182 and a C standard library (glibc or Darwin's libSystem, just for the
2183 dynamic loader) are all fed in, and dependency finding, hardening (see
2184 below), and purity checks for each are handled by Bintools Wrapper.
2185 Packages typically depend on CC Wrapper, which in turn (at run time)
2186 depends on Bintools Wrapper.
2187 </para>
2188 <para>
2189 Bintools Wrapper was only just recently split off from CC Wrapper, so
2190 the division of labor is still being worked out. For example, it
2191 shouldn't care about about the C standard library, but just take a
2192 derivation with the dynamic loader (which happens to be the glibc on
2193 linux). Dependency finding however is a task both wrappers will continue
2194 to need to share, and probably the most important to understand. It is
2195 currently accomplished by collecting directories of host-platform
2196 dependencies (i.e. <varname>buildInputs</varname> and
2197 <varname>nativeBuildInputs</varname>) in environment variables. Bintools
2198 Wrapper's setup hook causes any <filename>lib</filename> and
2199 <filename>lib64</filename> subdirectories to be added to
2200 <envar>NIX_LDFLAGS</envar>. Since CC Wrapper and Bintools Wrapper use
2201 the same strategy, most of the Bintools Wrapper code is sparsely
2202 commented and refers to CC Wrapper. But CC Wrapper's code, by contrast,
2203 has quite lengthy comments. Bintools Wrapper merely cites those, rather
2204 than repeating them, to avoid falling out of sync.
2205 </para>
2206 <para>
2207 A final task of the setup hook is defining a number of standard
2208 environment variables to tell build systems which executables full-fill
2209 which purpose. They are defined to just be the base name of the tools,
2210 under the assumption that Bintools Wrapper's binaries will be on the
2211 path. Firstly, this helps poorly-written packages, e.g. ones that look
2212 for just <command>gcc</command> when <envar>CC</envar> isn't defined yet
2213 <command>clang</command> is to be used. Secondly, this helps packages
2214 not get confused when cross-compiling, in which case multiple Bintools
2215 Wrappers may simultaneously be in use.
2216 <footnote xml:id="footnote-stdenv-per-platform-wrapper">
2217 <para>
2218 Each wrapper targets a single platform, so if binaries for multiple
2219 platforms are needed, the underlying binaries must be wrapped multiple
2220 times. As this is a property of the wrapper itself, the multiple
2221 wrappings are needed whether or not the same underlying binaries can
2222 target multiple platforms.
2223 </para>
2224 </footnote>
2225 <envar>BUILD_</envar>- and <envar>TARGET_</envar>-prefixed versions of
2226 the normal environment variable are defined for the additional Bintools
2227 Wrappers, properly disambiguating them.
2228 </para>
2229 <para>
2230 A problem with this final task is that Bintools Wrapper is honest and
2231 defines <envar>LD</envar> as <command>ld</command>. Most packages,
2232 however, firstly use the C compiler for linking, secondly use
2233 <envar>LD</envar> anyways, defining it as the C compiler, and thirdly,
2234 only so define <envar>LD</envar> when it is undefined as a fallback.
2235 This triple-threat means Bintools Wrapper will break those packages, as
2236 LD is already defined as the actual linker which the package won't
2237 override yet doesn't want to use. The workaround is to define, just for
2238 the problematic package, <envar>LD</envar> as the C compiler. A good way
2239 to do this would be <command>preConfigure = "LD=$CC"</command>.
2240 </para>
2241 </listitem>
2242 </varlistentry>
2243 <varlistentry>
2244 <term>
2245 CC Wrapper
2246 </term>
2247 <listitem>
2248 <para>
2249 CC Wrapper wraps a C toolchain for a bunch of miscellaneous purposes.
2250 Specifically, a C compiler (GCC or Clang), wrapped binary tools, and a C
2251 standard library (glibc or Darwin's libSystem, just for the dynamic
2252 loader) are all fed in, and dependency finding, hardening (see below),
2253 and purity checks for each are handled by CC Wrapper. Packages typically
2254 depend on CC Wrapper, which in turn (at run time) depends on Bintools
2255 Wrapper.
2256 </para>
2257 <para>
2258 Dependency finding is undoubtedly the main task of CC Wrapper. This
2259 works just like Bintools Wrapper, except that any
2260 <filename>include</filename> subdirectory of any relevant dependency is
2261 added to <envar>NIX_CFLAGS_COMPILE</envar>. The setup hook itself
2262 contains some lengthy comments describing the exact convoluted mechanism
2263 by which this is accomplished.
2264 </para>
2265 <para>
2266 CC Wrapper also like Bintools Wrapper defines standard environment
2267 variables with the names of the tools it wraps, for the same reasons
2268 described above. Importantly, while it includes a <command>cc</command>
2269 symlink to the c compiler for portability, the <envar>CC</envar> will be
2270 defined using the compiler's "real name" (i.e. <command>gcc</command> or
2271 <command>clang</command>). This helps lousy build systems that inspect
2272 on the name of the compiler rather than run it.
2273 </para>
2274 </listitem>
2275 </varlistentry>
2276 <varlistentry>
2277 <term>
2278 Perl
2279 </term>
2280 <listitem>
2281 <para>
2282 Adds the <filename>lib/site_perl</filename> subdirectory of each build
2283 input to the <envar>PERL5LIB</envar> environment variable. For instance,
2284 if <varname>buildInputs</varname> contains Perl, then the
2285 <filename>lib/site_perl</filename> subdirectory of each input is added
2286 to the <envar>PERL5LIB</envar> environment variable.
2287 </para>
2288 </listitem>
2289 </varlistentry>
2290 <varlistentry>
2291 <term>
2292 Python
2293 </term>
2294 <listitem>
2295 <para>
2296 Adds the <filename>lib/${python.libPrefix}/site-packages</filename>
2297 subdirectory of each build input to the <envar>PYTHONPATH</envar>
2298 environment variable.
2299 </para>
2300 </listitem>
2301 </varlistentry>
2302 <varlistentry>
2303 <term>
2304 pkg-config
2305 </term>
2306 <listitem>
2307 <para>
2308 Adds the <filename>lib/pkgconfig</filename> and
2309 <filename>share/pkgconfig</filename> subdirectories of each build input
2310 to the <envar>PKG_CONFIG_PATH</envar> environment variable.
2311 </para>
2312 </listitem>
2313 </varlistentry>
2314 <varlistentry>
2315 <term>
2316 Automake
2317 </term>
2318 <listitem>
2319 <para>
2320 Adds the <filename>share/aclocal</filename> subdirectory of each build
2321 input to the <envar>ACLOCAL_PATH</envar> environment variable.
2322 </para>
2323 </listitem>
2324 </varlistentry>
2325 <varlistentry>
2326 <term>
2327 Autoconf
2328 </term>
2329 <listitem>
2330 <para>
2331 The <varname>autoreconfHook</varname> derivation adds
2332 <varname>autoreconfPhase</varname>, which runs autoreconf, libtoolize
2333 and automake, essentially preparing the configure script in
2334 autotools-based builds.
2335 </para>
2336 </listitem>
2337 </varlistentry>
2338 <varlistentry>
2339 <term>
2340 libxml2
2341 </term>
2342 <listitem>
2343 <para>
2344 Adds every file named <filename>catalog.xml</filename> found under the
2345 <filename>xml/dtd</filename> and <filename>xml/xsl</filename>
2346 subdirectories of each build input to the
2347 <envar>XML_CATALOG_FILES</envar> environment variable.
2348 </para>
2349 </listitem>
2350 </varlistentry>
2351 <varlistentry>
2352 <term>
2353 teTeX / TeX Live
2354 </term>
2355 <listitem>
2356 <para>
2357 Adds the <filename>share/texmf-nix</filename> subdirectory of each build
2358 input to the <envar>TEXINPUTS</envar> environment variable.
2359 </para>
2360 </listitem>
2361 </varlistentry>
2362 <varlistentry>
2363 <term>
2364 Qt 4
2365 </term>
2366 <listitem>
2367 <para>
2368 Sets the <envar>QTDIR</envar> environment variable to Qt’s path.
2369 </para>
2370 </listitem>
2371 </varlistentry>
2372 <varlistentry>
2373 <term>
2374 gdk-pixbuf
2375 </term>
2376 <listitem>
2377 <para>
2378 Exports <envar>GDK_PIXBUF_MODULE_FILE</envar> environment variable the
2379 the builder. Add librsvg package to <varname>buildInputs</varname> to
2380 get svg support.
2381 </para>
2382 </listitem>
2383 </varlistentry>
2384 <varlistentry>
2385 <term>
2386 GHC
2387 </term>
2388 <listitem>
2389 <para>
2390 Creates a temporary package database and registers every Haskell build
2391 input in it (TODO: how?).
2392 </para>
2393 </listitem>
2394 </varlistentry>
2395 <varlistentry>
2396 <term>
2397 GStreamer
2398 </term>
2399 <listitem>
2400 <para>
2401 Adds the GStreamer plugins subdirectory of each build input to the
2402 <envar>GST_PLUGIN_SYSTEM_PATH_1_0</envar> or
2403 <envar>GST_PLUGIN_SYSTEM_PATH</envar> environment variable.
2404 </para>
2405 </listitem>
2406 </varlistentry>
2407 <varlistentry>
2408 <term>
2409 paxctl
2410 </term>
2411 <listitem>
2412 <para>
2413 Defines the <varname>paxmark</varname> helper for setting per-executable
2414 PaX flags on Linux (where it is available by default; on all other
2415 platforms, <varname>paxmark</varname> is a no-op). For example, to
2416 disable secure memory protections on the executable
2417 <replaceable>foo</replaceable>:
2418<programlisting>
2419 postFixup = ''
2420 paxmark m $out/bin/<replaceable>foo</replaceable>
2421 '';
2422 </programlisting>
2423 The <literal>m</literal> flag is the most common flag and is typically
2424 required for applications that employ JIT compilation or otherwise need
2425 to execute code generated at run-time. Disabling PaX protections should
2426 be considered a last resort: if possible, problematic features should be
2427 disabled or patched to work with PaX.
2428 </para>
2429 </listitem>
2430 </varlistentry>
2431 <varlistentry>
2432 <term>
2433 autoPatchelfHook
2434 </term>
2435 <listitem>
2436 <para>
2437 This is a special setup hook which helps in packaging proprietary
2438 software in that it automatically tries to find missing shared library
2439 dependencies of ELF files. All packages within the
2440 <envar>runtimeDependencies</envar> environment variable are
2441 unconditionally added to executables, which is useful for programs that
2442 use <citerefentry>
2443 <refentrytitle>dlopen</refentrytitle>
2444 <manvolnum>3</manvolnum> </citerefentry> to load libraries at runtime.
2445 </para>
2446 </listitem>
2447 </varlistentry>
2448 </variablelist>
2449 </para>
2450 </section>
2451 <section xml:id="sec-purity-in-nixpkgs">
2452 <title>Purity in Nixpkgs</title>
2453
2454 <para>
2455 [measures taken to prevent dependencies on packages outside the store, and
2456 what you can do to prevent them]
2457 </para>
2458
2459 <para>
2460 GCC doesn't search in locations such as <filename>/usr/include</filename>.
2461 In fact, attempts to add such directories through the <option>-I</option>
2462 flag are filtered out. Likewise, the linker (from GNU binutils) doesn't
2463 search in standard locations such as <filename>/usr/lib</filename>. Programs
2464 built on Linux are linked against a GNU C Library that likewise doesn't
2465 search in the default system locations.
2466 </para>
2467 </section>
2468 <section xml:id="sec-hardening-in-nixpkgs">
2469 <title>Hardening in Nixpkgs</title>
2470
2471 <para>
2472 There are flags available to harden packages at compile or link-time. These
2473 can be toggled using the <varname>stdenv.mkDerivation</varname> parameters
2474 <varname>hardeningDisable</varname> and <varname>hardeningEnable</varname>.
2475 </para>
2476
2477 <para>
2478 Both parameters take a list of flags as strings. The special
2479 <varname>"all"</varname> flag can be passed to
2480 <varname>hardeningDisable</varname> to turn off all hardening. These flags
2481 can also be used as environment variables for testing or development
2482 purposes.
2483 </para>
2484
2485 <para>
2486 The following flags are enabled by default and might require disabling with
2487 <varname>hardeningDisable</varname> if the program to package is
2488 incompatible.
2489 </para>
2490
2491 <variablelist>
2492 <varlistentry>
2493 <term>
2494 <varname>format</varname>
2495 </term>
2496 <listitem>
2497 <para>
2498 Adds the <option>-Wformat -Wformat-security
2499 -Werror=format-security</option> compiler options. At present, this warns
2500 about calls to <varname>printf</varname> and <varname>scanf</varname>
2501 functions where the format string is not a string literal and there are
2502 no format arguments, as in <literal>printf(foo);</literal>. This may be a
2503 security hole if the format string came from untrusted input and contains
2504 <literal>%n</literal>.
2505 </para>
2506 <para>
2507 This needs to be turned off or fixed for errors similar to:
2508 </para>
2509<programlisting>
2510/tmp/nix-build-zynaddsubfx-2.5.2.drv-0/zynaddsubfx-2.5.2/src/UI/guimain.cpp:571:28: error: format not a string literal and no format arguments [-Werror=format-security]
2511 printf(help_message);
2512 ^
2513cc1plus: some warnings being treated as errors
2514 </programlisting>
2515 </listitem>
2516 </varlistentry>
2517 <varlistentry>
2518 <term>
2519 <varname>stackprotector</varname>
2520 </term>
2521 <listitem>
2522 <para>
2523 Adds the <option>-fstack-protector-strong --param
2524 ssp-buffer-size=4</option> compiler options. This adds safety checks
2525 against stack overwrites rendering many potential code injection attacks
2526 into aborting situations. In the best case this turns code injection
2527 vulnerabilities into denial of service or into non-issues (depending on
2528 the application).
2529 </para>
2530 <para>
2531 This needs to be turned off or fixed for errors similar to:
2532 </para>
2533<programlisting>
2534bin/blib.a(bios_console.o): In function `bios_handle_cup':
2535/tmp/nix-build-ipxe-20141124-5cbdc41.drv-0/ipxe-5cbdc41/src/arch/i386/firmware/pcbios/bios_console.c:86: undefined reference to `__stack_chk_fail'
2536 </programlisting>
2537 </listitem>
2538 </varlistentry>
2539 <varlistentry>
2540 <term>
2541 <varname>fortify</varname>
2542 </term>
2543 <listitem>
2544 <para>
2545 Adds the <option>-O2 -D_FORTIFY_SOURCE=2</option> compiler options.
2546 During code generation the compiler knows a great deal of information
2547 about buffer sizes (where possible), and attempts to replace insecure
2548 unlimited length buffer function calls with length-limited ones. This is
2549 especially useful for old, crufty code. Additionally, format strings in
2550 writable memory that contain '%n' are blocked. If an application depends
2551 on such a format string, it will need to be worked around.
2552 </para>
2553 <para>
2554 Additionally, some warnings are enabled which might trigger build
2555 failures if compiler warnings are treated as errors in the package build.
2556 In this case, set <option>NIX_CFLAGS_COMPILE</option> to
2557 <option>-Wno-error=warning-type</option>.
2558 </para>
2559 <para>
2560 This needs to be turned off or fixed for errors similar to:
2561 </para>
2562<programlisting>
2563malloc.c:404:15: error: return type is an incomplete type
2564malloc.c:410:19: error: storage size of 'ms' isn't known
2565 </programlisting>
2566<programlisting>
2567strdup.h:22:1: error: expected identifier or '(' before '__extension__'
2568 </programlisting>
2569<programlisting>
2570strsep.c:65:23: error: register name not specified for 'delim'
2571 </programlisting>
2572<programlisting>
2573installwatch.c:3751:5: error: conflicting types for '__open_2'
2574 </programlisting>
2575<programlisting>
2576fcntl2.h:50:4: error: call to '__open_missing_mode' declared with attribute error: open with O_CREAT or O_TMPFILE in second argument needs 3 arguments
2577 </programlisting>
2578 </listitem>
2579 </varlistentry>
2580 <varlistentry>
2581 <term>
2582 <varname>pic</varname>
2583 </term>
2584 <listitem>
2585 <para>
2586 Adds the <option>-fPIC</option> compiler options. This options adds
2587 support for position independent code in shared libraries and thus making
2588 ASLR possible.
2589 </para>
2590 <para>
2591 Most notably, the Linux kernel, kernel modules and other code not running
2592 in an operating system environment like boot loaders won't build with PIC
2593 enabled. The compiler will is most cases complain that PIC is not
2594 supported for a specific build.
2595 </para>
2596 <para>
2597 This needs to be turned off or fixed for assembler errors similar to:
2598 </para>
2599<programlisting>
2600ccbLfRgg.s: Assembler messages:
2601ccbLfRgg.s:33: Error: missing or invalid displacement expression `private_key_len@GOTOFF'
2602 </programlisting>
2603 </listitem>
2604 </varlistentry>
2605 <varlistentry>
2606 <term>
2607 <varname>strictoverflow</varname>
2608 </term>
2609 <listitem>
2610 <para>
2611 Signed integer overflow is undefined behaviour according to the C
2612 standard. If it happens, it is an error in the program as it should check
2613 for overflow before it can happen, not afterwards. GCC provides built-in
2614 functions to perform arithmetic with overflow checking, which are correct
2615 and faster than any custom implementation. As a workaround, the option
2616 <option>-fno-strict-overflow</option> makes gcc behave as if signed
2617 integer overflows were defined.
2618 </para>
2619 <para>
2620 This flag should not trigger any build or runtime errors.
2621 </para>
2622 </listitem>
2623 </varlistentry>
2624 <varlistentry>
2625 <term>
2626 <varname>relro</varname>
2627 </term>
2628 <listitem>
2629 <para>
2630 Adds the <option>-z relro</option> linker option. During program load,
2631 several ELF memory sections need to be written to by the linker, but can
2632 be turned read-only before turning over control to the program. This
2633 prevents some GOT (and .dtors) overwrite attacks, but at least the part
2634 of the GOT used by the dynamic linker (.got.plt) is still vulnerable.
2635 </para>
2636 <para>
2637 This flag can break dynamic shared object loading. For instance, the
2638 module systems of Xorg and OpenCV are incompatible with this flag. In
2639 almost all cases the <varname>bindnow</varname> flag must also be
2640 disabled and incompatible programs typically fail with similar errors at
2641 runtime.
2642 </para>
2643 </listitem>
2644 </varlistentry>
2645 <varlistentry>
2646 <term>
2647 <varname>bindnow</varname>
2648 </term>
2649 <listitem>
2650 <para>
2651 Adds the <option>-z bindnow</option> linker option. During program load,
2652 all dynamic symbols are resolved, allowing for the complete GOT to be
2653 marked read-only (due to <varname>relro</varname>). This prevents GOT
2654 overwrite attacks. For very large applications, this can incur some
2655 performance loss during initial load while symbols are resolved, but this
2656 shouldn't be an issue for daemons.
2657 </para>
2658 <para>
2659 This flag can break dynamic shared object loading. For instance, the
2660 module systems of Xorg and PHP are incompatible with this flag. Programs
2661 incompatible with this flag often fail at runtime due to missing symbols,
2662 like:
2663 </para>
2664<programlisting>
2665intel_drv.so: undefined symbol: vgaHWFreeHWRec
2666 </programlisting>
2667 </listitem>
2668 </varlistentry>
2669 </variablelist>
2670
2671 <para>
2672 The following flags are disabled by default and should be enabled with
2673 <varname>hardeningEnable</varname> for packages that take untrusted input
2674 like network services.
2675 </para>
2676
2677 <variablelist>
2678 <varlistentry>
2679 <term>
2680 <varname>pie</varname>
2681 </term>
2682 <listitem>
2683 <para>
2684 Adds the <option>-fPIE</option> compiler and <option>-pie</option> linker
2685 options. Position Independent Executables are needed to take advantage of
2686 Address Space Layout Randomization, supported by modern kernel versions.
2687 While ASLR can already be enforced for data areas in the stack and heap
2688 (brk and mmap), the code areas must be compiled as position-independent.
2689 Shared libraries already do this with the <varname>pic</varname> flag, so
2690 they gain ASLR automatically, but binary .text regions need to be build
2691 with <varname>pie</varname> to gain ASLR. When this happens, ROP attacks
2692 are much harder since there are no static locations to bounce off of
2693 during a memory corruption attack.
2694 </para>
2695 </listitem>
2696 </varlistentry>
2697 </variablelist>
2698
2699 <para>
2700 For more in-depth information on these hardening flags and hardening in
2701 general, refer to the
2702 <link xlink:href="https://wiki.debian.org/Hardening">Debian Wiki</link>,
2703 <link xlink:href="https://wiki.ubuntu.com/Security/Features">Ubuntu
2704 Wiki</link>,
2705 <link xlink:href="https://wiki.gentoo.org/wiki/Project:Hardened">Gentoo
2706 Wiki</link>, and the
2707 <link xlink:href="https://wiki.archlinux.org/index.php/DeveloperWiki:Security">
2708 Arch Wiki</link>.
2709 </para>
2710 </section>
2711</chapter>