base.scm 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
  4. ;;; Copyright © 2015, 2016, 2020 Mark H Weaver <mhw@netris.org>
  5. ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
  6. ;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
  7. ;;; Copyright © 2016 David Craven <david@craven.ch>
  8. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  9. ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
  10. ;;; Copyright © 2019 Efraim Flashner <efraim@flashner.co.il>
  11. ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
  12. ;;; Copyright © 2019 John Soo <jsoo1@asu.edu>
  13. ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  14. ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
  15. ;;; Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
  16. ;;; Copyright © 2021 qblade <qblade@protonmail.com>
  17. ;;; Copyright © 2021 Hui Lu <luhuins@163.com>
  18. ;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  19. ;;; Copyright © 2022 Guillaume Le Vaillant <glv@posteo.net>
  20. ;;;
  21. ;;; This file is part of GNU Guix.
  22. ;;;
  23. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  24. ;;; under the terms of the GNU General Public License as published by
  25. ;;; the Free Software Foundation; either version 3 of the License, or (at
  26. ;;; your option) any later version.
  27. ;;;
  28. ;;; GNU Guix is distributed in the hope that it will be useful, but
  29. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  30. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. ;;; GNU General Public License for more details.
  32. ;;;
  33. ;;; You should have received a copy of the GNU General Public License
  34. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  35. (define-module (gnu services base)
  36. #:use-module (guix store)
  37. #:use-module (guix deprecation)
  38. #:autoload (guix diagnostics) (warning &fix-hint)
  39. #:autoload (guix i18n) (G_)
  40. #:use-module (guix combinators)
  41. #:use-module (gnu services)
  42. #:use-module (gnu services admin)
  43. #:use-module (gnu services shepherd)
  44. #:use-module (gnu services sysctl)
  45. #:use-module (gnu system pam)
  46. #:use-module (gnu system shadow) ; 'user-account', etc.
  47. #:use-module (gnu system uuid)
  48. #:use-module (gnu system file-systems) ; 'file-system', etc.
  49. #:use-module (gnu system keyboard)
  50. #:use-module (gnu system mapped-devices)
  51. #:use-module ((gnu system linux-initrd)
  52. #:select (file-system-packages))
  53. #:use-module (gnu packages admin)
  54. #:use-module ((gnu packages linux)
  55. #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
  56. #:use-module (gnu packages bash)
  57. #:use-module ((gnu packages base)
  58. #:select (coreutils glibc glibc-utf8-locales tar))
  59. #:use-module ((gnu packages compression) #:select (gzip))
  60. #:autoload (gnu packages guile-xyz) (guile-netlink)
  61. #:autoload (gnu packages hurd) (hurd)
  62. #:use-module (gnu packages package-management)
  63. #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
  64. #:use-module (gnu packages linux)
  65. #:use-module (gnu packages terminals)
  66. #:use-module ((gnu build file-systems)
  67. #:select (mount-flags->bit-mask
  68. swap-space->flags-bit-mask))
  69. #:use-module (guix gexp)
  70. #:use-module (guix records)
  71. #:use-module (guix modules)
  72. #:use-module ((guix self) #:select (make-config.scm))
  73. #:use-module (guix diagnostics)
  74. #:use-module (guix i18n)
  75. #:use-module (srfi srfi-1)
  76. #:use-module (srfi srfi-26)
  77. #:use-module (srfi srfi-34)
  78. #:use-module (srfi srfi-35)
  79. #:use-module (ice-9 match)
  80. #:use-module (ice-9 format)
  81. #:re-export (user-processes-service-type ;backwards compatibility
  82. %default-substitute-urls)
  83. #:export (fstab-service-type
  84. root-file-system-service
  85. file-system-service-type
  86. swap-service
  87. host-name-service
  88. %default-console-font
  89. console-font-service-type
  90. console-font-service
  91. virtual-terminal-service-type
  92. static-networking
  93. static-networking?
  94. static-networking-addresses
  95. static-networking-links
  96. static-networking-routes
  97. static-networking-requirement
  98. network-address
  99. network-address?
  100. network-address-device
  101. network-address-value
  102. network-address-ipv6?
  103. network-link
  104. network-link?
  105. network-link-name
  106. network-link-type
  107. network-link-arguments
  108. network-route
  109. network-route?
  110. network-route-destination
  111. network-route-source
  112. network-route-device
  113. network-route-ipv6?
  114. network-route-gateway
  115. static-networking-service
  116. static-networking-service-type
  117. %loopback-static-networking
  118. %qemu-static-networking
  119. udev-configuration
  120. udev-configuration?
  121. udev-configuration-rules
  122. udev-service-type
  123. udev-service
  124. udev-rule
  125. file->udev-rule
  126. udev-rules-service
  127. login-configuration
  128. login-configuration?
  129. login-service-type
  130. login-service
  131. agetty-configuration
  132. agetty-configuration?
  133. agetty-service
  134. agetty-service-type
  135. mingetty-configuration
  136. mingetty-configuration-tty
  137. mingetty-configuration-auto-login
  138. mingetty-configuration-login-program
  139. mingetty-configuration-login-pause?
  140. mingetty-configuration-clear-on-logout?
  141. mingetty-configuration-mingetty
  142. mingetty-configuration?
  143. mingetty-service
  144. mingetty-service-type
  145. %nscd-default-caches
  146. %nscd-default-configuration
  147. nscd-configuration
  148. nscd-configuration?
  149. nscd-cache
  150. nscd-cache?
  151. nscd-service-type
  152. nscd-service
  153. syslog-configuration
  154. syslog-configuration?
  155. syslog-service
  156. syslog-service-type
  157. %default-syslog.conf
  158. %default-authorized-guix-keys
  159. guix-configuration
  160. guix-configuration?
  161. guix-configuration-guix
  162. guix-configuration-build-group
  163. guix-configuration-build-accounts
  164. guix-configuration-authorize-key?
  165. guix-configuration-authorized-keys
  166. guix-configuration-use-substitutes?
  167. guix-configuration-substitute-urls
  168. guix-configuration-extra-options
  169. guix-configuration-log-file
  170. guix-service-type
  171. guix-publish-configuration
  172. guix-publish-configuration?
  173. guix-publish-configuration-guix
  174. guix-publish-configuration-port
  175. guix-publish-configuration-host
  176. guix-publish-configuration-compression
  177. guix-publish-configuration-compression-level ;deprecated
  178. guix-publish-configuration-nar-path
  179. guix-publish-configuration-cache
  180. guix-publish-configuration-ttl
  181. guix-publish-configuration-negative-ttl
  182. guix-publish-service-type
  183. gpm-configuration
  184. gpm-configuration?
  185. gpm-service-type
  186. urandom-seed-service-type
  187. rngd-configuration
  188. rngd-configuration?
  189. rngd-service-type
  190. rngd-service
  191. kmscon-configuration
  192. kmscon-configuration?
  193. kmscon-service-type
  194. pam-limits-service-type
  195. pam-limits-service
  196. references-file
  197. %base-services))
  198. ;;; Commentary:
  199. ;;;
  200. ;;; Base system services---i.e., services that 99% of the users will want to
  201. ;;; use.
  202. ;;;
  203. ;;; Code:
  204. ;;;
  205. ;;; File systems.
  206. ;;;
  207. (define (file-system->fstab-entry file-system)
  208. "Return a @file{/etc/fstab} entry for @var{file-system}."
  209. (string-append (match (file-system-device file-system)
  210. ((? file-system-label? label)
  211. (string-append "LABEL="
  212. (file-system-label->string label)))
  213. ((? uuid? uuid)
  214. (string-append "UUID=" (uuid->string uuid)))
  215. ((? string? device)
  216. device))
  217. "\t"
  218. (file-system-mount-point file-system) "\t"
  219. (file-system-type file-system) "\t"
  220. (or (file-system-options file-system) "defaults") "\t"
  221. ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
  222. ;; don't have anything sensible to put in there.
  223. ))
  224. (define (file-systems->fstab file-systems)
  225. "Return a @file{/etc} entry for an @file{fstab} describing
  226. @var{file-systems}."
  227. `(("fstab" ,(plain-file "fstab"
  228. (string-append
  229. "\
  230. # This file was generated from your Guix configuration. Any changes
  231. # will be lost upon reboot or reconfiguration.\n\n"
  232. (string-join (map file-system->fstab-entry
  233. file-systems)
  234. "\n")
  235. "\n")))))
  236. (define fstab-service-type
  237. ;; The /etc/fstab service.
  238. (service-type (name 'fstab)
  239. (extensions
  240. (list (service-extension etc-service-type
  241. file-systems->fstab)))
  242. (compose concatenate)
  243. (extend append)
  244. (description
  245. "Populate the @file{/etc/fstab} based on the given file
  246. system objects.")))
  247. (define %root-file-system-shepherd-service
  248. (shepherd-service
  249. (documentation "Take care of the root file system.")
  250. (provision '(root-file-system))
  251. (start #~(const #t))
  252. (stop #~(lambda _
  253. ;; Return #f if successfully stopped.
  254. (sync)
  255. (call-with-blocked-asyncs
  256. (lambda ()
  257. (let ((null (%make-void-port "w")))
  258. ;; Close 'shepherd.log'.
  259. (display "closing log\n")
  260. ((@ (shepherd comm) stop-logging))
  261. ;; Redirect the default output ports..
  262. (set-current-output-port null)
  263. (set-current-error-port null)
  264. ;; Close /dev/console.
  265. (for-each close-fdes '(0 1 2))
  266. ;; At this point, there are no open files left, so the
  267. ;; root file system can be re-mounted read-only.
  268. (mount #f "/" #f
  269. (logior MS_REMOUNT MS_RDONLY)
  270. #:update-mtab? #f)
  271. #f)))))
  272. (respawn? #f)))
  273. (define root-file-system-service-type
  274. (shepherd-service-type 'root-file-system
  275. (const %root-file-system-shepherd-service)
  276. (description "Take care of syncing the root file
  277. system and of remounting it read-only when the system shuts down.")))
  278. (define (root-file-system-service)
  279. "Return a service whose sole purpose is to re-mount read-only the root file
  280. system upon shutdown (aka. cleanly \"umounting\" root.)
  281. This service must be the root of the service dependency graph so that its
  282. 'stop' action is invoked when shepherd is the only process left."
  283. (service root-file-system-service-type #f))
  284. (define (file-system->shepherd-service-name file-system)
  285. "Return the symbol that denotes the service mounting and unmounting
  286. FILE-SYSTEM."
  287. (symbol-append 'file-system-
  288. (string->symbol (file-system-mount-point file-system))))
  289. (define (mapped-device->shepherd-service-name md)
  290. "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
  291. (symbol-append 'device-mapping-
  292. (string->symbol (string-join
  293. (mapped-device-targets md) "-"))))
  294. (define dependency->shepherd-service-name
  295. (match-lambda
  296. ((? mapped-device? md)
  297. (mapped-device->shepherd-service-name md))
  298. ((? file-system? fs)
  299. (file-system->shepherd-service-name fs))))
  300. (define (file-system-shepherd-service file-system)
  301. "Return the shepherd service for @var{file-system}, or @code{#f} if
  302. @var{file-system} is not auto-mounted or doesn't have its mount point created
  303. upon boot."
  304. (let ((target (file-system-mount-point file-system))
  305. (create? (file-system-create-mount-point? file-system))
  306. (mount? (file-system-mount? file-system))
  307. (dependencies (file-system-dependencies file-system))
  308. (packages (file-system-packages (list file-system))))
  309. (and (or mount? create?)
  310. (with-imported-modules (source-module-closure
  311. '((gnu build file-systems)))
  312. (shepherd-service
  313. (provision (list (file-system->shepherd-service-name file-system)))
  314. (requirement `(root-file-system
  315. udev
  316. ,@(map dependency->shepherd-service-name dependencies)))
  317. (documentation "Check, mount, and unmount the given file system.")
  318. (start #~(lambda args
  319. #$(if create?
  320. #~(mkdir-p #$target)
  321. #t)
  322. #$(if mount?
  323. #~(let (($PATH (getenv "PATH")))
  324. ;; Make sure fsck.ext2 & co. can be found.
  325. (dynamic-wind
  326. (lambda ()
  327. ;; Don’t display the PATH settings.
  328. (with-output-to-port (%make-void-port "w")
  329. (lambda ()
  330. (set-path-environment-variable "PATH"
  331. '("bin" "sbin")
  332. '#$packages))))
  333. (lambda ()
  334. (mount-file-system
  335. (spec->file-system
  336. '#$(file-system->spec file-system))
  337. #:root "/"))
  338. (lambda ()
  339. (setenv "PATH" $PATH))))
  340. #t)
  341. #t))
  342. (stop #~(lambda args
  343. ;; Normally there are no processes left at this point, so
  344. ;; TARGET can be safely unmounted.
  345. ;; Make sure PID 1 doesn't keep TARGET busy.
  346. (chdir "/")
  347. (umount #$target)
  348. #f))
  349. ;; We need additional modules.
  350. (modules `(((gnu build file-systems)
  351. #:select (mount-file-system))
  352. (gnu system file-systems)
  353. ,@%default-modules)))))))
  354. (define (file-system-shepherd-services file-systems)
  355. "Return the list of Shepherd services for FILE-SYSTEMS."
  356. (let* ((file-systems (filter (lambda (x)
  357. (or (file-system-mount? x)
  358. (file-system-create-mount-point? x)))
  359. file-systems)))
  360. (define sink
  361. (shepherd-service
  362. (provision '(file-systems))
  363. (requirement (cons* 'root-file-system 'user-file-systems
  364. (map file-system->shepherd-service-name
  365. file-systems)))
  366. (documentation "Target for all the initially-mounted file systems")
  367. (start #~(const #t))
  368. (stop #~(const #f))))
  369. (define known-mount-points
  370. (map file-system-mount-point file-systems))
  371. (define user-unmount
  372. (shepherd-service
  373. (documentation "Unmount manually-mounted file systems.")
  374. (provision '(user-file-systems))
  375. (start #~(const #t))
  376. (stop #~(lambda args
  377. (define (known? mount-point)
  378. (member mount-point
  379. (cons* "/proc" "/sys" '#$known-mount-points)))
  380. ;; Make sure we don't keep the user's mount points busy.
  381. (chdir "/")
  382. (for-each (lambda (mount-point)
  383. (format #t "unmounting '~a'...~%" mount-point)
  384. (catch 'system-error
  385. (lambda ()
  386. (umount mount-point))
  387. (lambda args
  388. (let ((errno (system-error-errno args)))
  389. (format #t "failed to unmount '~a': ~a~%"
  390. mount-point (strerror errno))))))
  391. (filter (negate known?) (mount-points)))
  392. #f))))
  393. (cons* sink user-unmount
  394. (map file-system-shepherd-service file-systems))))
  395. (define (file-system-fstab-entries file-systems)
  396. "Return the subset of @var{file-systems} that should have an entry in
  397. @file{/etc/fstab}."
  398. ;; /etc/fstab is about telling fsck(8), mount(8), and umount(8) about
  399. ;; relevant file systems they'll have to deal with. That excludes "pseudo"
  400. ;; file systems.
  401. ;;
  402. ;; In particular, things like GIO (part of GLib) use it to determine the set
  403. ;; of mounts, which is then used by graphical file managers and desktop
  404. ;; environments to display "volume" icons. Thus, we really need to exclude
  405. ;; those pseudo file systems from the list.
  406. (remove (lambda (file-system)
  407. (or (member (file-system-type file-system)
  408. %pseudo-file-system-types)
  409. (memq 'bind-mount (file-system-flags file-system))))
  410. file-systems))
  411. (define file-system-service-type
  412. (service-type (name 'file-systems)
  413. (extensions
  414. (list (service-extension shepherd-root-service-type
  415. file-system-shepherd-services)
  416. (service-extension fstab-service-type
  417. file-system-fstab-entries)
  418. ;; Have 'user-processes' depend on 'file-systems'.
  419. (service-extension user-processes-service-type
  420. (const '(file-systems)))))
  421. (compose concatenate)
  422. (extend append)
  423. (description
  424. "Provide Shepherd services to mount and unmount the given
  425. file systems, as well as corresponding @file{/etc/fstab} entries.")))
  426. ;;;
  427. ;;; Preserve entropy to seed /dev/urandom on boot.
  428. ;;;
  429. (define %random-seed-file
  430. "/var/lib/random-seed")
  431. (define (urandom-seed-shepherd-service _)
  432. "Return a shepherd service for the /dev/urandom seed."
  433. (list (shepherd-service
  434. (documentation "Preserve entropy across reboots for /dev/urandom.")
  435. (provision '(urandom-seed))
  436. ;; Depend on udev so that /dev/hwrng is available.
  437. (requirement '(file-systems udev))
  438. (start #~(lambda _
  439. ;; On boot, write random seed into /dev/urandom.
  440. (when (file-exists? #$%random-seed-file)
  441. (call-with-input-file #$%random-seed-file
  442. (lambda (seed)
  443. (call-with-output-file "/dev/urandom"
  444. (lambda (urandom)
  445. (dump-port seed urandom)
  446. ;; Writing SEED to URANDOM isn't enough: we must
  447. ;; also tell the kernel to account for these
  448. ;; extra bits of entropy.
  449. (let ((bits (* 8 (stat:size (stat seed)))))
  450. (add-to-entropy-count urandom bits)))))))
  451. ;; Try writing from /dev/hwrng into /dev/urandom.
  452. ;; It seems that the file /dev/hwrng always exists, even
  453. ;; when there is no hardware random number generator
  454. ;; available. So, we handle a failed read or any other error
  455. ;; reported by the operating system.
  456. (let ((buf (catch 'system-error
  457. (lambda ()
  458. (call-with-input-file "/dev/hwrng"
  459. (lambda (hwrng)
  460. (get-bytevector-n hwrng 512))))
  461. ;; Silence is golden...
  462. (const #f))))
  463. (when buf
  464. (call-with-output-file "/dev/urandom"
  465. (lambda (urandom)
  466. (put-bytevector urandom buf)
  467. (let ((bits (* 8 (bytevector-length buf))))
  468. (add-to-entropy-count urandom bits))))))
  469. ;; Immediately refresh the seed in case the system doesn't
  470. ;; shut down cleanly.
  471. (call-with-input-file "/dev/urandom"
  472. (lambda (urandom)
  473. (let ((previous-umask (umask #o077))
  474. (buf (make-bytevector 512)))
  475. (mkdir-p (dirname #$%random-seed-file))
  476. (get-bytevector-n! urandom buf 0 512)
  477. (call-with-output-file #$%random-seed-file
  478. (lambda (seed)
  479. (put-bytevector seed buf)))
  480. (umask previous-umask))))
  481. #t))
  482. (stop #~(lambda _
  483. ;; During shutdown, write from /dev/urandom into random seed.
  484. (let ((buf (make-bytevector 512)))
  485. (call-with-input-file "/dev/urandom"
  486. (lambda (urandom)
  487. (let ((previous-umask (umask #o077)))
  488. (get-bytevector-n! urandom buf 0 512)
  489. (mkdir-p (dirname #$%random-seed-file))
  490. (call-with-output-file #$%random-seed-file
  491. (lambda (seed)
  492. (put-bytevector seed buf)))
  493. (umask previous-umask))
  494. #t)))))
  495. (modules `((rnrs bytevectors)
  496. (rnrs io ports)
  497. ,@%default-modules)))))
  498. (define urandom-seed-service-type
  499. (service-type (name 'urandom-seed)
  500. (extensions
  501. (list (service-extension shepherd-root-service-type
  502. urandom-seed-shepherd-service)
  503. ;; Have 'user-processes' depend on 'urandom-seed'.
  504. ;; This ensures that user processes and daemons don't
  505. ;; start until we have seeded the PRNG.
  506. (service-extension user-processes-service-type
  507. (const '(urandom-seed)))))
  508. (default-value #f)
  509. (description
  510. "Seed the @file{/dev/urandom} pseudo-random number
  511. generator (RNG) with the value recorded when the system was last shut
  512. down.")))
  513. ;;;
  514. ;;; Add hardware random number generator to entropy pool.
  515. ;;;
  516. (define-record-type* <rngd-configuration>
  517. rngd-configuration make-rngd-configuration
  518. rngd-configuration?
  519. (rng-tools rngd-configuration-rng-tools) ;file-like
  520. (device rngd-configuration-device)) ;string
  521. (define rngd-service-type
  522. (shepherd-service-type
  523. 'rngd
  524. (lambda (config)
  525. (define rng-tools (rngd-configuration-rng-tools config))
  526. (define device (rngd-configuration-device config))
  527. (define rngd-command
  528. (list (file-append rng-tools "/sbin/rngd")
  529. "-f" "-r" device))
  530. (shepherd-service
  531. (documentation "Add TRNG to entropy pool.")
  532. (requirement '(udev))
  533. (provision '(trng))
  534. (start #~(make-forkexec-constructor '#$rngd-command))
  535. (stop #~(make-kill-destructor))))
  536. (description "Run the @command{rngd} random number generation daemon to
  537. supply entropy to the kernel's pool.")))
  538. (define* (rngd-service #:key
  539. (rng-tools rng-tools)
  540. (device "/dev/hwrng"))
  541. "Return a service that runs the @command{rngd} program from @var{rng-tools}
  542. to add @var{device} to the kernel's entropy pool. The service will fail if
  543. @var{device} does not exist."
  544. (service rngd-service-type
  545. (rngd-configuration
  546. (rng-tools rng-tools)
  547. (device device))))
  548. ;;;
  549. ;;; Console & co.
  550. ;;;
  551. (define host-name-service-type
  552. (shepherd-service-type
  553. 'host-name
  554. (lambda (name)
  555. (shepherd-service
  556. (documentation "Initialize the machine's host name.")
  557. (provision '(host-name))
  558. (start #~(lambda _
  559. (sethostname #$name)))
  560. (one-shot? #t)))
  561. (description "Initialize the machine's host name.")))
  562. (define (host-name-service name)
  563. "Return a service that sets the host name to @var{name}."
  564. (service host-name-service-type name))
  565. (define virtual-terminal-service-type
  566. ;; Ensure that virtual terminals run in UTF-8 mode. This is the case by
  567. ;; default with recent Linux kernels, but this service allows us to ensure
  568. ;; this. This service must start before any 'term-' service so that newly
  569. ;; created terminals inherit this property. See
  570. ;; <https://bugs.gnu.org/30505> for a discussion.
  571. (shepherd-service-type
  572. 'virtual-terminal
  573. (lambda (utf8?)
  574. (let ((knob "/sys/module/vt/parameters/default_utf8"))
  575. (shepherd-service
  576. (documentation "Set virtual terminals in UTF-8 module.")
  577. (provision '(virtual-terminal))
  578. (requirement '(root-file-system))
  579. (start #~(lambda _
  580. ;; In containers /sys is read-only so don't insist on
  581. ;; writing to this file.
  582. (unless (= 1 (call-with-input-file #$knob read))
  583. (call-with-output-file #$knob
  584. (lambda (port)
  585. (display 1 port))))
  586. #t))
  587. (stop #~(const #f)))))
  588. #t ;default to UTF-8
  589. (description "Ensure the Linux virtual terminals run in UTF-8 mode.")))
  590. (define console-keymap-service-type
  591. (shepherd-service-type
  592. 'console-keymap
  593. (lambda (files)
  594. (shepherd-service
  595. (documentation (string-append "Load console keymap (loadkeys)."))
  596. (provision '(console-keymap))
  597. (start #~(lambda _
  598. (zero? (system* #$(file-append kbd "/bin/loadkeys")
  599. #$@files))))
  600. (respawn? #f)))
  601. (description "@emph{This service is deprecated in favor of the
  602. @code{keyboard-layout} field of @code{operating-system}.} Load the given list
  603. of console keymaps with @command{loadkeys}.")))
  604. (define %default-console-font
  605. ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
  606. ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
  607. ;; codepoints notably found in the UTF-8 manual.
  608. "LatGrkCyr-8x16")
  609. (define (console-font-shepherd-services tty+font)
  610. "Return a list of Shepherd services for each pair in TTY+FONT."
  611. (map (match-lambda
  612. ((tty . font)
  613. (let ((device (string-append "/dev/" tty)))
  614. (shepherd-service
  615. (documentation "Load a Unicode console font.")
  616. (provision (list (symbol-append 'console-font-
  617. (string->symbol tty))))
  618. ;; Start after mingetty has been started on TTY, otherwise the settings
  619. ;; are ignored.
  620. (requirement (list (symbol-append 'term-
  621. (string->symbol tty))))
  622. (start #~(lambda _
  623. ;; It could be that mingetty is not fully ready yet,
  624. ;; which we check by calling 'ttyname'.
  625. (let loop ((i 10))
  626. (unless (or (zero? i)
  627. (call-with-input-file #$device
  628. (lambda (port)
  629. (false-if-exception (ttyname port)))))
  630. (usleep 500)
  631. (loop (- i 1))))
  632. ;; Assume the VT is already in UTF-8 mode, thanks to
  633. ;; the 'virtual-terminal' service.
  634. ;;
  635. ;; 'setfont' returns EX_OSERR (71) when an
  636. ;; KDFONTOP ioctl fails, for example. Like
  637. ;; systemd's vconsole support, let's not treat
  638. ;; this as an error.
  639. (case (status:exit-val
  640. (system* #$(file-append kbd "/bin/setfont")
  641. "-C" #$device #$font))
  642. ((0 71) #t)
  643. (else #f))))
  644. (stop #~(const #t))
  645. (respawn? #f)))))
  646. tty+font))
  647. (define console-font-service-type
  648. (service-type (name 'console-fonts)
  649. (extensions
  650. (list (service-extension shepherd-root-service-type
  651. console-font-shepherd-services)))
  652. (compose concatenate)
  653. (extend append)
  654. (description
  655. "Install the given fonts on the specified ttys (fonts are per
  656. virtual console on GNU/Linux). The value of this service is a list of
  657. tty/font pairs. The font can be the name of a font provided by the @code{kbd}
  658. package or any valid argument to @command{setfont}, as in this example:
  659. @example
  660. `((\"tty1\" . \"LatGrkCyr-8x16\")
  661. (\"tty2\" . ,(file-append
  662. font-tamzen
  663. \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
  664. (\"tty3\" . ,(file-append
  665. font-terminus
  666. \"/share/consolefonts/ter-132n\"))) ; for HDPI
  667. @end example\n")))
  668. (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
  669. "This procedure is deprecated in favor of @code{console-font-service-type}.
  670. Return a service that sets up Unicode support in @var{tty} and loads
  671. @var{font} for that tty (fonts are per virtual console in Linux.)"
  672. (simple-service (symbol-append 'console-font- (string->symbol tty))
  673. console-font-service-type `((,tty . ,font))))
  674. (define %default-motd
  675. (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
  676. (define-record-type* <login-configuration>
  677. login-configuration make-login-configuration
  678. login-configuration?
  679. (motd login-configuration-motd ;file-like
  680. (default %default-motd))
  681. ;; Allow empty passwords by default so that first-time users can log in when
  682. ;; the 'root' account has just been created.
  683. (allow-empty-passwords? login-configuration-allow-empty-passwords?
  684. (default #t))) ;Boolean
  685. (define (login-pam-service config)
  686. "Return the list of PAM service needed for CONF."
  687. ;; Let 'login' be known to PAM.
  688. (list (unix-pam-service "login"
  689. #:login-uid? #t
  690. #:allow-empty-passwords?
  691. (login-configuration-allow-empty-passwords? config)
  692. #:motd
  693. (login-configuration-motd config))))
  694. (define login-service-type
  695. (service-type (name 'login)
  696. (extensions (list (service-extension pam-root-service-type
  697. login-pam-service)))
  698. (default-value (login-configuration))
  699. (description
  700. "Provide a console log-in service as specified by its
  701. configuration value, a @code{login-configuration} object.")))
  702. (define* (login-service #:optional (config (login-configuration)))
  703. "Return a service configure login according to @var{config}, which specifies
  704. the message of the day, among other things."
  705. (service login-service-type config))
  706. (define-record-type* <agetty-configuration>
  707. agetty-configuration make-agetty-configuration
  708. agetty-configuration?
  709. (agetty agetty-configuration-agetty ;file-like
  710. (default util-linux))
  711. (tty agetty-configuration-tty) ;string | #f
  712. (term agetty-term ;string | #f
  713. (default #f))
  714. (baud-rate agetty-baud-rate ;string | #f
  715. (default #f))
  716. (auto-login agetty-auto-login ;list of strings | #f
  717. (default #f))
  718. (login-program agetty-login-program ;gexp
  719. (default (file-append shadow "/bin/login")))
  720. (login-pause? agetty-login-pause? ;Boolean
  721. (default #f))
  722. (eight-bits? agetty-eight-bits? ;Boolean
  723. (default #f))
  724. (no-reset? agetty-no-reset? ;Boolean
  725. (default #f))
  726. (remote? agetty-remote? ;Boolean
  727. (default #f))
  728. (flow-control? agetty-flow-control? ;Boolean
  729. (default #f))
  730. (host agetty-host ;string | #f
  731. (default #f))
  732. (no-issue? agetty-no-issue? ;Boolean
  733. (default #f))
  734. (init-string agetty-init-string ;string | #f
  735. (default #f))
  736. (no-clear? agetty-no-clear? ;Boolean
  737. (default #f))
  738. (local-line agetty-local-line ;always | never | auto
  739. (default #f))
  740. (extract-baud? agetty-extract-baud? ;Boolean
  741. (default #f))
  742. (skip-login? agetty-skip-login? ;Boolean
  743. (default #f))
  744. (no-newline? agetty-no-newline? ;Boolean
  745. (default #f))
  746. (login-options agetty-login-options ;string | #f
  747. (default #f))
  748. (chroot agetty-chroot ;string | #f
  749. (default #f))
  750. (hangup? agetty-hangup? ;Boolean
  751. (default #f))
  752. (keep-baud? agetty-keep-baud? ;Boolean
  753. (default #f))
  754. (timeout agetty-timeout ;integer | #f
  755. (default #f))
  756. (detect-case? agetty-detect-case? ;Boolean
  757. (default #f))
  758. (wait-cr? agetty-wait-cr? ;Boolean
  759. (default #f))
  760. (no-hints? agetty-no-hints? ;Boolean
  761. (default #f))
  762. (no-hostname? agetty-no hostname? ;Boolean
  763. (default #f))
  764. (long-hostname? agetty-long-hostname? ;Boolean
  765. (default #f))
  766. (erase-characters agetty-erase-characters ;string | #f
  767. (default #f))
  768. (kill-characters agetty-kill-characters ;string | #f
  769. (default #f))
  770. (chdir agetty-chdir ;string | #f
  771. (default #f))
  772. (delay agetty-delay ;integer | #f
  773. (default #f))
  774. (nice agetty-nice ;integer | #f
  775. (default #f))
  776. ;; "Escape hatch" for passing arbitrary command-line arguments.
  777. (extra-options agetty-extra-options ;list of strings
  778. (default '()))
  779. (shepherd-requirement agetty-shepherd-requirement ;list of SHEPHERD requirements
  780. (default '()))
  781. ;;; XXX Unimplemented for now!
  782. ;;; (issue-file agetty-issue-file ;file-like
  783. ;;; (default #f))
  784. )
  785. (define (default-serial-port)
  786. "Return a gexp that determines a reasonable default serial port
  787. to use as the tty. This is primarily useful for headless systems."
  788. (with-imported-modules (source-module-closure
  789. '((gnu build linux-boot))) ;for 'find-long-options'
  790. #~(begin
  791. ;; console=device,options
  792. ;; device: can be tty0, ttyS0, lp0, ttyUSB0 (serial).
  793. ;; options: BBBBPNF. P n|o|e, N number of bits,
  794. ;; F flow control (r RTS)
  795. (let* ((not-comma (char-set-complement (char-set #\,)))
  796. (command (linux-command-line))
  797. (agetty-specs (find-long-options "agetty.tty" command))
  798. (console-specs (filter (lambda (spec)
  799. (and (string-prefix? "tty" spec)
  800. (not (or
  801. (string-prefix? "tty0" spec)
  802. (string-prefix? "tty1" spec)
  803. (string-prefix? "tty2" spec)
  804. (string-prefix? "tty3" spec)
  805. (string-prefix? "tty4" spec)
  806. (string-prefix? "tty5" spec)
  807. (string-prefix? "tty6" spec)
  808. (string-prefix? "tty7" spec)
  809. (string-prefix? "tty8" spec)
  810. (string-prefix? "tty9" spec)))))
  811. (find-long-options "console" command)))
  812. (specs (append agetty-specs console-specs)))
  813. (match specs
  814. (() #f)
  815. ((spec _ ...)
  816. ;; Extract device name from first spec.
  817. (match (string-tokenize spec not-comma)
  818. ((device-name _ ...)
  819. device-name))))))))
  820. (define agetty-shepherd-service
  821. (match-lambda
  822. (($ <agetty-configuration> agetty tty term baud-rate auto-login
  823. login-program login-pause? eight-bits? no-reset? remote? flow-control?
  824. host no-issue? init-string no-clear? local-line extract-baud?
  825. skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
  826. detect-case? wait-cr? no-hints? no-hostname? long-hostname?
  827. erase-characters kill-characters chdir delay nice extra-options
  828. shepherd-requirement)
  829. (list
  830. (shepherd-service
  831. (documentation "Run agetty on a tty.")
  832. (provision (list (symbol-append 'term- (string->symbol (or tty "console")))))
  833. ;; Since the login prompt shows the host name, wait for the 'host-name'
  834. ;; service to be done. Also wait for udev essentially so that the tty
  835. ;; text is not lost in the middle of kernel messages (see also
  836. ;; mingetty-shepherd-service).
  837. (requirement (cons* 'user-processes 'host-name 'udev
  838. shepherd-requirement))
  839. (modules '((ice-9 match) (gnu build linux-boot)))
  840. (start
  841. (with-imported-modules (source-module-closure
  842. '((gnu build linux-boot)))
  843. #~(lambda args
  844. (let ((defaulted-tty #$(or tty (default-serial-port))))
  845. (apply
  846. (if defaulted-tty
  847. (make-forkexec-constructor
  848. (list #$(file-append util-linux "/sbin/agetty")
  849. #$@extra-options
  850. #$@(if eight-bits?
  851. #~("--8bits")
  852. #~())
  853. #$@(if no-reset?
  854. #~("--noreset")
  855. #~())
  856. #$@(if remote?
  857. #~("--remote")
  858. #~())
  859. #$@(if flow-control?
  860. #~("--flow-control")
  861. #~())
  862. #$@(if host
  863. #~("--host" #$host)
  864. #~())
  865. #$@(if no-issue?
  866. #~("--noissue")
  867. #~())
  868. #$@(if init-string
  869. #~("--init-string" #$init-string)
  870. #~())
  871. #$@(if no-clear?
  872. #~("--noclear")
  873. #~())
  874. ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
  875. ;;; is not passed, then the default is 'auto'. However, in my tests, when that
  876. ;;; option is selected, agetty never presents the login prompt, and the
  877. ;;; term-ttyS0 service respawns every few seconds.
  878. #$@(if local-line
  879. #~(#$(match local-line
  880. ('auto "--local-line=auto")
  881. ('always "--local-line=always")
  882. ('never "-local-line=never")))
  883. #~())
  884. #$@(if tty
  885. #~()
  886. #~("--keep-baud"))
  887. #$@(if extract-baud?
  888. #~("--extract-baud")
  889. #~())
  890. #$@(if skip-login?
  891. #~("--skip-login")
  892. #~())
  893. #$@(if no-newline?
  894. #~("--nonewline")
  895. #~())
  896. #$@(if login-options
  897. #~("--login-options" #$login-options)
  898. #~())
  899. #$@(if chroot
  900. #~("--chroot" #$chroot)
  901. #~())
  902. #$@(if hangup?
  903. #~("--hangup")
  904. #~())
  905. #$@(if keep-baud?
  906. #~("--keep-baud")
  907. #~())
  908. #$@(if timeout
  909. #~("--timeout" #$(number->string timeout))
  910. #~())
  911. #$@(if detect-case?
  912. #~("--detect-case")
  913. #~())
  914. #$@(if wait-cr?
  915. #~("--wait-cr")
  916. #~())
  917. #$@(if no-hints?
  918. #~("--nohints?")
  919. #~())
  920. #$@(if no-hostname?
  921. #~("--nohostname")
  922. #~())
  923. #$@(if long-hostname?
  924. #~("--long-hostname")
  925. #~())
  926. #$@(if erase-characters
  927. #~("--erase-chars" #$erase-characters)
  928. #~())
  929. #$@(if kill-characters
  930. #~("--kill-chars" #$kill-characters)
  931. #~())
  932. #$@(if chdir
  933. #~("--chdir" #$chdir)
  934. #~())
  935. #$@(if delay
  936. #~("--delay" #$(number->string delay))
  937. #~())
  938. #$@(if nice
  939. #~("--nice" #$(number->string nice))
  940. #~())
  941. #$@(if auto-login
  942. (list "--autologin" auto-login)
  943. '())
  944. #$@(if login-program
  945. #~("--login-program" #$login-program)
  946. #~())
  947. #$@(if login-pause?
  948. #~("--login-pause")
  949. #~())
  950. defaulted-tty
  951. #$@(if baud-rate
  952. #~(#$baud-rate)
  953. #~())
  954. #$@(if term
  955. #~(#$term)
  956. #~())))
  957. (const #f)) ; never start.
  958. args)))))
  959. (stop #~(make-kill-destructor)))))))
  960. (define agetty-service-type
  961. (service-type (name 'agetty)
  962. (extensions (list (service-extension shepherd-root-service-type
  963. agetty-shepherd-service)))
  964. (description
  965. "Provide console login using the @command{agetty}
  966. program.")))
  967. (define* (agetty-service config)
  968. "Return a service to run agetty according to @var{config}, which specifies
  969. the tty to run, among other things."
  970. (service agetty-service-type config))
  971. (define-record-type* <mingetty-configuration>
  972. mingetty-configuration make-mingetty-configuration
  973. mingetty-configuration?
  974. (mingetty mingetty-configuration-mingetty ;file-like
  975. (default mingetty))
  976. (tty mingetty-configuration-tty) ;string
  977. (auto-login mingetty-auto-login ;string | #f
  978. (default #f))
  979. (login-program mingetty-login-program ;gexp
  980. (default #f))
  981. (login-pause? mingetty-login-pause? ;Boolean
  982. (default #f))
  983. (clear-on-logout? mingetty-clear-on-logout? ;Boolean
  984. (default #t)))
  985. (define mingetty-shepherd-service
  986. (match-lambda
  987. (($ <mingetty-configuration> mingetty tty auto-login login-program
  988. login-pause? clear-on-logout?)
  989. (list
  990. (shepherd-service
  991. (documentation "Run mingetty on an tty.")
  992. (provision (list (symbol-append 'term- (string->symbol tty))))
  993. ;; Since the login prompt shows the host name, wait for the 'host-name'
  994. ;; service to be done. Also wait for udev essentially so that the tty
  995. ;; text is not lost in the middle of kernel messages (XXX).
  996. (requirement '(user-processes host-name udev virtual-terminal))
  997. (start #~(make-forkexec-constructor
  998. (list #$(file-append mingetty "/sbin/mingetty")
  999. ;; Avoiding 'vhangup' allows us to avoid 'setfont'
  1000. ;; errors down the path where various ioctls get
  1001. ;; EIO--see 'hung_up_tty_ioctl' in driver/tty/tty_io.c
  1002. ;; in Linux.
  1003. "--nohangup" #$tty
  1004. #$@(if clear-on-logout?
  1005. #~()
  1006. #~("--noclear"))
  1007. #$@(if auto-login
  1008. #~("--autologin" #$auto-login)
  1009. #~())
  1010. #$@(if login-program
  1011. #~("--loginprog" #$login-program)
  1012. #~())
  1013. #$@(if login-pause?
  1014. #~("--loginpause")
  1015. #~()))))
  1016. (stop #~(make-kill-destructor)))))))
  1017. (define mingetty-service-type
  1018. (service-type (name 'mingetty)
  1019. (extensions (list (service-extension shepherd-root-service-type
  1020. mingetty-shepherd-service)))
  1021. (description
  1022. "Provide console login using the @command{mingetty}
  1023. program.")))
  1024. (define* (mingetty-service config)
  1025. "Return a service to run mingetty according to @var{config}, which specifies
  1026. the tty to run, among other things."
  1027. (service mingetty-service-type config))
  1028. (define-record-type* <nscd-configuration> nscd-configuration
  1029. make-nscd-configuration
  1030. nscd-configuration?
  1031. (log-file nscd-configuration-log-file ;string
  1032. (default "/var/log/nscd.log"))
  1033. (debug-level nscd-debug-level ;integer
  1034. (default 0))
  1035. ;; TODO: See nscd.conf in glibc for other options to add.
  1036. (caches nscd-configuration-caches ;list of <nscd-cache>
  1037. (default %nscd-default-caches))
  1038. (name-services nscd-configuration-name-services ;list of file-like
  1039. (default '()))
  1040. (glibc nscd-configuration-glibc ;file-like
  1041. (default glibc)))
  1042. (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
  1043. nscd-cache?
  1044. (database nscd-cache-database) ;symbol
  1045. (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
  1046. (negative-time-to-live nscd-cache-negative-time-to-live
  1047. (default 20)) ;integer
  1048. (suggested-size nscd-cache-suggested-size ;integer ("default module
  1049. ;of hash table")
  1050. (default 211))
  1051. (check-files? nscd-cache-check-files? ;Boolean
  1052. (default #t))
  1053. (persistent? nscd-cache-persistent? ;Boolean
  1054. (default #t))
  1055. (shared? nscd-cache-shared? ;Boolean
  1056. (default #t))
  1057. (max-database-size nscd-cache-max-database-size ;integer
  1058. (default (* 32 (expt 2 20))))
  1059. (auto-propagate? nscd-cache-auto-propagate? ;Boolean
  1060. (default #t)))
  1061. (define %nscd-default-caches
  1062. ;; Caches that we want to enable by default. Note that when providing an
  1063. ;; empty nscd.conf, all caches are disabled.
  1064. (list (nscd-cache (database 'hosts)
  1065. ;; Aggressively cache the host name cache to improve
  1066. ;; privacy and resilience.
  1067. (positive-time-to-live (* 3600 12))
  1068. (negative-time-to-live 20)
  1069. (persistent? #t))
  1070. (nscd-cache (database 'services)
  1071. ;; Services are unlikely to change, so we can be even more
  1072. ;; aggressive.
  1073. (positive-time-to-live (* 3600 24))
  1074. (negative-time-to-live 3600)
  1075. (check-files? #t) ;check /etc/services changes
  1076. (persistent? #t))))
  1077. (define %nscd-default-configuration
  1078. ;; Default nscd configuration.
  1079. (nscd-configuration))
  1080. (define (nscd.conf-file config)
  1081. "Return the @file{nscd.conf} configuration file for @var{config}, an
  1082. @code{<nscd-configuration>} object."
  1083. (define cache->config
  1084. (match-lambda
  1085. (($ <nscd-cache> (= symbol->string database)
  1086. positive-ttl negative-ttl size check-files?
  1087. persistent? shared? max-size propagate?)
  1088. (string-append "\nenable-cache\t" database "\tyes\n"
  1089. "positive-time-to-live\t" database "\t"
  1090. (number->string positive-ttl) "\n"
  1091. "negative-time-to-live\t" database "\t"
  1092. (number->string negative-ttl) "\n"
  1093. "suggested-size\t" database "\t"
  1094. (number->string size) "\n"
  1095. "check-files\t" database "\t"
  1096. (if check-files? "yes\n" "no\n")
  1097. "persistent\t" database "\t"
  1098. (if persistent? "yes\n" "no\n")
  1099. "shared\t" database "\t"
  1100. (if shared? "yes\n" "no\n")
  1101. "max-db-size\t" database "\t"
  1102. (number->string max-size) "\n"
  1103. "auto-propagate\t" database "\t"
  1104. (if propagate? "yes\n" "no\n")))))
  1105. (match config
  1106. (($ <nscd-configuration> log-file debug-level caches)
  1107. (plain-file "nscd.conf"
  1108. (string-append "\
  1109. # Configuration of libc's name service cache daemon (nscd).\n\n"
  1110. (if log-file
  1111. (string-append "logfile\t" log-file)
  1112. "")
  1113. "\n"
  1114. (if debug-level
  1115. (string-append "debug-level\t"
  1116. (number->string debug-level))
  1117. "")
  1118. "\n"
  1119. (string-concatenate
  1120. (map cache->config caches)))))))
  1121. (define (nscd-action-procedure nscd config option)
  1122. ;; XXX: This is duplicated from mcron; factorize.
  1123. #~(lambda (_ . args)
  1124. ;; Run 'nscd' in a pipe so we can explicitly redirect its output to
  1125. ;; 'current-output-port', which at this stage is bound to the client
  1126. ;; connection.
  1127. (let ((pipe (apply open-pipe* OPEN_READ #$nscd
  1128. "-f" #$config #$option args)))
  1129. (let loop ()
  1130. (match (read-line pipe 'concat)
  1131. ((? eof-object?)
  1132. (catch 'system-error
  1133. (lambda ()
  1134. (zero? (close-pipe pipe)))
  1135. (lambda args
  1136. ;; There's a race with the SIGCHLD handler, which could
  1137. ;; call 'waitpid' before 'close-pipe' above does. If we
  1138. ;; get ECHILD, that means we lost the race; in that case, we
  1139. ;; cannot tell what the exit code was (FIXME).
  1140. (or (= ECHILD (system-error-errno args))
  1141. (apply throw args)))))
  1142. (line
  1143. (display line)
  1144. (loop)))))))
  1145. (define (nscd-actions nscd config)
  1146. "Return Shepherd actions for NSCD."
  1147. ;; Make this functionality available as actions because that's a simple way
  1148. ;; to run the right 'nscd' binary with the right config file.
  1149. (list (shepherd-action
  1150. (name 'statistics)
  1151. (documentation "Display statistics about nscd usage.")
  1152. (procedure (nscd-action-procedure nscd config "--statistics")))
  1153. (shepherd-action
  1154. (name 'invalidate)
  1155. (documentation
  1156. "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
  1157. (procedure (nscd-action-procedure nscd config "--invalidate")))))
  1158. (define (nscd-shepherd-service config)
  1159. "Return a shepherd service for CONFIG, an <nscd-configuration> object."
  1160. (let ((nscd (file-append (nscd-configuration-glibc config)
  1161. "/sbin/nscd"))
  1162. (nscd.conf (nscd.conf-file config))
  1163. (name-services (nscd-configuration-name-services config)))
  1164. (list (shepherd-service
  1165. (documentation "Run libc's name service cache daemon (nscd).")
  1166. (provision '(nscd))
  1167. (requirement '(user-processes))
  1168. (start #~(make-forkexec-constructor
  1169. (list #$nscd "-f" #$nscd.conf "--foreground")
  1170. ;; Wait for the PID file. However, the PID file is
  1171. ;; written before nscd is actually listening on its
  1172. ;; socket (XXX).
  1173. #:pid-file "/var/run/nscd/nscd.pid"
  1174. #:environment-variables
  1175. (list (string-append "LD_LIBRARY_PATH="
  1176. (string-join
  1177. (map (lambda (dir)
  1178. (string-append dir "/lib"))
  1179. (list #$@name-services))
  1180. ":")))))
  1181. (stop #~(make-kill-destructor))
  1182. (modules `((ice-9 popen) ;for the actions
  1183. (ice-9 rdelim)
  1184. (ice-9 match)
  1185. ,@%default-modules))
  1186. (actions (nscd-actions nscd nscd.conf))))))
  1187. (define nscd-activation
  1188. ;; Actions to take before starting nscd.
  1189. #~(begin
  1190. (use-modules (guix build utils))
  1191. (mkdir-p "/var/run/nscd")
  1192. (mkdir-p "/var/db/nscd") ;for the persistent cache
  1193. ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
  1194. ;; that file exists when it is started. Thus create it here. Note: on
  1195. ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
  1196. ;; is a symlink, hence 'lstat'.
  1197. (unless (false-if-exception (lstat "/etc/resolv.conf"))
  1198. (call-with-output-file "/etc/resolv.conf"
  1199. (lambda (port)
  1200. (display "# This is a placeholder.\n" port))))))
  1201. (define nscd-service-type
  1202. (service-type (name 'nscd)
  1203. (extensions
  1204. (list (service-extension activation-service-type
  1205. (const nscd-activation))
  1206. (service-extension shepherd-root-service-type
  1207. nscd-shepherd-service)))
  1208. ;; This can be extended by providing additional name services
  1209. ;; such as nss-mdns.
  1210. (compose concatenate)
  1211. (extend (lambda (config name-services)
  1212. (nscd-configuration
  1213. (inherit config)
  1214. (name-services (append
  1215. (nscd-configuration-name-services config)
  1216. name-services)))))
  1217. (default-value %nscd-default-configuration)
  1218. (description
  1219. "Runs libc's @dfn{name service cache daemon} (nscd) with the
  1220. given configuration---an @code{<nscd-configuration>} object. @xref{Name
  1221. Service Switch}, for an example.")))
  1222. (define* (nscd-service #:optional (config %nscd-default-configuration))
  1223. "Return a service that runs libc's name service cache daemon (nscd) with the
  1224. given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
  1225. Service Switch}, for an example."
  1226. (service nscd-service-type config))
  1227. (define-record-type* <syslog-configuration>
  1228. syslog-configuration make-syslog-configuration
  1229. syslog-configuration?
  1230. (syslogd syslog-configuration-syslogd
  1231. (default (file-append inetutils "/libexec/syslogd")))
  1232. (config-file syslog-configuration-config-file
  1233. (default %default-syslog.conf)))
  1234. (define syslog-service-type
  1235. (shepherd-service-type
  1236. 'syslog
  1237. (lambda (config)
  1238. (shepherd-service
  1239. (documentation "Run the syslog daemon (syslogd).")
  1240. (provision '(syslogd))
  1241. (requirement '(user-processes))
  1242. (start #~(let ((spawn (make-forkexec-constructor
  1243. (list #$(syslog-configuration-syslogd config)
  1244. "--rcfile"
  1245. #$(syslog-configuration-config-file config))
  1246. #:pid-file "/var/run/syslog.pid")))
  1247. (lambda ()
  1248. ;; Set the umask such that file permissions are #o640.
  1249. (let ((mask (umask #o137))
  1250. (pid (spawn)))
  1251. (umask mask)
  1252. pid))))
  1253. (stop #~(make-kill-destructor))))
  1254. (description "Run the syslog daemon, @command{syslogd}, which is
  1255. responsible for logging system messages.")))
  1256. ;; Snippet adapted from the GNU inetutils manual.
  1257. (define %default-syslog.conf
  1258. (plain-file "syslog.conf" "
  1259. # Log all error messages, authentication messages of
  1260. # level notice or higher and anything of level err or
  1261. # higher to the console.
  1262. # Don't log private authentication messages!
  1263. *.alert;auth.notice;authpriv.none /dev/console
  1264. # Log anything (except mail) of level info or higher.
  1265. # Don't log private authentication messages!
  1266. *.info;mail.none;authpriv.none /var/log/messages
  1267. # Like /var/log/messages, but also including \"debug\"-level logs.
  1268. *.debug;mail.none;authpriv.none /var/log/debug
  1269. # Same, in a different place.
  1270. *.info;mail.none;authpriv.none /dev/tty12
  1271. # The authpriv file has restricted access.
  1272. authpriv.* /var/log/secure
  1273. # Log all the mail messages in one place.
  1274. mail.* /var/log/maillog
  1275. "))
  1276. (define* (syslog-service #:optional (config (syslog-configuration)))
  1277. "Return a service that runs @command{syslogd} and takes
  1278. @var{<syslog-configuration>} as a parameter.
  1279. @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
  1280. information on the configuration file syntax."
  1281. (service syslog-service-type config))
  1282. (define pam-limits-service-type
  1283. (let ((security-limits
  1284. ;; Create /etc/security containing the provided "limits.conf" file.
  1285. (lambda (limits-file)
  1286. `(("security/limits.conf"
  1287. ,limits-file))))
  1288. (pam-extension
  1289. (lambda (pam)
  1290. (let ((pam-limits (pam-entry
  1291. (control "required")
  1292. (module "pam_limits.so")
  1293. (arguments '("conf=/etc/security/limits.conf")))))
  1294. (if (member (pam-service-name pam)
  1295. '("login" "su" "slim" "gdm-password" "sddm"))
  1296. (pam-service
  1297. (inherit pam)
  1298. (session (cons pam-limits
  1299. (pam-service-session pam))))
  1300. pam)))))
  1301. (service-type
  1302. (name 'limits)
  1303. (extensions
  1304. (list (service-extension etc-service-type security-limits)
  1305. (service-extension pam-root-service-type
  1306. (lambda _ (list pam-extension)))))
  1307. (description
  1308. "Install the specified resource usage limits by populating
  1309. @file{/etc/security/limits.conf} and using the @code{pam_limits}
  1310. authentication module."))))
  1311. (define* (pam-limits-service #:optional (limits '()))
  1312. "Return a service that makes selected programs respect the list of
  1313. pam-limits-entry specified in LIMITS via pam_limits.so."
  1314. (service pam-limits-service-type
  1315. (plain-file "limits.conf"
  1316. (string-join (map pam-limits-entry->string limits)
  1317. "\n"))))
  1318. ;;;
  1319. ;;; Guix services.
  1320. ;;;
  1321. (define* (guix-build-accounts count #:key
  1322. (group "guixbuild")
  1323. (shadow shadow))
  1324. "Return a list of COUNT user accounts for Guix build users with the given
  1325. GID."
  1326. (unfold (cut > <> count)
  1327. (lambda (n)
  1328. (user-account
  1329. (name (format #f "guixbuilder~2,'0d" n))
  1330. (system? #t)
  1331. (group group)
  1332. ;; guix-daemon expects GROUP to be listed as a
  1333. ;; supplementary group too:
  1334. ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
  1335. (supplementary-groups (list group "kvm"))
  1336. (comment (format #f "Guix Build User ~2d" n))
  1337. (home-directory "/var/empty")
  1338. (shell (file-append shadow "/sbin/nologin"))))
  1339. 1+
  1340. 1))
  1341. (define not-config?
  1342. ;; Select (guix …) and (gnu …) modules, except (guix config).
  1343. (match-lambda
  1344. (('guix 'config) #f)
  1345. (('guix rest ...) #t)
  1346. (('gnu rest ...) #t)
  1347. (rest #f)))
  1348. (define (substitute-key-authorization keys guix)
  1349. "Return a gexp with code to register KEYS, a list of files containing 'guix
  1350. archive' public keys, with GUIX."
  1351. (define default-acl
  1352. (with-extensions (list guile-gcrypt)
  1353. (with-imported-modules `(((guix config) => ,(make-config.scm))
  1354. ,@(source-module-closure '((guix pki))
  1355. #:select? not-config?))
  1356. (computed-file "acl"
  1357. #~(begin
  1358. (use-modules (guix pki)
  1359. (gcrypt pk-crypto)
  1360. (ice-9 rdelim))
  1361. (define keys
  1362. (map (lambda (file)
  1363. (call-with-input-file file
  1364. (compose string->canonical-sexp
  1365. read-string)))
  1366. '(#$@keys)))
  1367. (call-with-output-file #$output
  1368. (lambda (port)
  1369. (write-acl (public-keys->acl keys)
  1370. port))))))))
  1371. (with-imported-modules '((guix build utils))
  1372. #~(begin
  1373. (use-modules (guix build utils))
  1374. ;; If the ACL already exists, move it out of the way. Create a backup
  1375. ;; if it's a regular file: it's likely that the user manually updated
  1376. ;; it with 'guix archive --authorize'.
  1377. (if (file-exists? "/etc/guix/acl")
  1378. (if (and (symbolic-link? "/etc/guix/acl")
  1379. (store-file-name? (readlink "/etc/guix/acl")))
  1380. (delete-file "/etc/guix/acl")
  1381. (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
  1382. (mkdir-p "/etc/guix"))
  1383. ;; Installed the declared ACL.
  1384. (symlink #+default-acl "/etc/guix/acl"))))
  1385. (define %default-authorized-guix-keys
  1386. ;; List of authorized substitute keys.
  1387. (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")
  1388. (file-append guix "/share/guix/bordeaux.guix.gnu.org.pub")))
  1389. (define-record-type* <guix-configuration>
  1390. guix-configuration make-guix-configuration
  1391. guix-configuration?
  1392. (guix guix-configuration-guix ;file-like
  1393. (default guix))
  1394. (build-group guix-configuration-build-group ;string
  1395. (default "guixbuild"))
  1396. (build-accounts guix-configuration-build-accounts ;integer
  1397. (default 10))
  1398. (authorize-key? guix-configuration-authorize-key? ;Boolean
  1399. (default #t))
  1400. (authorized-keys guix-configuration-authorized-keys ;list of gexps
  1401. (default %default-authorized-guix-keys))
  1402. (use-substitutes? guix-configuration-use-substitutes? ;Boolean
  1403. (default #t))
  1404. (substitute-urls guix-configuration-substitute-urls ;list of strings
  1405. (default %default-substitute-urls))
  1406. (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
  1407. (default '()))
  1408. (max-silent-time guix-configuration-max-silent-time ;integer
  1409. (default 0))
  1410. (timeout guix-configuration-timeout ;integer
  1411. (default 0))
  1412. (log-compression guix-configuration-log-compression
  1413. (default 'gzip))
  1414. (discover? guix-configuration-discover?
  1415. (default #f))
  1416. (extra-options guix-configuration-extra-options ;list of strings
  1417. (default '()))
  1418. (log-file guix-configuration-log-file ;string
  1419. (default "/var/log/guix-daemon.log"))
  1420. (http-proxy guix-http-proxy ;string | #f
  1421. (default #f))
  1422. (tmpdir guix-tmpdir ;string | #f
  1423. (default #f)))
  1424. (define %default-guix-configuration
  1425. (guix-configuration))
  1426. (define shepherd-set-http-proxy-action
  1427. ;; Shepherd action to change the HTTP(S) proxy.
  1428. (shepherd-action
  1429. (name 'set-http-proxy)
  1430. (documentation
  1431. "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
  1432. (procedure #~(lambda* (_ #:optional proxy)
  1433. (let ((environment (environ)))
  1434. ;; A bit of a hack: communicate PROXY to the 'start'
  1435. ;; method via environment variables.
  1436. (if proxy
  1437. (begin
  1438. (format #t "changing HTTP/HTTPS \
  1439. proxy of 'guix-daemon' to ~s...~%"
  1440. proxy)
  1441. (setenv "http_proxy" proxy))
  1442. (begin
  1443. (format #t "clearing HTTP/HTTPS \
  1444. proxy of 'guix-daemon'...~%")
  1445. (unsetenv "http_proxy")))
  1446. (action 'guix-daemon 'restart)
  1447. (environ environment)
  1448. #t)))))
  1449. (define shepherd-discover-action
  1450. ;; Shepherd action to enable or disable substitute servers discovery.
  1451. (shepherd-action
  1452. (name 'discover)
  1453. (documentation
  1454. "Enable or disable substitute servers discovery and restart the
  1455. 'guix-daemon'.")
  1456. (procedure #~(lambda* (_ status)
  1457. (let ((environment (environ)))
  1458. (if (and status
  1459. (string=? status "on"))
  1460. (begin
  1461. (format #t "enable substitute servers discovery~%")
  1462. (setenv "discover" "on"))
  1463. (begin
  1464. (format #t "disable substitute servers discovery~%")
  1465. (unsetenv "discover")))
  1466. (action 'guix-daemon 'restart)
  1467. (environ environment)
  1468. #t)))))
  1469. (define (guix-shepherd-service config)
  1470. "Return a <shepherd-service> for the Guix daemon service with CONFIG."
  1471. (match-record config <guix-configuration>
  1472. (guix build-group build-accounts authorize-key? authorized-keys
  1473. use-substitutes? substitute-urls max-silent-time timeout
  1474. log-compression discover? extra-options log-file
  1475. http-proxy tmpdir chroot-directories)
  1476. (list (shepherd-service
  1477. (documentation "Run the Guix daemon.")
  1478. (provision '(guix-daemon))
  1479. (requirement '(user-processes))
  1480. (actions (list shepherd-set-http-proxy-action
  1481. shepherd-discover-action))
  1482. (modules '((srfi srfi-1)
  1483. (ice-9 match)
  1484. (gnu build shepherd)))
  1485. (start
  1486. (with-imported-modules `(((guix config) => ,(make-config.scm))
  1487. ,@(source-module-closure
  1488. '((gnu build shepherd))
  1489. #:select? not-config?))
  1490. #~(lambda args
  1491. (define proxy
  1492. ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
  1493. ;; the 'set-http-proxy' action.
  1494. (or (getenv "http_proxy") #$http-proxy))
  1495. (define discover?
  1496. (or (getenv "discover") #$discover?))
  1497. ;; Start the guix-daemon from a container, when supported,
  1498. ;; to solve an installation issue. See the comment below for
  1499. ;; more details.
  1500. (fork+exec-command/container
  1501. (cons* #$(file-append guix "/bin/guix-daemon")
  1502. "--build-users-group" #$build-group
  1503. "--max-silent-time"
  1504. #$(number->string max-silent-time)
  1505. "--timeout" #$(number->string timeout)
  1506. "--log-compression"
  1507. #$(symbol->string log-compression)
  1508. #$@(if use-substitutes?
  1509. '()
  1510. '("--no-substitutes"))
  1511. (string-append "--discover="
  1512. (if discover? "yes" "no"))
  1513. "--substitute-urls" #$(string-join substitute-urls)
  1514. #$@extra-options
  1515. ;; Add CHROOT-DIRECTORIES and all their dependencies
  1516. ;; (if these are store items) to the chroot.
  1517. (append-map
  1518. (lambda (file)
  1519. (append-map (lambda (directory)
  1520. (list "--chroot-directory"
  1521. directory))
  1522. (call-with-input-file file
  1523. read)))
  1524. '#$(map references-file
  1525. chroot-directories)))
  1526. ;; When running the installer, we need guix-daemon to
  1527. ;; operate from within the same MNT namespace as the
  1528. ;; installation container. In that case only, enter the
  1529. ;; namespace of the process PID passed as start argument.
  1530. ;; Otherwise, for symmetry purposes enter the caller
  1531. ;; namespaces which is a no-op.
  1532. #:pid (match args
  1533. ((pid) (string->number pid))
  1534. (else (getpid)))
  1535. #:environment-variables
  1536. (append (list #$@(if tmpdir
  1537. (list (string-append "TMPDIR=" tmpdir))
  1538. '())
  1539. ;; Make sure we run in a UTF-8 locale so that
  1540. ;; 'guix offload' correctly restores nars
  1541. ;; that contain UTF-8 file names such as
  1542. ;; 'nss-certs'. See
  1543. ;; <https://bugs.gnu.org/32942>.
  1544. (string-append "GUIX_LOCPATH="
  1545. #$glibc-utf8-locales
  1546. "/lib/locale")
  1547. "LC_ALL=en_US.utf8"
  1548. ;; Make 'tar' and 'gzip' available so
  1549. ;; that 'guix perform-download' can use
  1550. ;; them when downloading from Software
  1551. ;; Heritage via '(guix swh)'.
  1552. (string-append "PATH="
  1553. #$(file-append tar "/bin") ":"
  1554. #$(file-append gzip "/bin")))
  1555. (if proxy
  1556. (list (string-append "http_proxy=" proxy)
  1557. (string-append "https_proxy=" proxy))
  1558. '()))
  1559. #:log-file #$log-file))))
  1560. (stop #~(make-kill-destructor))))))
  1561. (define (guix-accounts config)
  1562. "Return the user accounts and user groups for CONFIG."
  1563. (match config
  1564. (($ <guix-configuration> _ build-group build-accounts)
  1565. (cons (user-group
  1566. (name build-group)
  1567. (system? #t)
  1568. ;; Use a fixed GID so that we can create the store with the right
  1569. ;; owner.
  1570. (id 30000))
  1571. (guix-build-accounts build-accounts
  1572. #:group build-group)))))
  1573. (define (guix-activation config)
  1574. "Return the activation gexp for CONFIG."
  1575. (match-record config <guix-configuration>
  1576. (guix authorize-key? authorized-keys)
  1577. #~(begin
  1578. ;; Assume that the store has BUILD-GROUP as its group. We could
  1579. ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
  1580. ;; chown leads to an entire copy of the tree, which is a bad idea.
  1581. ;; Generate a key pair and optionally authorize substitute server keys.
  1582. (unless (file-exists? "/etc/guix/signing-key.pub")
  1583. (system* #$(file-append guix "/bin/guix") "archive"
  1584. "--generate-key"))
  1585. #$(if authorize-key?
  1586. (substitute-key-authorization authorized-keys guix)
  1587. #~#f))))
  1588. (define* (references-file item #:optional (name "references"))
  1589. "Return a file that contains the list of references of ITEM."
  1590. (if (struct? item) ;lowerable object
  1591. (computed-file name
  1592. (with-extensions (list guile-gcrypt) ;for store-copy
  1593. (with-imported-modules (source-module-closure
  1594. '((guix build store-copy)))
  1595. #~(begin
  1596. (use-modules (guix build store-copy))
  1597. (call-with-output-file #$output
  1598. (lambda (port)
  1599. (write (map store-info-item
  1600. (call-with-input-file "graph"
  1601. read-reference-graph))
  1602. port))))))
  1603. #:options `(#:local-build? #f
  1604. #:references-graphs (("graph" ,item))))
  1605. (plain-file name "()")))
  1606. (define guix-service-type
  1607. (service-type
  1608. (name 'guix)
  1609. (extensions
  1610. (list (service-extension shepherd-root-service-type guix-shepherd-service)
  1611. (service-extension account-service-type guix-accounts)
  1612. (service-extension activation-service-type guix-activation)
  1613. (service-extension profile-service-type
  1614. (compose list guix-configuration-guix))))
  1615. ;; Extensions can specify extra directories to add to the build chroot.
  1616. (compose concatenate)
  1617. (extend (lambda (config directories)
  1618. (guix-configuration
  1619. (inherit config)
  1620. (chroot-directories
  1621. (append (guix-configuration-chroot-directories config)
  1622. directories)))))
  1623. (default-value (guix-configuration))
  1624. (description
  1625. "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
  1626. (define-record-type* <guix-publish-configuration>
  1627. guix-publish-configuration make-guix-publish-configuration
  1628. guix-publish-configuration?
  1629. (guix guix-publish-configuration-guix ;file-like
  1630. (default guix))
  1631. (port guix-publish-configuration-port ;number
  1632. (default 80))
  1633. (host guix-publish-configuration-host ;string
  1634. (default "localhost"))
  1635. (advertise? guix-publish-advertise? ;boolean
  1636. (default #f))
  1637. (compression guix-publish-configuration-compression
  1638. (thunked)
  1639. (default (default-compression this-record
  1640. (current-source-location))))
  1641. (compression-level %guix-publish-configuration-compression-level ;deprecated
  1642. (default #f))
  1643. (nar-path guix-publish-configuration-nar-path ;string
  1644. (default "nar"))
  1645. (cache guix-publish-configuration-cache ;#f | string
  1646. (default #f))
  1647. (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
  1648. (default (* 10 (expt 2 20)))) ;integer
  1649. (workers guix-publish-configuration-workers ;#f | integer
  1650. (default #f))
  1651. (ttl guix-publish-configuration-ttl ;#f | integer
  1652. (default #f))
  1653. (negative-ttl guix-publish-configuration-negative-ttl ;#f | integer
  1654. (default #f)))
  1655. (define-deprecated (guix-publish-configuration-compression-level config)
  1656. "Return a compression level, the old way."
  1657. (match (guix-publish-configuration-compression config)
  1658. (((_ level) _ ...) level)))
  1659. (define (default-compression config properties)
  1660. "Return the default 'guix publish' compression according to CONFIG, and
  1661. raise a deprecation warning if the 'compression-level' field was used."
  1662. (match (%guix-publish-configuration-compression-level config)
  1663. (#f
  1664. ;; Default to low compression levels when there's no cache so that users
  1665. ;; get good bandwidth by default.
  1666. (if (guix-publish-configuration-cache config)
  1667. '(("gzip" 5) ("zstd" 19))
  1668. '(("gzip" 3) ("zstd" 3)))) ;zstd compresses faster
  1669. (level
  1670. (warn-about-deprecation 'compression-level properties
  1671. #:replacement 'compression)
  1672. `(("gzip" ,level)))))
  1673. (define (guix-publish-shepherd-service config)
  1674. (define (config->compression-options config)
  1675. (match (guix-publish-configuration-compression config)
  1676. (() ;empty list means "no compression"
  1677. '("-C0"))
  1678. (lst
  1679. (append-map (match-lambda
  1680. ((type level)
  1681. `("-C" ,(string-append type ":"
  1682. (number->string level)))))
  1683. lst))))
  1684. (match-record config <guix-publish-configuration>
  1685. (guix port host nar-path cache workers ttl negative-ttl
  1686. cache-bypass-threshold advertise?)
  1687. (list (shepherd-service
  1688. (provision '(guix-publish))
  1689. (requirement `(user-processes
  1690. guix-daemon
  1691. ,@(if advertise? '(avahi-daemon) '())))
  1692. (start #~(make-forkexec-constructor
  1693. (list #$(file-append guix "/bin/guix")
  1694. "publish" "-u" "guix-publish"
  1695. "-p" #$(number->string port)
  1696. #$@(config->compression-options config)
  1697. (string-append "--nar-path=" #$nar-path)
  1698. (string-append "--listen=" #$host)
  1699. #$@(if advertise?
  1700. #~("--advertise")
  1701. #~())
  1702. #$@(if workers
  1703. #~((string-append "--workers="
  1704. #$(number->string
  1705. workers)))
  1706. #~())
  1707. #$@(if ttl
  1708. #~((string-append "--ttl="
  1709. #$(number->string ttl)
  1710. "s"))
  1711. #~())
  1712. #$@(if negative-ttl
  1713. #~((string-append "--negative-ttl="
  1714. #$(number->string negative-ttl)
  1715. "s"))
  1716. #~())
  1717. #$@(if cache
  1718. #~((string-append "--cache=" #$cache)
  1719. #$(string-append
  1720. "--cache-bypass-threshold="
  1721. (number->string
  1722. cache-bypass-threshold)))
  1723. #~()))
  1724. ;; Make sure we run in a UTF-8 locale so we can produce
  1725. ;; nars for packages that contain UTF-8 file names such
  1726. ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
  1727. #:environment-variables
  1728. (list (string-append "GUIX_LOCPATH="
  1729. #$glibc-utf8-locales "/lib/locale")
  1730. "LC_ALL=en_US.utf8")
  1731. #:log-file "/var/log/guix-publish.log"))
  1732. (stop #~(make-kill-destructor))))))
  1733. (define %guix-publish-accounts
  1734. (list (user-group (name "guix-publish") (system? #t))
  1735. (user-account
  1736. (name "guix-publish")
  1737. (group "guix-publish")
  1738. (system? #t)
  1739. (comment "guix publish user")
  1740. (home-directory "/var/empty")
  1741. (shell (file-append shadow "/sbin/nologin")))))
  1742. (define %guix-publish-log-rotations
  1743. (list (log-rotation
  1744. (files (list "/var/log/guix-publish.log")))))
  1745. (define (guix-publish-activation config)
  1746. (let ((cache (guix-publish-configuration-cache config)))
  1747. (if cache
  1748. (with-imported-modules '((guix build utils))
  1749. #~(begin
  1750. (use-modules (guix build utils))
  1751. (mkdir-p #$cache)
  1752. (let* ((pw (getpw "guix-publish"))
  1753. (uid (passwd:uid pw))
  1754. (gid (passwd:gid pw)))
  1755. (chown #$cache uid gid))))
  1756. #t)))
  1757. (define guix-publish-service-type
  1758. (service-type (name 'guix-publish)
  1759. (extensions
  1760. (list (service-extension shepherd-root-service-type
  1761. guix-publish-shepherd-service)
  1762. (service-extension account-service-type
  1763. (const %guix-publish-accounts))
  1764. (service-extension rottlog-service-type
  1765. (const %guix-publish-log-rotations))
  1766. (service-extension activation-service-type
  1767. guix-publish-activation)))
  1768. (default-value (guix-publish-configuration))
  1769. (description
  1770. "Add a Shepherd service running @command{guix publish}, a
  1771. command that allows you to share pre-built binaries with others over HTTP.")))
  1772. ;;;
  1773. ;;; Udev.
  1774. ;;;
  1775. (define-record-type* <udev-configuration>
  1776. udev-configuration make-udev-configuration
  1777. udev-configuration?
  1778. (udev udev-configuration-udev ;file-like
  1779. (default eudev))
  1780. (rules udev-configuration-rules ;list of file-like
  1781. (default '())))
  1782. (define (udev-rules-union packages)
  1783. "Return the union of the @code{lib/udev/rules.d} directories found in each
  1784. item of @var{packages}."
  1785. (define build
  1786. (with-imported-modules '((guix build union)
  1787. (guix build utils))
  1788. #~(begin
  1789. (use-modules (guix build union)
  1790. (guix build utils)
  1791. (srfi srfi-1)
  1792. (srfi srfi-26))
  1793. (define %standard-locations
  1794. '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
  1795. (define (rules-sub-directory directory)
  1796. ;; Return the sub-directory of DIRECTORY containing udev rules, or
  1797. ;; #f if none was found.
  1798. (find directory-exists?
  1799. (map (cut string-append directory <>) %standard-locations)))
  1800. (union-build #$output
  1801. (filter-map rules-sub-directory '#$packages)))))
  1802. (computed-file "udev-rules" build))
  1803. (define (udev-rule file-name contents)
  1804. "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
  1805. (computed-file file-name
  1806. (with-imported-modules '((guix build utils))
  1807. #~(begin
  1808. (use-modules (guix build utils))
  1809. (define rules.d
  1810. (string-append #$output "/lib/udev/rules.d"))
  1811. (mkdir-p rules.d)
  1812. (call-with-output-file
  1813. (string-append rules.d "/" #$file-name)
  1814. (lambda (port)
  1815. (display #$contents port)))))))
  1816. (define (file->udev-rule file-name file)
  1817. "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
  1818. (computed-file file-name
  1819. (with-imported-modules '((guix build utils))
  1820. #~(begin
  1821. (use-modules (guix build utils))
  1822. (define rules.d
  1823. (string-append #$output "/lib/udev/rules.d"))
  1824. (define file-copy-dest
  1825. (string-append rules.d "/" #$file-name))
  1826. (mkdir-p rules.d)
  1827. (copy-file #$file file-copy-dest)))))
  1828. (define kvm-udev-rule
  1829. ;; Return a directory with a udev rule that changes the group of /dev/kvm to
  1830. ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
  1831. ;; but now we have to add it by ourselves.
  1832. ;; Build users are part of the "kvm" group, so we can fearlessly make
  1833. ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
  1834. (udev-rule "90-kvm.rules"
  1835. "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
  1836. (define udev-shepherd-service
  1837. ;; Return a <shepherd-service> for UDEV with RULES.
  1838. (match-lambda
  1839. (($ <udev-configuration> udev)
  1840. (list
  1841. (shepherd-service
  1842. (provision '(udev))
  1843. ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
  1844. ;; be added: see
  1845. ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
  1846. (requirement '(root-file-system))
  1847. (documentation "Populate the /dev directory, dynamically.")
  1848. (start
  1849. (with-imported-modules (source-module-closure
  1850. '((gnu build linux-boot)))
  1851. #~(lambda ()
  1852. (define udevd
  1853. ;; 'udevd' from eudev.
  1854. #$(file-append udev "/sbin/udevd"))
  1855. (define (wait-for-udevd)
  1856. ;; Wait until someone's listening on udevd's control
  1857. ;; socket.
  1858. (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
  1859. (let try ()
  1860. (catch 'system-error
  1861. (lambda ()
  1862. (connect sock PF_UNIX "/run/udev/control")
  1863. (close-port sock))
  1864. (lambda args
  1865. (format #t "waiting for udevd...~%")
  1866. (usleep 500000)
  1867. (try))))))
  1868. ;; Allow udev to find the modules.
  1869. (setenv "LINUX_MODULE_DIRECTORY"
  1870. "/run/booted-system/kernel/lib/modules")
  1871. (let* ((kernel-release
  1872. (utsname:release (uname)))
  1873. (linux-module-directory
  1874. (getenv "LINUX_MODULE_DIRECTORY"))
  1875. (directory
  1876. (string-append linux-module-directory "/"
  1877. kernel-release))
  1878. (old-umask (umask #o022)))
  1879. ;; If we're in a container, DIRECTORY might not exist,
  1880. ;; for instance because the host runs a different
  1881. ;; kernel. In that case, skip it; we'll just miss a few
  1882. ;; nodes like /dev/fuse.
  1883. (when (file-exists? directory)
  1884. (make-static-device-nodes directory))
  1885. (umask old-umask))
  1886. (let ((pid (fork+exec-command
  1887. (list udevd)
  1888. #:environment-variables
  1889. (cons*
  1890. ;; The first one is for udev, the second one for
  1891. ;; eudev.
  1892. "UDEV_CONFIG_FILE=/etc/udev/udev.conf"
  1893. "EUDEV_RULES_DIRECTORY=/etc/udev/rules.d"
  1894. (string-append "LINUX_MODULE_DIRECTORY="
  1895. (getenv "LINUX_MODULE_DIRECTORY"))
  1896. (default-environment-variables)))))
  1897. ;; Wait until udevd is up and running. This appears to
  1898. ;; be needed so that the events triggered below are
  1899. ;; actually handled.
  1900. (wait-for-udevd)
  1901. ;; Trigger device node creation.
  1902. (system* #$(file-append udev "/bin/udevadm")
  1903. "trigger" "--action=add")
  1904. ;; Wait for things to settle down.
  1905. (system* #$(file-append udev "/bin/udevadm")
  1906. "settle")
  1907. pid))))
  1908. (stop #~(make-kill-destructor))
  1909. ;; When halting the system, 'udev' is actually killed by
  1910. ;; 'user-processes', i.e., before its own 'stop' method was called.
  1911. ;; Thus, make sure it is not respawned.
  1912. (respawn? #f)
  1913. ;; We need additional modules.
  1914. (modules `((gnu build linux-boot) ;'make-static-device-nodes'
  1915. ,@%default-modules)))))))
  1916. (define udev.conf
  1917. (computed-file "udev.conf"
  1918. #~(call-with-output-file #$output
  1919. (lambda (port)
  1920. (format port "udev_rules=\"/etc/udev/rules.d\"~%")))))
  1921. (define udev-etc
  1922. (match-lambda
  1923. (($ <udev-configuration> udev rules)
  1924. `(("udev"
  1925. ,(file-union
  1926. "udev" `(("udev.conf" ,udev.conf)
  1927. ("rules.d" ,(udev-rules-union (cons* udev kvm-udev-rule
  1928. rules))))))))))
  1929. (define udev-service-type
  1930. (service-type (name 'udev)
  1931. (extensions
  1932. (list (service-extension shepherd-root-service-type
  1933. udev-shepherd-service)
  1934. (service-extension etc-service-type udev-etc)))
  1935. (compose concatenate) ;concatenate the list of rules
  1936. (extend (lambda (config rules)
  1937. (match config
  1938. (($ <udev-configuration> udev initial-rules)
  1939. (udev-configuration
  1940. (udev udev)
  1941. (rules (append initial-rules rules)))))))
  1942. (default-value (udev-configuration))
  1943. (description
  1944. "Run @command{udev}, which populates the @file{/dev}
  1945. directory dynamically. Get extra rules from the packages listed in the
  1946. @code{rules} field of its value, @code{udev-configuration} object.")))
  1947. (define* (udev-service #:key (udev eudev) (rules '()))
  1948. "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
  1949. extra rules from the packages listed in @var{rules}."
  1950. (service udev-service-type
  1951. (udev-configuration (udev udev) (rules rules))))
  1952. (define* (udev-rules-service name rules #:key (groups '()))
  1953. "Return a service that extends udev-service-type with RULES and
  1954. account-service-type with GROUPS as system groups. This works by creating a
  1955. singleton service type NAME-udev-rules, of which the returned service is an
  1956. instance."
  1957. (let* ((name (symbol-append name '-udev-rules))
  1958. (account-extension
  1959. (const (map (lambda (group)
  1960. (user-group (name group) (system? #t)))
  1961. groups)))
  1962. (udev-extension (const (list rules)))
  1963. (type (service-type
  1964. (name name)
  1965. (extensions (list
  1966. (service-extension
  1967. account-service-type account-extension)
  1968. (service-extension
  1969. udev-service-type udev-extension))))))
  1970. (service type #f)))
  1971. (define (swap-space->shepherd-service-name space)
  1972. (let ((target (swap-space-target space)))
  1973. (symbol-append 'swap-
  1974. (string->symbol
  1975. (cond ((uuid? target)
  1976. (uuid->string target))
  1977. ((file-system-label? target)
  1978. (file-system-label->string target))
  1979. (else
  1980. target))))))
  1981. ; TODO Remove after deprecation
  1982. (define (swap-deprecated->shepherd-service-name sdep)
  1983. (symbol-append 'swap-
  1984. (string->symbol
  1985. (cond ((uuid? sdep)
  1986. (string-take (uuid->string sdep) 6))
  1987. ((file-system-label? sdep)
  1988. (file-system-label->string sdep))
  1989. (else
  1990. sdep)))))
  1991. (define swap->shepherd-service-name
  1992. (match-lambda ((? swap-space? space)
  1993. (swap-space->shepherd-service-name space))
  1994. (sdep
  1995. (swap-deprecated->shepherd-service-name sdep))))
  1996. (define swap-service-type
  1997. (shepherd-service-type
  1998. 'swap
  1999. (lambda (swap)
  2000. (define requirements
  2001. (cond ((swap-space? swap)
  2002. (map dependency->shepherd-service-name
  2003. (swap-space-dependencies swap)))
  2004. ; TODO Remove after deprecation
  2005. ((and (string? swap) (string-prefix? "/dev/mapper/" swap))
  2006. (list (symbol-append 'device-mapping-
  2007. (string->symbol (basename swap)))))
  2008. (else
  2009. '())))
  2010. (define device-lookup
  2011. ;; The generic 'find-partition' procedures could return a partition
  2012. ;; that's not swap space, but that's unlikely.
  2013. (cond ((swap-space? swap)
  2014. (let ((target (swap-space-target swap)))
  2015. (cond ((uuid? target)
  2016. #~(find-partition-by-uuid #$(uuid-bytevector target)))
  2017. ((file-system-label? target)
  2018. #~(find-partition-by-label
  2019. #$(file-system-label->string target)))
  2020. (else
  2021. target))))
  2022. ; TODO Remove after deprecation
  2023. ((uuid? swap)
  2024. #~(find-partition-by-uuid #$(uuid-bytevector swap)))
  2025. ((file-system-label? swap)
  2026. #~(find-partition-by-label
  2027. #$(file-system-label->string swap)))
  2028. (else
  2029. swap)))
  2030. (with-imported-modules (source-module-closure '((gnu build file-systems)))
  2031. (shepherd-service
  2032. (provision (list (swap->shepherd-service-name swap)))
  2033. (requirement `(udev ,@requirements))
  2034. (documentation "Enable the given swap space.")
  2035. (modules `((gnu build file-systems)
  2036. ,@%default-modules))
  2037. (start #~(lambda ()
  2038. (let ((device #$device-lookup))
  2039. (and device
  2040. (begin
  2041. (restart-on-EINTR (swapon device
  2042. #$(if (swap-space? swap)
  2043. (swap-space->flags-bit-mask
  2044. swap)
  2045. 0)))
  2046. #t)))))
  2047. (stop #~(lambda _
  2048. (let ((device #$device-lookup))
  2049. (when device
  2050. (restart-on-EINTR (swapoff device)))
  2051. #f)))
  2052. (respawn? #f))))
  2053. (description "Turn on the virtual memory swap area.")))
  2054. (define (swap-service swap)
  2055. "Return a service that uses @var{swap} as a swap space."
  2056. (service swap-service-type swap))
  2057. (define %default-gpm-options
  2058. ;; Default options for GPM.
  2059. '("-m" "/dev/input/mice" "-t" "ps2"))
  2060. (define-record-type* <gpm-configuration>
  2061. gpm-configuration make-gpm-configuration gpm-configuration?
  2062. (gpm gpm-configuration-gpm ;file-like
  2063. (default gpm))
  2064. (options gpm-configuration-options ;list of strings
  2065. (default %default-gpm-options)))
  2066. (define gpm-shepherd-service
  2067. (match-lambda
  2068. (($ <gpm-configuration> gpm options)
  2069. (list (shepherd-service
  2070. (requirement '(udev))
  2071. (provision '(gpm))
  2072. ;; 'gpm' runs in the background and sets a PID file.
  2073. ;; Note that it requires running as "root".
  2074. (start #~(make-forkexec-constructor
  2075. (list #$(file-append gpm "/sbin/gpm")
  2076. #$@options)
  2077. #:pid-file "/var/run/gpm.pid"
  2078. #:pid-file-timeout 3))
  2079. (stop #~(lambda (_)
  2080. ;; Return #f if successfully stopped.
  2081. (not (zero? (system* #$(file-append gpm "/sbin/gpm")
  2082. "-k"))))))))))
  2083. (define gpm-service-type
  2084. (service-type (name 'gpm)
  2085. (extensions
  2086. (list (service-extension shepherd-root-service-type
  2087. gpm-shepherd-service)))
  2088. (default-value (gpm-configuration))
  2089. (description
  2090. "Run GPM, the general-purpose mouse daemon, with the given
  2091. command-line options. GPM allows users to use the mouse in the console,
  2092. notably to select, copy, and paste text. The default options use the
  2093. @code{ps2} protocol, which works for both USB and PS/2 mice.")))
  2094. (define-record-type* <kmscon-configuration>
  2095. kmscon-configuration make-kmscon-configuration
  2096. kmscon-configuration?
  2097. (kmscon kmscon-configuration-kmscon
  2098. (default kmscon))
  2099. (virtual-terminal kmscon-configuration-virtual-terminal)
  2100. (login-program kmscon-configuration-login-program
  2101. (default (file-append shadow "/bin/login")))
  2102. (login-arguments kmscon-configuration-login-arguments
  2103. (default '("-p")))
  2104. (auto-login kmscon-configuration-auto-login
  2105. (default #f))
  2106. (hardware-acceleration? kmscon-configuration-hardware-acceleration?
  2107. (default #f)) ; #t causes failure
  2108. (font-engine kmscon-configuration-font-engine
  2109. (default "pango"))
  2110. (font-size kmscon-configuration-font-size
  2111. (default 12))
  2112. (keyboard-layout kmscon-configuration-keyboard-layout
  2113. (default #f))) ; #f | <keyboard-layout>
  2114. (define kmscon-service-type
  2115. (shepherd-service-type
  2116. 'kmscon
  2117. (lambda (config)
  2118. (let ((kmscon (kmscon-configuration-kmscon config))
  2119. (virtual-terminal (kmscon-configuration-virtual-terminal config))
  2120. (login-program (kmscon-configuration-login-program config))
  2121. (login-arguments (kmscon-configuration-login-arguments config))
  2122. (auto-login (kmscon-configuration-auto-login config))
  2123. (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config))
  2124. (font-engine (kmscon-configuration-font-engine config))
  2125. (font-size (kmscon-configuration-font-size config))
  2126. (keyboard-layout (kmscon-configuration-keyboard-layout config)))
  2127. (define kmscon-command
  2128. #~(list
  2129. #$(file-append kmscon "/bin/kmscon") "--login"
  2130. "--vt" #$virtual-terminal
  2131. "--no-switchvt" ;Prevent a switch to the virtual terminal.
  2132. "--font-engine" #$font-engine
  2133. "--font-size" #$(number->string font-size)
  2134. #$@(if keyboard-layout
  2135. (let* ((layout (keyboard-layout-name keyboard-layout))
  2136. (variant (keyboard-layout-variant keyboard-layout))
  2137. (model (keyboard-layout-model keyboard-layout))
  2138. (options (keyboard-layout-options keyboard-layout)))
  2139. `("--xkb-layout" ,layout
  2140. ,@(if variant `("--xkb-variant" ,variant) '())
  2141. ,@(if model `("--xkb-model" ,model) '())
  2142. ,@(if (null? options)
  2143. '()
  2144. `("--xkb-options" ,(string-join options ",")))))
  2145. '())
  2146. #$@(if hardware-acceleration? '("--hwaccel") '())
  2147. "--login" "--"
  2148. #$login-program #$@login-arguments
  2149. #$@(if auto-login
  2150. #~(#$auto-login)
  2151. #~())))
  2152. (shepherd-service
  2153. (documentation "kmscon virtual terminal")
  2154. (requirement '(user-processes udev dbus-system))
  2155. (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
  2156. (start #~(make-forkexec-constructor #$kmscon-command))
  2157. (stop #~(make-kill-destructor)))))
  2158. (description "Start the @command{kmscon} virtual terminal emulator for the
  2159. Linux @dfn{kernel mode setting} (KMS).")))
  2160. ;;;
  2161. ;;; Static networking.
  2162. ;;;
  2163. (define (ipv6-address? str)
  2164. "Return true if STR denotes an IPv6 address."
  2165. (false-if-exception (->bool (inet-pton AF_INET6 str))))
  2166. (define-compile-time-procedure (assert-valid-address (address string?))
  2167. "Ensure ADDRESS has a valid netmask."
  2168. (unless (cidr->netmask address)
  2169. (raise
  2170. (make-compound-condition
  2171. (formatted-message (G_ "address '~a' lacks a network mask")
  2172. address)
  2173. (condition (&error-location
  2174. (location
  2175. (source-properties->location procedure-call-location))))
  2176. (condition (&fix-hint
  2177. (hint (format #f (G_ "\
  2178. Write, say, @samp{\"~a/24\"} for a 24-bit network mask.")
  2179. address)))))))
  2180. address)
  2181. (define-record-type* <static-networking>
  2182. static-networking make-static-networking
  2183. static-networking?
  2184. (addresses static-networking-addresses) ;list of <network-address>
  2185. (links static-networking-links (default '())) ;list of <network-link>
  2186. (routes static-networking-routes (default '())) ;list of <network-routes>
  2187. (provision static-networking-provision
  2188. (default '(networking)))
  2189. (requirement static-networking-requirement
  2190. (default '(udev)))
  2191. (name-servers static-networking-name-servers ;FIXME: doesn't belong here
  2192. (default '())))
  2193. (define-record-type* <network-address>
  2194. network-address make-network-address
  2195. network-address?
  2196. (device network-address-device) ;string--e.g., "en01"
  2197. (value network-address-value ;string--CIDR notation
  2198. (sanitize assert-valid-address))
  2199. (ipv6? network-address-ipv6? ;Boolean
  2200. (thunked)
  2201. (default
  2202. (ipv6-address? (cidr->ip (network-address-value this-record))))))
  2203. (define-record-type* <network-link>
  2204. network-link make-network-link
  2205. network-link?
  2206. (name network-link-name) ;string--e.g, "v0p0"
  2207. (type network-link-type) ;symbol--e.g.,'veth
  2208. (arguments network-link-arguments)) ;list
  2209. (define-record-type* <network-route>
  2210. network-route make-network-route
  2211. network-route?
  2212. (destination network-route-destination)
  2213. (source network-route-source (default #f))
  2214. (device network-route-device (default #f))
  2215. (ipv6? network-route-ipv6? (thunked)
  2216. (default
  2217. (or (ipv6-address? (network-route-destination this-record))
  2218. (and=> (network-route-gateway this-record)
  2219. ipv6-address?))))
  2220. (gateway network-route-gateway (default #f)))
  2221. (define* (cidr->netmask str #:optional (family AF_INET))
  2222. "Given @var{str}, a string in CIDR notation (e.g., \"1.2.3.4/24\"), return
  2223. the netmask as a string like \"255.255.255.0\"."
  2224. (match (string-split str #\/)
  2225. ((ip (= string->number bits))
  2226. (let ((mask (ash (- (expt 2 bits) 1)
  2227. (- (if (= family AF_INET6) 128 32)
  2228. bits))))
  2229. (inet-ntop family mask)))
  2230. (_ #f)))
  2231. (define (cidr->ip str)
  2232. "Strip the netmask bit of @var{str}, a CIDR-notation IP/netmask address."
  2233. (match (string-split str #\/)
  2234. ((or (ip _) (ip))
  2235. ip)))
  2236. (define* (ip+netmask->cidr ip netmask #:optional (family AF_INET))
  2237. "Return the CIDR notation (a string) for @var{ip} and @var{netmask}, two
  2238. @var{family} address strings, where @var{family} is @code{AF_INET} or
  2239. @code{AF_INET6}."
  2240. (let* ((netmask (inet-pton family netmask))
  2241. (bits (logcount netmask)))
  2242. (string-append ip "/" (number->string bits))))
  2243. (define (static-networking->hurd-pfinet-options config)
  2244. "Return command-line options for the Hurd's pfinet translator corresponding
  2245. to CONFIG."
  2246. (unless (null? (static-networking-links config))
  2247. ;; XXX: Presumably this is not supported, or perhaps could be approximated
  2248. ;; by running separate pfinet instances in some cases?
  2249. (warning (G_ "network links are currently ignored on GNU/Hurd~%")))
  2250. (match (static-networking-addresses config)
  2251. ((and addresses (first _ ...))
  2252. `("--ipv6" "/servers/socket/26"
  2253. "--interface" ,(network-address-device first)
  2254. ,@(append-map (lambda (address)
  2255. `(,(if (network-address-ipv6? address)
  2256. "--address6"
  2257. "--address")
  2258. ,(cidr->ip (network-address-value address))
  2259. ,@(match (cidr->netmask (network-address-value address)
  2260. (if (network-address-ipv6? address)
  2261. AF_INET6
  2262. AF_INET))
  2263. (#f '())
  2264. (mask (list "--netmask" mask)))))
  2265. addresses)
  2266. ,@(append-map (lambda (route)
  2267. (match route
  2268. (($ <network-route> "default" #f device _ gateway)
  2269. (if (network-route-ipv6? route)
  2270. `("--gateway6" ,gateway)
  2271. `("--gateway" ,gateway)))
  2272. (($ <network-route> destination)
  2273. (warning (G_ "ignoring network route for '~a'~%")
  2274. destination)
  2275. '())))
  2276. (static-networking-routes config))))))
  2277. (define (network-set-up/hurd config)
  2278. "Set up networking for the Hurd."
  2279. ;; The Hurd implements SIOCGIFADDR and other old-style ioctls, but the only
  2280. ;; way to set up IPv6 is by starting pfinet with the right options.
  2281. (if (equal? (static-networking-provision config) '(loopback))
  2282. (scheme-file "set-up-pflocal" #~(begin 'nothing-to-do! #t))
  2283. (scheme-file "set-up-pfinet"
  2284. (with-imported-modules '((guix build utils))
  2285. #~(begin
  2286. (use-modules (guix build utils)
  2287. (ice-9 format))
  2288. ;; TODO: Do that without forking.
  2289. (let ((options '#$(static-networking->hurd-pfinet-options
  2290. config)))
  2291. (format #t "starting '~a~{ ~s~}'~%"
  2292. #$(file-append hurd "/hurd/pfinet")
  2293. options)
  2294. (apply invoke #$(file-append hurd "/bin/settrans") "-fac"
  2295. "/servers/socket/2"
  2296. #$(file-append hurd "/hurd/pfinet")
  2297. options)))))))
  2298. (define (network-tear-down/hurd config)
  2299. (scheme-file "tear-down-pfinet"
  2300. (with-imported-modules '((guix build utils))
  2301. #~(begin
  2302. (use-modules (guix build utils))
  2303. ;; Forcefully terminate pfinet. XXX: In theory this
  2304. ;; should just undo the addresses and routes of CONFIG;
  2305. ;; this could be done using ioctls like SIOCDELRT, but
  2306. ;; these are IPv4-only; another option would be to use
  2307. ;; fsysopts but that seems to crash pfinet.
  2308. (invoke #$(file-append hurd "/bin/settrans") "-fg"
  2309. "/servers/socket/2")
  2310. #f))))
  2311. (define network-set-up/linux
  2312. (match-lambda
  2313. (($ <static-networking> addresses links routes)
  2314. (scheme-file "set-up-network"
  2315. (with-extensions (list guile-netlink)
  2316. #~(begin
  2317. (use-modules (ip addr) (ip link) (ip route))
  2318. #$@(map (lambda (address)
  2319. #~(begin
  2320. (addr-add #$(network-address-device address)
  2321. #$(network-address-value address)
  2322. #:ipv6?
  2323. #$(network-address-ipv6? address))
  2324. ;; FIXME: loopback?
  2325. (link-set #$(network-address-device address)
  2326. #:multicast-on #t
  2327. #:up #t)))
  2328. addresses)
  2329. #$@(map (match-lambda
  2330. (($ <network-link> name type arguments)
  2331. #~(link-add #$name #$type
  2332. #:type-args '#$arguments)))
  2333. links)
  2334. #$@(map (lambda (route)
  2335. #~(route-add #$(network-route-destination route)
  2336. #:device
  2337. #$(network-route-device route)
  2338. #:ipv6?
  2339. #$(network-route-ipv6? route)
  2340. #:via
  2341. #$(network-route-gateway route)
  2342. #:src
  2343. #$(network-route-source route)))
  2344. routes)
  2345. #t))))))
  2346. (define network-tear-down/linux
  2347. (match-lambda
  2348. (($ <static-networking> addresses links routes)
  2349. (scheme-file "tear-down-network"
  2350. (with-extensions (list guile-netlink)
  2351. #~(begin
  2352. (use-modules (ip addr) (ip link) (ip route)
  2353. (netlink error)
  2354. (srfi srfi-34))
  2355. (define-syntax-rule (false-if-netlink-error exp)
  2356. (guard (c ((netlink-error? c) #f))
  2357. exp))
  2358. ;; Wrap calls in 'false-if-netlink-error' so this
  2359. ;; script goes as far as possible undoing the effects
  2360. ;; of "set-up-network".
  2361. #$@(map (lambda (route)
  2362. #~(false-if-netlink-error
  2363. (route-del #$(network-route-destination route)
  2364. #:device
  2365. #$(network-route-device route)
  2366. #:ipv6?
  2367. #$(network-route-ipv6? route)
  2368. #:via
  2369. #$(network-route-gateway route)
  2370. #:src
  2371. #$(network-route-source route))))
  2372. routes)
  2373. #$@(map (match-lambda
  2374. (($ <network-link> name type arguments)
  2375. #~(false-if-netlink-error
  2376. (link-del #$name))))
  2377. links)
  2378. #$@(map (lambda (address)
  2379. #~(false-if-netlink-error
  2380. (addr-del #$(network-address-device
  2381. address)
  2382. #$(network-address-value address)
  2383. #:ipv6?
  2384. #$(network-address-ipv6? address))))
  2385. addresses)
  2386. #f))))))
  2387. (define (static-networking-shepherd-service config)
  2388. (match config
  2389. (($ <static-networking> addresses links routes
  2390. provision requirement name-servers)
  2391. (let ((loopback? (and provision (memq 'loopback provision))))
  2392. (shepherd-service
  2393. (documentation
  2394. "Bring up the networking interface using a static IP address.")
  2395. (requirement requirement)
  2396. (provision provision)
  2397. (start #~(lambda _
  2398. ;; Return #t if successfully started.
  2399. (load #$(let-system (system target)
  2400. (if (string-contains (or target system) "-linux")
  2401. (network-set-up/linux config)
  2402. (network-set-up/hurd config))))))
  2403. (stop #~(lambda _
  2404. ;; Return #f is successfully stopped.
  2405. (load #$(let-system (system target)
  2406. (if (string-contains (or target system) "-linux")
  2407. (network-tear-down/linux config)
  2408. (network-tear-down/hurd config))))))
  2409. (respawn? #f))))))
  2410. (define (static-networking-shepherd-services networks)
  2411. (map static-networking-shepherd-service networks))
  2412. (define (static-networking-etc-files interfaces)
  2413. "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
  2414. (match (delete-duplicates
  2415. (append-map static-networking-name-servers
  2416. interfaces))
  2417. (()
  2418. '())
  2419. ((name-servers ...)
  2420. (let ((content (string-join
  2421. (map (cut string-append "nameserver " <>)
  2422. name-servers)
  2423. "\n" 'suffix)))
  2424. `(("resolv.conf"
  2425. ,(plain-file "resolv.conf"
  2426. (string-append "\
  2427. # Generated by 'static-networking-service'.\n"
  2428. content))))))))
  2429. (define static-networking-service-type
  2430. ;; The service type for statically-defined network interfaces.
  2431. (service-type (name 'static-networking)
  2432. (extensions
  2433. (list
  2434. (service-extension shepherd-root-service-type
  2435. static-networking-shepherd-services)
  2436. (service-extension etc-service-type
  2437. static-networking-etc-files)))
  2438. (compose concatenate)
  2439. (extend append)
  2440. (description
  2441. "Turn up the specified network interfaces upon startup,
  2442. with the given IP address, gateway, netmask, and so on. The value for
  2443. services of this type is a list of @code{static-networking} objects, one per
  2444. network interface.")))
  2445. (define-deprecated (static-networking-service interface ip
  2446. #:key
  2447. netmask gateway provision
  2448. ;; Most interfaces require udev to be usable.
  2449. (requirement '(udev))
  2450. (name-servers '()))
  2451. static-networking-service-type
  2452. "Return a service that starts @var{interface} with address @var{ip}. If
  2453. @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
  2454. it must be a string specifying the default network gateway.
  2455. This procedure can be called several times, one for each network
  2456. interface of interest. Behind the scenes what it does is extend
  2457. @code{static-networking-service-type} with additional network interfaces
  2458. to handle."
  2459. (simple-service 'static-network-interface
  2460. static-networking-service-type
  2461. (list (static-networking
  2462. (addresses
  2463. (list (network-address
  2464. (device interface)
  2465. (value (if netmask
  2466. (ip+netmask->cidr ip netmask)
  2467. ip))
  2468. (ipv6? #f))))
  2469. (routes
  2470. (if gateway
  2471. (list (network-route
  2472. (destination "default")
  2473. (gateway gateway)
  2474. (ipv6? #f)))
  2475. '()))
  2476. (requirement requirement)
  2477. (provision (or provision '(networking)))
  2478. (name-servers name-servers)))))
  2479. (define %loopback-static-networking
  2480. ;; The loopback device.
  2481. (static-networking
  2482. (addresses (list (network-address
  2483. (device "lo")
  2484. (value "127.0.0.1/8"))))
  2485. (requirement '())
  2486. (provision '(loopback))))
  2487. (define %qemu-static-networking
  2488. ;; Networking configuration for QEMU's user-mode network stack (info "(QEMU)
  2489. ;; Using the user mode network stack").
  2490. (static-networking
  2491. (addresses (list (network-address
  2492. (device "eth0")
  2493. (value "10.0.2.15/24"))))
  2494. (routes (list (network-route
  2495. (destination "default")
  2496. (gateway "10.0.2.2"))))
  2497. (requirement '())
  2498. (provision '(networking))
  2499. (name-servers '("10.0.2.3"))))
  2500. (define %base-services
  2501. ;; Convenience variable holding the basic services.
  2502. (list (service login-service-type)
  2503. (service virtual-terminal-service-type)
  2504. (service console-font-service-type
  2505. (map (lambda (tty)
  2506. (cons tty %default-console-font))
  2507. '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
  2508. (syslog-service)
  2509. (service agetty-service-type (agetty-configuration
  2510. (extra-options '("-L")) ; no carrier detect
  2511. (term "vt100")
  2512. (tty #f) ; automatic
  2513. (shepherd-requirement '(syslogd))))
  2514. (service mingetty-service-type (mingetty-configuration
  2515. (tty "tty1")))
  2516. (service mingetty-service-type (mingetty-configuration
  2517. (tty "tty2")))
  2518. (service mingetty-service-type (mingetty-configuration
  2519. (tty "tty3")))
  2520. (service mingetty-service-type (mingetty-configuration
  2521. (tty "tty4")))
  2522. (service mingetty-service-type (mingetty-configuration
  2523. (tty "tty5")))
  2524. (service mingetty-service-type (mingetty-configuration
  2525. (tty "tty6")))
  2526. (service static-networking-service-type
  2527. (list %loopback-static-networking))
  2528. (service urandom-seed-service-type)
  2529. (service guix-service-type)
  2530. (service nscd-service-type)
  2531. (service rottlog-service-type)
  2532. ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
  2533. ;; used, so enable them by default. The FUSE and ALSA rules are
  2534. ;; less critical, but handy.
  2535. (service udev-service-type
  2536. (udev-configuration
  2537. (rules (list lvm2 fuse alsa-utils crda))))
  2538. (service sysctl-service-type)
  2539. (service special-files-service-type
  2540. `(("/bin/sh" ,(file-append bash "/bin/sh"))
  2541. ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
  2542. ;;; base.scm ends here