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