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