base.scm 108 KB

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