guix-package: When rolling back to nothingness, point to the empty profile.
[jackhill/guix/guix.git] / doc / guix.texi
CommitLineData
568717fd
LC
1\input texinfo
2@c -*-texinfo-*-
3
4@c %**start of header
5@setfilename guix.info
6@documentencoding UTF-8
f8348b91 7@settitle GNU Guix Reference Manual
568717fd
LC
8@c %**end of header
9
10@include version.texi
58db733e 11@set YEARS 2012, 2013
568717fd 12
eeaf4427 13@dircategory Package management
568717fd
LC
14@direntry
15* guix: (guix). Guix, the functional package manager.
eeaf4427
LC
16* guix-package: (guix)Invoking guix-package
17 Managing packages with Guix.
568717fd
LC
18* guix-build: (guix)Invoking guix-build
19 Building packages with Guix.
20@end direntry
568717fd
LC
21
22@titlepage
f8348b91
LC
23@title{GNU Guix Reference Manual}
24@subtitle{Using the GNU Guix Functional Package Manager}
568717fd
LC
25@author Ludovic Courtès
26
27@page
28@vskip 0pt plus 1filll
29Edition @value{EDITION} @*
30@value{UPDATED} @*
31
58db733e 32Copyright @copyright{} @value{YEARS} Ludovic Court@`es
568717fd
LC
33
34@quotation
35Permission is granted to copy, distribute and/or modify this document
36under the terms of the GNU Free Documentation License, Version 1.3 or
37any later version published by the Free Software Foundation; with no
38Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
39copy of the license is included in the section entitled ``GNU Free
40Documentation License''.
41@end quotation
42@end titlepage
43
44@copying
f8348b91 45This manual documents GNU Guix version @value{VERSION}.
568717fd 46
58db733e 47Copyright @copyright{} @value{YEARS} Ludovic Courtès
568717fd
LC
48
49Permission is granted to copy, distribute and/or modify this document
50under the terms of the GNU Free Documentation License, Version 1.3 or
51any later version published by the Free Software Foundation; with no
52Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
53copy of the license is included in the section entitled ``GNU Free
54Documentation License.''
55@end copying
56
57@contents
58
59@c *********************************************************************
60@node Top
f8348b91 61@top GNU Guix
568717fd 62
f8348b91
LC
63This document describes GNU Guix version @value{VERSION}, a functional
64package management tool written for the GNU system.
568717fd 65
58db733e
LC
66@quotation
67Copyright @copyright{} @value{YEARS} Ludovic Courtès
68
69Permission is granted to copy, distribute and/or modify this document
70under the terms of the GNU Free Documentation License, Version 1.3 or
71any later version published by the Free Software Foundation; with no
72Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
73copy of the license is included in the section entitled ``GNU Free
74Documentation License.''
75@end quotation
76
568717fd
LC
77@menu
78* Introduction:: What is Guix about?
bd5e766b 79* Installation:: Installing Guix.
eeaf4427 80* Package Management:: Package installation, upgrade, etc.
568717fd
LC
81* Programming Interface:: Using Guix in Scheme.
82* Utilities:: Package management commands.
a1ba8475 83* GNU Distribution:: Software for your friendly GNU system.
568717fd
LC
84
85* Acknowledgments:: Thanks!
86* GNU Free Documentation License:: The license of this manual.
87* Concept Index:: Concepts.
88* Function Index:: Functions.
89@end menu
90
91@c *********************************************************************
92@node Introduction
93@chapter Introduction
94
c80e7e55
LC
95GNU Guix@footnote{``Guix'' is pronounced like ``geeks'', or ``ɡiːks''
96using the international phonetic alphabet (IPA).} is a functional
97package management tool for the GNU system. Package management consists
98in all the activities that relate to building packages from source,
99honoring the build-time and run-time dependencies on packages,
100installing packages in user environments, upgrading installed packages
101to new versions or rolling back to a previous set, removing unused
102software packages, etc.
568717fd
LC
103
104@cindex functional package management
105The term @dfn{functional} refers to a specific package management
106discipline. In Guix, the package build and installation process is seen
107as a function, in the mathematical sense: that function takes inputs,
108such as build scripts, a compiler, and libraries depended on, and
109returns the installed package. As a pure function, its result depends
110solely on its inputs---for instance, it cannot refer to software or
111scripts that were not explicitly passed as inputs. A build function
112always produces the same result when passed a given set of inputs. Last
113but not least, a build function cannot alter the system's environment in
114any way; for instance, it cannot create, modify, or delete files outside
115of its build and installation directories. This is achieved by running
116build processes in dedicated ``chroots'', where only their explicit
117inputs are visible.
118
e531ac2a 119@cindex store
568717fd 120The result of package build functions is @dfn{cached} in the file
e531ac2a
LC
121system, in a special directory called @dfn{the store} (@pxref{The
122Store}). Each package is installed in a directory of its own, in the
568717fd
LC
123store---by default under @file{/nix/store}. The directory name contains
124a hash of all the inputs used to build that package; thus, changing an
125input yields a different directory name.
126
127This approach is the foundation of Guix's salient features: support for
128transactional package upgrades and rollback, per-user installation, and
eeaf4427 129garbage collection of packages (@pxref{Features}).
568717fd
LC
130
131Guix has a command-line interface allowing users to build, install,
132upgrade, and remove packages, as well as a Scheme programming interface.
133The remainder of this manual describes them.
134
a1ba8475
LC
135Last but not least, Guix is used to build a distribution of the GNU
136system, with many GNU and non-GNU free software packages. @xref{GNU
137Distribution}.
138
bd5e766b
LC
139@c *********************************************************************
140@node Installation
141@chapter Installation
142
143This section describes the software requirements of Guix, as well as how
144to install it and get ready to use it.
145
b22a12fd 146The build procedure for Guix is the same as for other GNU software, and
1da983b9 147is not covered here. Please see the files @file{README} and
b22a12fd
LC
148@file{INSTALL} in the Guix source tree for additional details.
149
bd5e766b
LC
150@menu
151* Requirements:: Software needed to build and run Guix.
152* Setting Up the Daemon:: Preparing the build daemon's environment.
153* Invoking guix-daemon:: Running the build daemon.
154@end menu
155
156@node Requirements
157@section Requirements
158
159GNU Guix depends on the following packages:
160
161@itemize
162@item @url{http://gnu.org/software/guile/, GNU Guile 2.0.x};
163@item @url{http://gnupg.org/, GNU libgcrypt}
164@end itemize
165
166Unless @code{--disable-daemon} was passed to @command{configure}, the
167following packages are also needed:
168
169@itemize
170@item @url{http://sqlite.org, SQLite 3}
171@item @url{http://www.bzip.org, libbz2}
172@item @url{http://gcc.gnu.org, GCC's g++}
173@end itemize
174
175When a working installation of the Nix package manager is available, you
176can instead configure Guix with @code{--disable-daemon}. In that case,
177@url{http://nixos.org/nix/, Nix} replaces the three dependencies above.
178
b22a12fd
LC
179Guix is compatible with Nix, so it is possible to share the same store
180between both. To do so, you must pass @command{configure} not only the
181same @code{--with-store-dir} value, but also the same
182@code{--localstatedir} value (the latter is essential because it
183specifies where the database that store meta-data about the store is
184located, among other things.) The default values are
185@code{--with-store-dir=/nix/store} and @code{--localstatedir=/nix/var}.
186Note that @code{--disable-daemon} is orthogonal and is not required if
187your goal is to share the same store as Nix.
188
bd5e766b
LC
189@node Setting Up the Daemon
190@section Setting Up the Daemon
191
192@cindex daemon
193Operations such as building a package or running the garbage collector
194are all performed by a specialized process, the @dfn{Guix daemon}, on
195behalf of clients. Only the daemon may access the store and its
196associated database. Thus, any operation that manipulates the store
197goes through the daemon. For instance, command-line tools such as
198@command{guix-package} and @command{guix-build} communicate with the
199daemon (@i{via} remote procedure calls) to instruct it what to do.
200
201In a standard multi-user setup, Guix and its daemon---the
202@command{guix-daemon} program---are installed by the system
203administrator; @file{/nix/store} is owned by @code{root} and
204@command{guix-daemon} runs as @code{root}. Unprivileged users may use
205Guix tools to build packages or otherwise access the store, and the
206daemon will do it on their behalf, ensuring that the store is kept in a
207consistent state, and allowing built packages to be shared among users.
208
209@cindex build users
210When @command{guix-daemon} runs as @code{root}, you may not want package
211build processes themselves to run as @code{root} too, for obvious
212security reasons. To avoid that, a special pool of @dfn{build users}
213should be created for use by build processes started by the daemon.
214These build users need not have a shell and a home directory: they will
215just be used when the daemon drops @code{root} privileges in build
216processes. Having several such users allows the daemon to launch
217distinct build processes under separate UIDs, which guarantees that they
218do not interfere with each other---an essential feature since builds are
219regarded as pure functions (@pxref{Introduction}).
220
221On a GNU/Linux system, a build user pool may be created like this (using
222Bash syntax and the @code{shadow} commands):
223
091196b3
LC
224@c See http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html
225@c for why `-G' is needed.
bd5e766b
LC
226@example
227# groupadd guix-builder
228# for i in `seq 1 10`;
229 do
091196b3
LC
230 useradd -g guix-builder -G guix-builder \
231 -d /var/empty -s `which nologin` \
80ba8cc0 232 -c "Guix build user $i" guix-builder$i;
bd5e766b
LC
233 done
234@end example
235
236@noindent
237The @code{guix-daemon} program may then be run as @code{root} with:
238
239@example
240# guix-daemon --build-users-group=guix-builder
241@end example
242
243Guix may also be used in a single-user setup, with @command{guix-daemon}
1da983b9 244running as an unprivileged user. However, to maximize non-interference
bd5e766b
LC
245of build processes, the daemon still needs to perform certain operations
246that are restricted to @code{root} on GNU/Linux: it should be able to
247run build processes in a chroot, and to run them under different UIDs.
248To that end, the @command{nix-setuid-helper} program is provided; it is
249a small C program (less than 300 lines) that, if it is made setuid
250@code{root}, can be executed by the daemon to perform these operations
251on its behalf. The @code{root}-owned @file{/etc/nix-setuid.conf} file
252is read by @command{nix-setuid-helper}; it should contain exactly two
253words: the user name under which the authorized @command{guix-daemon}
254runs, and the name of the build users group.
255
256If you are installing Guix as an unprivileged user and do not have the
257ability to make @file{nix-setuid-helper} setuid-@code{root}, it is still
258possible to run @command{guix-daemon}. However, build processes will
259not be isolated from one another, and not from the rest of the system.
260Thus, build processes may interfere with each other, and may access
261programs, libraries, and other files available on the system---making it
262much harder to view them as @emph{pure} functions.
263
264@node Invoking guix-daemon
265@section Invoking @command{guix-daemon}
266
267The @command{guix-daemon} program implements all the functionality to
268access the store. This includes launching build processes, running the
269garbage collector, querying the availability of a build result, etc. It
270is normally run as @code{root} like this:
271
272@example
273# guix-daemon --build-users-group=guix-builder
274@end example
275
276@noindent
277For details on how to set it up, @ref{Setting Up the Daemon}.
278
279By default, @command{guix-daemon} launches build processes under
280different UIDs, taken from the build group specified with
281@code{--build-users-group}. In addition, each build process is run in a
282chroot environment that only contains the subset of the store that the
283build process depends on, as specified by its derivation
284(@pxref{Programming Interface, derivation}), plus a set of specific
285system directories. By default, the latter contains @file{/dev} and
286@file{/dev/pts}.
287
288The following command-line options are supported:
289
290@table @code
291@item --build-users-group=@var{group}
292Take users from @var{group} to run build processes (@pxref{Setting Up
293the Daemon, build users}).
294
295@item --cache-failures
296Cache build failures. By default, only successful builds are cached.
297
298@item --cores=@var{n}
299@itemx -c @var{n}
300Use @var{n} CPU cores to build each derivation; @code{0} means as many
301as available.
302
303The default value is @code{1}, but it may be overridden by clients, such
304as the @code{--cores} option of @command{guix-build} (@pxref{Invoking
305guix-build}).
306
307The effect is to define the @code{NIX_BUILD_CORES} environment variable
308in the build process, which can then use it to exploit internal
309parallelism---for instance, by running @code{make -j$NIX_BUILD_CORES}.
310
311@item --max-jobs=@var{n}
312@itemx -M @var{n}
313Allow at most @var{n} build jobs in parallel. The default value is
314@code{1}.
315
316@item --debug
317Produce debugging output.
318
319This is useful to debug daemon start-up issues, but then it may be
320overridden by clients, for example the @code{--verbosity} option of
321@command{guix-build} (@pxref{Invoking guix-build}).
322
323@item --chroot-directory=@var{dir}
324Add @var{dir} to the build chroot.
325
326Doing this may change the result of build processes---for instance if
327they use optional dependencies found in @var{dir} when it is available,
328and not otherwise. For that reason, it is not recommended to do so.
329Instead, make sure that each derivation declares all the inputs that it
330needs.
331
332@item --disable-chroot
333Disable chroot builds.
334
335Using this option is not recommended since, again, it would allow build
336processes to gain access to undeclared dependencies.
337
338@item --disable-log-compression
339Disable compression of the build logs.
340
1da983b9
LC
341Unless @code{--lose-logs} is used, all the build logs are kept in the
342@var{localstatedir}. To save space, the daemon automatically compresses
343them with bzip2 by default. This option disables that.
344
bd5e766b
LC
345@item --disable-store-optimization
346Disable automatic file ``deduplication'' in the store.
347
1da983b9
LC
348By default, files added to the store are automatically ``deduplicated'':
349if a newly added file is identical as another one found in the store,
350the daemon makes the new file a hard link to the other file. This
351slightly increases the input/output load at the end of a build process.
352This option disables this.
353
bd5e766b
LC
354@item --impersonate-linux-2.6
355On Linux-based systems, impersonate Linux 2.6. This means that the
356kernel's @code{uname} system call will report 2.6 as the release number.
357
358This might be helpful to build programs that (usually wrongfully) depend
359on the kernel version number.
360
361@item --lose-logs
362Do not keep build logs. By default they are kept under
363@code{@var{localstatedir}/nix/log}.
364
365@item --system=@var{system}
366Assume @var{system} as the current system type. By default it is the
367architecture/kernel pair found at configure time, such as
368@code{x86_64-linux}.
369@end table
370
371
eeaf4427
LC
372@c *********************************************************************
373@node Package Management
374@chapter Package Management
375
f8348b91 376The purpose of GNU Guix is to allow users to easily install, upgrade, and
eeaf4427
LC
377remove software packages, without having to know about their build
378procedure or dependencies. Guix also goes beyond this obvious set of
379features.
380
381This chapter describes the main features of Guix, as well as the package
382management tools it provides.
383
384@menu
385* Features:: How Guix will make your life brighter.
386* Invoking guix-package:: Package installation, removal, etc.
fe8ff028 387* Invoking guix-gc:: Running the garbage collector.
eeaf4427
LC
388@end menu
389
390@node Features
391@section Features
392
393When using Guix, each package ends up in the @dfn{package store}, in its
394own directory---something that resembles
395@file{/nix/store/xxx-package-1.2}, where @code{xxx} is a base32 string.
396
397Instead of referring to these directories, users have their own
398@dfn{profile}, which points to the packages that they actually want to
821b0015
LC
399use. These profiles are stored within each user's home directory, at
400@code{$HOME/.guix-profile}.
eeaf4427 401
821b0015 402For example, @code{alice} installs GCC 4.7.2. As a result,
eeaf4427 403@file{/home/alice/.guix-profile/bin/gcc} points to
821b0015
LC
404@file{/nix/store/@dots{}-gcc-4.7.2/bin/gcc}. Now, on the same machine,
405@code{bob} had already installed GCC 4.8.0. The profile of @code{bob}
406simply continues to point to
407@file{/nix/store/@dots{}-gcc-4.8.0/bin/gcc}---i.e., both versions of GCC
408coexist on the same system without any interference.
eeaf4427
LC
409
410The @command{guix-package} command is the central tool to manage
821b0015
LC
411packages (@pxref{Invoking guix-package}). It operates on those per-user
412profiles, and can be used @emph{with normal user privileges}.
eeaf4427
LC
413
414The command provides the obvious install, remove, and upgrade
415operations. Each invocation is actually a @emph{transaction}: either
ba55b1cb 416the specified operation succeeds, or nothing happens. Thus, if the
821b0015 417@command{guix-package} process is terminated during the transaction,
eeaf4427
LC
418or if a power outage occurs during the transaction, then the user's
419profile remains in its previous state, and remains usable.
420
421In addition, any package transaction may be @emph{rolled back}. So, if,
422for example, an upgrade installs a new version of a package that turns
423out to have a serious bug, users may roll back to the previous instance
424of their profile, which was known to work well.
425
426All those packages in the package store may be @emph{garbage-collected}.
427Guix can determine which packages are still referenced by the user
fe8ff028
LC
428profiles, and remove those that are provably no longer referenced
429(@pxref{Invoking guix-gc}). Users may also explicitly remove old
430generations of their profile so that the packages they refer to can be
431collected.
eeaf4427
LC
432
433Finally, Guix takes a @dfn{purely functional} approach to package
434management, as described in the introduction (@pxref{Introduction}).
435Each @file{/nix/store} package directory name contains a hash of all the
436inputs that were used to build that package---compiler, libraries, build
437scripts, etc. This direct correspondence allows users to make sure a
438given package installation matches the current state of their
a1ba8475 439distribution, and helps maximize @dfn{reproducibility}.
eeaf4427 440
75f1e8f7 441@c FIXME: Remove footnote when it's implemented.
eeaf4427 442This foundation allows Guix to support @dfn{transparent binary/source
75f1e8f7
LC
443deployment}@footnote{This feature is not implemented as of version
444@value{VERSION}. Please contact @email{bug-guix@@gnu.org} for more
445details.}. When a pre-built binary for a @file{/nix/store} path is
eeaf4427
LC
446available from an external source, Guix just downloads it; otherwise, it
447builds the package from source, locally.
448
449@node Invoking guix-package
450@section Invoking @command{guix-package}
451
ba55b1cb 452The @command{guix-package} command is the tool that allows users to
eeaf4427
LC
453install, upgrade, and remove packages, as well as rolling back to
454previous configurations. It operates only on the user's own profile,
455and works with normal user privileges (@pxref{Features}). Its syntax
456is:
457
458@example
459guix-package @var{options}
460@end example
461
ba55b1cb 462Primarily, @var{options} specifies the operations to be performed during
eeaf4427
LC
463the transaction. Upon completion, a new profile is created, but
464previous generations of the profile remain available, should the user
465want to roll back.
466
b9e5c0a9 467For each user, a symlink to the user's default profile is automatically
0ec1af59 468created in @file{$HOME/.guix-profile}. This symlink always points to the
b9e5c0a9
LC
469current generation of the user's default profile. Thus, users can add
470@file{$HOME/.guix-profile/bin} to their @code{PATH} environment
471variable, and so on.
472
0ec1af59
LC
473In a multi-user setup, user profiles must be stored in a place
474registered as a @dfn{garbage-collector root}, which
475@file{$HOME/.guix-profile} points to (@pxref{Invoking guix-gc}). That
476directory is normally
477@code{@var{localstatedir}/profiles/per-user/@var{user}}, where
478@var{localstatedir} is the value passed to @code{configure} as
479@code{--localstatedir}, and @var{user} is the user name. It must be
480created by @code{root}, with @var{user} as the owner. When it does not
481exist, @command{guix-package} emits an error about it.
482
483The @var{options} can be among the following:
484
eeaf4427
LC
485@table @code
486
487@item --install=@var{package}
51c8d790 488@itemx -i @var{package}
eeaf4427
LC
489Install @var{package}.
490
491@var{package} may specify either a simple package name, such as
492@code{guile}, or a package name followed by a hyphen and version number,
bfe384cc 493such as @code{guile-1.8.8}. In addition, @var{package} may contain a
eeaf4427 494colon, followed by the name of one of the outputs of the package, as in
f03e7115 495@code{gcc:doc} or @code{binutils-2.22:lib}.
eeaf4427
LC
496
497@item --remove=@var{package}
498@itemx -r @var{package}
499Remove @var{package}.
500
ba55b1cb
LC
501@item --upgrade=@var{regexp}
502@itemx -u @var{regexp}
eeaf4427
LC
503Upgrade all the installed packages matching @var{regexp}.
504
24e262f0
LC
505@item --roll-back
506Roll back to the previous @dfn{generation} of the profile---i.e., undo
507the last transaction.
508
509When combined with options such as @code{--install}, roll back occurs
510before any other actions.
511
d9307267
LC
512When rolling back from the first generation that actually contains
513installed packages, the profile is made to point to the @dfn{empty
514profile}, also known as @dfn{profile zero}---i.e., it contains no files
515apart from its own meta-data.
516
eeaf4427
LC
517@item --profile=@var{profile}
518@itemx -p @var{profile}
519Use @var{profile} instead of the user's default profile.
520
521@item --dry-run
522@itemx -n
523Show what would be done without actually doing it.
524
70915c1a
LC
525@item --verbose
526Produce verbose output. In particular, emit the environment's build log
527on the standard error port.
528
eeaf4427
LC
529@item --bootstrap
530Use the bootstrap Guile to build the profile. This option is only
531useful to distribution developers.
532
533@end table
534
733b4130
LC
535In addition to these actions @command{guix-package} supports the
536following options to query the current state of a profile, or the
537availability of packages:
eeaf4427 538
733b4130
LC
539@table @option
540
541@item --list-installed[=@var{regexp}]
542@itemx -I [@var{regexp}]
543List currently installed packages in the specified profile. When
544@var{regexp} is specified, list only installed packages whose name
545matches @var{regexp}.
546
547For each installed package, print the following items, separated by
548tabs: the package name, its version string, the part of the package that
549is installed (for instance, @code{out} for the default output,
550@code{include} for its headers, etc.), and the path of this package in
551the store.
552
64fc89b6
LC
553@item --list-available[=@var{regexp}]
554@itemx -A [@var{regexp}]
a1ba8475
LC
555List packages currently available in the software distribution
556(@pxref{GNU Distribution}). When @var{regexp} is specified, list only
557installed packages whose name matches @var{regexp}.
64fc89b6
LC
558
559For each package, print the following items separated by tabs: its name,
44b6be77
LC
560its version string, the parts of the package (@code{out} for the main
561files, @code{lib} for libraries and possibly headers, etc.), and the
562source location of its definition.
64fc89b6 563
733b4130 564@end table
eeaf4427
LC
565
566
fe8ff028
LC
567@node Invoking guix-gc
568@section Invoking @command{guix-gc}
569
570@cindex garbage collector
571Packages that are installed but not used may be @dfn{garbage-collected}.
572The @command{guix-gc} command allows users to explicitly run the garbage
573collector to reclaim space from the @file{/nix/store} directory.
574
575The garbage collector has a set of known @dfn{roots}: any file under
576@file{/nix/store} reachable from a root is considered @dfn{live} and
577cannot be deleted; any other file is considered @dfn{dead} and may be
578deleted. The set of garbage collector roots includes default user
579profiles, and may be augmented with @command{guix-build --root}, for
580example (@pxref{Invoking guix-build}).
581
1da983b9 582The @command{guix-gc} command has three modes of operation: it can be
fe8ff028
LC
583used to garbage-collect any dead files (the default), to delete specific
584files (the @code{--delete} option), or to print garbage-collector
585information. The available options are listed below:
586
587@table @code
588@item --collect-garbage[=@var{min}]
589@itemx -C [@var{min}]
590Collect garbage---i.e., unreachable @file{/nix/store} files and
591sub-directories. This is the default operation when no option is
592specified.
593
594When @var{min} is given, stop once @var{min} bytes have been collected.
595@var{min} may be a number of bytes, or it may include a unit as a
596suffix, such as @code{MiB} for mebibytes and @code{GB} for gigabytes.
597
598When @var{min} is omitted, collect all the garbage.
599
600@item --delete
601@itemx -d
602Attempt to delete all the store files and directories specified as
603arguments. This fails if some of the files are not in the store, or if
604they are still live.
605
606@item --list-dead
607Show the list of dead files and directories still present in the
608store---i.e., files and directories no longer reachable from any root.
609
610@item --list-live
611Show the list of live store files and directories.
612@end table
613
eeaf4427 614
568717fd
LC
615@c *********************************************************************
616@node Programming Interface
617@chapter Programming Interface
618
3dc1970d
LC
619GNU Guix provides several Scheme programming interfaces (APIs) to
620define, build, and query packages. The first interface allows users to
621write high-level package definitions. These definitions refer to
622familiar packaging concepts, such as the name and version of a package,
623its build system, and its dependencies. These definitions can then be
624turned into concrete build actions.
625
ba55b1cb 626Build actions are performed by the Guix daemon, on behalf of users. In a
3dc1970d
LC
627standard setup, the daemon has write access to the store---the
628@file{/nix/store} directory---whereas users do not. The recommended
629setup also has the daemon perform builds in chroots, under a specific
630build users, to minimize interference with the rest of the system.
631
632@cindex derivation
633Lower-level APIs are available to interact with the daemon and the
634store. To instruct the daemon to perform a build action, users actually
635provide it with a @dfn{derivation}. A derivation is a low-level
636representation of the build actions to be taken, and the environment in
637which they should occur---derivations are to package definitions what
638assembly is to C programs.
639
640This chapter describes all these APIs in turn, starting from high-level
641package definitions.
642
568717fd
LC
643@menu
644* Defining Packages:: Defining new packages.
645* The Store:: Manipulating the package store.
646* Derivations:: Low-level interface to package derivations.
647@end menu
648
649@node Defining Packages
650@section Defining Packages
651
3dc1970d
LC
652The high-level interface to package definitions is implemented in the
653@code{(guix packages)} and @code{(guix build-system)} modules. As an
654example, the package definition, or @dfn{recipe}, for the GNU Hello
655package looks like this:
656
657@example
b22a12fd
LC
658(use-modules (guix packages)
659 (guix download)
660 (guix build-system gnu)
661 (guix licenses))
662
3dc1970d
LC
663(define hello
664 (package
665 (name "hello")
666 (version "2.8")
667 (source (origin
668 (method url-fetch)
669 (uri (string-append "mirror://gnu/hello/hello-" version
670 ".tar.gz"))
671 (sha256
672 (base32 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6"))))
673 (build-system gnu-build-system)
674 (inputs `(("gawk" ,gawk)))
675 (synopsis "GNU Hello")
676 (description "Yeah...")
677 (home-page "http://www.gnu.org/software/hello/")
b22a12fd 678 (license gpl3+)))
3dc1970d
LC
679@end example
680
681@noindent
682Without being a Scheme expert, the reader may have guessed the meaning
683of the various fields here. This expression binds variable @var{hello}
684to a @code{<package>} object, which is essentially a record
685(@pxref{SRFI-9, Scheme records,, guile, GNU Guile Reference Manual}).
686This package object can be inspected using procedures found in the
687@code{(guix packages)} module; for instance, @code{(package-name hello)}
688returns---surprise!---@code{"hello"}.
689
690There are a few points worth noting in the above package definition:
691
692@itemize
693@item
694The @code{source} field of the package is an @code{<origin>} object.
695Here, the @code{url-fetch} method from @code{(guix download)} is used,
696meaning that the source is a file to be downloaded over FTP or HTTP.
697
698The @code{mirror://gnu} prefix instructs @code{url-fetch} to use one of
699the GNU mirrors defined in @code{(guix download)}.
700
701The @code{sha256} field specifies the expected SHA256 hash of the file
702being downloaded. It is mandatory, and allows Guix to check the
703integrity of the file. The @code{(base32 @dots{})} form introduces the
704base32 representation of the hash. A convenient way to obtain this
705information is with the @code{guix-download} tool.
706
707@item
708@cindex GNU Build System
709The @code{build-system} field is set to @var{gnu-build-system}. The
710@var{gnu-build-system} variable is defined in the @code{(guix
711build-system gnu)} module, and is bound to a @code{<build-system>}
712object.
713
714Naturally, @var{gnu-build-system} represents the familiar GNU Build
715System, and variants thereof (@pxref{Configuration, configuration and
716makefile conventions,, standards, GNU Coding Standards}). In a
ba55b1cb 717nutshell, packages using the GNU Build System may be configured, built,
3dc1970d
LC
718and installed with the usual @code{./configure && make && make check &&
719make install} command sequence. This is what @var{gnu-build-system}
720does.
721
722In addition, @var{gnu-build-system} ensures that the ``standard''
723environment for GNU packages is available. This includes tools such as
724GCC, Coreutils, Bash, Make, Diffutils, and Patch.
725
726@item
727The @code{inputs} field specifies inputs to the build process---i.e.,
728build-time or run-time dependencies of the package. Here, we define an
729input called @code{"gawk"} whose value is that of the @var{gawk}
730variable; @var{gawk} is itself bound to a @code{<package>} object.
731
732Note that GCC, Coreutils, Bash, and other essential tools do not need to
733be specified as inputs here. Instead, @var{gnu-build-system} takes care
734of ensuring that they are present.
735
736However, any other dependencies need to be specified in the
737@code{inputs} field. Any dependency not specified here will simply be
738unavailable to the build process, possibly leading to a build failure.
739@end itemize
740
741There are other fields that package definitions may provide. Of
742particular interest is the @code{arguments} field. When specified, it
743must be bound to a list of additional arguments to be passed to the
744build system. For instance, the above definition could be augmented
745with the following field initializer:
746
747@example
748 (arguments `(#:tests? #f
749 #:configure-flags '("--enable-silent-rules")))
750@end example
751
752@noindent
753These are keyword arguments (@pxref{Optional Arguments, keyword
754arguments in Guile,, guile, GNU Guile Reference Manual}). They are
755passed to @var{gnu-build-system}, which interprets them as meaning ``do
756not run @code{make check}'', and ``run @file{configure} with the
874e6874
LC
757@code{--enable-silent-rules} flag''. The value of these keyword
758parameters is actually evaluated in the @dfn{build stratum}---i.e., by a
759Guile process launched by the daemon (@pxref{Derivations}).
3dc1970d
LC
760
761Once a package definition is in place@footnote{Simple package
762definitions like the one above may be automatically converted from the
763Nixpkgs distribution using the @command{guix-import} command.}, the
764package may actually be built using the @code{guix-build} command-line
765tool (@pxref{Invoking guix-build}).
766
767Behind the scenes, a derivation corresponding to the @code{<package>}
768object is first computed by the @code{package-derivation} procedure.
769That derivation is stored in a @code{.drv} file under @file{/nix/store}.
ba55b1cb 770The build actions it prescribes may then be realized by using the
3dc1970d
LC
771@code{build-derivations} procedure (@pxref{The Store}).
772
773@deffn {Scheme Procedure} package-derivation @var{store} @var{package} [@var{system}]
e509d152
LC
774Return the derivation path and corresponding @code{<derivation>} object
775of @var{package} for @var{system} (@pxref{Derivations}).
3dc1970d
LC
776
777@var{package} must be a valid @code{<package>} object, and @var{system}
778must be a string denoting the target system type---e.g.,
779@code{"x86_64-linux"} for an x86_64 Linux-based GNU system. @var{store}
780must be a connection to the daemon, which operates on the store
781(@pxref{The Store}).
782@end deffn
568717fd
LC
783
784@node The Store
785@section The Store
786
e531ac2a
LC
787@cindex store
788@cindex store paths
789
790Conceptually, the @dfn{store} is where derivations that have been
791successfully built are stored---by default, under @file{/nix/store}.
792Sub-directories in the store are referred to as @dfn{store paths}. The
793store has an associated database that contains information such has the
794store paths referred to by each store path, and the list of @emph{valid}
795store paths---paths that result from a successful build.
796
797The store is always accessed by the daemon on behalf of its clients
798(@pxref{Invoking guix-daemon}). To manipulate the store, clients
799connect to the daemon over a Unix-domain socket, send it requests, and
800read the result---these are remote procedure calls, or RPCs.
801
802The @code{(guix store)} module provides procedures to connect to the
803daemon, and to perform RPCs. These are described below.
804
805@deffn {Scheme Procedure} open-connection [@var{file}] [#:reserve-space? #t]
806Connect to the daemon over the Unix-domain socket at @var{file}. When
807@var{reserve-space?} is true, instruct it to reserve a little bit of
808extra space on the file system so that the garbage collector can still
809operate, should the disk become full. Return a server object.
810
811@var{file} defaults to @var{%default-socket-path}, which is the normal
812location given the options that were passed to @command{configure}.
813@end deffn
814
815@deffn {Scheme Procedure} close-connection @var{server}
816Close the connection to @var{server}.
817@end deffn
818
819@defvr {Scheme Variable} current-build-output-port
820This variable is bound to a SRFI-39 parameter, which refers to the port
821where build and error logs sent by the daemon should be written.
822@end defvr
823
824Procedures that make RPCs all take a server object as their first
825argument.
826
827@deffn {Scheme Procedure} valid-path? @var{server} @var{path}
828Return @code{#t} when @var{path} is a valid store path.
829@end deffn
830
831@deffn {Scheme Procedure} add-text-to-store @var{server} @var{name} @var{text} @var{references}
832Add @var{text} under file @var{name} in the store, and return its store
833path. @var{references} is the list of store paths referred to by the
834resulting store path.
835@end deffn
836
874e6874
LC
837@deffn {Scheme Procedure} build-derivations @var{server} @var{derivations}
838Build @var{derivations} (a list of derivation paths), and return when
839the worker is done building them. Return @code{#t} on success.
840@end deffn
841
e531ac2a
LC
842@c FIXME
843@i{This section is currently incomplete.}
568717fd
LC
844
845@node Derivations
846@section Derivations
847
874e6874
LC
848@cindex derivations
849Low-level build actions and the environment in which they are performed
850are represented by @dfn{derivations}. A derivation contain the
851following pieces of information:
852
853@itemize
854@item
855The outputs of the derivation---derivations produce at least one file or
856directory in the store, but may produce more.
857
858@item
859The inputs of the derivations, which may be other derivations or plain
860files in the store (patches, build scripts, etc.)
861
862@item
863The system type targeted by the derivation---e.g., @code{x86_64-linux}.
864
865@item
866The file name of a build script in the store, along with the arguments
867to be passed.
868
869@item
870A list of environment variables to be defined.
871
872@end itemize
873
874@cindex derivation path
875Derivations allow clients of the daemon to communicate build actions to
876the store. They exist in two forms: as an in-memory representation,
877both on the client- and daemon-side, and as files in the store whose
878name end in @code{.drv}---these files are referred to as @dfn{derivation
879paths}. Derivations paths can be passed to the @code{build-derivations}
880procedure to perform the build actions they prescribe (@pxref{The
881Store}).
882
883The @code{(guix derivations)} module provides a representation of
884derivations as Scheme objects, along with procedures to create and
885otherwise manipulate derivations. The lowest-level primitive to create
886a derivation is the @code{derivation} procedure:
887
888@deffn {Scheme Procedure} derivation @var{store} @var{name} @var{system} @var{builder} @var{args} @var{env-vars} @var{inputs} [#:outputs '("out")] [#:hash #f] [#:hash-algo #f] [#:hash-mode #f]
889Build a derivation with the given arguments. Return the resulting store
890path and @code{<derivation>} object.
891
892When @var{hash}, @var{hash-algo}, and @var{hash-mode} are given, a
893@dfn{fixed-output derivation} is created---i.e., one whose result is
894known in advance, such as a file download.
895@end deffn
896
897@noindent
898Here's an example with a shell script as its builder, assuming
899@var{store} is an open connection to the daemon, and @var{bash} points
900to a Bash executable in the store:
901
902@lisp
903(use-modules (guix utils)
904 (guix store)
905 (guix derivations))
906
907(call-with-values
908 (lambda ()
909 (let ((builder ; add the Bash script to the store
910 (add-text-to-store store "my-builder.sh"
911 "echo hello world > $out\n" '())))
912 (derivation store "foo" (%current-system)
913 bash `("-e" ,builder)
914 '(("HOME" . "/homeless")) '())))
915 list)
916@result{} ("/nix/store/@dots{}-foo.drv" #<<derivation> @dots{}>)
917@end lisp
918
919As can be guessed, this primitive is cumbersome to use directly. An
920improved variant is @code{build-expression->derivation}, which allows
921the caller to directly pass a Guile expression as the build script:
922
923@deffn {Scheme Procedure} build-expression->derivation @var{store} @var{name} @var{system} @var{exp} @var{inputs} [#:outputs '("out")] [#:hash #f] [#:hash-algo #f] [#:env-vars '()] [#:modules '()] [#:guile-for-build #f]
924Return a derivation that executes Scheme expression @var{exp} as a
925builder for derivation @var{name}. @var{inputs} must be a list of
926@code{(name drv-path sub-drv)} tuples; when @var{sub-drv} is omitted,
927@code{"out"} is assumed. @var{modules} is a list of names of Guile
928modules from the current search path to be copied in the store,
929compiled, and made available in the load path during the execution of
930@var{exp}---e.g., @code{((guix build utils) (guix build
931gnu-build-system))}.
932
933@var{exp} is evaluated in an environment where @code{%outputs} is bound
934to a list of output/path pairs, and where @code{%build-inputs} is bound
935to a list of string/output-path pairs made from @var{inputs}.
936Optionally, @var{env-vars} is a list of string pairs specifying the name
937and value of environment variables visible to the builder. The builder
938terminates by passing the result of @var{exp} to @code{exit}; thus, when
939@var{exp} returns @code{#f}, the build is considered to have failed.
940
941@var{exp} is built using @var{guile-for-build} (a derivation). When
942@var{guile-for-build} is omitted or is @code{#f}, the value of the
943@code{%guile-for-build} fluid is used instead.
944@end deffn
945
946@noindent
947Here's an example of a single-output derivation that creates a directory
948containing one file:
949
950@lisp
951(let ((builder '(let ((out (assoc-ref %outputs "out")))
952 (mkdir out) ; create /nix/store/@dots{}-goo
953 (call-with-output-file (string-append out "/test")
954 (lambda (p)
955 (display '(hello guix) p))))))
956 (build-expression->derivation store "goo" (%current-system)
957 builder '()))
958
959@result{} "/nix/store/@dots{}-goo.drv"
960@result{} #<<derivation> @dots{}>
961@end lisp
962
963@cindex strata of code
964Remember that the build expression passed to
965@code{build-expression->derivation} is run by a separate Guile process
966than the one that calls @code{build-expression->derivation}: it is run
967by a Guile process launched by the daemon, typically in a chroot. So,
968while there is a single language for both the @dfn{host} and the build
969side, there are really two @dfn{strata} of code: the host-side, and the
970build-side code@footnote{The term @dfn{stratum} in this context was
971coined by Manuel Serrano et al. in the context of their work on Hop.}.
972This distinction is important to keep in mind, notably when using
973higher-level constructs such as @var{gnu-build-system} (@pxref{Defining
974Packages}). For this reason, Guix modules that are meant to be used in
975the build stratum are kept in the @code{(guix build @dots{})} name
976space.
568717fd
LC
977
978@c *********************************************************************
979@node Utilities
980@chapter Utilities
981
982@menu
983* Invoking guix-build:: Building packages from the command line.
984@end menu
985
986@node Invoking guix-build
987@section Invoking @command{guix-build}
988
c78bd12b 989The @command{guix-build} command builds packages or derivations and
6798a8e4
LC
990their dependencies, and prints the resulting store paths. Note that it
991does not modify the user's profile---this is the job of the
992@command{guix-package} command (@pxref{Invoking guix-package}). Thus,
993it is mainly useful for distribution developers.
994
995The general syntax is:
c78bd12b
LC
996
997@example
998guix-build @var{options} @var{package-or-derivation}@dots{}
999@end example
1000
1001@var{package-or-derivation} may be either the name of a package found in
5401dd75
LC
1002the software distribution such as @code{coreutils} or
1003@code{coreutils-8.20}, or a derivation such as
1004@file{/nix/store/@dots{}-coreutils-8.19.drv}. Alternatively, the
c78bd12b
LC
1005@code{--expression} option may be used to specify a Scheme expression
1006that evaluates to a package; this is useful when disambiguation among
1007several same-named packages or package variants is needed.
1008
1009The @var{options} may be zero or more of the following:
1010
1011@table @code
1012
1013@item --expression=@var{expr}
1014@itemx -e @var{expr}
1015Build the package @var{expr} evaluates to.
1016
5401dd75 1017For example, @var{expr} may be @code{(@@ (gnu packages guile)
c78bd12b
LC
1018guile-1.8)}, which unambiguously designates this specific variant of
1019version 1.8 of Guile.
1020
1021@item --source
1022@itemx -S
1023Build the packages' source derivations, rather than the packages
1024themselves.
1025
1026For instance, @code{guix-build -S gcc} returns something like
6798a8e4 1027@file{/nix/store/@dots{}-gcc-4.7.2.tar.bz2}, which is GCC's source tarball.
c78bd12b
LC
1028
1029@item --system=@var{system}
1030@itemx -s @var{system}
1031Attempt to build for @var{system}---e.g., @code{i686-linux}---instead of
1032the host's system type.
1033
1034An example use of this is on Linux-based systems, which can emulate
1035different personalities. For instance, passing
1036@code{--system=i686-linux} on an @code{x86_64-linux} system allows users
1037to build packages in a complete 32-bit environment.
1038
1039@item --derivations
1040@itemx -d
1041Return the derivation paths, not the output paths, of the given
1042packages.
1043
1044@item --keep-failed
1045@itemx -K
1046Keep the build tree of failed builds. Thus, if a build fail, its build
1047tree is kept under @file{/tmp}, in a directory whose name is shown at
1048the end of the build log. This is useful when debugging build issues.
1049
1050@item --dry-run
1051@itemx -n
1052Do not build the derivations.
1053
1054@item --no-substitutes
1055Build instead of resorting to pre-built substitutes.
1056
1057@item --cores=@var{n}
1058@itemx -c @var{n}
1059Allow the use of up to @var{n} CPU cores for the build. The special
1060value @code{0} means to use as many CPU cores as available.
1061
1062@item --root=@var{file}
1063@itemx -r @var{file}
1064Make @var{file} a symlink to the result, and register it as a garbage
1065collector root.
07ab4bf1
LC
1066
1067@item --verbosity=@var{level}
1068Use the given verbosity level. @var{level} must be an integer between 0
1069and 5; higher means more verbose output. Setting a level of 4 or more
1070may be helpful when debugging setup issues with the build daemon.
1071
c78bd12b
LC
1072@end table
1073
1074Behind the scenes, @command{guix-build} is essentially an interface to
1075the @code{package-derivation} procedure of the @code{(guix packages)}
1076module, and to the @code{build-derivations} procedure of the @code{(guix
1077store)} module.
1078
a1ba8475
LC
1079@c *********************************************************************
1080@node GNU Distribution
1081@chapter GNU Distribution
1082
1083Guix comes with a distribution of free software@footnote{The term
1084``free'' here refers to the
1085@url{http://www.gnu.org/philosophy/free-sw.html,freedom provided to
1086users of that software}.} that form the basis of the GNU system. This
1087includes core GNU packages such as GNU libc, GCC, and Binutils, as well
1088as many GNU and non-GNU applications. The complete list of available
1089packages can be seen by running @command{guix-package} (@pxref{Invoking
1090guix-package}):
1091
1092@example
1093guix-package --list-available
1094@end example
1095
1096The package definitions of the distribution may are provided by Guile
1097modules in the @code{(gnu packages ...)} name space---for instance, the
1098@code{(gnu packages emacs)} module exports a variable named
1099@code{emacs}, which is bound to a @code{<package>} object
1100(@pxref{Defining Packages}). The @code{(gnu packages)} module provides
1101facilities for searching for packages.
1102
1103The distribution is fully @dfn{bootstrapped} and @dfn{self-contained}:
1104each package is built based solely on other packages in the
1105distribution. The root of this dependency graph is a small set of
1106@dfn{bootstrap binaries}, provided by the @code{(gnu packages
1107bootstrap)} module. These are statically-linked binaries of the core
1108tools without which building anything at all would be impossible.
1109
1110
1111Our goal is to build a practical 100% free software distribution of
1112Linux-based and other variants of GNU, with a focus on the promotion and
1113tight integration of GNU components, and an emphasis on programs and
1114tools that help users exert that freedom.
1115
1116Building this distribution is a cooperative effort, and you are invited
1117to join! Please get in touch with us on @email{bug-guix@@gnu.org}. We
1118welcome ideas, bug reports, patches, and anything that may be helpful to
1119the project.
1120
c78bd12b 1121
568717fd
LC
1122@c *********************************************************************
1123@node Acknowledgments
1124@chapter Acknowledgments
1125
1126Guix is based on the Nix package manager, which was designed and
1127implemented by Eelco Dolstra. Nix pioneered functional package
1128management, and promoted unprecedented features, such as transactional
1129package upgrades and rollbacks, per-user profiles, and referentially
1130transparent build processes. Without this work, Guix would not exist.
1131
1132The Nix-based software distributions, Nixpkgs and NixOS, have also been
1133an inspiration for Guix.
1134
1135@c *********************************************************************
1136@node GNU Free Documentation License
1137@appendix GNU Free Documentation License
1138
1139@include fdl-1.3.texi
1140
1141@c *********************************************************************
1142@node Concept Index
1143@unnumbered Concept Index
1144@printindex cp
1145
1146@node Function Index
1147@unnumbered Function Index
1148@printindex fn
1149
1150@bye
1151
1152@c Local Variables:
1153@c ispell-local-dictionary: "american";
1154@c End: