base.scm 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  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 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"
  1243. ,(computed-file
  1244. "security"
  1245. #~(begin
  1246. (mkdir #$output)
  1247. (stat #$limits-file)
  1248. (symlink #$limits-file
  1249. (string-append #$output "/limits.conf"))))))))
  1250. (pam-extension
  1251. (lambda (pam)
  1252. (let ((pam-limits (pam-entry
  1253. (control "required")
  1254. (module "pam_limits.so")
  1255. (arguments '("conf=/etc/security/limits.conf")))))
  1256. (if (member (pam-service-name pam)
  1257. '("login" "su" "slim" "gdm-password" "sddm"))
  1258. (pam-service
  1259. (inherit pam)
  1260. (session (cons pam-limits
  1261. (pam-service-session pam))))
  1262. pam)))))
  1263. (service-type
  1264. (name 'limits)
  1265. (extensions
  1266. (list (service-extension etc-service-type security-limits)
  1267. (service-extension pam-root-service-type
  1268. (lambda _ (list pam-extension)))))
  1269. (description
  1270. "Install the specified resource usage limits by populating
  1271. @file{/etc/security/limits.conf} and using the @code{pam_limits}
  1272. authentication module."))))
  1273. (define* (pam-limits-service #:optional (limits '()))
  1274. "Return a service that makes selected programs respect the list of
  1275. pam-limits-entry specified in LIMITS via pam_limits.so."
  1276. (service pam-limits-service-type
  1277. (plain-file "limits.conf"
  1278. (string-join (map pam-limits-entry->string limits)
  1279. "\n"))))
  1280. ;;;
  1281. ;;; Guix services.
  1282. ;;;
  1283. (define* (guix-build-accounts count #:key
  1284. (group "guixbuild")
  1285. (shadow shadow))
  1286. "Return a list of COUNT user accounts for Guix build users with the given
  1287. GID."
  1288. (unfold (cut > <> count)
  1289. (lambda (n)
  1290. (user-account
  1291. (name (format #f "guixbuilder~2,'0d" n))
  1292. (system? #t)
  1293. (group group)
  1294. ;; guix-daemon expects GROUP to be listed as a
  1295. ;; supplementary group too:
  1296. ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
  1297. (supplementary-groups (list group "kvm"))
  1298. (comment (format #f "Guix Build User ~2d" n))
  1299. (home-directory "/var/empty")
  1300. (shell (file-append shadow "/sbin/nologin"))))
  1301. 1+
  1302. 1))
  1303. (define not-config?
  1304. ;; Select (guix …) and (gnu …) modules, except (guix config).
  1305. (match-lambda
  1306. (('guix 'config) #f)
  1307. (('guix rest ...) #t)
  1308. (('gnu rest ...) #t)
  1309. (rest #f)))
  1310. (define (substitute-key-authorization keys guix)
  1311. "Return a gexp with code to register KEYS, a list of files containing 'guix
  1312. archive' public keys, with GUIX."
  1313. (define default-acl
  1314. (with-extensions (list guile-gcrypt)
  1315. (with-imported-modules `(((guix config) => ,(make-config.scm))
  1316. ,@(source-module-closure '((guix pki))
  1317. #:select? not-config?))
  1318. (computed-file "acl"
  1319. #~(begin
  1320. (use-modules (guix pki)
  1321. (gcrypt pk-crypto)
  1322. (ice-9 rdelim))
  1323. (define keys
  1324. (map (lambda (file)
  1325. (call-with-input-file file
  1326. (compose string->canonical-sexp
  1327. read-string)))
  1328. '(#$@keys)))
  1329. (call-with-output-file #$output
  1330. (lambda (port)
  1331. (write-acl (public-keys->acl keys)
  1332. port))))))))
  1333. (with-imported-modules '((guix build utils))
  1334. #~(begin
  1335. (use-modules (guix build utils))
  1336. ;; If the ACL already exists, move it out of the way. Create a backup
  1337. ;; if it's a regular file: it's likely that the user manually updated
  1338. ;; it with 'guix archive --authorize'.
  1339. (if (file-exists? "/etc/guix/acl")
  1340. (if (and (symbolic-link? "/etc/guix/acl")
  1341. (store-file-name? (readlink "/etc/guix/acl")))
  1342. (delete-file "/etc/guix/acl")
  1343. (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
  1344. (mkdir-p "/etc/guix"))
  1345. ;; Installed the declared ACL.
  1346. (symlink #+default-acl "/etc/guix/acl"))))
  1347. (define %default-authorized-guix-keys
  1348. ;; List of authorized substitute keys.
  1349. (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")
  1350. (file-append guix "/share/guix/bordeaux.guix.gnu.org.pub")))
  1351. (define-record-type* <guix-configuration>
  1352. guix-configuration make-guix-configuration
  1353. guix-configuration?
  1354. (guix guix-configuration-guix ;<package>
  1355. (default guix))
  1356. (build-group guix-configuration-build-group ;string
  1357. (default "guixbuild"))
  1358. (build-accounts guix-configuration-build-accounts ;integer
  1359. (default 10))
  1360. (authorize-key? guix-configuration-authorize-key? ;Boolean
  1361. (default #t))
  1362. (authorized-keys guix-configuration-authorized-keys ;list of gexps
  1363. (default %default-authorized-guix-keys))
  1364. (use-substitutes? guix-configuration-use-substitutes? ;Boolean
  1365. (default #t))
  1366. (substitute-urls guix-configuration-substitute-urls ;list of strings
  1367. (default %default-substitute-urls))
  1368. (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
  1369. (default '()))
  1370. (max-silent-time guix-configuration-max-silent-time ;integer
  1371. (default 0))
  1372. (timeout guix-configuration-timeout ;integer
  1373. (default 0))
  1374. (log-compression guix-configuration-log-compression
  1375. (default 'bzip2))
  1376. (discover? guix-configuration-discover?
  1377. (default #f))
  1378. (extra-options guix-configuration-extra-options ;list of strings
  1379. (default '()))
  1380. (log-file guix-configuration-log-file ;string
  1381. (default "/var/log/guix-daemon.log"))
  1382. (http-proxy guix-http-proxy ;string | #f
  1383. (default #f))
  1384. (tmpdir guix-tmpdir ;string | #f
  1385. (default #f)))
  1386. (define %default-guix-configuration
  1387. (guix-configuration))
  1388. (define shepherd-set-http-proxy-action
  1389. ;; Shepherd action to change the HTTP(S) proxy.
  1390. (shepherd-action
  1391. (name 'set-http-proxy)
  1392. (documentation
  1393. "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
  1394. (procedure #~(lambda* (_ #:optional proxy)
  1395. (let ((environment (environ)))
  1396. ;; A bit of a hack: communicate PROXY to the 'start'
  1397. ;; method via environment variables.
  1398. (if proxy
  1399. (begin
  1400. (format #t "changing HTTP/HTTPS \
  1401. proxy of 'guix-daemon' to ~s...~%"
  1402. proxy)
  1403. (setenv "http_proxy" proxy))
  1404. (begin
  1405. (format #t "clearing HTTP/HTTPS \
  1406. proxy of 'guix-daemon'...~%")
  1407. (unsetenv "http_proxy")))
  1408. (action 'guix-daemon 'restart)
  1409. (environ environment)
  1410. #t)))))
  1411. (define shepherd-discover-action
  1412. ;; Shepherd action to enable or disable substitute servers discovery.
  1413. (shepherd-action
  1414. (name 'discover)
  1415. (documentation
  1416. "Enable or disable substitute servers discovery and restart the
  1417. 'guix-daemon'.")
  1418. (procedure #~(lambda* (_ status)
  1419. (let ((environment (environ)))
  1420. (if (and status
  1421. (string=? status "on"))
  1422. (begin
  1423. (format #t "enable substitute servers discovery~%")
  1424. (setenv "discover" "on"))
  1425. (begin
  1426. (format #t "disable substitute servers discovery~%")
  1427. (unsetenv "discover")))
  1428. (action 'guix-daemon 'restart)
  1429. (environ environment)
  1430. #t)))))
  1431. (define (guix-shepherd-service config)
  1432. "Return a <shepherd-service> for the Guix daemon service with CONFIG."
  1433. (match-record config <guix-configuration>
  1434. (guix build-group build-accounts authorize-key? authorized-keys
  1435. use-substitutes? substitute-urls max-silent-time timeout
  1436. log-compression discover? extra-options log-file
  1437. http-proxy tmpdir chroot-directories)
  1438. (list (shepherd-service
  1439. (documentation "Run the Guix daemon.")
  1440. (provision '(guix-daemon))
  1441. (requirement '(user-processes))
  1442. (actions (list shepherd-set-http-proxy-action
  1443. shepherd-discover-action))
  1444. (modules '((srfi srfi-1)
  1445. (ice-9 match)
  1446. (gnu build shepherd)))
  1447. (start
  1448. (with-imported-modules `(((guix config) => ,(make-config.scm))
  1449. ,@(source-module-closure
  1450. '((gnu build shepherd))
  1451. #:select? not-config?))
  1452. #~(lambda args
  1453. (define proxy
  1454. ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
  1455. ;; the 'set-http-proxy' action.
  1456. (or (getenv "http_proxy") #$http-proxy))
  1457. (define discover?
  1458. (or (getenv "discover") #$discover?))
  1459. ;; Start the guix-daemon from a container, when supported,
  1460. ;; to solve an installation issue. See the comment below for
  1461. ;; more details.
  1462. (fork+exec-command/container
  1463. (cons* #$(file-append guix "/bin/guix-daemon")
  1464. "--build-users-group" #$build-group
  1465. "--max-silent-time"
  1466. #$(number->string max-silent-time)
  1467. "--timeout" #$(number->string timeout)
  1468. "--log-compression"
  1469. #$(symbol->string log-compression)
  1470. #$@(if use-substitutes?
  1471. '()
  1472. '("--no-substitutes"))
  1473. (string-append "--discover="
  1474. (if discover? "yes" "no"))
  1475. "--substitute-urls" #$(string-join substitute-urls)
  1476. #$@extra-options
  1477. ;; Add CHROOT-DIRECTORIES and all their dependencies
  1478. ;; (if these are store items) to the chroot.
  1479. (append-map
  1480. (lambda (file)
  1481. (append-map (lambda (directory)
  1482. (list "--chroot-directory"
  1483. directory))
  1484. (call-with-input-file file
  1485. read)))
  1486. '#$(map references-file
  1487. chroot-directories)))
  1488. ;; When running the installer, we need guix-daemon to
  1489. ;; operate from within the same MNT namespace as the
  1490. ;; installation container. In that case only, enter the
  1491. ;; namespace of the process PID passed as start argument.
  1492. ;; Otherwise, for symmetry purposes enter the caller
  1493. ;; namespaces which is a no-op.
  1494. #:pid (match args
  1495. ((pid) (string->number pid))
  1496. (else (getpid)))
  1497. #:environment-variables
  1498. (append (list #$@(if tmpdir
  1499. (list (string-append "TMPDIR=" tmpdir))
  1500. '())
  1501. ;; Make sure we run in a UTF-8 locale so that
  1502. ;; 'guix offload' correctly restores nars
  1503. ;; that contain UTF-8 file names such as
  1504. ;; 'nss-certs'. See
  1505. ;; <https://bugs.gnu.org/32942>.
  1506. (string-append "GUIX_LOCPATH="
  1507. #$glibc-utf8-locales
  1508. "/lib/locale")
  1509. "LC_ALL=en_US.utf8")
  1510. (if proxy
  1511. (list (string-append "http_proxy=" proxy)
  1512. (string-append "https_proxy=" proxy))
  1513. '()))
  1514. #:log-file #$log-file))))
  1515. (stop #~(make-kill-destructor))))))
  1516. (define (guix-accounts config)
  1517. "Return the user accounts and user groups for CONFIG."
  1518. (match config
  1519. (($ <guix-configuration> _ build-group build-accounts)
  1520. (cons (user-group
  1521. (name build-group)
  1522. (system? #t)
  1523. ;; Use a fixed GID so that we can create the store with the right
  1524. ;; owner.
  1525. (id 30000))
  1526. (guix-build-accounts build-accounts
  1527. #:group build-group)))))
  1528. (define (guix-activation config)
  1529. "Return the activation gexp for CONFIG."
  1530. (match config
  1531. (($ <guix-configuration> guix build-group build-accounts authorize-key? keys)
  1532. ;; Assume that the store has BUILD-GROUP as its group. We could
  1533. ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
  1534. ;; chown leads to an entire copy of the tree, which is a bad idea.
  1535. ;; Generate a key pair and optionally authorize substitute server keys.
  1536. #~(begin
  1537. (unless (file-exists? "/etc/guix/signing-key.pub")
  1538. (system* #$(file-append guix "/bin/guix") "archive"
  1539. "--generate-key"))
  1540. #$(if authorize-key?
  1541. (substitute-key-authorization keys guix)
  1542. #~#f)))))
  1543. (define* (references-file item #:optional (name "references"))
  1544. "Return a file that contains the list of references of ITEM."
  1545. (if (struct? item) ;lowerable object
  1546. (computed-file name
  1547. (with-extensions (list guile-gcrypt) ;for store-copy
  1548. (with-imported-modules (source-module-closure
  1549. '((guix build store-copy)))
  1550. #~(begin
  1551. (use-modules (guix build store-copy))
  1552. (call-with-output-file #$output
  1553. (lambda (port)
  1554. (write (map store-info-item
  1555. (call-with-input-file "graph"
  1556. read-reference-graph))
  1557. port))))))
  1558. #:options `(#:local-build? #f
  1559. #:references-graphs (("graph" ,item))))
  1560. (plain-file name "()")))
  1561. (define guix-service-type
  1562. (service-type
  1563. (name 'guix)
  1564. (extensions
  1565. (list (service-extension shepherd-root-service-type guix-shepherd-service)
  1566. (service-extension account-service-type guix-accounts)
  1567. (service-extension activation-service-type guix-activation)
  1568. (service-extension profile-service-type
  1569. (compose list guix-configuration-guix))))
  1570. ;; Extensions can specify extra directories to add to the build chroot.
  1571. (compose concatenate)
  1572. (extend (lambda (config directories)
  1573. (guix-configuration
  1574. (inherit config)
  1575. (chroot-directories
  1576. (append (guix-configuration-chroot-directories config)
  1577. directories)))))
  1578. (default-value (guix-configuration))
  1579. (description
  1580. "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
  1581. (define-record-type* <guix-publish-configuration>
  1582. guix-publish-configuration make-guix-publish-configuration
  1583. guix-publish-configuration?
  1584. (guix guix-publish-configuration-guix ;package
  1585. (default guix))
  1586. (port guix-publish-configuration-port ;number
  1587. (default 80))
  1588. (host guix-publish-configuration-host ;string
  1589. (default "localhost"))
  1590. (advertise? guix-publish-advertise? ;boolean
  1591. (default #f))
  1592. (compression guix-publish-configuration-compression
  1593. (thunked)
  1594. (default (default-compression this-record
  1595. (current-source-location))))
  1596. (compression-level %guix-publish-configuration-compression-level ;deprecated
  1597. (default #f))
  1598. (nar-path guix-publish-configuration-nar-path ;string
  1599. (default "nar"))
  1600. (cache guix-publish-configuration-cache ;#f | string
  1601. (default #f))
  1602. (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
  1603. (default (* 10 (expt 2 20)))) ;integer
  1604. (workers guix-publish-configuration-workers ;#f | integer
  1605. (default #f))
  1606. (ttl guix-publish-configuration-ttl ;#f | integer
  1607. (default #f)))
  1608. (define-deprecated (guix-publish-configuration-compression-level config)
  1609. "Return a compression level, the old way."
  1610. (match (guix-publish-configuration-compression config)
  1611. (((_ level) _ ...) level)))
  1612. (define (default-compression config properties)
  1613. "Return the default 'guix publish' compression according to CONFIG, and
  1614. raise a deprecation warning if the 'compression-level' field was used."
  1615. (match (%guix-publish-configuration-compression-level config)
  1616. (#f
  1617. ;; Default to low compression levels when there's no cache so that users
  1618. ;; get good bandwidth by default.
  1619. (if (guix-publish-configuration-cache config)
  1620. '(("gzip" 5) ("zstd" 19))
  1621. '(("gzip" 3) ("zstd" 3)))) ;zstd compresses faster
  1622. (level
  1623. (warn-about-deprecation 'compression-level properties
  1624. #:replacement 'compression)
  1625. `(("gzip" ,level)))))
  1626. (define (guix-publish-shepherd-service config)
  1627. (define (config->compression-options config)
  1628. (match (guix-publish-configuration-compression config)
  1629. (() ;empty list means "no compression"
  1630. '("-C0"))
  1631. (lst
  1632. (append-map (match-lambda
  1633. ((type level)
  1634. `("-C" ,(string-append type ":"
  1635. (number->string level)))))
  1636. lst))))
  1637. (match-record config <guix-publish-configuration>
  1638. (guix port host nar-path cache workers ttl cache-bypass-threshold
  1639. advertise?)
  1640. (list (shepherd-service
  1641. (provision '(guix-publish))
  1642. (requirement `(user-processes
  1643. guix-daemon
  1644. ,@(if advertise? '(avahi-daemon) '())))
  1645. (start #~(make-forkexec-constructor
  1646. (list #$(file-append guix "/bin/guix")
  1647. "publish" "-u" "guix-publish"
  1648. "-p" #$(number->string port)
  1649. #$@(config->compression-options config)
  1650. (string-append "--nar-path=" #$nar-path)
  1651. (string-append "--listen=" #$host)
  1652. #$@(if advertise?
  1653. #~("--advertise")
  1654. #~())
  1655. #$@(if workers
  1656. #~((string-append "--workers="
  1657. #$(number->string
  1658. workers)))
  1659. #~())
  1660. #$@(if ttl
  1661. #~((string-append "--ttl="
  1662. #$(number->string ttl)
  1663. "s"))
  1664. #~())
  1665. #$@(if cache
  1666. #~((string-append "--cache=" #$cache)
  1667. #$(string-append
  1668. "--cache-bypass-threshold="
  1669. (number->string
  1670. cache-bypass-threshold)))
  1671. #~()))
  1672. ;; Make sure we run in a UTF-8 locale so we can produce
  1673. ;; nars for packages that contain UTF-8 file names such
  1674. ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
  1675. #:environment-variables
  1676. (list (string-append "GUIX_LOCPATH="
  1677. #$glibc-utf8-locales "/lib/locale")
  1678. "LC_ALL=en_US.utf8")
  1679. #:log-file "/var/log/guix-publish.log"))
  1680. (stop #~(make-kill-destructor))))))
  1681. (define %guix-publish-accounts
  1682. (list (user-group (name "guix-publish") (system? #t))
  1683. (user-account
  1684. (name "guix-publish")
  1685. (group "guix-publish")
  1686. (system? #t)
  1687. (comment "guix publish user")
  1688. (home-directory "/var/empty")
  1689. (shell (file-append shadow "/sbin/nologin")))))
  1690. (define %guix-publish-log-rotations
  1691. (list (log-rotation
  1692. (files (list "/var/log/guix-publish.log")))))
  1693. (define (guix-publish-activation config)
  1694. (let ((cache (guix-publish-configuration-cache config)))
  1695. (if cache
  1696. (with-imported-modules '((guix build utils))
  1697. #~(begin
  1698. (use-modules (guix build utils))
  1699. (mkdir-p #$cache)
  1700. (let* ((pw (getpw "guix-publish"))
  1701. (uid (passwd:uid pw))
  1702. (gid (passwd:gid pw)))
  1703. (chown #$cache uid gid))))
  1704. #t)))
  1705. (define guix-publish-service-type
  1706. (service-type (name 'guix-publish)
  1707. (extensions
  1708. (list (service-extension shepherd-root-service-type
  1709. guix-publish-shepherd-service)
  1710. (service-extension account-service-type
  1711. (const %guix-publish-accounts))
  1712. (service-extension rottlog-service-type
  1713. (const %guix-publish-log-rotations))
  1714. (service-extension activation-service-type
  1715. guix-publish-activation)))
  1716. (default-value (guix-publish-configuration))
  1717. (description
  1718. "Add a Shepherd service running @command{guix publish}, a
  1719. command that allows you to share pre-built binaries with others over HTTP.")))
  1720. ;;;
  1721. ;;; Udev.
  1722. ;;;
  1723. (define-record-type* <udev-configuration>
  1724. udev-configuration make-udev-configuration
  1725. udev-configuration?
  1726. (udev udev-configuration-udev ;<package>
  1727. (default eudev))
  1728. (rules udev-configuration-rules ;list of <package>
  1729. (default '())))
  1730. (define (udev-rules-union packages)
  1731. "Return the union of the @code{lib/udev/rules.d} directories found in each
  1732. item of @var{packages}."
  1733. (define build
  1734. (with-imported-modules '((guix build union)
  1735. (guix build utils))
  1736. #~(begin
  1737. (use-modules (guix build union)
  1738. (guix build utils)
  1739. (srfi srfi-1)
  1740. (srfi srfi-26))
  1741. (define %standard-locations
  1742. '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
  1743. (define (rules-sub-directory directory)
  1744. ;; Return the sub-directory of DIRECTORY containing udev rules, or
  1745. ;; #f if none was found.
  1746. (find directory-exists?
  1747. (map (cut string-append directory <>) %standard-locations)))
  1748. (mkdir-p (string-append #$output "/lib/udev"))
  1749. (union-build (string-append #$output "/lib/udev/rules.d")
  1750. (filter-map rules-sub-directory '#$packages)))))
  1751. (computed-file "udev-rules" build))
  1752. (define (udev-rule file-name contents)
  1753. "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
  1754. (computed-file file-name
  1755. (with-imported-modules '((guix build utils))
  1756. #~(begin
  1757. (use-modules (guix build utils))
  1758. (define rules.d
  1759. (string-append #$output "/lib/udev/rules.d"))
  1760. (mkdir-p rules.d)
  1761. (call-with-output-file
  1762. (string-append rules.d "/" #$file-name)
  1763. (lambda (port)
  1764. (display #$contents port)))))))
  1765. (define (file->udev-rule file-name file)
  1766. "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
  1767. (computed-file file-name
  1768. (with-imported-modules '((guix build utils))
  1769. #~(begin
  1770. (use-modules (guix build utils))
  1771. (define rules.d
  1772. (string-append #$output "/lib/udev/rules.d"))
  1773. (define file-copy-dest
  1774. (string-append rules.d "/" #$file-name))
  1775. (mkdir-p rules.d)
  1776. (copy-file #$file file-copy-dest)))))
  1777. (define kvm-udev-rule
  1778. ;; Return a directory with a udev rule that changes the group of /dev/kvm to
  1779. ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
  1780. ;; but now we have to add it by ourselves.
  1781. ;; Build users are part of the "kvm" group, so we can fearlessly make
  1782. ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
  1783. (udev-rule "90-kvm.rules"
  1784. "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
  1785. (define udev-shepherd-service
  1786. ;; Return a <shepherd-service> for UDEV with RULES.
  1787. (match-lambda
  1788. (($ <udev-configuration> udev rules)
  1789. (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
  1790. (udev.conf (computed-file "udev.conf"
  1791. #~(call-with-output-file #$output
  1792. (lambda (port)
  1793. (format port
  1794. "udev_rules=\"~a/lib/udev/rules.d\"\n"
  1795. #$rules))))))
  1796. (list
  1797. (shepherd-service
  1798. (provision '(udev))
  1799. ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
  1800. ;; be added: see
  1801. ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
  1802. (requirement '(root-file-system))
  1803. (documentation "Populate the /dev directory, dynamically.")
  1804. (start
  1805. (with-imported-modules (source-module-closure
  1806. '((gnu build linux-boot)))
  1807. #~(lambda ()
  1808. (define udevd
  1809. ;; 'udevd' from eudev.
  1810. #$(file-append udev "/sbin/udevd"))
  1811. (define (wait-for-udevd)
  1812. ;; Wait until someone's listening on udevd's control
  1813. ;; socket.
  1814. (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
  1815. (let try ()
  1816. (catch 'system-error
  1817. (lambda ()
  1818. (connect sock PF_UNIX "/run/udev/control")
  1819. (close-port sock))
  1820. (lambda args
  1821. (format #t "waiting for udevd...~%")
  1822. (usleep 500000)
  1823. (try))))))
  1824. ;; Allow udev to find the modules.
  1825. (setenv "LINUX_MODULE_DIRECTORY"
  1826. "/run/booted-system/kernel/lib/modules")
  1827. (let* ((kernel-release
  1828. (utsname:release (uname)))
  1829. (linux-module-directory
  1830. (getenv "LINUX_MODULE_DIRECTORY"))
  1831. (directory
  1832. (string-append linux-module-directory "/"
  1833. kernel-release))
  1834. (old-umask (umask #o022)))
  1835. ;; If we're in a container, DIRECTORY might not exist,
  1836. ;; for instance because the host runs a different
  1837. ;; kernel. In that case, skip it; we'll just miss a few
  1838. ;; nodes like /dev/fuse.
  1839. (when (file-exists? directory)
  1840. (make-static-device-nodes directory))
  1841. (umask old-umask))
  1842. (let ((pid (fork+exec-command (list udevd)
  1843. #:environment-variables
  1844. (cons*
  1845. ;; The first one is for udev, the second one for
  1846. ;; eudev.
  1847. (string-append "UDEV_CONFIG_FILE=" #$udev.conf)
  1848. (string-append "EUDEV_RULES_DIRECTORY="
  1849. #$(file-append
  1850. rules "/lib/udev/rules.d"))
  1851. (string-append "LINUX_MODULE_DIRECTORY="
  1852. (getenv "LINUX_MODULE_DIRECTORY"))
  1853. (default-environment-variables)))))
  1854. ;; Wait until udevd is up and running. This appears to
  1855. ;; be needed so that the events triggered below are
  1856. ;; actually handled.
  1857. (wait-for-udevd)
  1858. ;; Trigger device node creation.
  1859. (system* #$(file-append udev "/bin/udevadm")
  1860. "trigger" "--action=add")
  1861. ;; Wait for things to settle down.
  1862. (system* #$(file-append udev "/bin/udevadm")
  1863. "settle")
  1864. pid))))
  1865. (stop #~(make-kill-destructor))
  1866. ;; When halting the system, 'udev' is actually killed by
  1867. ;; 'user-processes', i.e., before its own 'stop' method was called.
  1868. ;; Thus, make sure it is not respawned.
  1869. (respawn? #f)
  1870. ;; We need additional modules.
  1871. (modules `((gnu build linux-boot) ;'make-static-device-nodes'
  1872. ,@%default-modules))
  1873. (actions (list (shepherd-action
  1874. (name 'rules)
  1875. (documentation "Display the directory containing
  1876. the udev rules in use.")
  1877. (procedure #~(lambda (_)
  1878. (display #$rules)
  1879. (newline))))))))))))
  1880. (define udev-service-type
  1881. (service-type (name 'udev)
  1882. (extensions
  1883. (list (service-extension shepherd-root-service-type
  1884. udev-shepherd-service)))
  1885. (compose concatenate) ;concatenate the list of rules
  1886. (extend (lambda (config rules)
  1887. (match config
  1888. (($ <udev-configuration> udev initial-rules)
  1889. (udev-configuration
  1890. (udev udev)
  1891. (rules (append initial-rules rules)))))))
  1892. (default-value (udev-configuration))
  1893. (description
  1894. "Run @command{udev}, which populates the @file{/dev}
  1895. directory dynamically. Get extra rules from the packages listed in the
  1896. @code{rules} field of its value, @code{udev-configuration} object.")))
  1897. (define* (udev-service #:key (udev eudev) (rules '()))
  1898. "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
  1899. extra rules from the packages listed in @var{rules}."
  1900. (service udev-service-type
  1901. (udev-configuration (udev udev) (rules rules))))
  1902. (define* (udev-rules-service name rules #:key (groups '()))
  1903. "Return a service that extends udev-service-type with RULES and
  1904. account-service-type with GROUPS as system groups. This works by creating a
  1905. singleton service type NAME-udev-rules, of which the returned service is an
  1906. instance."
  1907. (let* ((name (symbol-append name '-udev-rules))
  1908. (account-extension
  1909. (const (map (lambda (group)
  1910. (user-group (name group) (system? #t)))
  1911. groups)))
  1912. (udev-extension (const (list rules)))
  1913. (type (service-type
  1914. (name name)
  1915. (extensions (list
  1916. (service-extension
  1917. account-service-type account-extension)
  1918. (service-extension
  1919. udev-service-type udev-extension))))))
  1920. (service type #f)))
  1921. (define swap-service-type
  1922. (shepherd-service-type
  1923. 'swap
  1924. (lambda (device)
  1925. (define requirement
  1926. (if (and (string? device)
  1927. (string-prefix? "/dev/mapper/" device))
  1928. (list (symbol-append 'device-mapping-
  1929. (string->symbol (basename device))))
  1930. '()))
  1931. (define (device-lookup device)
  1932. ;; The generic 'find-partition' procedures could return a partition
  1933. ;; that's not swap space, but that's unlikely.
  1934. (cond ((uuid? device)
  1935. #~(find-partition-by-uuid #$(uuid-bytevector device)))
  1936. ((file-system-label? device)
  1937. #~(find-partition-by-label
  1938. #$(file-system-label->string device)))
  1939. (else
  1940. device)))
  1941. (define service-name
  1942. (symbol-append 'swap-
  1943. (string->symbol
  1944. (cond ((uuid? device)
  1945. (string-take (uuid->string device) 6))
  1946. ((file-system-label? device)
  1947. (file-system-label->string device))
  1948. (else
  1949. device)))))
  1950. (with-imported-modules (source-module-closure '((gnu build file-systems)))
  1951. (shepherd-service
  1952. (provision (list service-name))
  1953. (requirement `(udev ,@requirement))
  1954. (documentation "Enable the given swap device.")
  1955. (modules `((gnu build file-systems)
  1956. ,@%default-modules))
  1957. (start #~(lambda ()
  1958. (let ((device #$(device-lookup device)))
  1959. (and device
  1960. (begin
  1961. (restart-on-EINTR (swapon device))
  1962. #t)))))
  1963. (stop #~(lambda _
  1964. (let ((device #$(device-lookup device)))
  1965. (when device
  1966. (restart-on-EINTR (swapoff device)))
  1967. #f)))
  1968. (respawn? #f))))
  1969. (description "Turn on the virtual memory swap area.")))
  1970. (define (swap-service device)
  1971. "Return a service that uses @var{device} as a swap device."
  1972. (service swap-service-type device))
  1973. (define %default-gpm-options
  1974. ;; Default options for GPM.
  1975. '("-m" "/dev/input/mice" "-t" "ps2"))
  1976. (define-record-type* <gpm-configuration>
  1977. gpm-configuration make-gpm-configuration gpm-configuration?
  1978. (gpm gpm-configuration-gpm ;package
  1979. (default gpm))
  1980. (options gpm-configuration-options ;list of strings
  1981. (default %default-gpm-options)))
  1982. (define gpm-shepherd-service
  1983. (match-lambda
  1984. (($ <gpm-configuration> gpm options)
  1985. (list (shepherd-service
  1986. (requirement '(udev))
  1987. (provision '(gpm))
  1988. ;; 'gpm' runs in the background and sets a PID file.
  1989. ;; Note that it requires running as "root".
  1990. (start #~(make-forkexec-constructor
  1991. (list #$(file-append gpm "/sbin/gpm")
  1992. #$@options)
  1993. #:pid-file "/var/run/gpm.pid"
  1994. #:pid-file-timeout 3))
  1995. (stop #~(lambda (_)
  1996. ;; Return #f if successfully stopped.
  1997. (not (zero? (system* #$(file-append gpm "/sbin/gpm")
  1998. "-k"))))))))))
  1999. (define gpm-service-type
  2000. (service-type (name 'gpm)
  2001. (extensions
  2002. (list (service-extension shepherd-root-service-type
  2003. gpm-shepherd-service)))
  2004. (default-value (gpm-configuration))
  2005. (description
  2006. "Run GPM, the general-purpose mouse daemon, with the given
  2007. command-line options. GPM allows users to use the mouse in the console,
  2008. notably to select, copy, and paste text. The default options use the
  2009. @code{ps2} protocol, which works for both USB and PS/2 mice.")))
  2010. (define-record-type* <kmscon-configuration>
  2011. kmscon-configuration make-kmscon-configuration
  2012. kmscon-configuration?
  2013. (kmscon kmscon-configuration-kmscon
  2014. (default kmscon))
  2015. (virtual-terminal kmscon-configuration-virtual-terminal)
  2016. (login-program kmscon-configuration-login-program
  2017. (default (file-append shadow "/bin/login")))
  2018. (login-arguments kmscon-configuration-login-arguments
  2019. (default '("-p")))
  2020. (auto-login kmscon-configuration-auto-login
  2021. (default #f))
  2022. (hardware-acceleration? kmscon-configuration-hardware-acceleration?
  2023. (default #f)) ; #t causes failure
  2024. (font-engine kmscon-configuration-font-engine
  2025. (default "pango"))
  2026. (font-size kmscon-configuration-font-size
  2027. (default 12))
  2028. (keyboard-layout kmscon-configuration-keyboard-layout
  2029. (default #f))) ; #f | <keyboard-layout>
  2030. (define kmscon-service-type
  2031. (shepherd-service-type
  2032. 'kmscon
  2033. (lambda (config)
  2034. (let ((kmscon (kmscon-configuration-kmscon config))
  2035. (virtual-terminal (kmscon-configuration-virtual-terminal config))
  2036. (login-program (kmscon-configuration-login-program config))
  2037. (login-arguments (kmscon-configuration-login-arguments config))
  2038. (auto-login (kmscon-configuration-auto-login config))
  2039. (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config))
  2040. (font-engine (kmscon-configuration-font-engine config))
  2041. (font-size (kmscon-configuration-font-size config))
  2042. (keyboard-layout (kmscon-configuration-keyboard-layout config)))
  2043. (define kmscon-command
  2044. #~(list
  2045. #$(file-append kmscon "/bin/kmscon") "--login"
  2046. "--vt" #$virtual-terminal
  2047. "--no-switchvt" ;Prevent a switch to the virtual terminal.
  2048. "--font-engine" #$font-engine
  2049. "--font-size" #$(number->string font-size)
  2050. #$@(if keyboard-layout
  2051. (let* ((layout (keyboard-layout-name keyboard-layout))
  2052. (variant (keyboard-layout-variant keyboard-layout))
  2053. (model (keyboard-layout-model keyboard-layout))
  2054. (options (keyboard-layout-options keyboard-layout)))
  2055. `("--xkb-layout" ,layout
  2056. ,@(if variant `("--xkb-variant" ,variant) '())
  2057. ,@(if model `("--xkb-model" ,model) '())
  2058. ,@(if (null? options)
  2059. '()
  2060. `("--xkb-options" ,(string-join options ",")))))
  2061. '())
  2062. #$@(if hardware-acceleration? '("--hwaccel") '())
  2063. "--login" "--"
  2064. #$login-program #$@login-arguments
  2065. #$@(if auto-login
  2066. #~(#$auto-login)
  2067. #~())))
  2068. (shepherd-service
  2069. (documentation "kmscon virtual terminal")
  2070. (requirement '(user-processes udev dbus-system))
  2071. (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
  2072. (start #~(make-forkexec-constructor #$kmscon-command))
  2073. (stop #~(make-kill-destructor)))))
  2074. (description "Start the @command{kmscon} virtual terminal emulator for the
  2075. Linux @dfn{kernel mode setting} (KMS).")))
  2076. (define-record-type* <static-networking>
  2077. static-networking make-static-networking
  2078. static-networking?
  2079. (interface static-networking-interface)
  2080. (ip static-networking-ip)
  2081. (netmask static-networking-netmask
  2082. (default #f))
  2083. (gateway static-networking-gateway ;FIXME: doesn't belong here
  2084. (default #f))
  2085. (provision static-networking-provision
  2086. (default #f))
  2087. (requirement static-networking-requirement
  2088. (default '()))
  2089. (name-servers static-networking-name-servers ;FIXME: doesn't belong here
  2090. (default '())))
  2091. (define static-networking-shepherd-service
  2092. (match-lambda
  2093. (($ <static-networking> interface ip netmask gateway provision
  2094. requirement name-servers)
  2095. (let ((loopback? (and provision (memq 'loopback provision))))
  2096. (shepherd-service
  2097. (documentation
  2098. "Bring up the networking interface using a static IP address.")
  2099. (requirement requirement)
  2100. (provision (or provision
  2101. (list (symbol-append 'networking-
  2102. (string->symbol interface)))))
  2103. (start #~(lambda _
  2104. ;; Return #t if successfully started.
  2105. (let* ((addr (inet-pton AF_INET #$ip))
  2106. (sockaddr (make-socket-address AF_INET addr 0))
  2107. (mask (and #$netmask
  2108. (inet-pton AF_INET #$netmask)))
  2109. (maskaddr (and mask
  2110. (make-socket-address AF_INET
  2111. mask 0)))
  2112. (gateway (and #$gateway
  2113. (inet-pton AF_INET #$gateway)))
  2114. (gatewayaddr (and gateway
  2115. (make-socket-address AF_INET
  2116. gateway 0))))
  2117. (configure-network-interface #$interface sockaddr
  2118. (logior IFF_UP
  2119. #$(if loopback?
  2120. #~IFF_LOOPBACK
  2121. 0))
  2122. #:netmask maskaddr)
  2123. (when gateway
  2124. (let ((sock (socket AF_INET SOCK_DGRAM 0)))
  2125. (add-network-route/gateway sock gatewayaddr)
  2126. (close-port sock))))))
  2127. (stop #~(lambda _
  2128. ;; Return #f is successfully stopped.
  2129. (let ((sock (socket AF_INET SOCK_STREAM 0)))
  2130. (when #$gateway
  2131. (delete-network-route sock
  2132. (make-socket-address
  2133. AF_INET INADDR_ANY 0)))
  2134. (set-network-interface-flags sock #$interface 0)
  2135. (close-port sock)
  2136. #f)))
  2137. (respawn? #f))))))
  2138. (define (static-networking-etc-files interfaces)
  2139. "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
  2140. (match (delete-duplicates
  2141. (append-map static-networking-name-servers
  2142. interfaces))
  2143. (()
  2144. '())
  2145. ((name-servers ...)
  2146. (let ((content (string-join
  2147. (map (cut string-append "nameserver " <>)
  2148. name-servers)
  2149. "\n" 'suffix)))
  2150. `(("resolv.conf"
  2151. ,(plain-file "resolv.conf"
  2152. (string-append "\
  2153. # Generated by 'static-networking-service'.\n"
  2154. content))))))))
  2155. (define (static-networking-shepherd-services interfaces)
  2156. "Return the list of Shepherd services to bring up INTERFACES, a list of
  2157. <static-networking> objects."
  2158. (define (loopback? service)
  2159. (memq 'loopback (shepherd-service-provision service)))
  2160. (let ((services (map static-networking-shepherd-service interfaces)))
  2161. (match (remove loopback? services)
  2162. (()
  2163. ;; There's no interface other than 'loopback', so we assume that the
  2164. ;; 'networking' service will be provided by dhclient or similar.
  2165. services)
  2166. ((non-loopback ...)
  2167. ;; Assume we're providing all the interfaces, and thus, provide a
  2168. ;; 'networking' service.
  2169. (cons (shepherd-service
  2170. (provision '(networking))
  2171. (requirement (append-map shepherd-service-provision
  2172. services))
  2173. (start #~(const #t))
  2174. (stop #~(const #f))
  2175. (documentation "Bring up all the networking interfaces."))
  2176. services)))))
  2177. (define static-networking-service-type
  2178. ;; The service type for statically-defined network interfaces.
  2179. (service-type (name 'static-networking)
  2180. (extensions
  2181. (list
  2182. (service-extension shepherd-root-service-type
  2183. static-networking-shepherd-services)
  2184. (service-extension etc-service-type
  2185. static-networking-etc-files)))
  2186. (compose concatenate)
  2187. (extend append)
  2188. (description
  2189. "Turn up the specified network interfaces upon startup,
  2190. with the given IP address, gateway, netmask, and so on. The value for
  2191. services of this type is a list of @code{static-networking} objects, one per
  2192. network interface.")))
  2193. (define* (static-networking-service interface ip
  2194. #:key
  2195. netmask gateway provision
  2196. ;; Most interfaces require udev to be usable.
  2197. (requirement '(udev))
  2198. (name-servers '()))
  2199. "Return a service that starts @var{interface} with address @var{ip}. If
  2200. @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
  2201. it must be a string specifying the default network gateway.
  2202. This procedure can be called several times, one for each network
  2203. interface of interest. Behind the scenes what it does is extend
  2204. @code{static-networking-service-type} with additional network interfaces
  2205. to handle."
  2206. (simple-service 'static-network-interface
  2207. static-networking-service-type
  2208. (list (static-networking (interface interface) (ip ip)
  2209. (netmask netmask) (gateway gateway)
  2210. (provision provision)
  2211. (requirement requirement)
  2212. (name-servers name-servers)))))
  2213. (define %base-services
  2214. ;; Convenience variable holding the basic services.
  2215. (list (service login-service-type)
  2216. (service virtual-terminal-service-type)
  2217. (service console-font-service-type
  2218. (map (lambda (tty)
  2219. (cons tty %default-console-font))
  2220. '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
  2221. (service agetty-service-type (agetty-configuration
  2222. (extra-options '("-L")) ; no carrier detect
  2223. (term "vt100")
  2224. (tty #f))) ; automatic
  2225. (service mingetty-service-type (mingetty-configuration
  2226. (tty "tty1")))
  2227. (service mingetty-service-type (mingetty-configuration
  2228. (tty "tty2")))
  2229. (service mingetty-service-type (mingetty-configuration
  2230. (tty "tty3")))
  2231. (service mingetty-service-type (mingetty-configuration
  2232. (tty "tty4")))
  2233. (service mingetty-service-type (mingetty-configuration
  2234. (tty "tty5")))
  2235. (service mingetty-service-type (mingetty-configuration
  2236. (tty "tty6")))
  2237. (service static-networking-service-type
  2238. (list (static-networking (interface "lo")
  2239. (ip "127.0.0.1")
  2240. (requirement '())
  2241. (provision '(loopback)))))
  2242. (syslog-service)
  2243. (service urandom-seed-service-type)
  2244. (service guix-service-type)
  2245. (service nscd-service-type)
  2246. (service rottlog-service-type)
  2247. ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
  2248. ;; used, so enable them by default. The FUSE and ALSA rules are
  2249. ;; less critical, but handy.
  2250. (service udev-service-type
  2251. (udev-configuration
  2252. (rules (list lvm2 fuse alsa-utils crda))))
  2253. (service sysctl-service-type)
  2254. (service special-files-service-type
  2255. `(("/bin/sh" ,(file-append bash "/bin/sh"))
  2256. ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
  2257. ;;; base.scm ends here