doc: cookbook: Use @result{} & co. instead of a '>' prompt.
[jackhill/guix/guix.git] / doc / guix-cookbook.texi
CommitLineData
7bc46ecc
RW
1\input texinfo
2@c -*-texinfo-*-
3
4@c %**start of header
5@setfilename guix-cookbook.info
6@documentencoding UTF-8
7@settitle GNU Guix Cookbook
8@c %**end of header
9
10@copying
11Copyright @copyright{} 2019 Ricardo Wurmus@*
12Copyright @copyright{} 2019 Efraim Flashner@*
13Copyright @copyright{} 2019 Pierre Neidhardt@*
14
15Permission is granted to copy, distribute and/or modify this document
16under the terms of the GNU Free Documentation License, Version 1.3 or
17any later version published by the Free Software Foundation; with no
18Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
19copy of the license is included in the section entitled ``GNU Free
20Documentation License''.
21@end copying
22
23@dircategory System administration
24@direntry
25* Guix cookbook: (guix-cookbook). Tutorials and examples for GNU Guix.
26@end direntry
27
28@titlepage
29@title GNU Guix Cookbook
30@subtitle Tutorials and examples for using the GNU Guix Functional Package Manager
31@author The GNU Guix Developers
32
33@page
34@vskip 0pt plus 1filll
35
36@insertcopying
37@end titlepage
38
39@contents
40
41@c *********************************************************************
42@node Top
43@top GNU Guix Cookbook
44
45This document presents tutorials and detailed examples for GNU@tie{}Guix, a
46functional package management tool written for the GNU system. Please
47@pxref{Top,,, guix, GNU Guix reference manual} for details about the system,
48its API, and related concepts.
49
50@c TRANSLATORS: You can replace the following paragraph with information on
51@c how to join your own translation team and how to report issues with the
52@c translation.
53If you would like to translate this document in your native language, consider
54joining the @uref{https://translationproject.org/domain/guix-cookbook.html,
55Translation Project}.
56
57@menu
58* Scheme tutorials:: Meet your new favorite language!
59* Packaging:: Packaging tutorials
60* System Configuration:: Customizing the GNU System
4c463569 61* Advanced package management:: Power to the users!
7bc46ecc
RW
62
63* Acknowledgments:: Thanks!
64* GNU Free Documentation License:: The license of this document.
65* Concept Index:: Concepts.
66
67@detailmenu
68 --- The Detailed Node Listing ---
69
70Scheme tutorials
71
72* A Scheme Crash Course:: Learn the basics of Scheme
73
74Packaging
75
76* Packaging Tutorial:: Let's add a package to Guix!
77
78System Configuration
79
80* Customizing the Kernel:: Creating and using a custom Linux kernel
81
82
83@end detailmenu
84@end menu
85
86@c *********************************************************************
87@node Scheme tutorials
88@chapter Scheme tutorials
89
90GNU@tie{}Guix is written in the general purpose programming language Scheme,
91and many of its features can be accessed and manipulated programmatically.
92You can use Scheme to generate package definitions, to modify them, to build
93them, to deploy whole operating systems, etc.
94
95Knowing the basics of how to program in Scheme will unlock many of the
96advanced features Guix provides --- and you don't even need to be an
97experienced programmer to use them!
98
99Let's get started!
100
101@node A Scheme Crash Course
102@section A Scheme Crash Course
103
104@cindex Scheme, crash course
105
106Guix uses the Guile implementation of Scheme. To start playing with the
107language, install it with @code{guix install guile} and start a
108@uref{https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop,
109REPL} by running @code{guile} from the command line.
110
111Alternatively you can also run @code{guix environment --ad-hoc guile -- guile}
112if you'd rather not have Guile installed in your user profile.
113
cf1e6f5f
LC
114In the following examples, lines show what you would type at the REPL;
115lines starting with ``@result{}'' show evaluation results, while lines
116starting with ``@print{}'' show things that get printed. @xref{Using Guile
117Interactively,,, guile, GNU Guile Reference Manual}), for more details on the
7bc46ecc
RW
118REPL.
119
120@itemize
121@item
122Scheme syntax boils down to a tree of expressions (or @emph{s-expression} in
123Lisp lingo). An expression can be a literal such as numbers and strings, or a
124compound which is a parenthesized list of compounds and literals. @code{#t}
0cbef07b 125and @code{#f} stand for the Booleans ``true'' and ``false'', respectively.
7bc46ecc
RW
126
127Examples of valid expressions:
128
b1eecb5c 129@lisp
7bc46ecc 130"Hello World!"
cf1e6f5f
LC
131@result{} "Hello World!"
132
7bc46ecc 13317
cf1e6f5f
LC
134@result{} 17
135
136(display (string-append "Hello " "Guix" "\n"))
137@print{} Hello Guix!
138@result{} #<unspecified>
b1eecb5c 139@end lisp
7bc46ecc
RW
140
141@item
142This last example is a function call nested in another function call. When a
143parenthesized expression is evaluated, the first term is the function and the
144rest are the arguments passed to the function. Every function returns the
145last evaluated expression as its return value.
146
147@item
148Anonymous functions are declared with the @code{lambda} term:
149
b1eecb5c 150@lisp
cf1e6f5f
LC
151(lambda (x) (* x x))
152@result{} #<procedure 120e348 at <unknown port>:24:0 (x)>
b1eecb5c 153@end lisp
7bc46ecc
RW
154
155The above procedure returns the square of its argument. Since everything is
156an expression, the @code{lambda} expression returns an anonymous procedure,
157which can in turn be applied to an argument:
158
b1eecb5c 159@lisp
cf1e6f5f
LC
160((lambda (x) (* x x)) 3)
161@result{} 9
b1eecb5c 162@end lisp
7bc46ecc
RW
163
164@item
165Anything can be assigned a global name with @code{define}:
166
b1eecb5c 167@lisp
cf1e6f5f
LC
168(define a 3)
169(define square (lambda (x) (* x x)))
170(square a)
171@result{} 9
b1eecb5c 172@end lisp
7bc46ecc
RW
173
174@item
175Procedures can be defined more concisely with the following syntax:
176
b1eecb5c 177@lisp
7bc46ecc 178(define (square x) (* x x))
b1eecb5c 179@end lisp
7bc46ecc
RW
180
181@item
182A list structure can be created with the @code{list} procedure:
183
b1eecb5c 184@lisp
cf1e6f5f
LC
185(list 2 a 5 7)
186@result{} (2 3 5 7)
b1eecb5c 187@end lisp
7bc46ecc
RW
188
189@item
190The @emph{quote} disables evaluation of a parenthesized expression: the first
191term is not called over the other terms. Thus it effectively returns a list
192of terms.
193
b1eecb5c 194@lisp
cf1e6f5f
LC
195'(display (string-append "Hello " "Guix" "\n"))
196@result{} (display (string-append "Hello " "Guix" "\n"))
197
198'(2 a 5 7)
199@result{} (2 a 5 7)
b1eecb5c 200@end lisp
7bc46ecc
RW
201
202@item
cf1e6f5f 203The @dfn{quasiquote} disables evaluation of a parenthesized expression until
7bc46ecc
RW
204a comma re-enables it. Thus it provides us with fine-grained control over
205what is evaluated and what is not.
206
b1eecb5c 207@lisp
cf1e6f5f
LC
208`(2 a 5 7 (2 ,a 5 ,(+ a 4)))
209@result{} (2 a 5 7 (2 3 5 7))
b1eecb5c 210@end lisp
7bc46ecc
RW
211
212Note that the above result is a list of mixed elements: numbers, symbols (here
213@code{a}) and the last element is a list itself.
214
215@item
216Multiple variables can be named locally with @code{let}:
217
b1eecb5c 218@lisp
cf1e6f5f
LC
219(define x 10)
220(let ((x 2)
221 (y 3))
222 (list x y))
223@result{} (2 3)
224
225x
226@result{} 10
227
228y
229@error{} In procedure module-lookup: Unbound variable: y
b1eecb5c 230@end lisp
7bc46ecc
RW
231
232Use @code{let*} to allow later variable declarations to refer to earlier
233definitions.
234
b1eecb5c 235@lisp
cf1e6f5f
LC
236(let* ((x 2)
237 (y (* x 3)))
238 (list x y))
239@result{} (2 6)
b1eecb5c 240@end lisp
7bc46ecc
RW
241
242@item
243The keyword syntax is @code{#:}; it is used to create unique identifiers.
244@pxref{Keywords,,, guile, GNU Guile Reference Manual}.
245
246@item
247The percentage @code{%} is typically used for read-only global variables in
248the build stage. Note that it is merely a convention, like @code{_} in C.
249Scheme treats @code{%} exactly the same as any other letter.
250
251@item
252Modules are created with @code{define-module}. For instance
253
b1eecb5c 254@lisp
7bc46ecc
RW
255(define-module (guix build-system ruby)
256 #:use-module (guix store)
257 #:export (ruby-build
258 ruby-build-system))
b1eecb5c 259@end lisp
7bc46ecc
RW
260
261defines the module @code{guix build-system ruby} which must be located in
262@file{guix/build-system/ruby.scm} somewhere in the Guile load path. It
263depends on the @code{(guix store)} module and it exports two variables,
264@code{ruby-build} and @code{ruby-build-system}.
265@end itemize
266
267For a more detailed introduction, check out
268@uref{http://www.troubleshooters.com/codecorn/scheme_guile/hello.htm, Scheme
269at a Glance}, by Steve Litt.
270
271One of the reference Scheme books is the seminal ``Structure and
272Interpretation of Computer Programs'', by Harold Abelson and Gerald Jay
273Sussman, with Julie Sussman. You'll find a
274@uref{https://mitpress.mit.edu/sites/default/files/sicp/index.html, free copy
275online}, together with
276@uref{https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/,
277videos of the lectures by the authors}. The book is available in Texinfo
278format as the @code{sicp} Guix package. Go ahead, run @code{guix install
ed79636c 279sicp} and start reading with @code{info sicp} (@pxref{,,, sicp, Structure and Interpretation of Computer Programs}).
7bc46ecc
RW
280An @uref{https://sarabander.github.io/sicp/, unofficial ebook is also
281available}.
282
283You'll find more books, tutorials and other resources at
284@url{https://schemers.org/}.
285
286
287@c *********************************************************************
288@node Packaging
289@chapter Packaging
290
291@cindex packaging
292
293This chapter is dedicated to teaching you how to add packages to the
294collection of packages that come with GNU Guix. This involves writing package
295definitions in Guile Scheme, organizing them in package modules, and building
296them.
297
298@menu
299* Packaging Tutorial:: A tutorial on how to add packages to Guix.
300@end menu
301
302@node Packaging Tutorial
303@section Packaging Tutorial
304
305GNU Guix stands out as the @emph{hackable} package manager, mostly because it
306uses @uref{https://www.gnu.org/software/guile/, GNU Guile}, a powerful
307high-level programming language, one of the
308@uref{https://en.wikipedia.org/wiki/Scheme_%28programming_language%29, Scheme}
309dialects from the
310@uref{https://en.wikipedia.org/wiki/Lisp_%28programming_language%29, Lisp family}.
311
312Package definitions are also written in Scheme, which empowers Guix in some
313very unique ways, unlike most other package managers that use shell scripts or
314simple languages.
315
316@itemize
317@item
318Use functions, structures, macros and all of Scheme expressiveness for your
319package definitions.
320
321@item
322Inheritance makes it easy to customize a package by inheriting from it and
323modifying only what is needed.
324
325@item
326Batch processing: the whole package collection can be parsed, filtered and
327processed. Building a headless server with all graphical interfaces stripped
328out? It's possible. Want to rebuild everything from source using specific
329compiler optimization flags? Pass the @code{#:make-flags "..."} argument to
330the list of packages. It wouldn't be a stretch to think
331@uref{https://wiki.gentoo.org/wiki/USE_flag, Gentoo USE flags} here, but this
332goes even further: the changes don't have to be thought out beforehand by the
333packager, they can be @emph{programmed} by the user!
334@end itemize
335
336The following tutorial covers all the basics around package creation with Guix.
337It does not assume much knowledge of the Guix system nor of the Lisp language.
338The reader is only expected to be familiar with the command line and to have some
339basic programming knowledge.
340
0cbef07b
LC
341@node A ``Hello World'' package
342@subsection A ``Hello World'' package
7bc46ecc 343
0cbef07b 344The ``Defining Packages'' section of the manual introduces the basics of Guix
7bc46ecc
RW
345packaging (@pxref{Defining Packages,,, guix, GNU Guix Reference Manual}). In
346the following section, we will partly go over those basics again.
347
0cbef07b 348GNU@tie{}Hello is a dummy project that serves as an idiomatic example for
7bc46ecc
RW
349packaging. It uses the GNU build system (@code{./configure && make && make
350install}). Guix already provides a package definition which is a perfect
351example to start with. You can look up its declaration with @code{guix edit
352hello} from the command line. Let's see how it looks:
353
b1eecb5c 354@lisp
7bc46ecc
RW
355(define-public hello
356 (package
357 (name "hello")
358 (version "2.10")
359 (source (origin
360 (method url-fetch)
361 (uri (string-append "mirror://gnu/hello/hello-" version
362 ".tar.gz"))
363 (sha256
364 (base32
365 "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i"))))
366 (build-system gnu-build-system)
367 (synopsis "Hello, GNU world: An example GNU package")
368 (description
369 "GNU Hello prints the message \"Hello, world!\" and then exits. It
370serves as an example of standard GNU coding practices. As such, it supports
371command-line arguments, multiple languages, and so on.")
372 (home-page "https://www.gnu.org/software/hello/")
373 (license gpl3+)))
b1eecb5c 374@end lisp
7bc46ecc
RW
375
376As you can see, most of it is rather straightforward. But let's review the
377fields together:
378
379@table @samp
380@item name
381The project name. Using Scheme conventions, we prefer to keep it
382lower case, without underscore and using dash-separated words.
383
384@item source
385This field contains a description of the source code origin. The
386@code{origin} record contains these fields:
387
388@enumerate
389@item The method, here @code{url-fetch} to download via HTTP/FTP, but other methods
390 exist, such as @code{git-fetch} for Git repositories.
391@item The URI, which is typically some @code{https://} location for @code{url-fetch}. Here
392 the special `mirror://gnu` refers to a set of well known locations, all of
393 which can be used by Guix to fetch the source, should some of them fail.
394@item The @code{sha256} checksum of the requested file. This is essential to ensure
395 the source is not corrupted. Note that Guix works with base32 strings,
396 hence the call to the @code{base32} function.
397@end enumerate
398
399@item build-system
400
401This is where the power of abstraction provided by the Scheme language really
402shines: in this case, the @code{gnu-build-system} abstracts away the famous
403@code{./configure && make && make install} shell invocations. Other build
404systems include the @code{trivial-build-system} which does not do anything and
405requires from the packager to program all the build steps, the
406@code{python-build-system}, the @code{emacs-build-system}, and many more
407(@pxref{Build Systems,,, guix, GNU Guix Reference Manual}).
408
409@item synopsis
410It should be a concise summary of what the package does. For many packages a
411tagline from the project's home page can be used as the synopsis.
412
413@item description
414Same as for the synopsis, it's fine to re-use the project description from the
415homepage. Note that Guix uses Texinfo syntax.
416
417@item home-page
418Use HTTPS if available.
419
420@item license
421See @code{guix/licenses.scm} in the project source for a full list of
422available licenses.
423@end table
424
425Time to build our first package! Nothing fancy here for now: we will stick to a
0cbef07b 426dummy @code{my-hello}, a copy of the above declaration.
7bc46ecc 427
0cbef07b
LC
428As with the ritualistic ``Hello World'' taught with most programming languages,
429this will possibly be the most ``manual'' approach. We will work out an ideal
7bc46ecc
RW
430setup later; for now we will go the simplest route.
431
432Save the following to a file @file{my-hello.scm}.
433
b1eecb5c 434@lisp
7bc46ecc
RW
435(use-modules (guix packages)
436 (guix download)
437 (guix build-system gnu)
438 (guix licenses))
439
440(package
441 (name "my-hello")
442 (version "2.10")
443 (source (origin
444 (method url-fetch)
445 (uri (string-append "mirror://gnu/hello/hello-" version
446 ".tar.gz"))
447 (sha256
448 (base32
449 "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i"))))
450 (build-system gnu-build-system)
451 (synopsis "Hello, Guix world: An example custom Guix package")
452 (description
453 "GNU Hello prints the message \"Hello, world!\" and then exits. It
454serves as an example of standard GNU coding practices. As such, it supports
455command-line arguments, multiple languages, and so on.")
456 (home-page "https://www.gnu.org/software/hello/")
457 (license gpl3+))
b1eecb5c 458@end lisp
7bc46ecc
RW
459
460We will explain the extra code in a moment.
461
462Feel free to play with the different values of the various fields. If you
463change the source, you'll need to update the checksum. Indeed, Guix refuses to
464build anything if the given checksum does not match the computed checksum of the
465source code. To obtain the correct checksum of the package declaration, we
466need to download the source, compute the sha256 checksum and convert it to
467base32.
468
469Thankfully, Guix can automate this task for us; all we need is to provide the
470URI:
471
472@c TRANSLATORS: This is example shell output.
473@example sh
474$ guix download mirror://gnu/hello/hello-2.10.tar.gz
475
476Starting download of /tmp/guix-file.JLYgL7
477From https://ftpmirror.gnu.org/gnu/hello/hello-2.10.tar.gz...
478following redirection to `https://mirror.ibcp.fr/pub/gnu/hello/hello-2.10.tar.gz'...
479 …10.tar.gz 709KiB 2.5MiB/s 00:00 [##################] 100.0%
480/gnu/store/hbdalsf5lpf01x4dcknwx6xbn6n5km6k-hello-2.10.tar.gz
4810ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i
482@end example
483
484In this specific case the output tells us which mirror was chosen.
485If the result of the above command is not the same as in the above snippet,
486update your @code{my-hello} declaration accordingly.
487
488Note that GNU package tarballs come with an OpenPGP signature, so you
489should definitely check the signature of this tarball with `gpg` to
490authenticate it before going further:
491
492@c TRANSLATORS: This is example shell output.
493@example sh
494$ guix download mirror://gnu/hello/hello-2.10.tar.gz.sig
495
496Starting download of /tmp/guix-file.03tFfb
497From https://ftpmirror.gnu.org/gnu/hello/hello-2.10.tar.gz.sig...
498following redirection to `https://ftp.igh.cnrs.fr/pub/gnu/hello/hello-2.10.tar.gz.sig'...
499 ….tar.gz.sig 819B 1.2MiB/s 00:00 [##################] 100.0%
500/gnu/store/rzs8wba9ka7grrmgcpfyxvs58mly0sx6-hello-2.10.tar.gz.sig
5010q0v86n3y38z17rl146gdakw9xc4mcscpk8dscs412j22glrv9jf
502$ gpg --verify /gnu/store/rzs8wba9ka7grrmgcpfyxvs58mly0sx6-hello-2.10.tar.gz.sig /gnu/store/hbdalsf5lpf01x4dcknwx6xbn6n5km6k-hello-2.10.tar.gz
503gpg: Signature made Sun 16 Nov 2014 01:08:37 PM CET
504gpg: using RSA key A9553245FDE9B739
505gpg: Good signature from "Sami Kerola <kerolasa@@iki.fi>" [unknown]
506gpg: aka "Sami Kerola (http://www.iki.fi/kerolasa/) <kerolasa@@iki.fi>" [unknown]
507gpg: WARNING: This key is not certified with a trusted signature!
508gpg: There is no indication that the signature belongs to the owner.
509Primary key fingerprint: 8ED3 96E3 7E38 D471 A005 30D3 A955 3245 FDE9 B739
510@end example
511
512You can then happily run
513
514@c TRANSLATORS: Do not translate this command
515@example sh
516$ guix package --install-from-file=my-hello.scm
517@end example
518
519You should now have @code{my-hello} in your profile!
520
521@c TRANSLATORS: Do not translate this command
522@example sh
523$ guix package --list-installed=my-hello
524my-hello 2.10 out
525/gnu/store/f1db2mfm8syb8qvc357c53slbvf1g9m9-my-hello-2.10
526@end example
527
528We've gone as far as we could without any knowledge of Scheme. Before moving
529on to more complex packages, now is the right time to brush up on your Scheme
530knowledge. @pxref{A Scheme Crash Course} to get up to speed.
531
ffe059af
PN
532@node Setup
533@subsection Setup
7bc46ecc 534
ffe059af
PN
535In the rest of this chapter we will rely on some basic Scheme
536programming knowledge. Now let's detail the different possible setups
537for working on Guix packages.
538
539There are several ways to set up a Guix packaging environment.
540
541We recommend you work directly on the Guix source checkout since it makes it
542easier for everyone to contribute to the project.
543
544But first, let's look at other possibilities.
545
546@node Local file
547@subsubsection Local file
548
549This is what we previously did with @samp{my-hello}. With the Scheme basics we've
550covered, we are now able to explain the leading chunks. As stated in @code{guix
551package --help}:
552
553@example
554 -f, --install-from-file=FILE
555 install the package that the code within FILE
556 evaluates to
557@end example
558
559Thus the last expression @emph{must} return a package, which is the case in our
560earlier example.
561
562The @code{use-modules} expression tells which of the modules we need in the file.
563Modules are a collection of values and procedures. They are commonly called
0cbef07b 564``libraries'' or ``packages'' in other programming languages.
ffe059af
PN
565
566@node @samp{GUIX_PACKAGE_PATH}
567@subsubsection @samp{GUIX_PACKAGE_PATH}
568
0cbef07b 569@emph{Note: Starting from Guix 0.16, the more flexible Guix @dfn{channels} are the
ffe059af
PN
570preferred way and supersede @samp{GUIX_PACKAGE_PATH}. See next section.}
571
572It can be tedious to specify the file from the command line instead of simply
573calling @code{guix package --install my-hello} as you would do with the official
574packages.
575
0cbef07b
LC
576Guix makes it possible to streamline the process by adding as many ``package
577declaration directories'' as you want.
ffe059af
PN
578
579Create a directory, say @samp{~./guix-packages} and add it to the @samp{GUIX_PACKAGE_PATH}
580environment variable:
581
582@example
583$ mkdir ~/guix-packages
584$ export GUIX_PACKAGE_PATH=~/guix-packages
585@end example
586
587To add several directories, separate them with a colon (@code{:}).
588
589Our previous @samp{my-hello} needs some adjustments though:
590
d482e13f 591@lisp
ffe059af
PN
592(define-module (my-hello)
593 #:use-module (guix licenses)
594 #:use-module (guix packages)
595 #:use-module (guix build-system gnu)
596 #:use-module (guix download))
597
598(define-public my-hello
599 (package
600 (name "my-hello")
601 (version "2.10")
602 (source (origin
603 (method url-fetch)
604 (uri (string-append "mirror://gnu/hello/hello-" version
605 ".tar.gz"))
606 (sha256
607 (base32
608 "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i"))))
609 (build-system gnu-build-system)
610 (synopsis "Hello, Guix world: An example custom Guix package")
611 (description
612 "GNU Hello prints the message \"Hello, world!\" and then exits. It
613serves as an example of standard GNU coding practices. As such, it supports
614command-line arguments, multiple languages, and so on.")
615 (home-page "https://www.gnu.org/software/hello/")
616 (license gpl3+)))
d482e13f 617@end lisp
ffe059af
PN
618
619Note that we have assigned the package value to an exported variable name with
620@code{define-public}. This is effectively assigning the package to the @code{my-hello}
621variable so that it can be referenced, among other as dependency of other
622packages.
623
624If you use @code{guix package --install-from-file=my-hello.scm} on the above file, it
625will fail because the last expression, @code{define-public}, does not return a
626package. If you want to use @code{define-public} in this use-case nonetheless, make
627sure the file ends with an evaluation of @code{my-hello}:
628
d482e13f 629@lisp
ffe059af
PN
630; ...
631(define-public my-hello
632 ; ...
633 )
634
635my-hello
d482e13f 636@end lisp
ffe059af
PN
637
638This last example is not very typical.
639
640Now @samp{my-hello} should be part of the package collection like all other official
641packages. You can verify this with:
642
643@example
644$ guix package --show=my-hello
645@end example
646
647@node Guix channels
648@subsubsection Guix channels
649
650Guix 0.16 features channels, which is very similar to @samp{GUIX_PACKAGE_PATH} but
651provides better integration and provenance tracking. Channels are not
652necessarily local, they can be maintained as a public Git repository for
653instance. Of course, several channels can be used at the same time.
654
655@xref{Channels,,, guix, GNU Guix Reference Manual} for setup details.
656
657@node Direct checkout hacking
658@subsubsection Direct checkout hacking
659
660Working directly on the Guix project is recommended: it reduces the friction
661when the time comes to submit your changes upstream to let the community benefit
662from your hard work!
663
664Unlike most software distributions, the Guix repository holds in one place both
665the tooling (including the package manager) and the package definitions. This
666choice was made so that it would give developers the flexibility to modify the
667API without breakage by updating all packages at the same time. This reduces
668development inertia.
669
670Check out the official @uref{https://git-scm.com/, Git} repository:
671
672@example
673$ git clone https://git.savannah.gnu.org/git/guix.git
674@end example
675
676In the rest of this article, we use @samp{$GUIX_CHECKOUT} to refer to the location of
677the checkout.
678
679
e97a4a29 680Follow the instructions in the manual (@pxref{Contributing,,, guix, GNU Guix
ffe059af
PN
681Reference Manual}) to set up the repository environment.
682
683Once ready, you should be able to use the package definitions from the
684repository environment.
685
686Feel free to edit package definitions found in @samp{$GUIX_CHECKOUT/gnu/packages}.
687
688The @samp{$GUIX_CHECKOUT/pre-inst-env} script lets you use @samp{guix} over the package
e97a4a29
LC
689collection of the repository (@pxref{Running Guix Before It Is
690Installed,,, guix, GNU Guix Reference Manual}).
ffe059af
PN
691
692@itemize
693@item
694Search packages, such as Ruby:
695
696@example
697 $ cd $GUIX_CHECKOUT
698 $ ./pre-inst-env guix package --list-available=ruby
699 ruby 1.8.7-p374 out gnu/packages/ruby.scm:119:2
700 ruby 2.1.6 out gnu/packages/ruby.scm:91:2
701 ruby 2.2.2 out gnu/packages/ruby.scm:39:2
702@end example
703
704@item
705Build a package, here Ruby version 2.1:
706
707@example
708 $ ./pre-inst-env guix build --keep-failed ruby@@2.1
709 /gnu/store/c13v73jxmj2nir2xjqaz5259zywsa9zi-ruby-2.1.6
710@end example
711
712@item
713Install it to your user profile:
714
715@example
716 $ ./pre-inst-env guix package --install ruby@@2.1
717@end example
718
719@item
720Check for common mistakes:
721
722@example
723 $ ./pre-inst-env guix lint ruby@@2.1
724@end example
725@end itemize
726
727Guix strives at maintaining a high packaging standard; when contributing to the
728Guix project, remember to
729
730@itemize
731@item
732follow the coding style (@pxref{Coding Style,,, guix, GNU Guix Reference Manual}),
733@item
734and review the check list from the manual (@pxref{Submitting Patches,,, guix, GNU Guix Reference Manual}).
735@end itemize
736
737Once you are happy with the result, you are welcome to send your contribution to
738make it part of Guix. This process is also detailed in the manual. (@pxref{Contributing,,, guix, GNU Guix Reference Manual})
739
740
741It's a community effort so the more join in, the better Guix becomes!
742
743@node Extended example
744@subsection Extended example
745
0cbef07b 746The above ``Hello World'' example is as simple as it goes. Packages can be more
ffe059af
PN
747complex than that and Guix can handle more advanced scenarios. Let's look at
748another, more sophisticated package (slightly modified from the source):
749
d482e13f 750@lisp
ffe059af
PN
751(define-module (gnu packages version-control)
752 #:use-module ((guix licenses) #:prefix license:)
753 #:use-module (guix utils)
754 #:use-module (guix packages)
755 #:use-module (guix git-download)
756 #:use-module (guix build-system cmake)
757 #:use-module (gnu packages ssh)
758 #:use-module (gnu packages web)
759 #:use-module (gnu packages pkg-config)
760 #:use-module (gnu packages python)
761 #:use-module (gnu packages compression)
762 #:use-module (gnu packages tls))
763
764(define-public my-libgit2
765 (let ((commit "e98d0a37c93574d2c6107bf7f31140b548c6a7bf")
766 (revision "1"))
767 (package
768 (name "my-libgit2")
769 (version (git-version "0.26.6" revision commit))
770 (source (origin
771 (method git-fetch)
772 (uri (git-reference
773 (url "https://github.com/libgit2/libgit2/")
774 (commit commit)))
775 (file-name (git-file-name name version))
776 (sha256
777 (base32
778 "17pjvprmdrx4h6bb1hhc98w9qi6ki7yl57f090n9kbhswxqfs7s3"))
779 (patches (search-patches "libgit2-mtime-0.patch"))
780 (modules '((guix build utils)))
781 (snippet '(begin
782 ;; Remove bundled software.
783 (delete-file-recursively "deps")
784 #t))))
785 (build-system cmake-build-system)
786 (outputs '("out" "debug"))
787 (arguments
788 `(#:tests? #t ; Run the test suite (this is the default)
789 #:configure-flags '("-DUSE_SHA1DC=ON") ; SHA-1 collision detection
790 #:phases
791 (modify-phases %standard-phases
792 (add-after 'unpack 'fix-hardcoded-paths
793 (lambda _
794 (substitute* "tests/repo/init.c"
795 (("#!/bin/sh") (string-append "#!" (which "sh"))))
796 (substitute* "tests/clar/fs.h"
797 (("/bin/cp") (which "cp"))
798 (("/bin/rm") (which "rm")))
799 #t))
800 ;; Run checks more verbosely.
801 (replace 'check
802 (lambda _ (invoke "./libgit2_clar" "-v" "-Q")))
803 (add-after 'unpack 'make-files-writable-for-tests
804 (lambda _ (for-each make-file-writable (find-files "." ".*")))))))
805 (inputs
806 `(("libssh2" ,libssh2)
807 ("http-parser" ,http-parser)
808 ("python" ,python-wrapper)))
809 (native-inputs
810 `(("pkg-config" ,pkg-config)))
811 (propagated-inputs
812 ;; These two libraries are in 'Requires.private' in libgit2.pc.
813 `(("openssl" ,openssl)
814 ("zlib" ,zlib)))
815 (home-page "https://libgit2.github.com/")
816 (synopsis "Library providing Git core methods")
817 (description
818 "Libgit2 is a portable, pure C implementation of the Git core methods
819provided as a re-entrant linkable library with a solid API, allowing you to
820write native speed custom Git applications in any language with bindings.")
821 ;; GPLv2 with linking exception
822 (license license:gpl2))))
d482e13f 823@end lisp
ffe059af
PN
824
825(In those cases were you only want to tweak a few fields from a package
826definition, you should rely on inheritance instead of copy-pasting everything.
827See below.)
828
829Let's discuss those fields in depth.
830
831@subsubsection @code{git-fetch} method
832
833Unlike the @code{url-fetch} method, @code{git-fetch} expects a @code{git-reference} which takes
834a Git repository and a commit. The commit can be any Git reference such as
835tags, so if the @code{version} is tagged, then it can be used directly. Sometimes
836the tag is prefixed with a @code{v}, in which case you'd use @code{(commit (string-append
837"v" version))}.
838
839To ensure that the source code from the Git repository is stored in a unique
840directory with a readable name we use @code{(file-name (git-file-name name
841version))}.
842
843Note that there is also a @code{git-version} procedure that can be used to derive the
844version when packaging programs for a specific commit.
845
846@subsubsection Snippets
847
848Snippets are quoted (i.e. non-evaluated) Scheme code that are a means of patching
849the source. They are a Guix-y alternative to the traditional @samp{.patch} files.
850Because of the quote, the code in only evaluated when passed to the Guix daemon
0cbef07b 851for building. There can be as many snippets as needed.
ffe059af
PN
852
853Snippets might need additional Guile modules which can be imported from the
854@code{modules} field.
855
856@subsubsection Inputs
857
858First, a syntactic comment: See the quasi-quote / comma syntax?
859
d482e13f 860@lisp
ffe059af
PN
861 (native-inputs
862 `(("pkg-config" ,pkg-config)))
d482e13f 863@end lisp
ffe059af
PN
864
865is equivalent to
866
d482e13f 867@lisp
ffe059af
PN
868 (native-inputs
869 (list (list "pkg-config" pkg-config)))
d482e13f 870@end lisp
ffe059af
PN
871
872You'll mostly see the former because it's shorter.
873
874There are 3 different input types. In short:
875
876@table @asis
877@item native-inputs
878Required for building but not runtime -- installing a package
879through a substitute won't install these inputs.
880@item inputs
881Installed in the store but not in the profile, as well as being
882present at build time.
883@item propagated-inputs
884Installed in the store and in the profile, as well as
885being present at build time.
886@end table
887
888@xref{Package Reference,,, guix, GNU Guix Reference Manual} for more details.
889
890The distinction between the various inputs is important: if a dependency can be
891handled as an @emph{input} instead of a @emph{propagated input}, it should be done so, or
0cbef07b 892else it ``pollutes'' the user profile for no good reason.
ffe059af
PN
893
894For instance, a user installing a graphical program that depends on a
895command line tool might only be interested in the graphical part, so there is no
896need to force the command line tool into the user profile. The dependency is a
897concern to the package, not to the user. @emph{Inputs} make it possible to handle
898dependencies without bugging the user by adding undesired executable files (or
899libraries) to their profile.
900
901Same goes for @emph{native-inputs}: once the program is installed, build-time
902dependencies can be safely garbage-collected.
903It also matters when a substitute is available, in which case only the @emph{inputs}
904and @emph{propagated inputs} will be fetched: the @emph{native inputs} are not required to
905install a package from a substitute.
906
907@subsubsection Outputs
908
909Just like how a package can have multiple inputs, it can also produce multiple
910outputs.
911
912Each output corresponds to a separate directory in the store.
913
914The user can choose which output to install; this is useful to save space or
915to avoid polluting the user profile with unwanted executables or libraries.
916
917Output separation is optional. When the @code{outputs} field is left out, the
918default and only output (the complete package) is referred to as @code{"out"}.
919
920Typical separate output names include @code{debug} and @code{doc}.
921
922It's advised to separate outputs only when you've shown it's worth it: if the
923output size is significant (compare with @code{guix size}) or in case the package is
924modular.
925
926@subsubsection Build system arguments
927
928The @code{arguments} is a keyword-value list used to configure the build process.
929
930The simplest argument @code{#:tests?} can be used to disable the test suite when
931building the package. This is mostly useful when the package does not feature
932any test suite. It's strongly recommended to keep the test suite on if there is
933one.
934
935Another common argument is @code{:make-flags}, which specifies a list of flags to
936append when running make, as you would from the command line. For instance, the
937following flags
938
d482e13f 939@lisp
ffe059af
PN
940#:make-flags (list (string-append "prefix=" (assoc-ref %outputs "out"))
941 "CC=gcc")
d482e13f 942@end lisp
ffe059af
PN
943
944translate into
945
946@example
947$ make CC=gcc prefix=/gnu/store/...-<out>
948@end example
949
950This sets the C compiler to @code{gcc} and the @code{prefix} variable (the installation
951directory in Make parlance) to @code{(assoc-ref %outputs "out")}, which is a build-stage
952global variable pointing to the destination directory in the store (something like
953@samp{/gnu/store/...-my-libgit2-20180408}).
954
0cbef07b 955Similarly, it's possible to set the configure flags:
ffe059af 956
d482e13f 957@lisp
ffe059af 958#:configure-flags '("-DUSE_SHA1DC=ON")
d482e13f 959@end lisp
ffe059af
PN
960
961The @code{%build-inputs} variable is also generated in scope. It's an association
962table that maps the input names to their store directories.
963
964The @code{phases} keyword lists the sequential steps of the build system. Typically
965phases include @code{unpack}, @code{configure}, @code{build}, @code{install} and @code{check}. To know
966more about those phases, you need to work out the appropriate build system
967definition in @samp{$GUIX_CHECKOUT/guix/build/gnu-build-system.scm}:
968
d482e13f 969@lisp
ffe059af
PN
970(define %standard-phases
971 ;; Standard build phases, as a list of symbol/procedure pairs.
972 (let-syntax ((phases (syntax-rules ()
973 ((_ p ...) `((p . ,p) ...)))))
974 (phases set-SOURCE-DATE-EPOCH set-paths install-locale unpack
975 bootstrap
976 patch-usr-bin-file
977 patch-source-shebangs configure patch-generated-file-shebangs
978 build check install
979 patch-shebangs strip
980 validate-runpath
981 validate-documentation-location
982 delete-info-dir-file
983 patch-dot-desktop-files
984 install-license-files
985 reset-gzip-timestamps
986 compress-documentation)))
d482e13f 987@end lisp
ffe059af
PN
988
989Or from the REPL:
990
d482e13f 991@lisp
cf1e6f5f
LC
992(add-to-load-path "/path/to/guix/checkout")
993,use (guix build gnu-build-system)
994(map first %standard-phases)
995@result{} (set-SOURCE-DATE-EPOCH set-paths install-locale unpack bootstrap patch-usr-bin-file patch-source-shebangs configure patch-generated-file-shebangs build check install patch-shebangs strip validate-runpath validate-documentation-location delete-info-dir-file patch-dot-desktop-files install-license-files reset-gzip-timestamps compress-documentation)
d482e13f 996@end lisp
ffe059af
PN
997
998If you want to know more about what happens during those phases, consult the
999associated procedures.
1000
1001For instance, as of this writing the definition of @code{unpack} for the GNU build
1002system is
1003
d482e13f 1004@lisp
ffe059af
PN
1005(define* (unpack #:key source #:allow-other-keys)
1006 "Unpack SOURCE in the working directory, and change directory within the
1007source. When SOURCE is a directory, copy it in a sub-directory of the current
1008working directory."
1009 (if (file-is-directory? source)
1010 (begin
1011 (mkdir "source")
1012 (chdir "source")
1013
1014 ;; Preserve timestamps (set to the Epoch) on the copied tree so that
1015 ;; things work deterministically.
1016 (copy-recursively source "."
1017 #:keep-mtime? #t))
1018 (begin
1019 (if (string-suffix? ".zip" source)
1020 (invoke "unzip" source)
1021 (invoke "tar" "xvf" source))
1022 (chdir (first-subdirectory "."))))
1023 #t)
d482e13f 1024@end lisp
ffe059af
PN
1025
1026Note the @code{chdir} call: it changes the working directory to where the source was
1027unpacked.
1028Thus every phase following the @code{unpack} will use the source as a working
1029directory, which is why we can directly work on the source files.
1030That is to say, unless a later phase changes the working directory to something
1031else.
1032
1033We modify the list of @code{%standard-phases} of the build system with the
1034@code{modify-phases} macro as per the list of specified modifications, which may have
1035the following forms:
1036
1037@itemize
1038@item
1039@code{(add-before PHASE NEW-PHASE PROCEDURE)}: Run @code{PROCEDURE} named @code{NEW-PHASE} before @code{PHASE}.
1040@item
1041@code{(add-after PHASE NEW-PHASE PROCEDURE)}: Same, but afterwards.
1042@item
1043@code{(replace PHASE PROCEDURE)}.
1044@item
1045@code{(delete PHASE)}.
1046@end itemize
1047
1048The @code{PROCEDURE} supports the keyword arguments @code{inputs} and @code{outputs}. Each
1049input (whether @emph{native}, @emph{propagated} or not) and output directory is referenced
1050by their name in those variables. Thus @code{(assoc-ref outputs "out")} is the store
1051directory of the main output of the package. A phase procedure may look like
1052this:
1053
d482e13f 1054@lisp
ffe059af
PN
1055(lambda* (#:key inputs outputs #:allow-other-keys)
1056 (let (((bash-directory (assoc-ref inputs "bash"))
1057 (output-directory (assoc-ref outputs "out"))
1058 (doc-directory (assoc-ref outputs "doc"))
1059 ; ...
1060 #t)
d482e13f 1061@end lisp
ffe059af
PN
1062
1063The procedure must return @code{#t} on success. It's brittle to rely on the return
1064value of the last expression used to tweak the phase because there is no
1065guarantee it would be a @code{#t}. Hence the trailing @code{#t} to ensure the right value
1066is returned on success.
1067
1068@subsubsection Code staging
1069
1070The astute reader may have noticed the quasi-quote and comma syntax in the
1071argument field. Indeed, the build code in the package declaration should not be
1072evaluated on the client side, but only when passed to the Guix daemon. This
1073mechanism of passing code around two running processes is called @uref{https://arxiv.org/abs/1709.00833, code staging}.
1074
0cbef07b 1075@subsubsection Utility functions
ffe059af
PN
1076
1077When customizing @code{phases}, we often need to write code that mimics the
1078equivalent system invocations (@code{make}, @code{mkdir}, @code{cp}, etc.) commonly used during
0cbef07b 1079regular ``Unix-style'' installations.
ffe059af
PN
1080
1081Some like @code{chmod} are native to Guile.
1082@xref{,,, guile, Guile reference manual} for a complete list.
1083
1084Guix provides additional helper functions which prove especially handy in the
1085context of package management.
1086
1087Some of those functions can be found in
1088@samp{$GUIX_CHECKOUT/guix/guix/build/utils.scm}. Most of them mirror the behaviour
1089of the traditional Unix system commands:
1090
1091@table @asis
1092@item which
1093Like the @samp{which} system command.
1094@item find-files
1095Akin to the @samp{find} system command.
1096@item mkdir-p
1097Like @samp{mkdir -p}, which creates all parents as needed.
1098@item install-file
1099Similar to @samp{install} when installing a file to a (possibly
1100non-existing) directory. Guile has @code{copy-file} which works
1101like @samp{cp}.
1102@item copy-recursively
1103Like @samp{cp -r}.
1104@item delete-file-recursively
1105Like @samp{rm -rf}.
1106@item invoke
1107Run an executable. This should be used instead of @code{system*}.
1108@item with-directory-excursion
1109Run the body in a different working directory,
1110then restore the previous working directory.
1111@item substitute*
0cbef07b 1112A ``@command{sed}-like'' function.
ffe059af
PN
1113@end table
1114
1115@subsubsection Module prefix
1116
1117The license in our last example needs a prefix: this is because of how the
1118@code{license} module was imported in the package, as @code{#:use-module ((guix licenses)
1119#:prefix license:)}. The Guile module import mechanism
1120(@pxref{Using Guile Modules,,, guile, Guile reference manual})
1121gives the user full control over namespacing: this is needed to avoid
1122clashes between, say, the
1123@samp{zlib} variable from @samp{licenses.scm} (a @emph{license} value) and the @samp{zlib} variable
1124from @samp{compression.scm} (a @emph{package} value).
1125
1126@node Other build systems
1127@subsection Other build systems
1128
1129What we've seen so far covers the majority of packages using a build system
1130other than the @code{trivial-build-system}. The latter does not automate anything
1131and leaves you to build everything manually. This can be more demanding and we
1132won't cover it here for now, but thankfully it is rarely necessary to fall back
1133on this system.
1134
1135For the other build systems, such as ASDF, Emacs, Perl, Ruby and many more, the
1136process is very similar to the GNU build system except for a few specialized
1137arguments.
1138
1139Learn more about build systems in
1140@itemize
1141@item
1142@uref{https://www.gnu.org/software/guix/manual/en/html_node/Build-Systems.html#Build-Systems, the manual, section 4.2 Build systems},
1143@item
1144the source code in the @samp{$GUIX_CHECKOUT/guix/build} and
1145@samp{$GUIX_CHECKOUT/guix/build-system} directories.
1146@end itemize
1147
1148@node Programmable and automated package definition
1149@subsection Programmable and automated package definition
1150
1151We can't repeat it enough: having a full-fledged programming language at hand
1152empowers us in ways that reach far beyond traditional package management.
1153
1154Let's illustrate this with some awesome features of Guix!
1155
1156@node Recursive importers
1157@subsubsection Recursive importers
1158
1159You might find some build systems good enough that there is little to do at all
1160to write a package, to the point that it becomes repetitive and tedious after a
1161while. A @emph{raison d'être} of computers is to replace human beings at those
1162boring tasks. So let's tell Guix to do this for us and create the package
1163definition of an R package from CRAN (the output is trimmed for conciseness):
1164
1165@example
1166$ guix import cran --recursive walrus
1167
1168(define-public r-mc2d
1169 ; ...
1170 (license gpl2+)))
1171
1172(define-public r-jmvcore
1173 ; ...
1174 (license gpl2+)))
1175
1176(define-public r-wrs2
1177 ; ...
1178 (license gpl3)))
1179
1180(define-public r-walrus
1181 (package
1182 (name "r-walrus")
1183 (version "1.0.3")
1184 (source
1185 (origin
1186 (method url-fetch)
1187 (uri (cran-uri "walrus" version))
1188 (sha256
1189 (base32
1190 "1nk2glcvy4hyksl5ipq2mz8jy4fss90hx6cq98m3w96kzjni6jjj"))))
1191 (build-system r-build-system)
1192 (propagated-inputs
1193 `(("r-ggplot2" ,r-ggplot2)
1194 ("r-jmvcore" ,r-jmvcore)
1195 ("r-r6" ,r-r6)
1196 ("r-wrs2" ,r-wrs2)))
1197 (home-page "https://github.com/jamovi/walrus")
1198 (synopsis "Robust Statistical Methods")
1199 (description
1200 "This package provides a toolbox of common robust statistical
1201tests, including robust descriptives, robust t-tests, and robust ANOVA.
1202It is also available as a module for 'jamovi' (see
1203<https://www.jamovi.org> for more information). Walrus is based on the
1204WRS2 package by Patrick Mair, which is in turn based on the scripts and
1205work of Rand Wilcox. These analyses are described in depth in the book
1206'Introduction to Robust Estimation & Hypothesis Testing'.")
1207 (license gpl3)))
1208@end example
1209
1210The recursive importer won't import packages for which Guix already has package
1211definitions, except for the very first.
1212
1213Not all applications can be packaged this way, only those relying on a select
1214number of supported systems. Read about the full list of importers in
1215the guix import section of the manual
1216(@pxref{Invoking guix import,,, guix, GNU Guix Reference Manual}).
1217
1218@node Automatic update
1219@subsubsection Automatic update
1220
1221Guix can be smart enough to check for updates on systems it knows. It can
1222report outdated package definitions with
1223
1224@example
1225$ guix refresh hello
1226@end example
1227
1228In most cases, updating a package to a newer version requires little more than
1229changing the version number and the checksum. Guix can do that automatically as
1230well:
1231
1232@example
1233$ guix refresh hello --update
1234@end example
1235
1236@node Inheritance
1237@subsubsection Inheritance
1238
1239If you've started browsing the existing package definitions, you might have
1240noticed that a significant number of them have a @code{inherit} field:
1241
d482e13f 1242@lisp
ffe059af
PN
1243(define-public adwaita-icon-theme
1244 (package (inherit gnome-icon-theme)
1245 (name "adwaita-icon-theme")
1246 (version "3.26.1")
1247 (source (origin
1248 (method url-fetch)
1249 (uri (string-append "mirror://gnome/sources/" name "/"
1250 (version-major+minor version) "/"
1251 name "-" version ".tar.xz"))
1252 (sha256
1253 (base32
1254 "17fpahgh5dyckgz7rwqvzgnhx53cx9kr2xw0szprc6bnqy977fi8"))))
1255 (native-inputs
1256 `(("gtk-encode-symbolic-svg" ,gtk+ "bin")))))
d482e13f 1257@end lisp
ffe059af
PN
1258
1259All unspecified fields are inherited from the parent package. This is very
1260convenient to create alternative packages, for instance with different source,
1261version or compilation options.
1262
1263@node Getting help
1264@subsection Getting help
1265
1266Sadly, some applications can be tough to package. Sometimes they need a patch to
1267work with the non-standard filesystem hierarchy enforced by the store.
1268Sometimes the tests won't run properly. (They can be skipped but this is not
1269recommended.) Other times the resulting package won't be reproducible.
1270
1271Should you be stuck, unable to figure out how to fix any sort of packaging
1272issue, don't hesitate to ask the community for help.
1273
1274See the @uref{https://www.gnu.org/software/guix/contact/, Guix homepage} for information on the mailing lists, IRC, etc.
1275
1276@node Conclusion
1277@subsection Conclusion
1278
1279This tutorial was a showcase of the sophisticated package management that Guix
1280boasts. At this point we have mostly restricted this introduction to the
1281@code{gnu-build-system} which is a core abstraction layer on which more advanced
1282abstractions are based.
1283
1284Where do we go from here? Next we ought to dissect the innards of the build
1285system by removing all abstractions, using the @code{trivial-build-system}: this
1286should give us a thorough understanding of the process before investigating some
1287more advanced packaging techniques and edge cases.
1288
1289Other features worth exploring are the interactive editing and debugging
1290capabilities of Guix provided by the Guile REPL@.
1291
1292Those fancy features are completely optional and can wait; now is a good time
1293to take a well-deserved break. With what we've introduced here you should be
1294well armed to package lots of programs. You can get started right away and
1295hopefully we will see your contributions soon!
1296
1297@node References
1298@subsection References
1299
1300@itemize
1301@item
1302The @uref{https://www.gnu.org/software/guix/manual/en/html_node/Defining-Packages.html, package reference in the manual}
1303
1304@item
1305@uref{https://gitlab.com/pjotrp/guix-notes/blob/master/HACKING.org, Pjotr’s hacking guide to GNU Guix}
1306
1307@item
0cbef07b 1308@uref{https://www.gnu.org/software/guix/guix-ghm-andreas-20130823.pdf, ``GNU Guix: Package without a scheme!''}, by Andreas Enge
ffe059af 1309@end itemize
7bc46ecc
RW
1310
1311@c *********************************************************************
1312@node System Configuration
1313@chapter System Configuration
1314
1315Guix offers a flexible language for declaratively configuring your Guix
1316System. This flexibility can at times be overwhelming. The purpose of this
1317chapter is to demonstrate some advanced configuration concepts.
1318
1319@pxref{System Configuration,,, guix, GNU Guix Reference Manual} for a complete
1320reference.
1321
1322@menu
1323* Customizing the Kernel:: Creating and using a custom Linux kernel on Guix System.
1324@end menu
1325
1326@node Customizing the Kernel
1327@section Customizing the Kernel
1328
1329Guix is, at its core, a source based distribution with substitutes
1330(@pxref{Substitutes,,, guix, GNU Guix Reference Manual}), and as such building
1331packages from their source code is an expected part of regular package
1332installations and upgrades. Given this starting point, it makes sense that
1333efforts are made to reduce the amount of time spent compiling packages, and
1334recent changes and upgrades to the building and distribution of substitutes
1335continues to be a topic of discussion within Guix.
1336
1337The kernel, while not requiring an overabundance of RAM to build, does take a
1338rather long time on an average machine. The official kernel configuration, as
1339is the case with many GNU/Linux distributions, errs on the side of
1340inclusiveness, and this is really what causes the build to take such a long
1341time when the kernel is built from source.
1342
1343The Linux kernel, however, can also just be described as a regular old
1344package, and as such can be customized just like any other package. The
1345procedure is a little bit different, although this is primarily due to the
1346nature of how the package definition is written.
1347
1348The @code{linux-libre} kernel package definition is actually a procedure which
1349creates a package.
1350
b1eecb5c 1351@lisp
7bc46ecc
RW
1352(define* (make-linux-libre version hash supported-systems
1353 #:key
1354 ;; A function that takes an arch and a variant.
1355 ;; See kernel-config for an example.
1356 (extra-version #f)
1357 (configuration-file #f)
1358 (defconfig "defconfig")
1359 (extra-options %default-extra-linux-options)
1360 (patches (list %boot-logo-patch)))
1361 ...)
b1eecb5c 1362@end lisp
7bc46ecc
RW
1363
1364The current @code{linux-libre} package is for the 5.1.x series, and is
1365declared like this:
1366
b1eecb5c 1367@lisp
7bc46ecc
RW
1368(define-public linux-libre
1369 (make-linux-libre %linux-libre-version
1370 %linux-libre-hash
1371 '("x86_64-linux" "i686-linux" "armhf-linux" "aarch64-linux")
1372 #:patches %linux-libre-5.1-patches
1373 #:configuration-file kernel-config))
b1eecb5c 1374@end lisp
7bc46ecc
RW
1375
1376Any keys which are not assigned values inherit their default value from the
1377@code{make-linux-libre} definition. When comparing the two snippets above,
1378you may notice that the code comment in the first doesn't actually refer to
1379the @code{#:extra-version} keyword; it is actually for
1380@code{#:configuration-file}. Because of this, it is not actually easy to
1381include a custom kernel configuration from the definition, but don't worry,
1382there are other ways to work with what we do have.
1383
1384There are two ways to create a kernel with a custom kernel configuration. The
1385first is to provide a standard @file{.config} file during the build process by
1386including an actual @file{.config} file as a native input to our custom
1387kernel. The following is a snippet from the custom @code{'configure} phase of
1388the @code{make-linux-libre} package definition:
1389
b1eecb5c 1390@lisp
7bc46ecc
RW
1391(let ((build (assoc-ref %standard-phases 'build))
1392 (config (assoc-ref (or native-inputs inputs) "kconfig")))
1393
1394 ;; Use a custom kernel configuration file or a default
1395 ;; configuration file.
1396 (if config
1397 (begin
1398 (copy-file config ".config")
1399 (chmod ".config" #o666))
1400 (invoke "make" ,defconfig))
b1eecb5c 1401@end lisp
7bc46ecc
RW
1402
1403Below is a sample kernel package. The @code{linux-libre} package is nothing
1404special and can be inherited from and have its fields overridden like any
1405other package:
1406
b1eecb5c 1407@lisp
7bc46ecc
RW
1408(define-public linux-libre/E2140
1409 (package
1410 (inherit linux-libre)
1411 (native-inputs
1412 `(("kconfig" ,(local-file "E2140.config"))
1413 ,@@(alist-delete "kconfig"
1414 (package-native-inputs linux-libre))))))
b1eecb5c 1415@end lisp
7bc46ecc
RW
1416
1417In the same directory as the file defining @code{linux-libre-E2140} is a file
1418named @file{E2140.config}, which is an actual kernel configuration file. The
1419@code{defconfig} keyword of @code{make-linux-libre} is left blank here, so the
1420only kernel configuration in the package is the one which was included in the
1421@code{native-inputs} field.
1422
1423The second way to create a custom kernel is to pass a new value to the
1424@code{extra-options} keyword of the @code{make-linux-libre} procedure. The
1425@code{extra-options} keyword works with another function defined right below
1426it:
1427
b1eecb5c 1428@lisp
7bc46ecc
RW
1429(define %default-extra-linux-options
1430 `(;; https://lists.gnu.org/archive/html/guix-devel/2014-04/msg00039.html
1431 ("CONFIG_DEVPTS_MULTIPLE_INSTANCES" . #t)
1432 ;; Modules required for initrd:
1433 ("CONFIG_NET_9P" . m)
1434 ("CONFIG_NET_9P_VIRTIO" . m)
1435 ("CONFIG_VIRTIO_BLK" . m)
1436 ("CONFIG_VIRTIO_NET" . m)
1437 ("CONFIG_VIRTIO_PCI" . m)
1438 ("CONFIG_VIRTIO_BALLOON" . m)
1439 ("CONFIG_VIRTIO_MMIO" . m)
1440 ("CONFIG_FUSE_FS" . m)
1441 ("CONFIG_CIFS" . m)
1442 ("CONFIG_9P_FS" . m)))
1443
1444(define (config->string options)
1445 (string-join (map (match-lambda
1446 ((option . 'm)
1447 (string-append option "=m"))
1448 ((option . #t)
1449 (string-append option "=y"))
1450 ((option . #f)
1451 (string-append option "=n")))
1452 options)
1453 "\n"))
b1eecb5c 1454@end lisp
7bc46ecc
RW
1455
1456And in the custom configure script from the `make-linux-libre` package:
1457
b1eecb5c 1458@lisp
7bc46ecc
RW
1459;; Appending works even when the option wasn't in the
1460;; file. The last one prevails if duplicated.
1461(let ((port (open-file ".config" "a"))
1462 (extra-configuration ,(config->string extra-options)))
1463 (display extra-configuration port)
1464 (close-port port))
1465
1466(invoke "make" "oldconfig"))))
b1eecb5c 1467@end lisp
7bc46ecc
RW
1468
1469So by not providing a configuration-file the @file{.config} starts blank, and
1470then we write into it the collection of flags that we want. Here's another
1471custom kernel:
1472
b1eecb5c 1473@lisp
7bc46ecc
RW
1474(define %macbook41-full-config
1475 (append %macbook41-config-options
1476 %filesystems
1477 %efi-support
1478 %emulation
1479 (@@@@ (gnu packages linux) %default-extra-linux-options)))
1480
1481(define-public linux-libre-macbook41
1482 ;; XXX: Access the internal 'make-linux-libre' procedure, which is
1483 ;; private and unexported, and is liable to change in the future.
1484 ((@@@@ (gnu packages linux) make-linux-libre) (@@@@ (gnu packages linux) %linux-libre-version)
1485 (@@@@ (gnu packages linux) %linux-libre-hash)
1486 '("x86_64-linux")
1487 #:extra-version "macbook41"
1488 #:patches (@@@@ (gnu packages linux) %linux-libre-5.1-patches)
1489 #:extra-options %macbook41-config-options))
b1eecb5c 1490@end lisp
7bc46ecc
RW
1491
1492In the above example @code{%filesystems} is a collection of flags enabling
1493different filesystem support, @code{%efi-support} enables EFI support and
1494@code{%emulation} enables a x86_64-linux machine to act in 32-bit mode also.
1495@code{%default-extra-linux-options} are the ones quoted above, which had to be
1496added in since they were replaced in the @code{extra-options} keyword.
1497
1498This all sounds like it should be doable, but how does one even know which
1499modules are required for a particular system? Two places that can be helpful
1500in trying to answer this question is the
1501@uref{https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Kernel, Gentoo
1502Handbook} and the
1503@uref{https://www.kernel.org/doc/html/latest/admin-guide/README.html?highlight=localmodconfig,
1504documentation from the kernel itself}. From the kernel documentation, it
1505seems that @code{make localmodconfig} is the command we want.
1506
1507In order to actually run @code{make localmodconfig} we first need to get and
1508unpack the kernel source code:
1509
1510@example shell
1511tar xf $(guix build linux-libre --source)
1512@end example
1513
1514Once inside the directory containing the source code run @code{touch .config}
1515to create an initial, empty @file{.config} to start with. @code{make
1516localmodconfig} works by seeing what you already have in @file{.config} and
1517letting you know what you're missing. If the file is blank then you're
1518missing everything. The next step is to run:
1519
1520@example shell
1521guix environment linux-libre -- make localmodconfig
1522@end example
1523
1524and note the output. Do note that the @file{.config} file is still empty.
1525The output generally contains two types of warnings. The first start with
1526"WARNING" and can actually be ignored in our case. The second read:
1527
1528@example shell
1529module pcspkr did not have configs CONFIG_INPUT_PCSPKR
1530@end example
1531
1532For each of these lines, copy the @code{CONFIG_XXXX_XXXX} portion into the
1533@file{.config} in the directory, and append @code{=m}, so in the end it looks
1534like this:
1535
1536@example shell
1537CONFIG_INPUT_PCSPKR=m
1538CONFIG_VIRTIO=m
1539@end example
1540
1541After copying all the configuration options, run @code{make localmodconfig}
0cbef07b 1542again to make sure that you don't have any output starting with ``module''.
7bc46ecc
RW
1543After all of these machine specific modules there are a couple more left that
1544are also needed. @code{CONFIG_MODULES} is necessary so that you can build and
1545load modules separately and not have everything built into the kernel.
1546@code{CONFIG_BLK_DEV_SD} is required for reading from hard drives. It is
1547possible that there are other modules which you will need.
1548
1549This post does not aim to be a guide to configuring your own kernel however,
1550so if you do decide to build a custom kernel you'll have to seek out other
1551guides to create a kernel which is just right for your needs.
1552
1553The second way to setup the kernel configuration makes more use of Guix's
1554features and allows you to share configuration segments between different
1555kernels. For example, all machines using EFI to boot have a number of EFI
1556configuration flags that they need. It is likely that all the kernels will
1557share a list of filesystems to support. By using variables it is easier to
1558see at a glance what features are enabled and to make sure you don't have
1559features in one kernel but missing in another.
1560
1561Left undiscussed however, is Guix's initrd and its customization. It is
1562likely that you'll need to modify the initrd on a machine using a custom
1563kernel, since certain modules which are expected to be built may not be
1564available for inclusion into the initrd.
1565
4c463569
PN
1566@c *********************************************************************
1567@node Advanced package management
1568@chapter Advanced package management
1569
1570Guix is a functional package manager that offers many features beyond
1571what more traditional package managers can do. To the uninitiated,
1572those features might not have obvious use cases at first. The purpose
1573of this chapter is to demonstrate some advanced package management
1574concepts.
1575
1576@pxref{Package Management,,, guix, GNU Guix Reference Manual} for a complete
1577reference.
1578
1579@menu
1580* Guix Profiles in Practice:: Strategies for multiple profiles and manifests.
1581@end menu
1582
1583@node Guix Profiles in Practice
1584@section Guix Profiles in Practice
1585
1586Guix provides a very useful feature that may be quite foreign to newcomers:
1587@emph{profiles}. They are a way to group package installations together and all users
f6c27c55 1588on the same system are free to use as many profiles as they want.
4c463569
PN
1589
1590Whether you're a developer or not, you may find that multiple profiles bring you
1591great power and flexibility. While they shift the paradigm somewhat compared to
1592@emph{traditional package managers}, they are very convenient to use once you've
1593understood how to set them up.
1594
1595If you are familiar with Python's @samp{virtualenv}, you can think of a profile as a
1596kind of universal @samp{virtualenv} that can hold any kind of software whatsoever, not
1597just Python software. Furthermore, profiles are self-sufficient: they capture
1598all the runtime dependencies which guarantees that all programs within a profile
1599will always work at any point in time.
1600
1601Multiple profiles have many benefits:
1602
1603@itemize
1604@item
1605Clean semantic separation of the various packages a user needs for different contexts.
1606
1607@item
1608Multiple profiles can be made available into the environment either on login
1609or within a dedicated shell.
1610
1611@item
1612Profiles can be loaded on demand. For instance, the user can use multiple
1613shells, each of them running different profiles.
1614
1615@item
1616Isolation: Programs from one profile will not use programs from the other, and
f6c27c55 1617the user can even install different versions of the same programs to the two
4c463569
PN
1618profiles without conflict.
1619
1620@item
1621Deduplication: Profiles share dependencies that happens to be the exact same.
1622This makes multiple profiles storage-efficient.
1623
1624@item
1625Reproducible: when used with declarative manifests, a profile can be fully
1626specified by the Guix commit that was active when it was set up. This means
f6c27c55
PN
1627that the exact same profile can be
1628@uref{https://guix.gnu.org/blog/2018/multi-dimensional-transactions-and-rollbacks-oh-my/,
1629set up anywhere and anytime}, with just the commit information. See the
1630section on @ref{Reproducible profiles}.
4c463569
PN
1631
1632@item
1633Easier upgrades and maintenance: Multiple profiles make it easy to keep
1634package listings at hand and make upgrades completely friction-less.
1635@end itemize
1636
1637Concretely, here follows some typical profiles:
1638
1639@itemize
1640@item
1641The dependencies of a project you are working on.
1642
1643@item
1644Your favourite programming language libraries.
1645
1646@item
1647Laptop-specific programs (like @samp{powertop}) that you don't need on a desktop.
1648
1649@item
1650@TeX{}live (this one can be really useful when you need to install just one
1651package for this one document you've just received over email).
1652
1653@item
1654Games.
1655@end itemize
1656
1657Let's dive in the set up!
1658
1659@node Basic setup with manifests
1660@subsection Basic setup with manifests
1661
1662A Guix profile can be set up @emph{via} a so-called @emph{manifest specification} that looks like
1663this:
1664
b1eecb5c 1665@lisp
4c463569
PN
1666(specifications->manifest
1667 '("package-1"
1668 ;; Version 1.3 of package-2.
1669 "package-2@@1.3"
1670 ;; The "lib" output of package-3.
1671 "package-3:lib"
1672 ; ...
1673 "package-N"))
b1eecb5c 1674@end lisp
4c463569 1675
b1eecb5c 1676@pxref{Invoking guix package,,, guix, GNU Guix Reference Manual}, for
4c463569
PN
1677the syntax details.
1678
1679We can create a manifest specification per profile and install them this way:
1680
1681@example
1682GUIX_EXTRA_PROFILES=$HOME/.guix-extra-profiles
1683mkdir -p "$GUIX_EXTRA_PROFILES"/my-project # if it does not exist yet
1684guix package --manifest=/path/to/guix-my-project-manifest.scm --profile="$GUIX_EXTRA_PROFILES"/my-project/my-project
1685@end example
1686
1687Here we set an arbitrary variable @samp{GUIX_EXTRA_PROFILES} to point to the directory
1688where we will store our profiles in the rest of this article.
1689
1690Placing all your profiles in a single directory, with each profile getting its
1691own sub-directory, is somewhat cleaner. This way, each sub-directory will
1692contain all the symlinks for precisely one profile. Besides, "looping over
1693profiles" becomes obvious from any programming language (e.g. a shell script) by
1694simply looping over the sub-directories of @samp{$GUIX_EXTRA_PROFILES}.
1695
1696Note that it's also possible to loop over the output of
1697
1698@example
1699guix package --list-profiles
1700@end example
1701
1702although you'll probably have to filter out @samp{~/.config/guix/current}.
1703
1704To enable all profiles on login, add this to your @samp{~/.bash_profile} (or similar):
1705
1706@example
1707for i in $GUIX_EXTRA_PROFILES/*; do
1708 profile=$i/$(basename "$i")
1709 if [ -f "$profile"/etc/profile ]; then
1710 GUIX_PROFILE="$profile"
1711 . "$GUIX_PROFILE"/etc/profile
1712 fi
1713 unset profile
1714done
1715@end example
1716
1717Note to Guix System users: the above reflects how your default profile
1718@samp{~/.guix-profile} is activated from @samp{/etc/profile}, that latter being loaded by
1719@samp{~/.bashrc} by default.
1720
1721You can obviously choose to only enable a subset of them:
1722
1723@example
1724for i in "$GUIX_EXTRA_PROFILES"/my-project-1 "$GUIX_EXTRA_PROFILES"/my-project-2; do
1725 profile=$i/$(basename "$i")
1726 if [ -f "$profile"/etc/profile ]; then
1727 GUIX_PROFILE="$profile"
1728 . "$GUIX_PROFILE"/etc/profile
1729 fi
1730 unset profile
1731done
1732@end example
1733
1734When a profile is off, it's straightforward to enable it for an individual shell
1735without "polluting" the rest of the user session:
1736
1737@example
1738GUIX_PROFILE="path/to/my-project" ; . "$GUIX_PROFILE"/etc/profile
1739@end example
1740
1741The key to enabling a profile is to @emph{source} its @samp{etc/profile} file. This file
1742contains shell code that exports the right environment variables necessary to
1743activate the software contained in the profile. It is built automatically by
1744Guix and meant to be sourced.
1745It contains the same variables you would get if you ran:
1746
1747@example
1748guix package --search-paths=prefix --profile=$my_profile"
1749@end example
1750
1751Once again, see (@pxref{Invoking guix package,,, guix, GNU Guix Reference Manual})
1752for the command line options.
1753
1754To upgrade a profile, simply install the manifest again:
1755
1756@example
1757guix package -m /path/to/guix-my-project-manifest.scm -p "$GUIX_EXTRA_PROFILES"/my-project/my-project
1758@end example
1759
1760To upgrade all profiles, it's easy enough to loop over them. For instance,
1761assuming your manifest specifications are stored in
1762@samp{~/.guix-manifests/guix-$profile-manifest.scm}, with @samp{$profile} being the name
1763of the profile (e.g. "project1"), you could do the following in Bourne shell:
1764
1765@example
1766for profile in "$GUIX_EXTRA_PROFILES"/*; do
1767 guix package --profile="$profile" --manifest="$HOME/.guix-manifests/guix-$profile-manifest.scm"
1768done
1769@end example
1770
1771Each profile has its own generations:
1772
1773@example
1774guix package -p "$GUIX_EXTRA_PROFILES"/my-project/my-project --list-generations
1775@end example
1776
1777You can roll-back to any generation of a given profile:
1778
1779@example
1780guix package -p "$GUIX_EXTRA_PROFILES"/my-project/my-project --switch-generations=17
1781@end example
cb7b501d
PN
1782
1783Finally, if you want to switch to a profile without inheriting from the
1784current environment, you can activate it from an empty shell:
1785
1786@example
1787env -i $(which bash) --login --noprofile --norc
1788. my-project/etc/profile
1789@end example
4c463569
PN
1790
1791@node Required packages
1792@subsection Required packages
1793
1794Activating a profile essentially boils down to exporting a bunch of
1795environmental variables. This is the role of the @samp{etc/profile} within the
1796profile.
1797
1798@emph{Note: Only the environmental variables of the packages that consume them will
1799be set.}
1800
1801For instance, @samp{MANPATH} won't be set if there is no consumer application for man
1802pages within the profile. So if you need to transparently access man pages once
1803the profile is loaded, you've got two options:
1804
1805@itemize
1806@item
1807Either export the variable manually, e.g.
1808@example
f6c27c55 1809export MANPATH=/path/to/profile$@{MANPATH:+:@}$MANPATH
4c463569
PN
1810@end example
1811
1812@item
1813Or include @samp{man-db} to the profile manifest.
1814@end itemize
1815
1816The same is true for @samp{INFOPATH} (you can install @samp{info-reader}),
1817@samp{PKG_CONFIG_PATH} (install @samp{pkg-config}), etc.
1818
1819@node Default profile
1820@subsection Default profile
1821
1822What about the default profile that Guix keeps in @samp{~/.guix-profile}?
1823
1824You can assign it the role you want. Typically you would install the manifest
1825of the packages you want to use all the time.
1826
1827Alternatively, you could keep it "manifest-less" for throw-away packages
1828that you would just use for a couple of days.
1829This way makes it convenient to run
1830
1831@example
1832guix install package-foo
1833guix upgrade package-bar
1834@end example
1835
1836without having to specify the path to a profile.
1837
1838@node The benefits of manifests
1839@subsection The benefits of manifests
1840
1841Manifests are a convenient way to keep your package lists around and, say,
1842to synchronize them across multiple machines using a version control system.
1843
1844A common complaint about manifests is that they can be slow to install when they
1845contain large number of packages. This is especially cumbersome when you just
1846want get an upgrade for one package within a big manifest.
1847
1848This is one more reason to use multiple profiles, which happen to be just
1849perfect to break down manifests into multiple sets of semantically connected
1850packages. Using multiple, small profiles provides more flexibility and
1851usability.
1852
1853Manifests come with multiple benefits. In particular, they ease maintenance:
1854
1855@itemize
1856@item
1857When a profile is set up from a manifest, the manifest itself is
1858self-sufficient to keep a "package listing" around and reinstall the profile
1859later or on a different system. For ad-hoc profiles, we would need to
1860generate a manifest specification manually and maintain the package versions
1861for the packages that don't use the default version.
1862
1863@item
1864@code{guix package --upgrade} always tries to update the packages that have
1865propagated inputs, even if there is nothing to do. Guix manifests remove this
1866problem.
1867
1868@item
1869When partially upgrading a profile, conflicts may arise (due to diverging
1870dependencies between the updated and the non-updated packages) and they can be
1871annoying to resolve manually. Manifests remove this problem altogether since
1872all packages are always upgraded at once.
1873
1874@item
1875As mentioned above, manifests allow for reproducible profiles, while the
1876imperative @code{guix install}, @code{guix upgrade}, etc. do not, since they produce
1877different profiles every time even when they hold the same packages. See
1878@uref{https://issues.guix.gnu.org/issue/33285, the related discussion on the matter}.
1879
1880@item
1881Manifest specifications are usable by other @samp{guix} commands. For example, you
1882can run @code{guix weather -m manifest.scm} to see how many substitutes are
1883available, which can help you decide whether you want to try upgrading today
1884or wait a while. Another example: you can run @code{guix pack -m manifest.scm} to
1885create a pack containing all the packages in the manifest (and their
1886transitive references).
1887
1888@item
1889Finally, manifests have a Scheme representation, the @samp{<manifest>} record type.
1890They can be manipulated in Scheme and passed to the various Guix @uref{https://en.wikipedia.org/wiki/Api, APIs}.
1891@end itemize
1892
1893It's important to understand that while manifests can be used to declare
1894profiles, they are not strictly equivalent: profiles have the side effect that
1895they "pin" packages in the store, which prevents them from being
1896garbage-collected (@pxref{Invoking guix gc,,, guix, GNU Guix Reference Manual})
1897and ensures that they will still be available at any point in
1898the future.
1899
1900Let's take an example:
1901
1902@enumerate
1903@item
1904We have an environment for hacking on a project for which there isn't a Guix
1905package yet. We build the environment using a manifest, and then run @code{guix
1906 environment -m manifest.scm}. So far so good.
1907
1908@item
1909Many weeks pass and we have run a couple of @code{guix pull} in the mean time.
1910Maybe a dependency from our manifest has been updated; or we may have run
1911@code{guix gc} and some packages needed by our manifest have been
1912garbage-collected.
1913
1914@item
1915Eventually, we set to work on that project again, so we run @code{guix environment
1916 -m manifest.scm}. But now we have to wait for Guix to build and install
1917stuff!
1918@end enumerate
1919
1920Ideally, we could spare the rebuild time. And indeed we can, all we need is to
1921install the manifest to a profile and use @code{GUIX_PROFILE=/the/profile;
1922. "$GUIX_PROFILE"/etc/profile} as explained above: this guarantees that our
1923hacking environment will be available at all times.
1924
1925@emph{Security warning:} While keeping old profiles around can be convenient, keep in
1926mind that outdated packages may not have received the latest security fixes.
1927
1928@node Reproducible profiles
1929@subsection Reproducible profiles
1930
1931To reproduce a profile bit-for-bit, we need two pieces of information:
1932
1933@itemize
1934@item
1935a manifest,
1936@item
1937a Guix channel specification.
1938@end itemize
1939
1940Indeed, manifests alone might not be enough: different Guix versions (or
1941different channels) can produce different outputs for a given manifest.
1942
1943You can output the Guix channel specification with @samp{guix describe
1944--format=channels}.
1945Save this to a file, say @samp{channel-specs.scm}.
1946
1947On another computer, you can use the channel specification file and the manifest
1948to reproduce the exact same profile:
1949
1950@example
1951GUIX_EXTRA_PROFILES=$HOME/.guix-extra-profiles
1952GUIX_EXTRA=$HOME/.guix-extra
1953
1954mkdir "$GUIX_EXTRA"/my-project
1955guix pull --channels=channel-specs.scm --profile "$GUIX_EXTRA/my-project/guix"
1956
1957mkdir -p "$GUIX_EXTRA_PROFILES/my-project"
1958"$GUIX_EXTRA"/my-project/guix/bin/guix package --manifest=/path/to/guix-my-project-manifest.scm --profile="$GUIX_EXTRA_PROFILES"/my-project/my-project
1959@end example
1960
1961It's safe to delete the Guix channel profile you've just installed with the
1962channel specification, the project profile does not depend on it.
1963
7bc46ecc
RW
1964@c *********************************************************************
1965@node Acknowledgments
1966@chapter Acknowledgments
1967
1968Guix is based on the @uref{https://nixos.org/nix/, Nix package manager},
1969which was designed and
1970implemented by Eelco Dolstra, with contributions from other people (see
1971the @file{nix/AUTHORS} file in Guix.) Nix pioneered functional package
1972management, and promoted unprecedented features, such as transactional
1973package upgrades and rollbacks, per-user profiles, and referentially
1974transparent build processes. Without this work, Guix would not exist.
1975
1976The Nix-based software distributions, Nixpkgs and NixOS, have also been
1977an inspiration for Guix.
1978
1979GNU@tie{}Guix itself is a collective work with contributions from a
1980number of people. See the @file{AUTHORS} file in Guix for more
1981information on these fine people. The @file{THANKS} file lists people
1982who have helped by reporting bugs, taking care of the infrastructure,
1983providing artwork and themes, making suggestions, and more---thank you!
1984
1985This document includes adapted sections from articles that have previously
1986been published on the Guix blog at @uref{https://guix.gnu.org/blog}.
1987
1988
1989@c *********************************************************************
1990@node GNU Free Documentation License
1991@appendix GNU Free Documentation License
1992@cindex license, GNU Free Documentation License
1993@include fdl-1.3.texi
1994
1995@c *********************************************************************
1996@node Concept Index
1997@unnumbered Concept Index
1998@printindex cp
1999
2000@bye
2001
2002@c Local Variables:
2003@c ispell-local-dictionary: "american";
2004@c End: