base.scm 108 KB

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