gnu: subversion: Update to 1.7.14.
[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
7df7a74e
NK
11
12@copying
425b0bfc 13Copyright @copyright{} 2012, 2013, 2014 Ludovic Courtès@*
7df7a74e
NK
14Copyright @copyright{} 2013 Andreas Enge@*
15Copyright @copyright{} 2013 Nikita Karetnikov
16
17Permission is granted to copy, distribute and/or modify this document
18under the terms of the GNU Free Documentation License, Version 1.3 or
19any later version published by the Free Software Foundation; with no
20Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
21copy of the license is included in the section entitled ``GNU Free
22Documentation License''.
23@end copying
568717fd 24
eeaf4427 25@dircategory Package management
568717fd
LC
26@direntry
27* guix: (guix). Guix, the functional package manager.
e49951eb 28* guix package: (guix)Invoking guix package
eeaf4427 29 Managing packages with Guix.
e49951eb 30* guix build: (guix)Invoking guix build
568717fd
LC
31 Building packages with Guix.
32@end direntry
568717fd
LC
33
34@titlepage
7730d112
LC
35@title GNU Guix Reference Manual
36@subtitle Using the GNU Guix Functional Package Manager
568717fd 37@author Ludovic Courtès
da7cabd4 38@author Andreas Enge
acc08466 39@author Nikita Karetnikov
568717fd
LC
40
41@page
42@vskip 0pt plus 1filll
43Edition @value{EDITION} @*
44@value{UPDATED} @*
45
7df7a74e 46@insertcopying
568717fd
LC
47@end titlepage
48
568717fd
LC
49@contents
50
51@c *********************************************************************
52@node Top
f8348b91 53@top GNU Guix
568717fd 54
f8348b91
LC
55This document describes GNU Guix version @value{VERSION}, a functional
56package management tool written for the GNU system.
568717fd
LC
57
58@menu
59* Introduction:: What is Guix about?
bd5e766b 60* Installation:: Installing Guix.
eeaf4427 61* Package Management:: Package installation, upgrade, etc.
568717fd
LC
62* Programming Interface:: Using Guix in Scheme.
63* Utilities:: Package management commands.
a1ba8475 64* GNU Distribution:: Software for your friendly GNU system.
9bf3c1a7 65* Contributing:: Your help needed!
568717fd
LC
66
67* Acknowledgments:: Thanks!
68* GNU Free Documentation License:: The license of this manual.
69* Concept Index:: Concepts.
70* Function Index:: Functions.
71@end menu
72
73@c *********************************************************************
74@node Introduction
75@chapter Introduction
76
c80e7e55
LC
77GNU Guix@footnote{``Guix'' is pronounced like ``geeks'', or ``ɡiːks''
78using the international phonetic alphabet (IPA).} is a functional
79package management tool for the GNU system. Package management consists
4bfc4ea3
NK
80of all activities that relate to building packages from sources,
81honoring their build-time and run-time dependencies,
c80e7e55
LC
82installing packages in user environments, upgrading installed packages
83to new versions or rolling back to a previous set, removing unused
84software packages, etc.
568717fd
LC
85
86@cindex functional package management
87The term @dfn{functional} refers to a specific package management
88discipline. In Guix, the package build and installation process is seen
4bfc4ea3
NK
89as a function, in the mathematical sense. That function takes inputs,
90such as build scripts, a compiler, and libraries, and
91returns an installed package. As a pure function, its result depends
568717fd
LC
92solely on its inputs---for instance, it cannot refer to software or
93scripts that were not explicitly passed as inputs. A build function
4bfc4ea3
NK
94always produces the same result when passed a given set of inputs. It
95cannot alter the system's environment in
568717fd
LC
96any way; for instance, it cannot create, modify, or delete files outside
97of its build and installation directories. This is achieved by running
e900c503 98build processes in isolated environments (or @dfn{containers}), where only their
4bfc4ea3 99explicit inputs are visible.
568717fd 100
e531ac2a 101@cindex store
568717fd 102The result of package build functions is @dfn{cached} in the file
e531ac2a
LC
103system, in a special directory called @dfn{the store} (@pxref{The
104Store}). Each package is installed in a directory of its own, in the
568717fd
LC
105store---by default under @file{/nix/store}. The directory name contains
106a hash of all the inputs used to build that package; thus, changing an
107input yields a different directory name.
108
109This approach is the foundation of Guix's salient features: support for
4bfc4ea3 110transactional package upgrade and rollback, per-user installation, and
eeaf4427 111garbage collection of packages (@pxref{Features}).
568717fd 112
4bfc4ea3 113Guix has a command-line interface, which allows users to build, install,
568717fd 114upgrade, and remove packages, as well as a Scheme programming interface.
568717fd 115
a1ba8475
LC
116Last but not least, Guix is used to build a distribution of the GNU
117system, with many GNU and non-GNU free software packages. @xref{GNU
118Distribution}.
119
bd5e766b
LC
120@c *********************************************************************
121@node Installation
122@chapter Installation
123
48febeb8
LC
124GNU Guix is available for download from its website at
125@url{http://www.gnu.org/software/guix/}. This section describes the
126software requirements of Guix, as well as how to install it and get
127ready to use it.
bd5e766b 128
b22a12fd 129The build procedure for Guix is the same as for other GNU software, and
1da983b9 130is not covered here. Please see the files @file{README} and
b22a12fd
LC
131@file{INSTALL} in the Guix source tree for additional details.
132
bd5e766b
LC
133@menu
134* Requirements:: Software needed to build and run Guix.
135* Setting Up the Daemon:: Preparing the build daemon's environment.
136* Invoking guix-daemon:: Running the build daemon.
137@end menu
138
139@node Requirements
140@section Requirements
141
142GNU Guix depends on the following packages:
143
144@itemize
4a328f73 145@item @url{http://gnu.org/software/guile/, GNU Guile}, version 2.0.5 or later;
bd5e766b
LC
146@item @url{http://gnupg.org/, GNU libgcrypt}
147@end itemize
148
149Unless @code{--disable-daemon} was passed to @command{configure}, the
150following packages are also needed:
151
152@itemize
153@item @url{http://sqlite.org, SQLite 3}
154@item @url{http://www.bzip.org, libbz2}
155@item @url{http://gcc.gnu.org, GCC's g++}
156@end itemize
157
4bfc4ea3
NK
158When a working installation of @url{http://nixos.org/nix/, the Nix package
159manager} is available, you
bd5e766b 160can instead configure Guix with @code{--disable-daemon}. In that case,
4bfc4ea3 161Nix replaces the three dependencies above.
bd5e766b 162
b22a12fd
LC
163Guix is compatible with Nix, so it is possible to share the same store
164between both. To do so, you must pass @command{configure} not only the
165same @code{--with-store-dir} value, but also the same
4bfc4ea3
NK
166@code{--localstatedir} value. The latter is essential because it
167specifies where the database that stores metadata about the store is
168located, among other things. The default values are
b22a12fd 169@code{--with-store-dir=/nix/store} and @code{--localstatedir=/nix/var}.
4bfc4ea3
NK
170Note that @code{--disable-daemon} is not required if
171your goal is to share the store with Nix.
b22a12fd 172
bd5e766b
LC
173@node Setting Up the Daemon
174@section Setting Up the Daemon
175
176@cindex daemon
177Operations such as building a package or running the garbage collector
49e6291a 178are all performed by a specialized process, the @dfn{build daemon}, on
bd5e766b
LC
179behalf of clients. Only the daemon may access the store and its
180associated database. Thus, any operation that manipulates the store
181goes through the daemon. For instance, command-line tools such as
e49951eb 182@command{guix package} and @command{guix build} communicate with the
bd5e766b
LC
183daemon (@i{via} remote procedure calls) to instruct it what to do.
184
49e6291a
LC
185The following sections explain how to prepare the build daemon's
186environment.
187
188@menu
189* Build Environment Setup:: Preparing the isolated build environment.
190* Daemon Offload Setup:: Offloading builds to remote machines.
191@end menu
192
193@node Build Environment Setup
194@subsection Build Environment Setup
195
bd5e766b
LC
196In a standard multi-user setup, Guix and its daemon---the
197@command{guix-daemon} program---are installed by the system
198administrator; @file{/nix/store} is owned by @code{root} and
199@command{guix-daemon} runs as @code{root}. Unprivileged users may use
200Guix tools to build packages or otherwise access the store, and the
201daemon will do it on their behalf, ensuring that the store is kept in a
202consistent state, and allowing built packages to be shared among users.
203
204@cindex build users
205When @command{guix-daemon} runs as @code{root}, you may not want package
206build processes themselves to run as @code{root} too, for obvious
207security reasons. To avoid that, a special pool of @dfn{build users}
208should be created for use by build processes started by the daemon.
209These build users need not have a shell and a home directory: they will
210just be used when the daemon drops @code{root} privileges in build
211processes. Having several such users allows the daemon to launch
212distinct build processes under separate UIDs, which guarantees that they
213do not interfere with each other---an essential feature since builds are
214regarded as pure functions (@pxref{Introduction}).
215
216On a GNU/Linux system, a build user pool may be created like this (using
217Bash syntax and the @code{shadow} commands):
218
091196b3
LC
219@c See http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html
220@c for why `-G' is needed.
bd5e766b
LC
221@example
222# groupadd guix-builder
223# for i in `seq 1 10`;
224 do
091196b3
LC
225 useradd -g guix-builder -G guix-builder \
226 -d /var/empty -s `which nologin` \
4d1a2b50
LC
227 -c "Guix build user $i" --system \
228 guix-builder$i;
bd5e766b
LC
229 done
230@end example
231
232@noindent
233The @code{guix-daemon} program may then be run as @code{root} with:
234
235@example
236# guix-daemon --build-users-group=guix-builder
237@end example
238
e900c503 239@cindex chroot
b095792f
LC
240@noindent
241This way, the daemon starts build processes in a chroot, under one of
242the @code{guix-builder} users. On GNU/Linux, by default, the chroot
6dc99317
LC
243environment contains nothing but:
244
245@c Keep this list in sync with libstore/build.cc! -----------------------
246@itemize
247@item
248the @code{/dev} and @code{/proc} directories@footnote{On some systems
249@code{/dev/shm}, which supports shared memory, is a symlink to another
250directory such as @code{/run/shm}, that is @emph{not} is the chroot.
251When that is the case, shared memory support is unavailable in the
252chroot environment. The workaround is to make sure that @file{/dev/shm}
253is directly a @code{tmpfs} mount point.};
254
255@item
256@file{/etc/passwd} with an entry for the current user and an entry for
257user @file{nobody};
258
259@item
260@file{/etc/group} with an entry for the user's group;
261
262@item
263@file{/etc/hosts} with an entry that maps @code{localhost} to
264@code{127.0.0.1};
265
266@item
267a writable @file{/tmp} directory.
268@end itemize
b095792f 269
d43eb499 270If you are installing Guix as an unprivileged user, it is still
bd5e766b
LC
271possible to run @command{guix-daemon}. However, build processes will
272not be isolated from one another, and not from the rest of the system.
273Thus, build processes may interfere with each other, and may access
274programs, libraries, and other files available on the system---making it
275much harder to view them as @emph{pure} functions.
276
49e6291a
LC
277
278@node Daemon Offload Setup
279@subsection Using the Offload Facility
280
281@cindex offloading
4ec2e92d
LC
282@cindex build hook
283When desired, the build daemon can @dfn{offload}
284derivation builds to other machines
49e6291a
LC
285running Guix, using the @code{offload} @dfn{build hook}. When that
286feature is enabled, a list of user-specified build machines is read from
287@file{/etc/guix/machines.scm}; anytime a build is requested, for
288instance via @code{guix build}, the daemon attempts to offload it to one
289of the machines that satisfies the derivation's constraints, in
290particular its system type---e.g., @file{x86_64-linux}. Missing
291prerequisites for the build are copied over SSH to the target machine,
292which then proceeds with the build; upon success the output(s) of the
293build are copied back to the initial machine.
294
4ec2e92d 295The @file{/etc/guix/machines.scm} file typically looks like this:
49e6291a
LC
296
297@example
298(list (build-machine
299 (name "eightysix.example.org")
300 (system "x86_64-linux")
301 (user "bob")
302 (speed 2.)) ; incredibly fast!
303
304 (build-machine
305 (name "meeps.example.org")
306 (system "mips64el-linux")
307 (user "alice")
308 (private-key
309 (string-append (getenv "HOME")
310 "/.ssh/id-rsa-for-guix"))))
311@end example
312
313@noindent
314In the example above we specify a list of two build machines, one for
315the @code{x86_64} architecture and one for the @code{mips64el}
4ec2e92d
LC
316architecture.
317
318In fact, this file is---not surprisingly!---a Scheme file that is
319evaluated when the @code{offload} hook is started. Its return value
320must be a list of @code{build-machine} objects. While this example
321shows a fixed list of build machines, one could imagine, say, using
322DNS-SD to return a list of potential build machines discovered in the
323local network (@pxref{Introduction, Guile-Avahi,, guile-avahi, Using
324Avahi in Guile Scheme Programs}).
325
326The compulsory fields for a @code{build-machine} declaration are:
49e6291a
LC
327
328@table @code
329
330@item name
331The remote machine's host name.
332
333@item system
334The remote machine's system type.
335
336@item user
337The user account to use when connecting to the remote machine over SSH.
338Note that the SSH key pair must @emph{not} be passphrase-protected, to
339allow non-interactive logins.
340
341@end table
342
343@noindent
4ec2e92d 344A number of optional fields may be specified:
49e6291a
LC
345
346@table @code
347
348@item private-key
349The SSH private key file to use when connecting to the machine.
350
351@item parallel-builds
352The number of builds that may run in parallel on the machine (1 by
353default.)
354
355@item speed
356A ``relative speed factor''. The offload scheduler will tend to prefer
357machines with a higher speed factor.
358
359@item features
360A list of strings denoting specific features supported by the machine.
361An example is @code{"kvm"} for machines that have the KVM Linux modules
362and corresponding hardware support. Derivations can request features by
363name, and they will be scheduled on matching build machines.
364
365@end table
366
367The @code{guix} command must be in the search path on the build
368machines, since offloading works by invoking the @code{guix archive} and
369@code{guix build} commands.
370
371There's one last thing to do once @file{machines.scm} is in place. As
372explained above, when offloading, files are transferred back and forth
373between the machine stores. For this to work, you need to generate a
374key pair to allow the daemon to export signed archives of files from the
375store (@pxref{Invoking guix archive}):
376
377@example
378# guix archive --generate-key
379@end example
380
381@noindent
382Thus, when receiving files, a machine's build daemon can make sure they
383are genuine, have not been tampered with, and that they are signed by an
384authorized key.
385
386
bd5e766b
LC
387@node Invoking guix-daemon
388@section Invoking @command{guix-daemon}
389
390The @command{guix-daemon} program implements all the functionality to
391access the store. This includes launching build processes, running the
392garbage collector, querying the availability of a build result, etc. It
393is normally run as @code{root} like this:
394
395@example
396# guix-daemon --build-users-group=guix-builder
397@end example
398
399@noindent
400For details on how to set it up, @ref{Setting Up the Daemon}.
401
e900c503
LC
402@cindex chroot
403@cindex container, build environment
404@cindex build environment
405@cindex reproducible builds
bd5e766b
LC
406By default, @command{guix-daemon} launches build processes under
407different UIDs, taken from the build group specified with
408@code{--build-users-group}. In addition, each build process is run in a
409chroot environment that only contains the subset of the store that the
410build process depends on, as specified by its derivation
411(@pxref{Programming Interface, derivation}), plus a set of specific
412system directories. By default, the latter contains @file{/dev} and
e900c503
LC
413@file{/dev/pts}. Furthermore, on GNU/Linux, the build environment is a
414@dfn{container}: in addition to having its own file system tree, it has
415a separate mount name space, its own PID name space, network name space,
416etc. This helps achieve reproducible builds (@pxref{Features}).
bd5e766b
LC
417
418The following command-line options are supported:
419
420@table @code
421@item --build-users-group=@var{group}
422Take users from @var{group} to run build processes (@pxref{Setting Up
423the Daemon, build users}).
424
6858f9d1 425@item --no-substitutes
b5385b52 426@cindex substitutes
6858f9d1
LC
427Do not use substitutes for build products. That is, always build things
428locally instead of allowing downloads of pre-built binaries.
429
b5385b52
LC
430By default substitutes are used, unless the client---such as the
431@command{guix package} command---is explicitly invoked with
432@code{--no-substitutes}.
433
434When the daemon runs with @code{--no-substitutes}, clients can still
435explicitly enable substitution @i{via} the @code{set-build-options}
436remote procedure call (@pxref{The Store}).
437
4ec2e92d
LC
438@cindex build hook
439@item --no-build-hook
440Do not use the @dfn{build hook}.
441
442The build hook is a helper program that the daemon can start and to
443which it submits build requests. This mechanism is used to offload
444builds to other machines (@pxref{Daemon Offload Setup}).
445
bd5e766b
LC
446@item --cache-failures
447Cache build failures. By default, only successful builds are cached.
448
449@item --cores=@var{n}
450@itemx -c @var{n}
451Use @var{n} CPU cores to build each derivation; @code{0} means as many
452as available.
453
454The default value is @code{1}, but it may be overridden by clients, such
e49951eb
MW
455as the @code{--cores} option of @command{guix build} (@pxref{Invoking
456guix build}).
bd5e766b
LC
457
458The effect is to define the @code{NIX_BUILD_CORES} environment variable
459in the build process, which can then use it to exploit internal
460parallelism---for instance, by running @code{make -j$NIX_BUILD_CORES}.
461
462@item --max-jobs=@var{n}
463@itemx -M @var{n}
464Allow at most @var{n} build jobs in parallel. The default value is
465@code{1}.
466
467@item --debug
468Produce debugging output.
469
470This is useful to debug daemon start-up issues, but then it may be
471overridden by clients, for example the @code{--verbosity} option of
e49951eb 472@command{guix build} (@pxref{Invoking guix build}).
bd5e766b
LC
473
474@item --chroot-directory=@var{dir}
475Add @var{dir} to the build chroot.
476
477Doing this may change the result of build processes---for instance if
478they use optional dependencies found in @var{dir} when it is available,
479and not otherwise. For that reason, it is not recommended to do so.
480Instead, make sure that each derivation declares all the inputs that it
481needs.
482
483@item --disable-chroot
484Disable chroot builds.
485
486Using this option is not recommended since, again, it would allow build
487processes to gain access to undeclared dependencies.
488
489@item --disable-log-compression
490Disable compression of the build logs.
491
1da983b9
LC
492Unless @code{--lose-logs} is used, all the build logs are kept in the
493@var{localstatedir}. To save space, the daemon automatically compresses
494them with bzip2 by default. This option disables that.
495
bd5e766b
LC
496@item --disable-store-optimization
497Disable automatic file ``deduplication'' in the store.
498
1da983b9
LC
499By default, files added to the store are automatically ``deduplicated'':
500if a newly added file is identical as another one found in the store,
501the daemon makes the new file a hard link to the other file. This
502slightly increases the input/output load at the end of a build process.
503This option disables this.
504
6e37066e
LC
505@item --gc-keep-outputs[=yes|no]
506Tell whether the garbage collector (GC) must keep outputs of live
507derivations.
508
509When set to ``yes'', the GC will keep the outputs of any live derivation
510available in the store---the @code{.drv} files. The default is ``no'',
511meaning that derivation outputs are kept only if they are GC roots.
512
513@item --gc-keep-derivations[=yes|no]
514Tell whether the garbage collector (GC) must keep derivations
515corresponding to live outputs.
516
517When set to ``yes'', as is the case by default, the GC keeps
518derivations---i.e., @code{.drv} files---as long as at least one of their
519outputs is live. This allows users to keep track of the origins of
520items in their store. Setting it to ``no'' saves a bit of disk space.
521
522Note that when both @code{--gc-keep-derivations} and
523@code{--gc-keep-outputs} are used, the effect is to keep all the build
524prerequisites (the sources, compiler, libraries, and other build-time
525tools) of live objects in the store, regardless of whether these
526prerequisites are live. This is convenient for developers since it
527saves rebuilds or downloads.
528
bd5e766b
LC
529@item --impersonate-linux-2.6
530On Linux-based systems, impersonate Linux 2.6. This means that the
531kernel's @code{uname} system call will report 2.6 as the release number.
532
533This might be helpful to build programs that (usually wrongfully) depend
534on the kernel version number.
535
536@item --lose-logs
537Do not keep build logs. By default they are kept under
538@code{@var{localstatedir}/nix/log}.
539
540@item --system=@var{system}
541Assume @var{system} as the current system type. By default it is the
542architecture/kernel pair found at configure time, such as
543@code{x86_64-linux}.
b8d2aa26
LC
544
545@item --listen=@var{socket}
546Listen for connections on @var{socket}, the file name of a Unix-domain
547socket. The default socket is
548@file{@var{localstatedir}/daemon-socket/socket}. This option is only
549useful in exceptional circumstances, such as if you need to run several
550daemons on the same machine.
bd5e766b
LC
551@end table
552
553
eeaf4427
LC
554@c *********************************************************************
555@node Package Management
556@chapter Package Management
557
f8348b91 558The purpose of GNU Guix is to allow users to easily install, upgrade, and
eeaf4427
LC
559remove software packages, without having to know about their build
560procedure or dependencies. Guix also goes beyond this obvious set of
561features.
562
563This chapter describes the main features of Guix, as well as the package
564management tools it provides.
565
566@menu
567* Features:: How Guix will make your life brighter.
e49951eb 568* Invoking guix package:: Package installation, removal, etc.
760c60d6 569* Packages with Multiple Outputs:: Single source package, multiple outputs.
e49951eb 570* Invoking guix gc:: Running the garbage collector.
f651b477 571* Invoking guix pull:: Fetching the latest Guix and distribution.
760c60d6 572* Invoking guix archive:: Exporting and importing store files.
eeaf4427
LC
573@end menu
574
575@node Features
576@section Features
577
578When using Guix, each package ends up in the @dfn{package store}, in its
579own directory---something that resembles
580@file{/nix/store/xxx-package-1.2}, where @code{xxx} is a base32 string.
581
582Instead of referring to these directories, users have their own
583@dfn{profile}, which points to the packages that they actually want to
821b0015
LC
584use. These profiles are stored within each user's home directory, at
585@code{$HOME/.guix-profile}.
eeaf4427 586
821b0015 587For example, @code{alice} installs GCC 4.7.2. As a result,
eeaf4427 588@file{/home/alice/.guix-profile/bin/gcc} points to
821b0015
LC
589@file{/nix/store/@dots{}-gcc-4.7.2/bin/gcc}. Now, on the same machine,
590@code{bob} had already installed GCC 4.8.0. The profile of @code{bob}
591simply continues to point to
592@file{/nix/store/@dots{}-gcc-4.8.0/bin/gcc}---i.e., both versions of GCC
593coexist on the same system without any interference.
eeaf4427 594
e49951eb
MW
595The @command{guix package} command is the central tool to manage
596packages (@pxref{Invoking guix package}). It operates on those per-user
821b0015 597profiles, and can be used @emph{with normal user privileges}.
eeaf4427
LC
598
599The command provides the obvious install, remove, and upgrade
600operations. Each invocation is actually a @emph{transaction}: either
ba55b1cb 601the specified operation succeeds, or nothing happens. Thus, if the
e49951eb 602@command{guix package} process is terminated during the transaction,
eeaf4427
LC
603or if a power outage occurs during the transaction, then the user's
604profile remains in its previous state, and remains usable.
605
606In addition, any package transaction may be @emph{rolled back}. So, if,
607for example, an upgrade installs a new version of a package that turns
608out to have a serious bug, users may roll back to the previous instance
4af2447e
LC
609of their profile, which was known to work well. Similarly, the global
610system configuration is subject to transactional upgrades and roll-back
611(@pxref{Using the Configuration System}).
eeaf4427
LC
612
613All those packages in the package store may be @emph{garbage-collected}.
614Guix can determine which packages are still referenced by the user
fe8ff028 615profiles, and remove those that are provably no longer referenced
e49951eb 616(@pxref{Invoking guix gc}). Users may also explicitly remove old
fe8ff028
LC
617generations of their profile so that the packages they refer to can be
618collected.
eeaf4427 619
e900c503
LC
620@cindex reproducibility
621@cindex reproducible builds
eeaf4427
LC
622Finally, Guix takes a @dfn{purely functional} approach to package
623management, as described in the introduction (@pxref{Introduction}).
624Each @file{/nix/store} package directory name contains a hash of all the
625inputs that were used to build that package---compiler, libraries, build
626scripts, etc. This direct correspondence allows users to make sure a
627given package installation matches the current state of their
e900c503
LC
628distribution. It also helps maximize @dfn{build reproducibility}:
629thanks to the isolated build environments that are used, a given build
630is likely to yield bit-identical files when performed on different
631machines (@pxref{Invoking guix-daemon, container}).
eeaf4427 632
18f2887b 633@cindex substitute
eeaf4427 634This foundation allows Guix to support @dfn{transparent binary/source
3b78d1ea 635deployment}. When a pre-built binary for a @file{/nix/store} path is
18f2887b
LC
636available from an external source---a @dfn{substitute}, Guix just
637downloads it@footnote{@c XXX: Remove me when outdated.
638As of version @value{VERSION}, substitutes are downloaded from
639@url{http://hydra.gnu.org/} but are @emph{not} authenticated---i.e.,
640Guix cannot tell whether binaries it downloaded have been tampered with,
641nor whether they come from the genuine @code{gnu.org} build farm. This
642will be fixed in future versions. In the meantime, concerned users can
643opt for @code{--no-substitutes} (@pxref{Invoking guix-daemon}).};
644otherwise, it builds the package from source, locally.
eeaf4427 645
e49951eb
MW
646@node Invoking guix package
647@section Invoking @command{guix package}
eeaf4427 648
e49951eb 649The @command{guix package} command is the tool that allows users to
eeaf4427
LC
650install, upgrade, and remove packages, as well as rolling back to
651previous configurations. It operates only on the user's own profile,
652and works with normal user privileges (@pxref{Features}). Its syntax
653is:
654
655@example
e49951eb 656guix package @var{options}
eeaf4427
LC
657@end example
658
ba55b1cb 659Primarily, @var{options} specifies the operations to be performed during
eeaf4427
LC
660the transaction. Upon completion, a new profile is created, but
661previous generations of the profile remain available, should the user
662want to roll back.
663
6447738c
MW
664For example, to remove @code{lua} and install @code{guile} and
665@code{guile-cairo} in a single transaction:
666
667@example
668guix package -r lua -i guile guile-cairo
669@end example
670
b9e5c0a9 671For each user, a symlink to the user's default profile is automatically
0ec1af59 672created in @file{$HOME/.guix-profile}. This symlink always points to the
b9e5c0a9
LC
673current generation of the user's default profile. Thus, users can add
674@file{$HOME/.guix-profile/bin} to their @code{PATH} environment
675variable, and so on.
676
0ec1af59
LC
677In a multi-user setup, user profiles must be stored in a place
678registered as a @dfn{garbage-collector root}, which
e49951eb 679@file{$HOME/.guix-profile} points to (@pxref{Invoking guix gc}). That
0ec1af59
LC
680directory is normally
681@code{@var{localstatedir}/profiles/per-user/@var{user}}, where
682@var{localstatedir} is the value passed to @code{configure} as
683@code{--localstatedir}, and @var{user} is the user name. It must be
684created by @code{root}, with @var{user} as the owner. When it does not
70c43291
LC
685exist, or is not owned by @var{user}, @command{guix package} emits an
686error about it.
0ec1af59
LC
687
688The @var{options} can be among the following:
689
eeaf4427
LC
690@table @code
691
6447738c
MW
692@item --install=@var{package} @dots{}
693@itemx -i @var{package} @dots{}
694Install the specified @var{package}s.
eeaf4427 695
6447738c 696Each @var{package} may specify either a simple package name, such as
eeaf4427 697@code{guile}, or a package name followed by a hyphen and version number,
dc5669cd
MW
698such as @code{guile-1.8.8}. If no version number is specified, the
699newest available version will be selected. In addition, @var{package}
700may contain a colon, followed by the name of one of the outputs of the
6e721c4d
LC
701package, as in @code{gcc:doc} or @code{binutils-2.22:lib}
702(@pxref{Packages with Multiple Outputs}).
eeaf4427 703
461572cc
LC
704@cindex propagated inputs
705Sometimes packages have @dfn{propagated inputs}: these are dependencies
706that automatically get installed along with the required package.
707
708An example is the GNU MPC library: its C header files refer to those of
709the GNU MPFR library, which in turn refer to those of the GMP library.
710Thus, when installing MPC, the MPFR and GMP libraries also get installed
711in the profile; removing MPC also removes MPFR and GMP---unless they had
712also been explicitly installed independently.
713
ba7ea5ce 714Besides, packages sometimes rely on the definition of environment
5924080d 715variables for their search paths (see explanation of
ba7ea5ce 716@code{--search-paths} below). Any missing or possibly incorrect
5924080d
LC
717environment variable definitions are reported here.
718
ef010c0f 719@c XXX: keep me up-to-date
5924080d 720Finally, when installing a GNU package, the tool reports the
ef010c0f
LC
721availability of a newer upstream version. In the future, it may provide
722the option of installing directly from the upstream version, even if
723that version is not yet in the distribution.
724
5d4b411f
LC
725@item --install-from-expression=@var{exp}
726@itemx -e @var{exp}
727Install the package @var{exp} evaluates to.
728
729@var{exp} must be a Scheme expression that evaluates to a
730@code{<package>} object. This option is notably useful to disambiguate
731between same-named variants of a package, with expressions such as
732@code{(@@ (gnu packages base) guile-final)}.
733
734Note that this option installs the first output of the specified
735package, which may be insufficient when needing a specific output of a
736multiple-output package.
737
6447738c
MW
738@item --remove=@var{package} @dots{}
739@itemx -r @var{package} @dots{}
740Remove the specified @var{package}s.
eeaf4427 741
6447738c 742As for @code{--install}, each @var{package} may specify a version number
13ed095c
LC
743and/or output name in addition to the package name. For instance,
744@code{-r glibc:debug} would remove the @code{debug} output of
745@code{glibc}.
746
6447738c
MW
747@item --upgrade[=@var{regexp} @dots{}]
748@itemx -u [@var{regexp} @dots{}]
749Upgrade all the installed packages. If one or more @var{regexp}s are
750specified, upgrade only installed packages whose name matches a
751@var{regexp}.
eeaf4427 752
f651b477
LC
753Note that this upgrades package to the latest version of packages found
754in the distribution currently installed. To update your distribution,
755you should regularly run @command{guix pull} (@pxref{Invoking guix
756pull}).
757
24e262f0
LC
758@item --roll-back
759Roll back to the previous @dfn{generation} of the profile---i.e., undo
760the last transaction.
761
762When combined with options such as @code{--install}, roll back occurs
763before any other actions.
764
d9307267 765When rolling back from the first generation that actually contains
4b2bc804
NK
766installed packages, the profile is made to point to the @dfn{zeroth
767generation}, which contains no files apart from its own meta-data.
d9307267 768
82fe08ed
LC
769Installing, removing, or upgrading packages from a generation that has
770been rolled back to overwrites previous future generations. Thus, the
771history of a profile's generations is always linear.
772
5924080d
LC
773@item --search-paths
774@cindex search paths
775Report environment variable definitions, in Bash syntax, that may be
776needed in order to use the set of installed packages. These environment
777variables are used to specify @dfn{search paths} for files used by some
778of the installed packages.
779
780For example, GCC needs the @code{CPATH} and @code{LIBRARY_PATH}
781environment variables to be defined so it can look for headers and
782libraries in the user's profile (@pxref{Environment Variables,,, gcc,
783Using the GNU Compiler Collection (GCC)}). If GCC and, say, the C
784library are installed in the profile, then @code{--search-paths} will
785suggest setting these variables to @code{@var{profile}/include} and
786@code{@var{profile}/lib}, respectively.
787
eeaf4427
LC
788@item --profile=@var{profile}
789@itemx -p @var{profile}
790Use @var{profile} instead of the user's default profile.
791
792@item --dry-run
793@itemx -n
794Show what would be done without actually doing it.
795
56b1f4b7
LC
796@item --fallback
797When substituting a pre-built binary fails, fall back to building
798packages locally.
799
3b824605 800@item --no-substitutes
b5385b52
LC
801Do not use substitutes for build products. That is, always build things
802locally instead of allowing downloads of pre-built binaries.
803
804@item --max-silent-time=@var{seconds}
969e678e 805Same as for @command{guix build} (@pxref{Invoking guix build}).
3b824605 806
70915c1a
LC
807@item --verbose
808Produce verbose output. In particular, emit the environment's build log
809on the standard error port.
810
eeaf4427
LC
811@item --bootstrap
812Use the bootstrap Guile to build the profile. This option is only
813useful to distribution developers.
814
815@end table
816
e49951eb 817In addition to these actions @command{guix package} supports the
733b4130
LC
818following options to query the current state of a profile, or the
819availability of packages:
eeaf4427 820
733b4130
LC
821@table @option
822
acc08466
NK
823@item --search=@var{regexp}
824@itemx -s @var{regexp}
825List the available packages whose synopsis or description matches
299112d3
LC
826@var{regexp}. Print all the meta-data of matching packages in
827@code{recutils} format (@pxref{Top, GNU recutils databases,, recutils,
828GNU recutils manual}).
acc08466 829
299112d3
LC
830This allows specific fields to be extracted using the @command{recsel}
831command, for instance:
832
833@example
e49951eb 834$ guix package -s malloc | recsel -p name,version
299112d3
LC
835name: glibc
836version: 2.17
837
838name: libgc
839version: 7.2alpha6
840@end example
acc08466 841
733b4130
LC
842@item --list-installed[=@var{regexp}]
843@itemx -I [@var{regexp}]
bd9bde1c
LC
844List the currently installed packages in the specified profile, with the
845most recently installed packages shown last. When @var{regexp} is
846specified, list only installed packages whose name matches @var{regexp}.
733b4130
LC
847
848For each installed package, print the following items, separated by
849tabs: the package name, its version string, the part of the package that
850is installed (for instance, @code{out} for the default output,
851@code{include} for its headers, etc.), and the path of this package in
852the store.
853
64fc89b6
LC
854@item --list-available[=@var{regexp}]
855@itemx -A [@var{regexp}]
a1ba8475
LC
856List packages currently available in the software distribution
857(@pxref{GNU Distribution}). When @var{regexp} is specified, list only
858installed packages whose name matches @var{regexp}.
64fc89b6
LC
859
860For each package, print the following items separated by tabs: its name,
6e721c4d
LC
861its version string, the parts of the package (@pxref{Packages with
862Multiple Outputs}), and the source location of its definition.
64fc89b6 863
f566d765
LC
864@item --list-generations[=@var{pattern}]
865@itemx -l [@var{pattern}]
866Return a list of generations along with their creation dates; for each
867generation, show the installed packages, with the most recently
4b2bc804
NK
868installed packages shown last. Note that the zeroth generation is never
869shown.
f566d765
LC
870
871For each installed package, print the following items, separated by
872tabs: the name of a package, its version string, the part of the package
873that is installed (@pxref{Packages with Multiple Outputs}), and the
874location of this package in the store.
875
876When @var{pattern} is used, the command returns only matching
877generations. Valid patterns include:
878
879@itemize
880@item @emph{Integers and comma-separated integers}. Both patterns denote
881generation numbers. For instance, @code{--list-generations=1} returns
882the first one.
883
884And @code{--list-generations=1,8,2} outputs three generations in the
885specified order. Neither spaces nor trailing commas are allowed.
886
887@item @emph{Ranges}. @code{--list-generations=2..9} prints the
888specified generations and everything in between. Note that the start of
889a range must be lesser than its end.
890
891It is also possible to omit the endpoint. For example,
892@code{--list-generations=2..}, returns all generations starting from the
893second one.
894
895@item @emph{Durations}. You can also get the last @emph{N}@tie{}days, weeks,
896or months by passing an integer along with the first letter of the
d7ddb257
LC
897duration. For example, @code{--list-generations=20d} lists generations
898that are up to 20 days old.
f566d765
LC
899@end itemize
900
b7884ca3
NK
901@item --delete-generations[=@var{pattern}]
902@itemx -d [@var{pattern}]
d7ddb257
LC
903When @var{pattern} is omitted, delete all generations except the current
904one.
b7884ca3
NK
905
906This command accepts the same patterns as @option{--list-generations}.
d7ddb257
LC
907When @var{pattern} is specified, delete the matching generations. When
908@var{pattern} specifies a duration, generations @emph{older} than the
909specified duration match. For instance, @code{--delete-generations=1m}
910deletes generations that are more than one month old.
911
912If the current generation matches, it is deleted atomically---i.e., by
913switching to the previous available generation. Note that the zeroth
914generation is never deleted.
b7884ca3 915
1bb9900a
LC
916Note that deleting generations prevents roll-back to them.
917Consequently, this command must be used with care.
918
733b4130 919@end table
eeaf4427 920
6e721c4d
LC
921@node Packages with Multiple Outputs
922@section Packages with Multiple Outputs
923
924@cindex multiple-output packages
925@cindex package outputs
926
927Often, packages defined in Guix have a single @dfn{output}---i.e., the
928source package leads exactly one directory in the store. When running
929@command{guix package -i glibc}, one installs the default output of the
930GNU libc package; the default output is called @code{out}, but its name
931can be omitted as shown in this command. In this particular case, the
932default output of @code{glibc} contains all the C header files, shared
933libraries, static libraries, Info documentation, and other supporting
934files.
935
936Sometimes it is more appropriate to separate the various types of files
937produced from a single source package into separate outputs. For
938instance, the GLib C library (used by GTK+ and related packages)
939installs more than 20 MiB of reference documentation as HTML pages.
940To save space for users who do not need it, the documentation goes to a
941separate output, called @code{doc}. To install the main GLib output,
942which contains everything but the documentation, one would run:
943
944@example
945guix package -i glib
946@end example
947
948The command to install its documentation is:
949
950@example
951guix package -i glib:doc
952@end example
953
954Some packages install programs with different ``dependency footprints''.
955For instance, the WordNet package install both command-line tools and
956graphical user interfaces (GUIs). The former depend solely on the C
957library, whereas the latter depend on Tcl/Tk and the underlying X
958libraries. In this case, we leave the command-line tools in the default
959output, whereas the GUIs are in a separate output. This allows users
960who do not need the GUIs to save space.
961
962There are several such multiple-output packages in the GNU distribution.
91ef73d4
LC
963Other conventional output names include @code{lib} for libraries and
964possibly header files, @code{bin} for stand-alone programs, and
965@code{debug} for debugging information (@pxref{Installing Debugging
966Files}). The outputs of a packages are listed in the third column of
967the output of @command{guix package --list-available} (@pxref{Invoking
968guix package}).
6e721c4d 969
eeaf4427 970
e49951eb
MW
971@node Invoking guix gc
972@section Invoking @command{guix gc}
fe8ff028
LC
973
974@cindex garbage collector
975Packages that are installed but not used may be @dfn{garbage-collected}.
e49951eb 976The @command{guix gc} command allows users to explicitly run the garbage
fe8ff028
LC
977collector to reclaim space from the @file{/nix/store} directory.
978
979The garbage collector has a set of known @dfn{roots}: any file under
980@file{/nix/store} reachable from a root is considered @dfn{live} and
981cannot be deleted; any other file is considered @dfn{dead} and may be
982deleted. The set of garbage collector roots includes default user
e49951eb
MW
983profiles, and may be augmented with @command{guix build --root}, for
984example (@pxref{Invoking guix build}).
fe8ff028 985
1bb9900a
LC
986Prior to running @code{guix gc --collect-garbage} to make space, it is
987often useful to remove old generations from user profiles; that way, old
988package builds referenced by those generations can be reclaimed. This
989is achieved by running @code{guix package --delete-generations}
990(@pxref{Invoking guix package}).
991
e49951eb 992The @command{guix gc} command has three modes of operation: it can be
fe8ff028
LC
993used to garbage-collect any dead files (the default), to delete specific
994files (the @code{--delete} option), or to print garbage-collector
995information. The available options are listed below:
996
997@table @code
998@item --collect-garbage[=@var{min}]
999@itemx -C [@var{min}]
1000Collect garbage---i.e., unreachable @file{/nix/store} files and
1001sub-directories. This is the default operation when no option is
1002specified.
1003
1004When @var{min} is given, stop once @var{min} bytes have been collected.
1005@var{min} may be a number of bytes, or it may include a unit as a
1006suffix, such as @code{MiB} for mebibytes and @code{GB} for gigabytes.
1007
1008When @var{min} is omitted, collect all the garbage.
1009
1010@item --delete
1011@itemx -d
1012Attempt to delete all the store files and directories specified as
1013arguments. This fails if some of the files are not in the store, or if
1014they are still live.
1015
1016@item --list-dead
1017Show the list of dead files and directories still present in the
1018store---i.e., files and directories no longer reachable from any root.
1019
1020@item --list-live
1021Show the list of live store files and directories.
ba8b732d
LC
1022
1023@end table
1024
1025In addition, the references among existing store files can be queried:
1026
1027@table @code
1028
1029@item --references
1030@itemx --referrers
1031List the references (respectively, the referrers) of store files given
1032as arguments.
1033
8e59fdd5
LC
1034@item --requisites
1035@itemx -R
1036List the requisites of the store files passed as arguments. Requisites
1037include the store files themselves, their references, and the references
1038of these, recursively. In other words, the returned list is the
1039@dfn{transitive closure} of the store files.
1040
fe8ff028
LC
1041@end table
1042
eeaf4427 1043
f651b477
LC
1044@node Invoking guix pull
1045@section Invoking @command{guix pull}
1046
1047Packages are installed or upgraded to the latest version available in
1048the distribution currently available on your local machine. To update
1049that distribution, along with the Guix tools, you must run @command{guix
1050pull}: the command downloads the latest Guix source code and package
1051descriptions, and deploys it.
1052
1053On completion, @command{guix package} will use packages and package
1054versions from this just-retrieved copy of Guix. Not only that, but all
1055the Guix commands and Scheme modules will also be taken from that latest
1056version. New @command{guix} sub-commands added by the update also
1057become available.
1058
1059The @command{guix pull} command is usually invoked with no arguments,
1060but it supports the following options:
1061
1062@table @code
1063@item --verbose
1064Produce verbose output, writing build logs to the standard error output.
1065
ab5d72ad
LC
1066@item --url=@var{url}
1067Download the source tarball of Guix from @var{url}.
1068
1069By default, the tarball is taken from its canonical address at
1070@code{gnu.org}, for the stable branch of Guix.
1071
f651b477
LC
1072@item --bootstrap
1073Use the bootstrap Guile to build the latest Guix. This option is only
1074useful to Guix developers.
1075@end table
1076
760c60d6
LC
1077
1078@node Invoking guix archive
1079@section Invoking @command{guix archive}
1080
1081The @command{guix archive} command allows users to @dfn{export} files
1082from the store into a single archive, and to later @dfn{import} them.
1083In particular, it allows store files to be transferred from one machine
1084to another machine's store. For example, to transfer the @code{emacs}
1085package to a machine connected over SSH, one would run:
1086
1087@example
1088guix archive --export emacs | ssh the-machine guix archive --import
1089@end example
1090
87236aed
LC
1091@noindent
1092However, note that, in this example, all of @code{emacs} and its
1093dependencies are transferred, regardless of what is already available in
1094the target machine's store. The @code{--missing} option can help figure
1095out which items are missing from the target's store.
1096
760c60d6 1097Archives are stored in the ``Nix archive'' or ``Nar'' format, which is
0dbd88db
LC
1098comparable in spirit to `tar', but with a few noteworthy differences
1099that make it more appropriate for our purposes. First, rather than
1100recording all Unix meta-data for each file, the Nar format only mentions
1101the file type (regular, directory, or symbolic link); Unix permissions
1102and owner/group are dismissed. Second, the order in which directory
1103entries are stored always follows the order of file names according to
1104the C locale collation order. This makes archive production fully
1105deterministic.
1106
1107When exporting, the daemon digitally signs the contents of the archive,
1108and that digital signature is appended. When importing, the daemon
1109verifies the signature and rejects the import in case of an invalid
1110signature or if the signing key is not authorized.
760c60d6
LC
1111@c FIXME: Add xref to daemon doc about signatures.
1112
1113The main options are:
1114
1115@table @code
1116@item --export
1117Export the specified store files or packages (see below.) Write the
1118resulting archive to the standard output.
1119
1120@item --import
1121Read an archive from the standard input, and import the files listed
1122therein into the store. Abort if the archive has an invalid digital
f82cc5fd
LC
1123signature, or if it is signed by a public key not among the authorized
1124keys (see @code{--authorize} below.)
554f26ec 1125
87236aed
LC
1126@item --missing
1127Read a list of store file names from the standard input, one per line,
1128and write on the standard output the subset of these files missing from
1129the store.
1130
554f26ec 1131@item --generate-key[=@var{parameters}]
f82cc5fd 1132@cindex signing, archives
554f26ec
LC
1133Generate a new key pair for the daemons. This is a prerequisite before
1134archives can be exported with @code{--export}. Note that this operation
1135usually takes time, because it needs to gather enough entropy to
1136generate the key pair.
1137
1138The generated key pair is typically stored under @file{/etc/guix}, in
1139@file{signing-key.pub} (public key) and @file{signing-key.sec} (private
1140key, which must be kept secret.) When @var{parameters} is omitted, it
1141is a 4096-bit RSA key. Alternately, @var{parameters} can specify
1142@code{genkey} parameters suitable for Libgcrypt (@pxref{General
1143public-key related Functions, @code{gcry_pk_genkey},, gcrypt, The
1144Libgcrypt Reference Manual}).
f82cc5fd
LC
1145
1146@item --authorize
1147@cindex authorizing, archives
1148Authorize imports signed by the public key passed on standard input.
1149The public key must be in ``s-expression advanced format''---i.e., the
1150same format as the @file{signing-key.pub} file.
1151
1152The list of authorized keys is kept in the human-editable file
1153@file{/etc/guix/acl}. The file contains
1154@url{http://people.csail.mit.edu/rivest/Sexp.txt, ``advanced-format
1155s-expressions''} and is structured as an access-control list in the
1156@url{http://theworld.com/~cme/spki.txt, Simple Public-Key Infrastructure
1157(SPKI)}.
760c60d6
LC
1158@end table
1159
1160To export store files as an archive to the standard output, run:
1161
1162@example
1163guix archive --export @var{options} @var{specifications}...
1164@end example
1165
1166@var{specifications} may be either store file names or package
1167specifications, as for @command{guix package} (@pxref{Invoking guix
1168package}). For instance, the following command creates an archive
1169containing the @code{gui} output of the @code{git} package and the main
1170output of @code{emacs}:
1171
1172@example
1173guix archive --export git:gui /nix/store/...-emacs-24.3 > great.nar
1174@end example
1175
1176If the specified packages are not built yet, @command{guix archive}
1177automatically builds them. The build process may be controlled with the
1178same options that can be passed to the @command{guix build} command
1179(@pxref{Invoking guix build}).
1180
1181
568717fd
LC
1182@c *********************************************************************
1183@node Programming Interface
1184@chapter Programming Interface
1185
3dc1970d
LC
1186GNU Guix provides several Scheme programming interfaces (APIs) to
1187define, build, and query packages. The first interface allows users to
1188write high-level package definitions. These definitions refer to
1189familiar packaging concepts, such as the name and version of a package,
1190its build system, and its dependencies. These definitions can then be
1191turned into concrete build actions.
1192
ba55b1cb 1193Build actions are performed by the Guix daemon, on behalf of users. In a
3dc1970d
LC
1194standard setup, the daemon has write access to the store---the
1195@file{/nix/store} directory---whereas users do not. The recommended
1196setup also has the daemon perform builds in chroots, under a specific
1197build users, to minimize interference with the rest of the system.
1198
1199@cindex derivation
1200Lower-level APIs are available to interact with the daemon and the
1201store. To instruct the daemon to perform a build action, users actually
1202provide it with a @dfn{derivation}. A derivation is a low-level
1203representation of the build actions to be taken, and the environment in
1204which they should occur---derivations are to package definitions what
1205assembly is to C programs.
1206
1207This chapter describes all these APIs in turn, starting from high-level
1208package definitions.
1209
568717fd 1210@menu
b860f382
LC
1211* Defining Packages:: Defining new packages.
1212* The Store:: Manipulating the package store.
1213* Derivations:: Low-level interface to package derivations.
1214* The Store Monad:: Purely functional interface to the store.
568717fd
LC
1215@end menu
1216
1217@node Defining Packages
1218@section Defining Packages
1219
3dc1970d
LC
1220The high-level interface to package definitions is implemented in the
1221@code{(guix packages)} and @code{(guix build-system)} modules. As an
1222example, the package definition, or @dfn{recipe}, for the GNU Hello
1223package looks like this:
1224
1225@example
b22a12fd
LC
1226(use-modules (guix packages)
1227 (guix download)
1228 (guix build-system gnu)
1229 (guix licenses))
1230
3dc1970d
LC
1231(define hello
1232 (package
1233 (name "hello")
1234 (version "2.8")
1235 (source (origin
1236 (method url-fetch)
1237 (uri (string-append "mirror://gnu/hello/hello-" version
1238 ".tar.gz"))
1239 (sha256
1240 (base32 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6"))))
1241 (build-system gnu-build-system)
1242 (inputs `(("gawk" ,gawk)))
1243 (synopsis "GNU Hello")
1244 (description "Yeah...")
1245 (home-page "http://www.gnu.org/software/hello/")
b22a12fd 1246 (license gpl3+)))
3dc1970d
LC
1247@end example
1248
1249@noindent
1250Without being a Scheme expert, the reader may have guessed the meaning
1251of the various fields here. This expression binds variable @var{hello}
1252to a @code{<package>} object, which is essentially a record
1253(@pxref{SRFI-9, Scheme records,, guile, GNU Guile Reference Manual}).
1254This package object can be inspected using procedures found in the
1255@code{(guix packages)} module; for instance, @code{(package-name hello)}
1256returns---surprise!---@code{"hello"}.
1257
1258There are a few points worth noting in the above package definition:
1259
1260@itemize
1261@item
1262The @code{source} field of the package is an @code{<origin>} object.
1263Here, the @code{url-fetch} method from @code{(guix download)} is used,
1264meaning that the source is a file to be downloaded over FTP or HTTP.
1265
1266The @code{mirror://gnu} prefix instructs @code{url-fetch} to use one of
1267the GNU mirrors defined in @code{(guix download)}.
1268
1269The @code{sha256} field specifies the expected SHA256 hash of the file
1270being downloaded. It is mandatory, and allows Guix to check the
1271integrity of the file. The @code{(base32 @dots{})} form introduces the
6c365eca 1272base32 representation of the hash. You can obtain this information with
210cc920
LC
1273@code{guix download} (@pxref{Invoking guix download}) and @code{guix
1274hash} (@pxref{Invoking guix hash}).
3dc1970d 1275
f9cc8971
LC
1276@cindex patches
1277When needed, the @code{origin} form can also have a @code{patches} field
1278listing patches to be applied, and a @code{snippet} field giving a
1279Scheme expression to modify the source code.
1280
3dc1970d
LC
1281@item
1282@cindex GNU Build System
1283The @code{build-system} field is set to @var{gnu-build-system}. The
1284@var{gnu-build-system} variable is defined in the @code{(guix
1285build-system gnu)} module, and is bound to a @code{<build-system>}
1286object.
1287
1288Naturally, @var{gnu-build-system} represents the familiar GNU Build
1289System, and variants thereof (@pxref{Configuration, configuration and
1290makefile conventions,, standards, GNU Coding Standards}). In a
ba55b1cb 1291nutshell, packages using the GNU Build System may be configured, built,
3dc1970d
LC
1292and installed with the usual @code{./configure && make && make check &&
1293make install} command sequence. This is what @var{gnu-build-system}
1294does.
1295
1296In addition, @var{gnu-build-system} ensures that the ``standard''
1297environment for GNU packages is available. This includes tools such as
1298GCC, Coreutils, Bash, Make, Diffutils, and Patch.
1299
1300@item
1301The @code{inputs} field specifies inputs to the build process---i.e.,
1302build-time or run-time dependencies of the package. Here, we define an
1303input called @code{"gawk"} whose value is that of the @var{gawk}
1304variable; @var{gawk} is itself bound to a @code{<package>} object.
1305
1306Note that GCC, Coreutils, Bash, and other essential tools do not need to
1307be specified as inputs here. Instead, @var{gnu-build-system} takes care
1308of ensuring that they are present.
1309
1310However, any other dependencies need to be specified in the
1311@code{inputs} field. Any dependency not specified here will simply be
1312unavailable to the build process, possibly leading to a build failure.
1313@end itemize
1314
1315There are other fields that package definitions may provide. Of
1316particular interest is the @code{arguments} field. When specified, it
1317must be bound to a list of additional arguments to be passed to the
1318build system. For instance, the above definition could be augmented
1319with the following field initializer:
1320
1321@example
1322 (arguments `(#:tests? #f
1323 #:configure-flags '("--enable-silent-rules")))
1324@end example
1325
1326@noindent
1327These are keyword arguments (@pxref{Optional Arguments, keyword
1328arguments in Guile,, guile, GNU Guile Reference Manual}). They are
1329passed to @var{gnu-build-system}, which interprets them as meaning ``do
1330not run @code{make check}'', and ``run @file{configure} with the
874e6874
LC
1331@code{--enable-silent-rules} flag''. The value of these keyword
1332parameters is actually evaluated in the @dfn{build stratum}---i.e., by a
1333Guile process launched by the daemon (@pxref{Derivations}).
3dc1970d
LC
1334
1335Once a package definition is in place@footnote{Simple package
1336definitions like the one above may be automatically converted from the
e49951eb
MW
1337Nixpkgs distribution using the @command{guix import} command.}, the
1338package may actually be built using the @code{guix build} command-line
37166310
LC
1339tool (@pxref{Invoking guix build}). Eventually, updating the package
1340definition to a new upstream version can be partly automated by the
1341@command{guix refresh} command (@pxref{Invoking guix refresh}).
3dc1970d
LC
1342
1343Behind the scenes, a derivation corresponding to the @code{<package>}
1344object is first computed by the @code{package-derivation} procedure.
1345That derivation is stored in a @code{.drv} file under @file{/nix/store}.
ba55b1cb 1346The build actions it prescribes may then be realized by using the
3dc1970d
LC
1347@code{build-derivations} procedure (@pxref{The Store}).
1348
1349@deffn {Scheme Procedure} package-derivation @var{store} @var{package} [@var{system}]
59688fc4
LC
1350Return the @code{<derivation>} object of @var{package} for @var{system}
1351(@pxref{Derivations}).
3dc1970d
LC
1352
1353@var{package} must be a valid @code{<package>} object, and @var{system}
1354must be a string denoting the target system type---e.g.,
1355@code{"x86_64-linux"} for an x86_64 Linux-based GNU system. @var{store}
1356must be a connection to the daemon, which operates on the store
1357(@pxref{The Store}).
1358@end deffn
568717fd 1359
9c1edabd
LC
1360@noindent
1361@cindex cross-compilation
1362Similarly, it is possible to compute a derivation that cross-builds a
1363package for some other system:
1364
1365@deffn {Scheme Procedure} package-cross-derivation @var{store} @
1366 @var{package} @var{target} [@var{system}]
59688fc4
LC
1367Return the @code{<derivation>} object of @var{package} cross-built from
1368@var{system} to @var{target}.
9c1edabd
LC
1369
1370@var{target} must be a valid GNU triplet denoting the target hardware
1371and operating system, such as @code{"mips64el-linux-gnu"}
1372(@pxref{Configuration Names, GNU configuration triplets,, configure, GNU
1373Configure and Build System}).
1374@end deffn
1375
1376
568717fd
LC
1377@node The Store
1378@section The Store
1379
e531ac2a
LC
1380@cindex store
1381@cindex store paths
1382
1383Conceptually, the @dfn{store} is where derivations that have been
1384successfully built are stored---by default, under @file{/nix/store}.
1385Sub-directories in the store are referred to as @dfn{store paths}. The
1386store has an associated database that contains information such has the
1387store paths referred to by each store path, and the list of @emph{valid}
1388store paths---paths that result from a successful build.
1389
1390The store is always accessed by the daemon on behalf of its clients
1391(@pxref{Invoking guix-daemon}). To manipulate the store, clients
1392connect to the daemon over a Unix-domain socket, send it requests, and
1393read the result---these are remote procedure calls, or RPCs.
1394
1395The @code{(guix store)} module provides procedures to connect to the
1396daemon, and to perform RPCs. These are described below.
1397
1398@deffn {Scheme Procedure} open-connection [@var{file}] [#:reserve-space? #t]
1399Connect to the daemon over the Unix-domain socket at @var{file}. When
1400@var{reserve-space?} is true, instruct it to reserve a little bit of
1401extra space on the file system so that the garbage collector can still
1402operate, should the disk become full. Return a server object.
1403
1404@var{file} defaults to @var{%default-socket-path}, which is the normal
1405location given the options that were passed to @command{configure}.
1406@end deffn
1407
1408@deffn {Scheme Procedure} close-connection @var{server}
1409Close the connection to @var{server}.
1410@end deffn
1411
1412@defvr {Scheme Variable} current-build-output-port
1413This variable is bound to a SRFI-39 parameter, which refers to the port
1414where build and error logs sent by the daemon should be written.
1415@end defvr
1416
1417Procedures that make RPCs all take a server object as their first
1418argument.
1419
1420@deffn {Scheme Procedure} valid-path? @var{server} @var{path}
1421Return @code{#t} when @var{path} is a valid store path.
1422@end deffn
1423
cfbf9160 1424@deffn {Scheme Procedure} add-text-to-store @var{server} @var{name} @var{text} [@var{references}]
e531ac2a
LC
1425Add @var{text} under file @var{name} in the store, and return its store
1426path. @var{references} is the list of store paths referred to by the
1427resulting store path.
1428@end deffn
1429
874e6874 1430@deffn {Scheme Procedure} build-derivations @var{server} @var{derivations}
59688fc4
LC
1431Build @var{derivations} (a list of @code{<derivation>} objects or
1432derivation paths), and return when the worker is done building them.
1433Return @code{#t} on success.
874e6874
LC
1434@end deffn
1435
b860f382
LC
1436Note that the @code{(guix monads)} module provides a monad as well as
1437monadic versions of the above procedures, with the goal of making it
1438more convenient to work with code that accesses the store (@pxref{The
1439Store Monad}).
1440
e531ac2a
LC
1441@c FIXME
1442@i{This section is currently incomplete.}
568717fd
LC
1443
1444@node Derivations
1445@section Derivations
1446
874e6874
LC
1447@cindex derivations
1448Low-level build actions and the environment in which they are performed
1449are represented by @dfn{derivations}. A derivation contain the
1450following pieces of information:
1451
1452@itemize
1453@item
1454The outputs of the derivation---derivations produce at least one file or
1455directory in the store, but may produce more.
1456
1457@item
1458The inputs of the derivations, which may be other derivations or plain
1459files in the store (patches, build scripts, etc.)
1460
1461@item
1462The system type targeted by the derivation---e.g., @code{x86_64-linux}.
1463
1464@item
1465The file name of a build script in the store, along with the arguments
1466to be passed.
1467
1468@item
1469A list of environment variables to be defined.
1470
1471@end itemize
1472
1473@cindex derivation path
1474Derivations allow clients of the daemon to communicate build actions to
1475the store. They exist in two forms: as an in-memory representation,
1476both on the client- and daemon-side, and as files in the store whose
1477name end in @code{.drv}---these files are referred to as @dfn{derivation
1478paths}. Derivations paths can be passed to the @code{build-derivations}
1479procedure to perform the build actions they prescribe (@pxref{The
1480Store}).
1481
1482The @code{(guix derivations)} module provides a representation of
1483derivations as Scheme objects, along with procedures to create and
1484otherwise manipulate derivations. The lowest-level primitive to create
1485a derivation is the @code{derivation} procedure:
1486
1909431c
LC
1487@deffn {Scheme Procedure} derivation @var{store} @var{name} @var{builder} @
1488 @var{args} [#:outputs '("out")] [#:hash #f] [#:hash-algo #f] @
2096ef47 1489 [#:recursive? #f] [#:inputs '()] [#:env-vars '()] @
1909431c
LC
1490 [#:system (%current-system)] [#:references-graphs #f] @
1491 [#:local-build? #f]
59688fc4
LC
1492Build a derivation with the given arguments, and return the resulting
1493@code{<derivation>} object.
874e6874 1494
2096ef47 1495When @var{hash} and @var{hash-algo} are given, a
874e6874 1496@dfn{fixed-output derivation} is created---i.e., one whose result is
36bbbbd1
LC
1497known in advance, such as a file download. If, in addition,
1498@var{recursive?} is true, then that fixed output may be an executable
1499file or a directory and @var{hash} must be the hash of an archive
1500containing this output.
5b0c9d16 1501
858e9282 1502When @var{references-graphs} is true, it must be a list of file
5b0c9d16
LC
1503name/store path pairs. In that case, the reference graph of each store
1504path is exported in the build environment in the corresponding file, in
1505a simple text format.
1909431c
LC
1506
1507When @var{local-build?} is true, declare that the derivation is not a
1508good candidate for offloading and should rather be built locally
1509(@pxref{Daemon Offload Setup}). This is the case for small derivations
1510where the costs of data transfers would outweigh the benefits.
874e6874
LC
1511@end deffn
1512
1513@noindent
1514Here's an example with a shell script as its builder, assuming
1515@var{store} is an open connection to the daemon, and @var{bash} points
1516to a Bash executable in the store:
1517
1518@lisp
1519(use-modules (guix utils)
1520 (guix store)
1521 (guix derivations))
1522
59688fc4
LC
1523(let ((builder ; add the Bash script to the store
1524 (add-text-to-store store "my-builder.sh"
1525 "echo hello world > $out\n" '())))
1526 (derivation store "foo"
1527 bash `("-e" ,builder)
1528 #:env-vars '(("HOME" . "/homeless"))))
1529@result{} #<derivation /nix/store/@dots{}-foo.drv => /nix/store/@dots{}-foo>
874e6874
LC
1530@end lisp
1531
1532As can be guessed, this primitive is cumbersome to use directly. An
1533improved variant is @code{build-expression->derivation}, which allows
1534the caller to directly pass a Guile expression as the build script:
1535
dd1a5a15
LC
1536@deffn {Scheme Procedure} build-expression->derivation @var{store} @
1537 @var{name} @var{exp} @
1538 [#:system (%current-system)] [#:inputs '()] @
1539 [#:outputs '("out")] [#:hash #f] [#:hash-algo #f] @
36bbbbd1 1540 [#:recursive? #f] [#:env-vars '()] [#:modules '()] @
1909431c 1541 [#:references-graphs #f] [#:local-build? #f] [#:guile-for-build #f]
874e6874
LC
1542Return a derivation that executes Scheme expression @var{exp} as a
1543builder for derivation @var{name}. @var{inputs} must be a list of
1544@code{(name drv-path sub-drv)} tuples; when @var{sub-drv} is omitted,
1545@code{"out"} is assumed. @var{modules} is a list of names of Guile
1546modules from the current search path to be copied in the store,
1547compiled, and made available in the load path during the execution of
1548@var{exp}---e.g., @code{((guix build utils) (guix build
1549gnu-build-system))}.
1550
1551@var{exp} is evaluated in an environment where @code{%outputs} is bound
1552to a list of output/path pairs, and where @code{%build-inputs} is bound
1553to a list of string/output-path pairs made from @var{inputs}.
1554Optionally, @var{env-vars} is a list of string pairs specifying the name
1555and value of environment variables visible to the builder. The builder
1556terminates by passing the result of @var{exp} to @code{exit}; thus, when
1557@var{exp} returns @code{#f}, the build is considered to have failed.
1558
1559@var{exp} is built using @var{guile-for-build} (a derivation). When
1560@var{guile-for-build} is omitted or is @code{#f}, the value of the
1561@code{%guile-for-build} fluid is used instead.
9c629a27 1562
1909431c
LC
1563See the @code{derivation} procedure for the meaning of @var{references-graphs}
1564and @var{local-build?}.
874e6874
LC
1565@end deffn
1566
1567@noindent
1568Here's an example of a single-output derivation that creates a directory
1569containing one file:
1570
1571@lisp
1572(let ((builder '(let ((out (assoc-ref %outputs "out")))
1573 (mkdir out) ; create /nix/store/@dots{}-goo
1574 (call-with-output-file (string-append out "/test")
1575 (lambda (p)
1576 (display '(hello guix) p))))))
dd1a5a15 1577 (build-expression->derivation store "goo" builder))
874e6874 1578
59688fc4 1579@result{} #<derivation /nix/store/@dots{}-goo.drv => @dots{}>
874e6874
LC
1580@end lisp
1581
1582@cindex strata of code
1583Remember that the build expression passed to
1584@code{build-expression->derivation} is run by a separate Guile process
1585than the one that calls @code{build-expression->derivation}: it is run
1586by a Guile process launched by the daemon, typically in a chroot. So,
1587while there is a single language for both the @dfn{host} and the build
1588side, there are really two @dfn{strata} of code: the host-side, and the
1589build-side code@footnote{The term @dfn{stratum} in this context was
1590coined by Manuel Serrano et al. in the context of their work on Hop.}.
1591This distinction is important to keep in mind, notably when using
1592higher-level constructs such as @var{gnu-build-system} (@pxref{Defining
1593Packages}). For this reason, Guix modules that are meant to be used in
1594the build stratum are kept in the @code{(guix build @dots{})} name
1595space.
568717fd 1596
b860f382
LC
1597@node The Store Monad
1598@section The Store Monad
1599
1600@cindex monad
1601
1602The procedures that operate on the store described in the previous
1603sections all take an open connection to the build daemon as their first
1604argument. Although the underlying model is functional, they either have
1605side effects or depend on the current state of the store.
1606
1607The former is inconvenient: the connection to the build daemon has to be
1608carried around in all those functions, making it impossible to compose
1609functions that do not take that parameter with functions that do. The
1610latter can be problematic: since store operations have side effects
1611and/or depend on external state, they have to be properly sequenced.
1612
1613@cindex monadic values
1614@cindex monadic functions
1615This is where the @code{(guix monads)} module comes in. This module
1616provides a framework for working with @dfn{monads}, and a particularly
1617useful monad for our uses, the @dfn{store monad}. Monads are a
1618construct that allows two things: associating ``context'' with values
1619(in our case, the context is the store), and building sequences of
1620computations (here computations includes accesses to the store.) Values
1621in a monad---values that carry this additional context---are called
1622@dfn{monadic values}; procedures that return such values are called
1623@dfn{monadic procedures}.
1624
1625Consider this ``normal'' procedure:
1626
1627@example
45adbd62
LC
1628(define (sh-symlink store)
1629 ;; Return a derivation that symlinks the 'bash' executable.
1630 (let* ((drv (package-derivation store bash))
1631 (out (derivation->output-path drv))
1632 (sh (string-append out "/bin/bash")))
1633 (build-expression->derivation store "sh"
1634 `(symlink ,sh %output))))
b860f382
LC
1635@end example
1636
1637Using @code{(guix monads)}, it may be rewritten as a monadic function:
1638
1639@example
45adbd62 1640(define (sh-symlink)
b860f382 1641 ;; Same, but return a monadic value.
45adbd62
LC
1642 (mlet %store-monad ((sh (package-file bash "bin")))
1643 (derivation-expression "sh" `(symlink ,sh %output))))
b860f382
LC
1644@end example
1645
1646There are two things to note in the second version: the @code{store}
1647parameter is now implicit, and the monadic value returned by
1648@code{package-file}---a wrapper around @code{package-derivation} and
1649@code{derivation->output-path}---is @dfn{bound} using @code{mlet}
1650instead of plain @code{let}.
1651
1652Calling the monadic @code{profile.sh} has no effect. To get the desired
1653effect, one must use @code{run-with-store}:
1654
1655@example
1656(run-with-store (open-connection) (profile.sh))
1657@result{} /nix/store/...-profile.sh
1658@end example
1659
1660The main syntactic forms to deal with monads in general are described
1661below.
1662
1663@deffn {Scheme Syntax} with-monad @var{monad} @var{body} ...
1664Evaluate any @code{>>=} or @code{return} forms in @var{body} as being
1665in @var{monad}.
1666@end deffn
1667
1668@deffn {Scheme Syntax} return @var{val}
1669Return a monadic value that encapsulates @var{val}.
1670@end deffn
1671
1672@deffn {Scheme Syntax} >>= @var{mval} @var{mproc}
1673@dfn{Bind} monadic value @var{mval}, passing its ``contents'' to monadic
1674procedure @var{mproc}@footnote{This operation is commonly referred to as
1675``bind'', but that name denotes an unrelated procedure in Guile. Thus
1676we use this somewhat cryptic symbol inherited from the Haskell
1677language.}.
1678@end deffn
1679
1680@deffn {Scheme Syntax} mlet @var{monad} ((@var{var} @var{mval}) ...) @
1681 @var{body} ...
1682@deffnx {Scheme Syntax} mlet* @var{monad} ((@var{var} @var{mval}) ...) @
1683 @var{body} ...
1684Bind the variables @var{var} to the monadic values @var{mval} in
1685@var{body}. The form (@var{var} -> @var{val}) binds @var{var} to the
1686``normal'' value @var{val}, as per @code{let}.
1687
1688@code{mlet*} is to @code{mlet} what @code{let*} is to @code{let}
1689(@pxref{Local Bindings,,, guile, GNU Guile Reference Manual}).
1690@end deffn
1691
1692The interface to the store monad provided by @code{(guix monads)} is as
1693follows.
1694
1695@defvr {Scheme Variable} %store-monad
1696The store monad. Values in the store monad encapsulate accesses to the
1697store. When its effect is needed, a value of the store monad must be
1698``evaluated'' by passing it to the @code{run-with-store} procedure (see
1699below.)
1700@end defvr
1701
1702@deffn {Scheme Procedure} run-with-store @var{store} @var{mval} [#:guile-for-build] [#:system (%current-system)]
1703Run @var{mval}, a monadic value in the store monad, in @var{store}, an
1704open store connection.
1705@end deffn
1706
1707@deffn {Monadic Procedure} text-file @var{name} @var{text}
1708Return as a monadic value the absolute file name in the store of the file
45adbd62
LC
1709containing @var{text}, a string.
1710@end deffn
1711
1712@deffn {Monadic Procedure} text-file* @var{name} @var{text} @dots{}
1713Return as a monadic value a derivation that builds a text file
1714containing all of @var{text}. @var{text} may list, in addition to
1715strings, packages, derivations, and store file names; the resulting
1716store file holds references to all these.
1717
1718This variant should be preferred over @code{text-file} anytime the file
1719to create will reference items from the store. This is typically the
1720case when building a configuration file that embeds store file names,
1721like this:
1722
1723@example
1724(define (profile.sh)
1725 ;; Return the name of a shell script in the store that
1726 ;; initializes the 'PATH' environment variable.
1727 (text-file* "profile.sh"
1728 "export PATH=" coreutils "/bin:"
1729 grep "/bin:" sed "/bin\n"))
1730@end example
1731
1732In this example, the resulting @file{/nix/store/@dots{}-profile.sh} file
1733will references @var{coreutils}, @var{grep}, and @var{sed}, thereby
1734preventing them from being garbage-collected during its lifetime.
b860f382
LC
1735@end deffn
1736
1737@deffn {Monadic Procedure} package-file @var{package} [@var{file}] @
1738 [#:system (%current-system)] [#:output "out"] Return as a monadic
1739value in the absolute file name of @var{file} within the @var{output}
1740directory of @var{package}. When @var{file} is omitted, return the name
1741of the @var{output} directory of @var{package}.
1742@end deffn
1743
dd1a5a15
LC
1744@deffn {Monadic Procedure} derivation-expression @var{name} @var{exp} @
1745 [#:system (%current-system)] [#:inputs '()] @
1746 [#:outputs '("out")] [#:hash #f] @
b860f382
LC
1747 [#:hash-algo #f] [#:env-vars '()] [#:modules '()] @
1748 [#:references-graphs #f] [#:guile-for-build #f]
1749Monadic version of @code{build-expression->derivation}
1750(@pxref{Derivations}).
1751@end deffn
1752
1753@deffn {Monadic Procedure} package->derivation @var{package} [@var{system}]
1754Monadic version of @code{package-derivation} (@pxref{Defining
1755Packages}).
1756@end deffn
1757
1758
568717fd
LC
1759@c *********************************************************************
1760@node Utilities
1761@chapter Utilities
1762
210cc920
LC
1763This section describes tools primarily targeted at developers and users
1764who write new package definitions. They complement the Scheme
1765programming interface of Guix in a convenient way.
1766
568717fd 1767@menu
37166310 1768* Invoking guix build:: Building packages from the command line.
210cc920 1769* Invoking guix download:: Downloading a file and printing its hash.
37166310
LC
1770* Invoking guix hash:: Computing the cryptographic hash of a file.
1771* Invoking guix refresh:: Updating package definitions.
568717fd
LC
1772@end menu
1773
e49951eb
MW
1774@node Invoking guix build
1775@section Invoking @command{guix build}
568717fd 1776
e49951eb 1777The @command{guix build} command builds packages or derivations and
6798a8e4
LC
1778their dependencies, and prints the resulting store paths. Note that it
1779does not modify the user's profile---this is the job of the
e49951eb 1780@command{guix package} command (@pxref{Invoking guix package}). Thus,
6798a8e4
LC
1781it is mainly useful for distribution developers.
1782
1783The general syntax is:
c78bd12b
LC
1784
1785@example
e49951eb 1786guix build @var{options} @var{package-or-derivation}@dots{}
c78bd12b
LC
1787@end example
1788
1789@var{package-or-derivation} may be either the name of a package found in
5401dd75
LC
1790the software distribution such as @code{coreutils} or
1791@code{coreutils-8.20}, or a derivation such as
1792@file{/nix/store/@dots{}-coreutils-8.19.drv}. Alternatively, the
c78bd12b
LC
1793@code{--expression} option may be used to specify a Scheme expression
1794that evaluates to a package; this is useful when disambiguation among
1795several same-named packages or package variants is needed.
1796
1797The @var{options} may be zero or more of the following:
1798
1799@table @code
1800
1801@item --expression=@var{expr}
1802@itemx -e @var{expr}
ac5de156 1803Build the package or derivation @var{expr} evaluates to.
c78bd12b 1804
5401dd75 1805For example, @var{expr} may be @code{(@@ (gnu packages guile)
c78bd12b
LC
1806guile-1.8)}, which unambiguously designates this specific variant of
1807version 1.8 of Guile.
1808
ac5de156
LC
1809Alternately, @var{expr} may refer to a zero-argument monadic procedure
1810(@pxref{The Store Monad}). The procedure must return a derivation as a
1811monadic value, which is then passed through @code{run-with-store}.
1812
c78bd12b
LC
1813@item --source
1814@itemx -S
1815Build the packages' source derivations, rather than the packages
1816themselves.
1817
e49951eb 1818For instance, @code{guix build -S gcc} returns something like
6798a8e4 1819@file{/nix/store/@dots{}-gcc-4.7.2.tar.bz2}, which is GCC's source tarball.
c78bd12b 1820
f9cc8971
LC
1821The returned source tarball is the result of applying any patches and
1822code snippets specified in the package's @code{origin} (@pxref{Defining
1823Packages}).
1824
c78bd12b
LC
1825@item --system=@var{system}
1826@itemx -s @var{system}
1827Attempt to build for @var{system}---e.g., @code{i686-linux}---instead of
1828the host's system type.
1829
1830An example use of this is on Linux-based systems, which can emulate
1831different personalities. For instance, passing
1832@code{--system=i686-linux} on an @code{x86_64-linux} system allows users
1833to build packages in a complete 32-bit environment.
1834
e55ec43d
LC
1835@item --target=@var{triplet}
1836@cindex cross-compilation
1837Cross-build for @var{triplet}, which must be a valid GNU triplet, such
1838as @code{"mips64el-linux-gnu"} (@pxref{Configuration Names, GNU
1839configuration triplets,, configure, GNU Configure and Build System}).
1840
c78bd12b
LC
1841@item --derivations
1842@itemx -d
1843Return the derivation paths, not the output paths, of the given
1844packages.
1845
1846@item --keep-failed
1847@itemx -K
1848Keep the build tree of failed builds. Thus, if a build fail, its build
1849tree is kept under @file{/tmp}, in a directory whose name is shown at
1850the end of the build log. This is useful when debugging build issues.
1851
1852@item --dry-run
1853@itemx -n
1854Do not build the derivations.
1855
56b1f4b7
LC
1856@item --fallback
1857When substituting a pre-built binary fails, fall back to building
1858packages locally.
1859
c78bd12b 1860@item --no-substitutes
b5385b52
LC
1861Do not use substitutes for build products. That is, always build things
1862locally instead of allowing downloads of pre-built binaries.
c78bd12b 1863
425b0bfc 1864@item --no-build-hook
4ec2e92d
LC
1865Do not attempt to offload builds @i{via} the daemon's ``build hook''
1866(@pxref{Daemon Offload Setup}). That is, always build things locally
1867instead of offloading builds to remote machines.
425b0bfc 1868
969e678e
LC
1869@item --max-silent-time=@var{seconds}
1870When the build or substitution process remains silent for more than
1871@var{seconds}, terminate it and report a build failure.
1872
c78bd12b
LC
1873@item --cores=@var{n}
1874@itemx -c @var{n}
1875Allow the use of up to @var{n} CPU cores for the build. The special
1876value @code{0} means to use as many CPU cores as available.
1877
1878@item --root=@var{file}
1879@itemx -r @var{file}
1880Make @var{file} a symlink to the result, and register it as a garbage
1881collector root.
07ab4bf1
LC
1882
1883@item --verbosity=@var{level}
1884Use the given verbosity level. @var{level} must be an integer between 0
1885and 5; higher means more verbose output. Setting a level of 4 or more
1886may be helpful when debugging setup issues with the build daemon.
1887
bf421152
LC
1888@item --log-file
1889Return the build log file names for the given
1890@var{package-or-derivation}s, or raise an error if build logs are
1891missing.
1892
1893This works regardless of how packages or derivations are specified. For
1894instance, the following invocations are equivalent:
1895
1896@example
1897guix build --log-file `guix build -d guile`
1898guix build --log-file `guix build guile`
1899guix build --log-file guile
1900guix build --log-file -e '(@@ (gnu packages guile) guile-2.0)'
1901@end example
1902
1903
c78bd12b
LC
1904@end table
1905
e49951eb 1906Behind the scenes, @command{guix build} is essentially an interface to
c78bd12b
LC
1907the @code{package-derivation} procedure of the @code{(guix packages)}
1908module, and to the @code{build-derivations} procedure of the @code{(guix
1909store)} module.
1910
210cc920
LC
1911@node Invoking guix download
1912@section Invoking @command{guix download}
1913
1914When writing a package definition, developers typically need to download
1915the package's source tarball, compute its SHA256 hash, and write that
1916hash in the package definition (@pxref{Defining Packages}). The
1917@command{guix download} tool helps with this task: it downloads a file
1918from the given URI, adds it to the store, and prints both its file name
1919in the store and its SHA256 hash.
1920
1921The fact that the downloaded file is added to the store saves bandwidth:
1922when the developer eventually tries to build the newly defined package
1923with @command{guix build}, the source tarball will not have to be
1924downloaded again because it is already in the store. It is also a
1925convenient way to temporarily stash files, which may be deleted
1926eventually (@pxref{Invoking guix gc}).
1927
1928The @command{guix download} command supports the same URIs as used in
1929package definitions. In particular, it supports @code{mirror://} URIs.
1930@code{https} URIs (HTTP over TLS) are supported @emph{provided} the
1931Guile bindings for GnuTLS are available in the user's environment; when
1932they are not available, an error is raised.
1933
1934The following option is available:
1935
1936@table @code
1937@item --format=@var{fmt}
1938@itemx -f @var{fmt}
1939Write the hash in the format specified by @var{fmt}. For more
1940information on the valid values for @var{fmt}, @ref{Invoking guix hash}.
1941@end table
1942
6c365eca
NK
1943@node Invoking guix hash
1944@section Invoking @command{guix hash}
1945
210cc920 1946The @command{guix hash} command computes the SHA256 hash of a file.
6c365eca
NK
1947It is primarily a convenience tool for anyone contributing to the
1948distribution: it computes the cryptographic hash of a file, which can be
1949used in the definition of a package (@pxref{Defining Packages}).
1950
1951The general syntax is:
1952
1953@example
1954guix hash @var{option} @var{file}
1955@end example
1956
1957@command{guix hash} has the following option:
1958
1959@table @code
1960
1961@item --format=@var{fmt}
1962@itemx -f @var{fmt}
210cc920 1963Write the hash in the format specified by @var{fmt}.
6c365eca
NK
1964
1965Supported formats: @code{nix-base32}, @code{base32}, @code{base16}
1966(@code{hex} and @code{hexadecimal} can be used as well).
1967
1968If the @option{--format} option is not specified, @command{guix hash}
1969will output the hash in @code{nix-base32}. This representation is used
1970in the definitions of packages.
1971
3140f2df
LC
1972@item --recursive
1973@itemx -r
1974Compute the hash on @var{file} recursively.
1975
1976In this case, the hash is computed on an archive containing @var{file},
1977including its children if it is a directory. Some of @var{file}'s
1978meta-data is part of the archive; for instance, when @var{file} is a
1979regular file, the hash is different depending on whether @var{file} is
1980executable or not. Meta-data such as time stamps has no impact on the
1981hash (@pxref{Invoking guix archive}).
1982@c FIXME: Replace xref above with xref to an ``Archive'' section when
1983@c it exists.
1984
6c365eca
NK
1985@end table
1986
37166310
LC
1987@node Invoking guix refresh
1988@section Invoking @command{guix refresh}
1989
1990The primary audience of the @command{guix refresh} command is developers
1991of the GNU software distribution. By default, it reports any packages
1992provided by the distribution that are outdated compared to the latest
1993upstream version, like this:
1994
1995@example
1996$ guix refresh
1997gnu/packages/gettext.scm:29:13: gettext would be upgraded from 0.18.1.1 to 0.18.2.1
1998gnu/packages/glib.scm:77:12: glib would be upgraded from 2.34.3 to 2.37.0
1999@end example
2000
2001It does so by browsing each package's FTP directory and determining the
2002highest version number of the source tarballs
2003therein@footnote{Currently, this only works for GNU packages.}.
2004
2005When passed @code{--update}, it modifies distribution source files to
2006update the version numbers and source tarball hashes of those packages'
2007recipes (@pxref{Defining Packages}). This is achieved by downloading
2008each package's latest source tarball and its associated OpenPGP
2009signature, authenticating the downloaded tarball against its signature
2010using @command{gpg}, and finally computing its hash. When the public
2011key used to sign the tarball is missing from the user's keyring, an
2012attempt is made to automatically retrieve it from a public key server;
2013when it's successful, the key is added to the user's keyring; otherwise,
2014@command{guix refresh} reports an error.
2015
2016The following options are supported:
2017
2018@table @code
2019
2020@item --update
2021@itemx -u
2022Update distribution source files (package recipes) in place.
2023@ref{Defining Packages}, for more information on package definitions.
2024
2025@item --select=[@var{subset}]
2026@itemx -s @var{subset}
2027Select all the packages in @var{subset}, one of @code{core} or
2028@code{non-core}.
2029
2030The @code{core} subset refers to all the packages at the core of the
2031distribution---i.e., packages that are used to build ``everything
2032else''. This includes GCC, libc, Binutils, Bash, etc. Usually,
2033changing one of these packages in the distribution entails a rebuild of
2034all the others. Thus, such updates are an inconvenience to users in
2035terms of build time or bandwidth used to achieve the upgrade.
2036
2037The @code{non-core} subset refers to the remaining packages. It is
2038typically useful in cases where an update of the core packages would be
2039inconvenient.
2040
2041@end table
2042
2043In addition, @command{guix refresh} can be passed one or more package
2044names, as in this example:
2045
2046@example
2047guix refresh -u emacs idutils
2048@end example
2049
2050@noindent
2051The command above specifically updates the @code{emacs} and
2052@code{idutils} packages. The @code{--select} option would have no
2053effect in this case.
2054
f9230085
LC
2055The following options can be used to customize GnuPG operation:
2056
2057@table @code
2058
2059@item --key-server=@var{host}
2060Use @var{host} as the OpenPGP key server when importing a public key.
2061
2062@item --gpg=@var{command}
2063Use @var{command} as the GnuPG 2.x command. @var{command} is searched
2064for in @code{$PATH}.
2065
2066@end table
2067
37166310 2068
a1ba8475
LC
2069@c *********************************************************************
2070@node GNU Distribution
2071@chapter GNU Distribution
2072
2073Guix comes with a distribution of free software@footnote{The term
2074``free'' here refers to the
2075@url{http://www.gnu.org/philosophy/free-sw.html,freedom provided to
c320011d 2076users of that software}.} that forms the basis of the GNU system. This
a1ba8475
LC
2077includes core GNU packages such as GNU libc, GCC, and Binutils, as well
2078as many GNU and non-GNU applications. The complete list of available
d03bb653
LC
2079packages can be browsed
2080@url{http://www.gnu.org/software/guix/package-list.html,on-line} or by
2081running @command{guix package} (@pxref{Invoking guix package}):
a1ba8475
LC
2082
2083@example
e49951eb 2084guix package --list-available
a1ba8475
LC
2085@end example
2086
401c53c4
LC
2087Our goal is to build a practical 100% free software distribution of
2088Linux-based and other variants of GNU, with a focus on the promotion and
2089tight integration of GNU components, and an emphasis on programs and
2090tools that help users exert that freedom.
2091
c320011d
LC
2092The GNU distribution is currently available on the following platforms:
2093
2094@table @code
2095
2096@item x86_64-linux
2097Intel/AMD @code{x86_64} architecture, Linux-Libre kernel;
2098
2099@item i686-linux
2100Intel 32-bit architecture (IA32), Linux-Libre kernel;
2101
2102@item mips64el-linux
2103little-endian 64-bit MIPS processors, specifically the Loongson series,
2104n32 application binary interface (ABI), and Linux-Libre kernel.
2105
2106@end table
2107
2108@noindent
2109For information on porting to other architectures or kernels,
2110@xref{Porting}.
2111
401c53c4 2112@menu
91ef73d4 2113* Installing Debugging Files:: Feeding the debugger.
401c53c4 2114* Package Modules:: Packages from the programmer's viewpoint.
da7cabd4 2115* Packaging Guidelines:: Growing the distribution.
401c53c4 2116* Bootstrapping:: GNU/Linux built from scratch.
8b315a6d 2117* Porting:: Targeting another platform or kernel.
4af2447e 2118* System Configuration:: Configuring a GNU system.
401c53c4
LC
2119@end menu
2120
2121Building this distribution is a cooperative effort, and you are invited
2122to join! @ref{Contributing}, for information about how you can help.
2123
b208a005 2124
91ef73d4
LC
2125@node Installing Debugging Files
2126@section Installing Debugging Files
2127
2128Program binaries, as produced by the GCC compilers for instance, are
2129typically written in the ELF format, with a section containing
2130@dfn{debugging information}. Debugging information is what allows the
2131debugger, GDB, to map binary code to source code; it is required to
2132debug a compiled program in good conditions.
2133
2134The problem with debugging information is that is takes up a fair amount
2135of disk space. For example, debugging information for the GNU C Library
2136weighs in at more than 60 MiB. Thus, as a user, keeping all the
2137debugging info of all the installed programs is usually not an option.
2138Yet, space savings should not come at the cost of an impediment to
2139debugging---especially in the GNU system, which should make it easier
2140for users to exert their computing freedom (@pxref{GNU Distribution}).
2141
2142Thankfully, the GNU Binary Utilities (Binutils) and GDB provide a
2143mechanism that allows users to get the best of both worlds: debugging
2144information can be stripped from the binaries and stored in separate
2145files. GDB is then able to load debugging information from those files,
2146when they are available (@pxref{Separate Debug Files,,, gdb, Debugging
2147with GDB}).
2148
2149The GNU distribution takes advantage of this by storing debugging
2150information in the @code{lib/debug} sub-directory of a separate package
2151output unimaginatively called @code{debug} (@pxref{Packages with
2152Multiple Outputs}). Users can choose to install the @code{debug} output
2153of a package when they need it. For instance, the following command
2154installs the debugging information for the GNU C Library and for GNU
2155Guile:
2156
2157@example
2158guix package -i glibc:debug -i guile:debug
2159@end example
2160
2161GDB must then be told to look for debug files in the user's profile, by
2162setting the @code{debug-file-directory} variable (consider setting it
2163from the @file{~/.gdbinit} file, @pxref{Startup,,, gdb, Debugging with
2164GDB}):
2165
2166@example
2167(gdb) set debug-file-directory ~/.guix-profile/lib/debug
2168@end example
2169
2170From there on, GDB will pick up debugging information from the
2171@code{.debug} files under @file{~/.guix-profile/lib/debug}.
2172
2173@c XXX: keep me up-to-date
2174The @code{debug} output mechanism in Guix is implemented by the
2175@code{gnu-build-system} (@pxref{Defining Packages}). Currently, it is
2176opt-in---debugging information is available only for those packages
2177whose definition explicitly declares a @code{debug} output. This may be
2178changed to opt-out in the future, if our build farm servers can handle
2179the load. To check whether a package has a @code{debug} output, use
2180@command{guix package --list-available} (@pxref{Invoking guix package}).
2181
2182
401c53c4
LC
2183@node Package Modules
2184@section Package Modules
2185
2186From a programming viewpoint, the package definitions of the
2187distribution are provided by Guile modules in the @code{(gnu packages
7e17f65d
LC
2188@dots{})} name space@footnote{Note that packages under the @code{(gnu
2189packages @dots{})} module name space are not necessarily ``GNU
2190packages''. This module naming scheme follows the usual Guile module
2191naming convention: @code{gnu} means that these modules are distributed
2192as part of the GNU system, and @code{packages} identifies modules that
2193define packages.} (@pxref{Modules, Guile modules,, guile, GNU Guile
b81e1947
LC
2194Reference Manual}). For instance, the @code{(gnu packages emacs)}
2195module exports a variable named @code{emacs}, which is bound to a
401c53c4
LC
2196@code{<package>} object (@pxref{Defining Packages}). The @code{(gnu
2197packages)} module provides facilities for searching for packages.
a1ba8475
LC
2198
2199The distribution is fully @dfn{bootstrapped} and @dfn{self-contained}:
2200each package is built based solely on other packages in the
2201distribution. The root of this dependency graph is a small set of
2202@dfn{bootstrap binaries}, provided by the @code{(gnu packages
b81e1947
LC
2203bootstrap)} module. For more information on bootstrapping,
2204@ref{Bootstrapping}.
2205
da7cabd4
AE
2206@node Packaging Guidelines
2207@section Packaging Guidelines
b81e1947
LC
2208
2209The GNU distribution is nascent and may well lack some of your favorite
2210packages. This section describes how you can help make the distribution
c8c871d1 2211grow. @xref{Contributing}, for additional information on how you can
b81e1947
LC
2212help.
2213
b81e1947
LC
2214Free software packages are usually distributed in the form of
2215@dfn{source code tarballs}---typically @file{tar.gz} files that contain
2216all the source files. Adding a package to the distribution means
2217essentially two things: adding a @dfn{recipe} that describes how to
2218build the package, including a list of other packages required to build
2219it, and adding @dfn{package meta-data} along with that recipe, such as a
2220description and licensing information.
2221
2222In Guix all this information is embodied in @dfn{package definitions}.
2223Package definitions provide a high-level view of the package. They are
2224written using the syntax of the Scheme programming language; in fact,
2225for each package we define a variable bound to the package definition,
2226and export that variable from a module (@pxref{Package Modules}).
2227However, in-depth Scheme knowledge is @emph{not} a prerequisite for
2228creating packages. For more information on package definitions,
2229@ref{Defining Packages}.
2230
2231Once a package definition is in place, stored in a file in the Guix
2232source tree, it can be tested using the @command{guix build} command
2233(@pxref{Invoking guix build}). For example, assuming the new package is
2234called @code{gnew}, you may run this command from the Guix build tree:
2235
2236@example
2237./pre-inst-env guix build gnew --keep-failed
2238@end example
2239
2240Using @code{--keep-failed} makes it easier to debug build failures since
2241it provides access to the failed build tree.
2242
2243Once your package builds correctly, please send us a patch
2244(@pxref{Contributing}). Well, if you need help, we will be happy to
2245help you too. Once the patch is committed in the Guix repository, the
2246new package automatically gets built on the supported platforms by
2247@url{http://hydra.gnu.org/gnu/master, our continuous integration
2248system}.
2249
2250@cindex substituter
2251Users can obtain the new package definition simply by running
2252@command{guix pull} (@pxref{Invoking guix pull}). When
2253@code{hydra.gnu.org} is done building the package, installing the
2254package automatically downloads binaries from there (except when using
2255@code{--no-substitutes}). The only place where human intervention is
2256needed is to review and apply the patch.
401c53c4
LC
2257
2258
da7cabd4 2259@menu
ee85f3db
AE
2260* Software Freedom:: What may go into the distribution.
2261* Package Naming:: What's in a name?
2262* Version Numbers:: When the name is not enough.
2263* Python Modules:: Taming the snake.
da7cabd4
AE
2264@end menu
2265
2266@node Software Freedom
2267@subsection Software Freedom
2268
2269@c Adapted from http://www.gnu.org/philosophy/philosophy.html.
2270
2271The GNU operating system has been developed so that users can have
2272freedom in their computing. GNU is @dfn{free software}, meaning that
2273users have the @url{http://www.gnu.org/philosophy/free-sw.html,four
2274essential freedoms}: to run the program, to study and change the program
2275in source code form, to redistribute exact copies, and to distribute
2276modified versions. Packages found in the GNU distribution provide only
2277software that conveys these four freedoms.
2278
2279In addition, the GNU distribution follow the
2280@url{http://www.gnu.org/distros/free-system-distribution-guidelines.html,free
2281software distribution guidelines}. Among other things, these guidelines
2282reject non-free firmware, recommendations of non-free software, and
2283discuss ways to deal with trademarks and patents.
2284
f9cc8971
LC
2285Some packages contain a small and optional subset that violates the
2286above guidelines, for instance because this subset is itself non-free
2287code. When that happens, the offending items are removed with
2288appropriate patches or code snippets in the package definition's
2289@code{origin} form (@pxref{Defining Packages}). That way, @code{guix
2290build --source} returns the ``freed'' source rather than the unmodified
2291upstream source.
2292
da7cabd4 2293
ee85f3db
AE
2294@node Package Naming
2295@subsection Package Naming
2296
c8c871d1 2297A package has actually two names associated with it:
ee85f3db 2298First, there is the name of the @emph{Scheme variable}, the one following
c8c871d1
AE
2299@code{define-public}. By this name, the package can be made known in the
2300Scheme code, for instance as input to another package. Second, there is
2301the string in the @code{name} field of a package definition. This name
2302is used by package management commands such as
2303@command{guix package} and @command{guix build}.
ee85f3db
AE
2304
2305Both are usually the same and correspond to the lowercase conversion of the
c8c871d1
AE
2306project name chosen upstream. For instance, the GNUnet project is packaged
2307as @code{gnunet}. We do not add @code{lib} prefixes for library packages,
2308unless these are already part of the official project name. But see
2309@ref{Python Modules} for special rules concerning modules for
ee85f3db
AE
2310the Python language.
2311
2312
2313@node Version Numbers
2314@subsection Version Numbers
2315
2316We usually package only the latest version of a given free software
c8c871d1
AE
2317project. But sometimes, for instance for incompatible library versions,
2318two (or more) versions of the same package are needed. These require
2319different Scheme variable names. We use the name as defined
2320in @ref{Package Naming}
ee85f3db
AE
2321for the most recent version; previous versions use the same name, suffixed
2322by @code{-} and the smallest prefix of the version number that may
2323distinguish the two versions.
2324
2325The name inside the package definition is the same for all versions of a
2326package and does not contain any version number.
2327
2328For instance, the versions 2.24.20 and 3.9.12 of GTK+ may be packaged as follows:
c8c871d1 2329
ee85f3db
AE
2330@example
2331(define-public gtk+
2332 (package
2333 (name "gtk+")
2334 (version "3.9.12")
2335 ...))
2336(define-public gtk+-2
2337 (package
2338 (name "gtk+")
2339 (version "2.24.20")
2340 ...))
2341@end example
2342If we also wanted GTK+ 3.8.2, this would be packaged as
2343@example
2344(define-public gtk+-3.8
2345 (package
2346 (name "gtk+")
2347 (version "3.8.2")
2348 ...))
2349@end example
2350
2351
2352@node Python Modules
2353@subsection Python Modules
2354
2355We currently package Python 2 and Python 3, under the Scheme variable names
2356@code{python-2} and @code{python} as explained in @ref{Version Numbers}.
2357To avoid confusion and naming clashes with other programming languages, it
2358seems desirable that the name of a package for a Python module contains
2359the word @code{python}.
c8c871d1 2360
ee85f3db
AE
2361Some modules are compatible with only one version of Python, others with both.
2362If the package Foo compiles only with Python 3, we name it
2363@code{python-foo}; if it compiles only with Python 2, we name it
2364@code{python2-foo}. If it is compatible with both versions, we create two
2365packages with the corresponding names.
2366
2367If a project already contains the word @code{python}, we drop this;
2368for instance, the module python-dateutil is packaged under the names
2369@code{python-dateutil} and @code{python2-dateutil}.
2370
2371
2372
2373
2374
401c53c4
LC
2375@node Bootstrapping
2376@section Bootstrapping
2377
2378@c Adapted from the ELS 2013 paper.
2379
2380@cindex bootstrapping
2381
2382Bootstrapping in our context refers to how the distribution gets built
2383``from nothing''. Remember that the build environment of a derivation
2384contains nothing but its declared inputs (@pxref{Introduction}). So
2385there's an obvious chicken-and-egg problem: how does the first package
2386get built? How does the first compiler get compiled? Note that this is
2387a question of interest only to the curious hacker, not to the regular
2388user, so you can shamelessly skip this section if you consider yourself
2389a ``regular user''.
2390
2391@cindex bootstrap binaries
2392The GNU system is primarily made of C code, with libc at its core. The
2393GNU build system itself assumes the availability of a Bourne shell and
2394command-line tools provided by GNU Coreutils, Awk, Findutils, `sed', and
2395`grep'. Furthermore, build programs---programs that run
2396@code{./configure}, @code{make}, etc.---are written in Guile Scheme
2397(@pxref{Derivations}). Consequently, to be able to build anything at
2398all, from scratch, Guix relies on pre-built binaries of Guile, GCC,
2399Binutils, libc, and the other packages mentioned above---the
2400@dfn{bootstrap binaries}.
2401
2402These bootstrap binaries are ``taken for granted'', though we can also
ba7ea5ce 2403re-create them if needed (more on that later).
401c53c4
LC
2404
2405@unnumberedsubsec Preparing to Use the Bootstrap Binaries
2406
2407@c As of Emacs 24.3, Info-mode displays the image, but since it's a
2408@c large image, it's hard to scroll. Oh well.
29f66ddd 2409@image{images/bootstrap-graph,6in,,Dependency graph of the early bootstrap derivations}
401c53c4
LC
2410
2411The figure above shows the very beginning of the dependency graph of the
2412distribution, corresponding to the package definitions of the @code{(gnu
2413packages bootstrap)} module. At this level of detail, things are
2414slightly complex. First, Guile itself consists of an ELF executable,
2415along with many source and compiled Scheme files that are dynamically
2416loaded when it runs. This gets stored in the @file{guile-2.0.7.tar.xz}
2417tarball shown in this graph. This tarball is part of Guix's ``source''
2418distribution, and gets inserted into the store with @code{add-to-store}
2419(@pxref{The Store}).
a1ba8475 2420
401c53c4
LC
2421But how do we write a derivation that unpacks this tarball and adds it
2422to the store? To solve this problem, the @code{guile-bootstrap-2.0.drv}
2423derivation---the first one that gets built---uses @code{bash} as its
2424builder, which runs @code{build-bootstrap-guile.sh}, which in turn calls
2425@code{tar} to unpack the tarball. Thus, @file{bash}, @file{tar},
2426@file{xz}, and @file{mkdir} are statically-linked binaries, also part of
2427the Guix source distribution, whose sole purpose is to allow the Guile
2428tarball to be unpacked.
2429
2430Once @code{guile-bootstrap-2.0.drv} is built, we have a functioning
2431Guile that can be used to run subsequent build programs. Its first task
2432is to download tarballs containing the other pre-built binaries---this
2433is what the @code{.tar.xz.drv} derivations do. Guix modules such as
2434@code{ftp-client.scm} are used for this purpose. The
2435@code{module-import.drv} derivations import those modules in a directory
2436in the store, using the original layout. The
2437@code{module-import-compiled.drv} derivations compile those modules, and
2438write them in an output directory with the right layout. This
2439corresponds to the @code{#:modules} argument of
2440@code{build-expression->derivation} (@pxref{Derivations}).
2441
2442Finally, the various tarballs are unpacked by the
2443derivations @code{gcc-bootstrap-0.drv}, @code{glibc-bootstrap-0.drv},
2444etc., at which point we have a working C tool chain.
2445
2446
2447@unnumberedsubsec Building the Build Tools
2448
2449@c TODO: Add a package-level dependency graph generated from (gnu
2450@c packages base).
2451
2452Bootstrapping is complete when we have a full tool chain that does not
2453depend on the pre-built bootstrap tools discussed above. This
2454no-dependency requirement is verified by checking whether the files of
2455the final tool chain contain references to the @file{/nix/store}
2456directories of the bootstrap inputs. The process that leads to this
2457``final'' tool chain is described by the package definitions found in
2458the @code{(gnu packages base)} module.
2459
2460@c See <http://lists.gnu.org/archive/html/gnu-system-discuss/2012-10/msg00000.html>.
2461The first tool that gets built with the bootstrap binaries is
2462GNU Make, which is a prerequisite for all the following packages.
2463From there Findutils and Diffutils get built.
2464
2465Then come the first-stage Binutils and GCC, built as pseudo cross
2466tools---i.e., with @code{--target} equal to @code{--host}. They are
2467used to build libc. Thanks to this cross-build trick, this libc is
2468guaranteed not to hold any reference to the initial tool chain.
2469
2470From there the final Binutils and GCC are built. GCC uses @code{ld}
2471from the final Binutils, and links programs against the just-built libc.
2472This tool chain is used to build the other packages used by Guix and by
2473the GNU Build System: Guile, Bash, Coreutils, etc.
2474
2475And voilà! At this point we have the complete set of build tools that
2476the GNU Build System expects. These are in the @code{%final-inputs}
2477variables of the @code{(gnu packages base)} module, and are implicitly
2478used by any package that uses @code{gnu-build-system} (@pxref{Defining
2479Packages}).
2480
2481
2482@unnumberedsubsec Building the Bootstrap Binaries
2483
2484Because the final tool chain does not depend on the bootstrap binaries,
2485those rarely need to be updated. Nevertheless, it is useful to have an
2486automated way to produce them, should an update occur, and this is what
2487the @code{(gnu packages make-bootstrap)} module provides.
2488
2489The following command builds the tarballs containing the bootstrap
2490binaries (Guile, Binutils, GCC, libc, and a tarball containing a mixture
2491of Coreutils and other basic command-line tools):
a1ba8475 2492
401c53c4
LC
2493@example
2494guix build bootstrap-tarballs
2495@end example
2496
2497The generated tarballs are those that should be referred to in the
2498@code{(gnu packages bootstrap)} module mentioned at the beginning of
2499this section.
2500
2501Still here? Then perhaps by now you've started to wonder: when do we
2502reach a fixed point? That is an interesting question! The answer is
2503unknown, but if you would like to investigate further (and have
2504significant computational and storage resources to do so), then let us
2505know.
a1ba8475 2506
8b315a6d
LC
2507@node Porting
2508@section Porting to a New Platform
2509
2510As discussed above, the GNU distribution is self-contained, and
2511self-containment is achieved by relying on pre-built ``bootstrap
2512binaries'' (@pxref{Bootstrapping}). These binaries are specific to an
2513operating system kernel, CPU architecture, and application binary
2514interface (ABI). Thus, to port the distribution to a platform that is
2515not yet supported, one must build those bootstrap binaries, and update
2516the @code{(gnu packages bootstrap)} module to use them on that platform.
2517
2518Fortunately, Guix can @emph{cross compile} those bootstrap binaries.
2519When everything goes well, and assuming the GNU tool chain supports the
2520target platform, this can be as simple as running a command like this
2521one:
2522
2523@example
2524guix build --target=armv5tel-linux-gnueabi bootstrap-tarballs
2525@end example
2526
72e25e35
LC
2527Once these are built, the @code{(gnu packages bootstrap)} module needs
2528to be updated to refer to these binaries on the target platform. In
2529addition, the @code{glibc-dynamic-linker} procedure in that module must
2530be augmented to return the right file name for libc's dynamic linker on
2531that platform; likewise, @code{system->linux-architecture} in @code{(gnu
2532packages linux)} must be taught about the new platform.
2533
8b315a6d
LC
2534In practice, there may be some complications. First, it may be that the
2535extended GNU triplet that specifies an ABI (like the @code{eabi} suffix
2536above) is not recognized by all the GNU tools. Typically, glibc
2537recognizes some of these, whereas GCC uses an extra @code{--with-abi}
ba7ea5ce 2538configure flag (see @code{gcc.scm} for examples of how to handle this).
8b315a6d
LC
2539Second, some of the required packages could fail to build for that
2540platform. Lastly, the generated binaries could be broken for some
2541reason.
2542
9bf3c1a7 2543
4af2447e
LC
2544@node System Configuration
2545@section System Configuration
2546
2547@emph{This section documents work-in-progress. As such it may be
2548incomplete, outdated, or open to discussions. Please discuss it on
2549@email{guix-devel@@gnu.org}.}
2550
2551@cindex system configuration
2552The GNU system supports a consistent whole-system configuration
2553mechanism. By that we mean that all aspects of the global system
2554configuration---such as the available system services, timezone and
68ad877c 2555locale settings, user accounts---are declared in a single place. Such
4af2447e
LC
2556a @dfn{system configuration} can be @dfn{instantiated}---i.e., effected.
2557
68ad877c
LC
2558One of the advantages of putting all the system configuration under the
2559control of Guix is that it supports transactional system upgrades, and
2560makes it possible to roll-back to a previous system instantiation,
2561should something go wrong with the new one (@pxref{Features}). Another
2562one is that it makes it easy to replicate the exact same configuration
2563across different machines, or at different points in time, without
2564having to resort to additional administration tools layered on top of
2565the system's own tools.
2566@c Yes, we're talking of Puppet, Chef, & co. here. ↑
2567
4af2447e
LC
2568This section describes this mechanism. First we focus on the system
2569administrator's viewpoint---explaining how the system is configured and
2570instantiated. Then we show how this mechanism can be extended, for
2571instance to support new system services.
2572
2573@menu
523e4896
LC
2574* Using the Configuration System:: Customizing your GNU system.
2575* Invoking guix system:: Instantiating a system configuration.
2576* Defining Services:: Adding new service definitions.
4af2447e
LC
2577@end menu
2578
2579@node Using the Configuration System
2580@subsection Using the Configuration System
2581
2582The operating system is configured by filling in an
2583@code{operating-system} structure, as defined by the @code{(gnu system)}
2584module. A simple setup, with the default system services, the default
2585Linux-Libre kernel, initial RAM disk, and boot loader looks like this:
2586
2587@findex operating-system
2588@lisp
8b198abe 2589(use-modules (gnu services base) ; for '%base-services'
db4fdc04 2590 (gnu services ssh) ; for 'lsh-service'
4af2447e 2591 (gnu system shadow) ; for 'user-account'
4af2447e
LC
2592 (gnu packages base) ; Coreutils, grep, etc.
2593 (gnu packages bash) ; Bash
4eaad71d 2594 (gnu packages admin) ; dmd, Inetutils
4af2447e
LC
2595 (gnu packages zile) ; Zile
2596 (gnu packages less) ; less
2597 (gnu packages guile) ; Guile
2598 (gnu packages linux)) ; procps, psmisc
2599
68ad877c 2600(define komputilo
4af2447e
LC
2601 (operating-system
2602 (host-name "komputilo")
2603 (timezone "Europe/Paris")
2604 (locale "fr_FR.UTF-8")
2605 (users (list (user-account
2606 (name "alice")
2607 (password "")
2608 (uid 1000) (gid 100)
2609 (comment "Bob's sister")
2610 (home-directory "/home/alice"))))
2611 (packages (list coreutils bash guile-2.0
2612 guix dmd
2613 inetutils
2614 findutils grep sed
2615 procps psmisc
2616 zile less))
2617 (services (cons (lsh-service #:port 2222 #:allow-root-login? #t)
8b198abe 2618 %base-services))))
4af2447e
LC
2619@end lisp
2620
2621This example should be self-describing. The @code{packages} field lists
68ad877c
LC
2622packages provided by the various @code{(gnu packages ...)} modules above
2623(@pxref{Package Modules}). These are the packages that will be globally
2624visible on the system, for all user accounts---i.e., in every user's
2625@code{PATH} environment variable---in addition to the per-user profiles
2626(@pxref{Invoking guix package}).
4af2447e 2627
8b198abe 2628@vindex %base-services
4af2447e 2629The @code{services} field lists @dfn{system services} to be made
8b198abe 2630available when the system starts. The @var{%base-services} list,
db4fdc04 2631from the @code{(gnu services base)} module, provides the basic services one
4af2447e
LC
2632would expect from a GNU system: a login service (mingetty) on each tty,
2633syslogd, libc's name service cache daemon (nscd), etc.
2634
2635The @code{operating-system} declaration above specifies that, in
2636addition to those services, we want the @command{lshd} secure shell
2637daemon listening on port 2222, and allowing remote @code{root} logins
2638(@pxref{Invoking lshd,,, lsh, GNU lsh Manual}). Under the hood,
2639@code{lsh-service} arranges so that @code{lshd} is started with the
2640right command-line options, possibly with supporting configuration files
2641generated as needed (@pxref{Defining Services}).
2642
4af2447e 2643Assuming the above snippet is stored in the @file{my-system-config.scm}
523e4896
LC
2644file, the @command{guix system boot my-system-config.scm} command
2645instantiates that configuration, and makes it the default GRUB boot
2646entry (@pxref{Invoking guix system}). The normal way to change the
2647system's configuration is by updating this file and re-running the
2648@command{guix system} command.
4af2447e
LC
2649
2650At the Scheme level, the bulk of an @code{operating-system} declaration
2651is instantiated with the following monadic procedure (@pxref{The Store
2652Monad}):
2653
2654@deffn {Monadic Procedure} operating-system-derivation os
2655Return a derivation that builds @var{os}, an @code{operating-system}
2656object (@pxref{Derivations}).
2657
2658The output of the derivation is a single directory that refers to all
2659the packages, configuration files, and other supporting files needed to
2660instantiate @var{os}.
2661@end deffn
2662
523e4896
LC
2663@node Invoking guix system
2664@subsection Invoking @code{guix system}
2665
2666Once you have written an operating system declaration, as seen in the
2667previous section, it can be @dfn{instantiated} using the @command{guix
2668system} command. The synopsis is:
2669
2670@example
2671guix system @var{options}@dots{} @var{action} @var{file}
2672@end example
2673
2674@var{file} must be the name of a file containing an
2675@code{operating-system} declaration. @var{action} specifies how the
2676operating system is instantiate. Currently only one value is supported:
2677
2678@table @code
2679@item vm
2680@cindex virtual machine
2681Build a virtual machine that contain the operating system declared in
2682@var{file}, and return a script to run that virtual machine (VM).
2683
2684The VM shares its store with the host system.
2685@end table
2686
2687@var{options} can contain any of the common build options provided by
2688@command{guix build} (@pxref{Invoking guix build}).
2689
4af2447e
LC
2690
2691@node Defining Services
2692@subsection Defining Services
2693
db4fdc04 2694The @code{(gnu services @dots{})} modules define several procedures that allow
4af2447e
LC
2695users to declare the operating system's services (@pxref{Using the
2696Configuration System}). These procedures are @emph{monadic
2697procedures}---i.e., procedures that return a monadic value in the store
2698monad (@pxref{The Store Monad}). Examples of such procedures include:
2699
2700@table @code
2701@item mingetty-service
2702return the definition of a service that runs @command{mingetty} to
2703offer a login service on the given console tty;
2704
2705@item nscd-service
2706return a definition for libc's name service cache daemon (nscd);
2707
2708@item guix-service
2709return a definition for a service that runs @command{guix-daemon}
2710(@pxref{Invoking guix-daemon}).
2711@end table
2712
2713@cindex service definition
2714The monadic value returned by those procedures is a @dfn{service
2715definition}---a structure as returned by the @code{service} form.
2716Service definitions specifies the inputs the service depends on, and an
2717expression to start and stop the service. Behind the scenes, service
2718definitions are ``translated'' into the form suitable for the
2719configuration file of dmd, the init system (@pxref{Services,,, dmd, GNU
2720dmd Manual}).
2721
2722As an example, here is what the @code{nscd-service} procedure looks
2723like:
2724
2725@lisp
2726(define (nscd-service)
2727 (mlet %store-monad ((nscd (package-file glibc "sbin/nscd")))
2728 (return (service
2729 (documentation "Run libc's name service cache daemon.")
2730 (provision '(nscd))
2731 (start `(make-forkexec-constructor ,nscd "-f" "/dev/null"
2732 "--foreground"))
2733 (stop `(make-kill-destructor))
2734
2735 (respawn? #f)
2736 (inputs `(("glibc" ,glibc)))))))
2737@end lisp
2738
2739@noindent
2740The @code{inputs} field specifies that this service depends on the
2741@var{glibc} package---the package that contains the @command{nscd}
2742program. The @code{start} and @code{stop} fields are expressions that
2743make use of dmd's facilities to start and stop processes (@pxref{Service
2744De- and Constructors,,, dmd, GNU dmd Manual}). The @code{provision}
2745field specifies the name under which this service is known to dmd, and
2746@code{documentation} specifies on-line documentation. Thus, the
2747commands @command{deco start ncsd}, @command{deco stop nscd}, and
2748@command{deco doc nscd} will do what you would expect (@pxref{Invoking
2749deco,,, dmd, GNU dmd Manual}).
2750
2751
9bf3c1a7
LC
2752@c *********************************************************************
2753@node Contributing
2754@chapter Contributing
2755
2756This project is a cooperative effort, and we need your help to make it
63f6004b 2757grow! Please get in touch with us on @email{guix-devel@@gnu.org}. We
a1ba8475 2758welcome ideas, bug reports, patches, and anything that may be helpful to
da7cabd4
AE
2759the project. We particularly welcome help on packaging
2760(@pxref{Packaging Guidelines}).
a1ba8475 2761
9bf3c1a7
LC
2762Please see the
2763@url{http://git.savannah.gnu.org/cgit/guix.git/tree/HACKING,
2764@file{HACKING} file} that comes with the Guix source code for practical
2765details about contributions.
2766
c78bd12b 2767
568717fd
LC
2768@c *********************************************************************
2769@node Acknowledgments
2770@chapter Acknowledgments
2771
2772Guix is based on the Nix package manager, which was designed and
2773implemented by Eelco Dolstra. Nix pioneered functional package
2774management, and promoted unprecedented features, such as transactional
2775package upgrades and rollbacks, per-user profiles, and referentially
2776transparent build processes. Without this work, Guix would not exist.
2777
2778The Nix-based software distributions, Nixpkgs and NixOS, have also been
2779an inspiration for Guix.
2780
2781@c *********************************************************************
2782@node GNU Free Documentation License
2783@appendix GNU Free Documentation License
2784
2785@include fdl-1.3.texi
2786
2787@c *********************************************************************
2788@node Concept Index
2789@unnumbered Concept Index
2790@printindex cp
2791
2792@node Function Index
2793@unnumbered Function Index
2794@printindex fn
2795
2796@bye
2797
2798@c Local Variables:
2799@c ispell-local-dictionary: "american";
2800@c End: