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