guix-cookbook.texi 105 KB

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