base.scm 109 KB

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