guix-cookbook.texi 94 KB

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