guix-cookbook.texi 103 KB

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