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