base.scm 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
  4. ;;; Copyright © 2015, 2016 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. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (gnu services base)
  25. #:use-module (guix store)
  26. #:use-module (gnu services)
  27. #:use-module (gnu services shepherd)
  28. #:use-module (gnu services networking)
  29. #:use-module (gnu system pam)
  30. #:use-module (gnu system shadow) ; 'user-account', etc.
  31. #:use-module (gnu system uuid)
  32. #:use-module (gnu system file-systems) ; 'file-system', etc.
  33. #:use-module (gnu system mapped-devices)
  34. #:use-module ((gnu system linux-initrd)
  35. #:select (file-system-packages))
  36. #:use-module (gnu packages admin)
  37. #:use-module ((gnu packages linux)
  38. #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
  39. #:use-module ((gnu packages base)
  40. #:select (canonical-package glibc glibc-utf8-locales))
  41. #:use-module (gnu packages bash)
  42. #:use-module (gnu packages package-management)
  43. #:use-module (gnu packages linux)
  44. #:use-module (gnu packages terminals)
  45. #:use-module ((gnu build file-systems)
  46. #:select (mount-flags->bit-mask))
  47. #:use-module (guix gexp)
  48. #:use-module (guix records)
  49. #:use-module (guix modules)
  50. #:use-module (srfi srfi-1)
  51. #:use-module (srfi srfi-26)
  52. #:use-module (ice-9 match)
  53. #:use-module (ice-9 format)
  54. #:export (fstab-service-type
  55. root-file-system-service
  56. file-system-service-type
  57. user-unmount-service
  58. swap-service
  59. user-processes-service-type
  60. host-name-service
  61. console-keymap-service
  62. %default-console-font
  63. console-font-service-type
  64. console-font-service
  65. udev-configuration
  66. udev-configuration?
  67. udev-configuration-rules
  68. udev-service-type
  69. udev-service
  70. udev-rule
  71. file->udev-rule
  72. login-configuration
  73. login-configuration?
  74. login-service-type
  75. login-service
  76. agetty-configuration
  77. agetty-configuration?
  78. agetty-service
  79. agetty-service-type
  80. mingetty-configuration
  81. mingetty-configuration?
  82. mingetty-service
  83. mingetty-service-type
  84. %nscd-default-caches
  85. %nscd-default-configuration
  86. nscd-configuration
  87. nscd-configuration?
  88. nscd-cache
  89. nscd-cache?
  90. nscd-service-type
  91. nscd-service
  92. syslog-configuration
  93. syslog-configuration?
  94. syslog-service
  95. syslog-service-type
  96. %default-syslog.conf
  97. %default-authorized-guix-keys
  98. guix-configuration
  99. guix-configuration?
  100. guix-configuration-guix
  101. guix-configuration-build-group
  102. guix-configuration-build-accounts
  103. guix-configuration-authorize-key?
  104. guix-configuration-authorized-keys
  105. guix-configuration-use-substitutes?
  106. guix-configuration-substitute-urls
  107. guix-configuration-extra-options
  108. guix-configuration-log-file
  109. guix-service
  110. guix-service-type
  111. guix-publish-configuration
  112. guix-publish-configuration?
  113. guix-publish-configuration-guix
  114. guix-publish-configuration-port
  115. guix-publish-configuration-host
  116. guix-publish-configuration-compression-level
  117. guix-publish-configuration-nar-path
  118. guix-publish-configuration-cache
  119. guix-publish-configuration-ttl
  120. guix-publish-service
  121. guix-publish-service-type
  122. gpm-configuration
  123. gpm-configuration?
  124. gpm-service-type
  125. gpm-service
  126. urandom-seed-service-type
  127. urandom-seed-service
  128. rngd-configuration
  129. rngd-configuration?
  130. rngd-service-type
  131. rngd-service
  132. kmscon-configuration
  133. kmscon-configuration?
  134. kmscon-service-type
  135. pam-limits-service-type
  136. pam-limits-service
  137. %base-services))
  138. ;;; Commentary:
  139. ;;;
  140. ;;; Base system services---i.e., services that 99% of the users will want to
  141. ;;; use.
  142. ;;;
  143. ;;; Code:
  144. ;;;
  145. ;;; User processes.
  146. ;;;
  147. (define %do-not-kill-file
  148. ;; Name of the file listing PIDs of processes that must survive when halting
  149. ;; the system. Typical example is user-space file systems.
  150. "/etc/shepherd/do-not-kill")
  151. (define (user-processes-shepherd-service requirements)
  152. "Return the 'user-processes' Shepherd service with dependencies on
  153. REQUIREMENTS (a list of service names).
  154. This is a synchronization point used to make sure user processes and daemons
  155. get started only after crucial initial services have been started---file
  156. system mounts, etc. This is similar to the 'sysvinit' target in systemd."
  157. (define grace-delay
  158. ;; Delay after sending SIGTERM and before sending SIGKILL.
  159. 4)
  160. (list (shepherd-service
  161. (documentation "When stopped, terminate all user processes.")
  162. (provision '(user-processes))
  163. (requirement requirements)
  164. (start #~(const #t))
  165. (stop #~(lambda _
  166. (define (kill-except omit signal)
  167. ;; Kill all the processes with SIGNAL except those listed
  168. ;; in OMIT and the current process.
  169. (let ((omit (cons (getpid) omit)))
  170. (for-each (lambda (pid)
  171. (unless (memv pid omit)
  172. (false-if-exception
  173. (kill pid signal))))
  174. (processes))))
  175. (define omitted-pids
  176. ;; List of PIDs that must not be killed.
  177. (if (file-exists? #$%do-not-kill-file)
  178. (map string->number
  179. (call-with-input-file #$%do-not-kill-file
  180. (compose string-tokenize
  181. (@ (ice-9 rdelim) read-string))))
  182. '()))
  183. (define (now)
  184. (car (gettimeofday)))
  185. (define (sleep* n)
  186. ;; Really sleep N seconds.
  187. ;; Work around <http://bugs.gnu.org/19581>.
  188. (define start (now))
  189. (let loop ((elapsed 0))
  190. (when (> n elapsed)
  191. (sleep (- n elapsed))
  192. (loop (- (now) start)))))
  193. (define lset= (@ (srfi srfi-1) lset=))
  194. (display "sending all processes the TERM signal\n")
  195. (if (null? omitted-pids)
  196. (begin
  197. ;; Easy: terminate all of them.
  198. (kill -1 SIGTERM)
  199. (sleep* #$grace-delay)
  200. (kill -1 SIGKILL))
  201. (begin
  202. ;; Kill them all except OMITTED-PIDS. XXX: We would
  203. ;; like to (kill -1 SIGSTOP) to get a fixed list of
  204. ;; processes, like 'killall5' does, but that seems
  205. ;; unreliable.
  206. (kill-except omitted-pids SIGTERM)
  207. (sleep* #$grace-delay)
  208. (kill-except omitted-pids SIGKILL)
  209. (delete-file #$%do-not-kill-file)))
  210. (let wait ()
  211. ;; Reap children, if any, so that we don't end up with
  212. ;; zombies and enter an infinite loop.
  213. (let reap-children ()
  214. (define result
  215. (false-if-exception
  216. (waitpid WAIT_ANY (if (null? omitted-pids)
  217. 0
  218. WNOHANG))))
  219. (when (and (pair? result)
  220. (not (zero? (car result))))
  221. (reap-children)))
  222. (let ((pids (processes)))
  223. (unless (lset= = pids (cons 1 omitted-pids))
  224. (format #t "waiting for process termination\
  225. (processes left: ~s)~%"
  226. pids)
  227. (sleep* 2)
  228. (wait))))
  229. (display "all processes have been terminated\n")
  230. #f))
  231. (respawn? #f))))
  232. (define user-processes-service-type
  233. (service-type
  234. (name 'user-processes)
  235. (extensions (list (service-extension shepherd-root-service-type
  236. user-processes-shepherd-service)))
  237. (compose concatenate)
  238. (extend append)
  239. ;; The value is the list of Shepherd services 'user-processes' depends on.
  240. ;; Extensions can add new services to this list.
  241. (default-value '())
  242. (description "The @code{user-processes} service is responsible for
  243. terminating all the processes so that the root file system can be re-mounted
  244. read-only, just before rebooting/halting. Processes still running after a few
  245. seconds after @code{SIGTERM} has been sent are terminated with
  246. @code{SIGKILL}.")))
  247. ;;;
  248. ;;; File systems.
  249. ;;;
  250. (define (file-system->fstab-entry file-system)
  251. "Return a @file{/etc/fstab} entry for @var{file-system}."
  252. (string-append (case (file-system-title file-system)
  253. ((label)
  254. (string-append "LABEL=" (file-system-device file-system)))
  255. ((uuid)
  256. (string-append
  257. "UUID="
  258. (uuid->string (file-system-device file-system))))
  259. (else
  260. (file-system-device file-system)))
  261. "\t"
  262. (file-system-mount-point file-system) "\t"
  263. (file-system-type file-system) "\t"
  264. (or (file-system-options file-system) "defaults") "\t"
  265. ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
  266. ;; don't have anything sensible to put in there.
  267. ))
  268. (define (file-systems->fstab file-systems)
  269. "Return a @file{/etc} entry for an @file{fstab} describing
  270. @var{file-systems}."
  271. `(("fstab" ,(plain-file "fstab"
  272. (string-append
  273. "\
  274. # This file was generated from your GuixSD configuration. Any changes
  275. # will be lost upon reboot or reconfiguration.\n\n"
  276. (string-join (map file-system->fstab-entry
  277. file-systems)
  278. "\n")
  279. "\n")))))
  280. (define fstab-service-type
  281. ;; The /etc/fstab service.
  282. (service-type (name 'fstab)
  283. (extensions
  284. (list (service-extension etc-service-type
  285. file-systems->fstab)))
  286. (compose concatenate)
  287. (extend append)
  288. (description
  289. "Populate the @file{/etc/fstab} based on the given file
  290. system objects.")))
  291. (define %root-file-system-shepherd-service
  292. (shepherd-service
  293. (documentation "Take care of the root file system.")
  294. (provision '(root-file-system))
  295. (start #~(const #t))
  296. (stop #~(lambda _
  297. ;; Return #f if successfully stopped.
  298. (sync)
  299. (call-with-blocked-asyncs
  300. (lambda ()
  301. (let ((null (%make-void-port "w")))
  302. ;; Close 'shepherd.log'.
  303. (display "closing log\n")
  304. ((@ (shepherd comm) stop-logging))
  305. ;; Redirect the default output ports..
  306. (set-current-output-port null)
  307. (set-current-error-port null)
  308. ;; Close /dev/console.
  309. (for-each close-fdes '(0 1 2))
  310. ;; At this point, there are no open files left, so the
  311. ;; root file system can be re-mounted read-only.
  312. (mount #f "/" #f
  313. (logior MS_REMOUNT MS_RDONLY)
  314. #:update-mtab? #f)
  315. #f)))))
  316. (respawn? #f)))
  317. (define root-file-system-service-type
  318. (shepherd-service-type 'root-file-system
  319. (const %root-file-system-shepherd-service)))
  320. (define (root-file-system-service)
  321. "Return a service whose sole purpose is to re-mount read-only the root file
  322. system upon shutdown (aka. cleanly \"umounting\" root.)
  323. This service must be the root of the service dependency graph so that its
  324. 'stop' action is invoked when shepherd is the only process left."
  325. (service root-file-system-service-type #f))
  326. (define (file-system->shepherd-service-name file-system)
  327. "Return the symbol that denotes the service mounting and unmounting
  328. FILE-SYSTEM."
  329. (symbol-append 'file-system-
  330. (string->symbol (file-system-mount-point file-system))))
  331. (define (mapped-device->shepherd-service-name md)
  332. "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
  333. (symbol-append 'device-mapping-
  334. (string->symbol (mapped-device-target md))))
  335. (define dependency->shepherd-service-name
  336. (match-lambda
  337. ((? mapped-device? md)
  338. (mapped-device->shepherd-service-name md))
  339. ((? file-system? fs)
  340. (file-system->shepherd-service-name fs))))
  341. (define (file-system-shepherd-service file-system)
  342. "Return the shepherd service for @var{file-system}, or @code{#f} if
  343. @var{file-system} is not auto-mounted upon boot."
  344. (let ((target (file-system-mount-point file-system))
  345. (create? (file-system-create-mount-point? file-system))
  346. (dependencies (file-system-dependencies file-system))
  347. (packages (file-system-packages (list file-system))))
  348. (and (file-system-mount? file-system)
  349. (with-imported-modules (source-module-closure
  350. '((gnu build file-systems)))
  351. (shepherd-service
  352. (provision (list (file-system->shepherd-service-name file-system)))
  353. (requirement `(root-file-system
  354. ,@(map dependency->shepherd-service-name dependencies)))
  355. (documentation "Check, mount, and unmount the given file system.")
  356. (start #~(lambda args
  357. #$(if create?
  358. #~(mkdir-p #$target)
  359. #t)
  360. (let (($PATH (getenv "PATH")))
  361. ;; Make sure fsck.ext2 & co. can be found.
  362. (dynamic-wind
  363. (lambda ()
  364. ;; Don’t display the PATH settings.
  365. (with-output-to-port (%make-void-port "w")
  366. (lambda ()
  367. (set-path-environment-variable "PATH"
  368. '("bin" "sbin")
  369. '#$packages))))
  370. (lambda ()
  371. (mount-file-system
  372. (spec->file-system
  373. '#$(file-system->spec file-system))
  374. #:root "/"))
  375. (lambda ()
  376. (setenv "PATH" $PATH)))
  377. #t)))
  378. (stop #~(lambda args
  379. ;; Normally there are no processes left at this point, so
  380. ;; TARGET can be safely unmounted.
  381. ;; Make sure PID 1 doesn't keep TARGET busy.
  382. (chdir "/")
  383. (umount #$target)
  384. #f))
  385. ;; We need additional modules.
  386. (modules `(((gnu build file-systems)
  387. #:select (mount-file-system))
  388. (gnu system file-systems)
  389. ,@%default-modules)))))))
  390. (define (file-system-shepherd-services file-systems)
  391. "Return the list of Shepherd services for FILE-SYSTEMS."
  392. (let* ((file-systems (filter file-system-mount? file-systems)))
  393. (define sink
  394. (shepherd-service
  395. (provision '(file-systems))
  396. (requirement (cons* 'root-file-system 'user-file-systems
  397. (map file-system->shepherd-service-name
  398. file-systems)))
  399. (documentation "Target for all the initially-mounted file systems")
  400. (start #~(const #t))
  401. (stop #~(const #f))))
  402. (cons sink (map file-system-shepherd-service file-systems))))
  403. (define file-system-service-type
  404. (service-type (name 'file-systems)
  405. (extensions
  406. (list (service-extension shepherd-root-service-type
  407. file-system-shepherd-services)
  408. (service-extension fstab-service-type
  409. identity)
  410. ;; Have 'user-processes' depend on 'file-systems'.
  411. (service-extension user-processes-service-type
  412. (const '(file-systems)))))
  413. (compose concatenate)
  414. (extend append)
  415. (description
  416. "Provide Shepherd services to mount and unmount the given
  417. file systems, as well as corresponding @file{/etc/fstab} entries.")))
  418. (define user-unmount-service-type
  419. (shepherd-service-type
  420. 'user-file-systems
  421. (lambda (known-mount-points)
  422. (shepherd-service
  423. (documentation "Unmount manually-mounted file systems.")
  424. (provision '(user-file-systems))
  425. (start #~(const #t))
  426. (stop #~(lambda args
  427. (define (known? mount-point)
  428. (member mount-point
  429. (cons* "/proc" "/sys" '#$known-mount-points)))
  430. ;; Make sure we don't keep the user's mount points busy.
  431. (chdir "/")
  432. (for-each (lambda (mount-point)
  433. (format #t "unmounting '~a'...~%" mount-point)
  434. (catch 'system-error
  435. (lambda ()
  436. (umount mount-point))
  437. (lambda args
  438. (let ((errno (system-error-errno args)))
  439. (format #t "failed to unmount '~a': ~a~%"
  440. mount-point (strerror errno))))))
  441. (filter (negate known?) (mount-points)))
  442. #f))))))
  443. (define (user-unmount-service known-mount-points)
  444. "Return a service whose sole purpose is to unmount file systems not listed
  445. in KNOWN-MOUNT-POINTS when it is stopped."
  446. (service user-unmount-service-type known-mount-points))
  447. ;;;
  448. ;;; Preserve entropy to seed /dev/urandom on boot.
  449. ;;;
  450. (define %random-seed-file
  451. "/var/lib/random-seed")
  452. (define (urandom-seed-shepherd-service _)
  453. "Return a shepherd service for the /dev/urandom seed."
  454. (list (shepherd-service
  455. (documentation "Preserve entropy across reboots for /dev/urandom.")
  456. (provision '(urandom-seed))
  457. ;; Depend on udev so that /dev/hwrng is available.
  458. (requirement '(file-systems udev))
  459. (start #~(lambda _
  460. ;; On boot, write random seed into /dev/urandom.
  461. (when (file-exists? #$%random-seed-file)
  462. (call-with-input-file #$%random-seed-file
  463. (lambda (seed)
  464. (call-with-output-file "/dev/urandom"
  465. (lambda (urandom)
  466. (dump-port seed urandom))))))
  467. ;; Try writing from /dev/hwrng into /dev/urandom.
  468. ;; It seems that the file /dev/hwrng always exists, even
  469. ;; when there is no hardware random number generator
  470. ;; available. So, we handle a failed read or any other error
  471. ;; reported by the operating system.
  472. (let ((buf (catch 'system-error
  473. (lambda ()
  474. (call-with-input-file "/dev/hwrng"
  475. (lambda (hwrng)
  476. (get-bytevector-n hwrng 512))))
  477. ;; Silence is golden...
  478. (const #f))))
  479. (when buf
  480. (call-with-output-file "/dev/urandom"
  481. (lambda (urandom)
  482. (put-bytevector urandom buf)))))
  483. ;; Immediately refresh the seed in case the system doesn't
  484. ;; shut down cleanly.
  485. (call-with-input-file "/dev/urandom"
  486. (lambda (urandom)
  487. (let ((previous-umask (umask #o077))
  488. (buf (make-bytevector 512)))
  489. (mkdir-p (dirname #$%random-seed-file))
  490. (get-bytevector-n! urandom buf 0 512)
  491. (call-with-output-file #$%random-seed-file
  492. (lambda (seed)
  493. (put-bytevector seed buf)))
  494. (umask previous-umask))))
  495. #t))
  496. (stop #~(lambda _
  497. ;; During shutdown, write from /dev/urandom into random seed.
  498. (let ((buf (make-bytevector 512)))
  499. (call-with-input-file "/dev/urandom"
  500. (lambda (urandom)
  501. (let ((previous-umask (umask #o077)))
  502. (get-bytevector-n! urandom buf 0 512)
  503. (mkdir-p (dirname #$%random-seed-file))
  504. (call-with-output-file #$%random-seed-file
  505. (lambda (seed)
  506. (put-bytevector seed buf)))
  507. (umask previous-umask))
  508. #t)))))
  509. (modules `((rnrs bytevectors)
  510. (rnrs io ports)
  511. ,@%default-modules)))))
  512. (define urandom-seed-service-type
  513. (service-type (name 'urandom-seed)
  514. (extensions
  515. (list (service-extension shepherd-root-service-type
  516. urandom-seed-shepherd-service)
  517. ;; Have 'user-processes' depend on 'urandom-seed'.
  518. ;; This ensures that user processes and daemons don't
  519. ;; start until we have seeded the PRNG.
  520. (service-extension user-processes-service-type
  521. (const '(urandom-seed)))))
  522. (default-value #f)
  523. (description
  524. "Seed the @file{/dev/urandom} pseudo-random number
  525. generator (RNG) with the value recorded when the system was last shut
  526. down.")))
  527. (define (urandom-seed-service) ;deprecated
  528. (service urandom-seed-service-type #f))
  529. ;;;
  530. ;;; Add hardware random number generator to entropy pool.
  531. ;;;
  532. (define-record-type* <rngd-configuration>
  533. rngd-configuration make-rngd-configuration
  534. rngd-configuration?
  535. (rng-tools rngd-configuration-rng-tools) ;package
  536. (device rngd-configuration-device)) ;string
  537. (define rngd-service-type
  538. (shepherd-service-type
  539. 'rngd
  540. (lambda (config)
  541. (define rng-tools (rngd-configuration-rng-tools config))
  542. (define device (rngd-configuration-device config))
  543. (define rngd-command
  544. (list (file-append rng-tools "/sbin/rngd")
  545. "-f" "-r" device))
  546. (shepherd-service
  547. (documentation "Add TRNG to entropy pool.")
  548. (requirement '(udev))
  549. (provision '(trng))
  550. (start #~(make-forkexec-constructor #$@rngd-command))
  551. (stop #~(make-kill-destructor))))))
  552. (define* (rngd-service #:key
  553. (rng-tools rng-tools)
  554. (device "/dev/hwrng"))
  555. "Return a service that runs the @command{rngd} program from @var{rng-tools}
  556. to add @var{device} to the kernel's entropy pool. The service will fail if
  557. @var{device} does not exist."
  558. (service rngd-service-type
  559. (rngd-configuration
  560. (rng-tools rng-tools)
  561. (device device))))
  562. ;;;
  563. ;;; Console & co.
  564. ;;;
  565. (define host-name-service-type
  566. (shepherd-service-type
  567. 'host-name
  568. (lambda (name)
  569. (shepherd-service
  570. (documentation "Initialize the machine's host name.")
  571. (provision '(host-name))
  572. (start #~(lambda _
  573. (sethostname #$name)))
  574. (respawn? #f)))))
  575. (define (host-name-service name)
  576. "Return a service that sets the host name to @var{name}."
  577. (service host-name-service-type name))
  578. (define (unicode-start tty)
  579. "Return a gexp to start Unicode support on @var{tty}."
  580. (with-imported-modules '((guix build syscalls))
  581. #~(let* ((fd (open-fdes #$tty O_RDWR))
  582. (termios (tcgetattr fd)))
  583. (define (set-utf8-input termios)
  584. (set-field termios (termios-input-flags)
  585. (logior (input-flags IUTF8)
  586. (termios-input-flags termios))))
  587. ;; See console_codes(4).
  588. (display "\x1b%G" (fdes->outport fd))
  589. (tcsetattr fd (tcsetattr-action TCSAFLUSH)
  590. (set-utf8-input termios))
  591. ;; TODO: ioctl(fd, KDSKBMODE, K_UNICODE);
  592. (close-fdes fd)
  593. #t)))
  594. (define console-keymap-service-type
  595. (shepherd-service-type
  596. 'console-keymap
  597. (lambda (files)
  598. (shepherd-service
  599. (documentation (string-append "Load console keymap (loadkeys)."))
  600. (provision '(console-keymap))
  601. (start #~(lambda _
  602. (zero? (system* #$(file-append kbd "/bin/loadkeys")
  603. #$@files))))
  604. (respawn? #f)))))
  605. (define (console-keymap-service . files)
  606. "Return a service to load console keymaps from @var{files}."
  607. (service console-keymap-service-type files))
  608. (define %default-console-font
  609. ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
  610. ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
  611. ;; codepoints notably found in the UTF-8 manual.
  612. "LatGrkCyr-8x16")
  613. (define (console-font-shepherd-services tty+font)
  614. "Return a list of Shepherd services for each pair in TTY+FONT."
  615. (map (match-lambda
  616. ((tty . font)
  617. (let ((device (string-append "/dev/" tty)))
  618. (shepherd-service
  619. (documentation "Load a Unicode console font.")
  620. (provision (list (symbol-append 'console-font-
  621. (string->symbol tty))))
  622. ;; Start after mingetty has been started on TTY, otherwise the settings
  623. ;; are ignored.
  624. (requirement (list (symbol-append 'term-
  625. (string->symbol tty))))
  626. (modules '((guix build syscalls) ;for 'tcsetattr'
  627. (srfi srfi-9 gnu))) ;for 'set-field'
  628. (start #~(lambda _
  629. ;; It could be that mingetty is not fully ready yet,
  630. ;; which we check by calling 'ttyname'.
  631. (let loop ((i 10))
  632. (unless (or (zero? i)
  633. (call-with-input-file #$device
  634. (lambda (port)
  635. (false-if-exception (ttyname port)))))
  636. (usleep 500)
  637. (loop (- i 1))))
  638. (and #$(unicode-start device)
  639. ;; 'setfont' returns EX_OSERR (71) when an
  640. ;; KDFONTOP ioctl fails, for example. Like
  641. ;; systemd's vconsole support, let's not treat
  642. ;; this as an error.
  643. (case (status:exit-val
  644. (system* #$(file-append kbd "/bin/setfont")
  645. "-C" #$device #$font))
  646. ((0 71) #t)
  647. (else #f)))))
  648. (stop #~(const #t))
  649. (respawn? #f)))))
  650. tty+font))
  651. (define console-font-service-type
  652. (service-type (name 'console-fonts)
  653. (extensions
  654. (list (service-extension shepherd-root-service-type
  655. console-font-shepherd-services)))
  656. (compose concatenate)
  657. (extend append)
  658. (description
  659. "Install the given fonts on the specified ttys (fonts are per
  660. virtual console on GNU/Linux). The value of this service is a list of
  661. tty/font pairs like:
  662. @example
  663. '((\"tty1\" . \"LatGrkCyr-8x16\"))
  664. @end example\n")))
  665. (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
  666. "This procedure is deprecated in favor of @code{console-font-service-type}.
  667. Return a service that sets up Unicode support in @var{tty} and loads
  668. @var{font} for that tty (fonts are per virtual console in Linux.)"
  669. (simple-service (symbol-append 'console-font- (string->symbol tty))
  670. console-font-service-type `((,tty . ,font))))
  671. (define %default-motd
  672. (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
  673. (define-record-type* <login-configuration>
  674. login-configuration make-login-configuration
  675. login-configuration?
  676. (motd login-configuration-motd ;file-like
  677. (default %default-motd))
  678. ;; Allow empty passwords by default so that first-time users can log in when
  679. ;; the 'root' account has just been created.
  680. (allow-empty-passwords? login-configuration-allow-empty-passwords?
  681. (default #t))) ;Boolean
  682. (define (login-pam-service config)
  683. "Return the list of PAM service needed for CONF."
  684. ;; Let 'login' be known to PAM.
  685. (list (unix-pam-service "login"
  686. #:allow-empty-passwords?
  687. (login-configuration-allow-empty-passwords? config)
  688. #:motd
  689. (login-configuration-motd config))))
  690. (define login-service-type
  691. (service-type (name 'login)
  692. (extensions (list (service-extension pam-root-service-type
  693. login-pam-service)))
  694. (description
  695. "Provide a console log-in service as specified by its
  696. configuration value, a @code{login-configuration} object.")))
  697. (define* (login-service #:optional (config (login-configuration)))
  698. "Return a service configure login according to @var{config}, which specifies
  699. the message of the day, among other things."
  700. (service login-service-type config))
  701. (define-record-type* <agetty-configuration>
  702. agetty-configuration make-agetty-configuration
  703. agetty-configuration?
  704. (agetty agetty-configuration-agetty ;<package>
  705. (default util-linux))
  706. (tty agetty-configuration-tty) ;string
  707. (term agetty-term ;string | #f
  708. (default #f))
  709. (baud-rate agetty-baud-rate ;string | #f
  710. (default #f))
  711. (auto-login agetty-auto-login ;list of strings | #f
  712. (default #f))
  713. (login-program agetty-login-program ;gexp
  714. (default (file-append shadow "/bin/login")))
  715. (login-pause? agetty-login-pause? ;Boolean
  716. (default #f))
  717. (eight-bits? agetty-eight-bits? ;Boolean
  718. (default #f))
  719. (no-reset? agetty-no-reset? ;Boolean
  720. (default #f))
  721. (remote? agetty-remote? ;Boolean
  722. (default #f))
  723. (flow-control? agetty-flow-control? ;Boolean
  724. (default #f))
  725. (host agetty-host ;string | #f
  726. (default #f))
  727. (no-issue? agetty-no-issue? ;Boolean
  728. (default #f))
  729. (init-string agetty-init-string ;string | #f
  730. (default #f))
  731. (no-clear? agetty-no-clear? ;Boolean
  732. (default #f))
  733. (local-line agetty-local-line ;always | never | auto
  734. (default #f))
  735. (extract-baud? agetty-extract-baud? ;Boolean
  736. (default #f))
  737. (skip-login? agetty-skip-login? ;Boolean
  738. (default #f))
  739. (no-newline? agetty-no-newline? ;Boolean
  740. (default #f))
  741. (login-options agetty-login-options ;string | #f
  742. (default #f))
  743. (chroot agetty-chroot ;string | #f
  744. (default #f))
  745. (hangup? agetty-hangup? ;Boolean
  746. (default #f))
  747. (keep-baud? agetty-keep-baud? ;Boolean
  748. (default #f))
  749. (timeout agetty-timeout ;integer | #f
  750. (default #f))
  751. (detect-case? agetty-detect-case? ;Boolean
  752. (default #f))
  753. (wait-cr? agetty-wait-cr? ;Boolean
  754. (default #f))
  755. (no-hints? agetty-no-hints? ;Boolean
  756. (default #f))
  757. (no-hostname? agetty-no hostname? ;Boolean
  758. (default #f))
  759. (long-hostname? agetty-long-hostname? ;Boolean
  760. (default #f))
  761. (erase-characters agetty-erase-characters ;string | #f
  762. (default #f))
  763. (kill-characters agetty-kill-characters ;string | #f
  764. (default #f))
  765. (chdir agetty-chdir ;string | #f
  766. (default #f))
  767. (delay agetty-delay ;integer | #f
  768. (default #f))
  769. (nice agetty-nice ;integer | #f
  770. (default #f))
  771. ;; "Escape hatch" for passing arbitrary command-line arguments.
  772. (extra-options agetty-extra-options ;list of strings
  773. (default '()))
  774. ;;; XXX Unimplemented for now!
  775. ;;; (issue-file agetty-issue-file ;file-like
  776. ;;; (default #f))
  777. )
  778. (define agetty-shepherd-service
  779. (match-lambda
  780. (($ <agetty-configuration> agetty tty term baud-rate auto-login
  781. login-program login-pause? eight-bits? no-reset? remote? flow-control?
  782. host no-issue? init-string no-clear? local-line extract-baud?
  783. skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
  784. detect-case? wait-cr? no-hints? no-hostname? long-hostname?
  785. erase-characters kill-characters chdir delay nice extra-options)
  786. (list
  787. (shepherd-service
  788. (documentation "Run agetty on a tty.")
  789. (provision (list (symbol-append 'term- (string->symbol tty))))
  790. ;; Since the login prompt shows the host name, wait for the 'host-name'
  791. ;; service to be done. Also wait for udev essentially so that the tty
  792. ;; text is not lost in the middle of kernel messages (see also
  793. ;; mingetty-shepherd-service).
  794. (requirement '(user-processes host-name udev))
  795. (start #~(make-forkexec-constructor
  796. (list #$(file-append util-linux "/sbin/agetty")
  797. #$@extra-options
  798. #$@(if eight-bits?
  799. #~("--8bits")
  800. #~())
  801. #$@(if no-reset?
  802. #~("--noreset")
  803. #~())
  804. #$@(if remote?
  805. #~("--remote")
  806. #~())
  807. #$@(if flow-control?
  808. #~("--flow-control")
  809. #~())
  810. #$@(if host
  811. #~("--host" #$host)
  812. #~())
  813. #$@(if no-issue?
  814. #~("--noissue")
  815. #~())
  816. #$@(if init-string
  817. #~("--init-string" #$init-string)
  818. #~())
  819. #$@(if no-clear?
  820. #~("--noclear")
  821. #~())
  822. ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
  823. ;;; is not passed, then the default is 'auto'. However, in my tests, when that
  824. ;;; option is selected, agetty never presents the login prompt, and the
  825. ;;; term-ttyS0 service respawns every few seconds.
  826. #$@(if local-line
  827. #~(#$(match local-line
  828. ('auto "--local-line=auto")
  829. ('always "--local-line=always")
  830. ('never "-local-line=never")))
  831. #~())
  832. #$@(if extract-baud?
  833. #~("--extract-baud")
  834. #~())
  835. #$@(if skip-login?
  836. #~("--skip-login")
  837. #~())
  838. #$@(if no-newline?
  839. #~("--nonewline")
  840. #~())
  841. #$@(if login-options
  842. #~("--login-options" #$login-options)
  843. #~())
  844. #$@(if chroot
  845. #~("--chroot" #$chroot)
  846. #~())
  847. #$@(if hangup?
  848. #~("--hangup")
  849. #~())
  850. #$@(if keep-baud?
  851. #~("--keep-baud")
  852. #~())
  853. #$@(if timeout
  854. #~("--timeout" #$(number->string timeout))
  855. #~())
  856. #$@(if detect-case?
  857. #~("--detect-case")
  858. #~())
  859. #$@(if wait-cr?
  860. #~("--wait-cr")
  861. #~())
  862. #$@(if no-hints?
  863. #~("--nohints?")
  864. #~())
  865. #$@(if no-hostname?
  866. #~("--nohostname")
  867. #~())
  868. #$@(if long-hostname?
  869. #~("--long-hostname")
  870. #~())
  871. #$@(if erase-characters
  872. #~("--erase-chars" #$erase-characters)
  873. #~())
  874. #$@(if kill-characters
  875. #~("--kill-chars" #$kill-characters)
  876. #~())
  877. #$@(if chdir
  878. #~("--chdir" #$chdir)
  879. #~())
  880. #$@(if delay
  881. #~("--delay" #$(number->string delay))
  882. #~())
  883. #$@(if nice
  884. #~("--nice" #$(number->string nice))
  885. #~())
  886. #$@(if auto-login
  887. (list "--autologin" auto-login)
  888. '())
  889. #$@(if login-program
  890. #~("--login-program" #$login-program)
  891. #~())
  892. #$@(if login-pause?
  893. #~("--login-pause")
  894. #~())
  895. #$tty
  896. #$@(if baud-rate
  897. #~(#$baud-rate)
  898. #~())
  899. #$@(if term
  900. #~(#$term)
  901. #~()))))
  902. (stop #~(make-kill-destructor)))))))
  903. (define agetty-service-type
  904. (service-type (name 'agetty)
  905. (extensions (list (service-extension shepherd-root-service-type
  906. agetty-shepherd-service)))
  907. (description
  908. "Provide console login using the @command{agetty}
  909. program.")))
  910. (define* (agetty-service config)
  911. "Return a service to run agetty according to @var{config}, which specifies
  912. the tty to run, among other things."
  913. (service agetty-service-type config))
  914. (define-record-type* <mingetty-configuration>
  915. mingetty-configuration make-mingetty-configuration
  916. mingetty-configuration?
  917. (mingetty mingetty-configuration-mingetty ;<package>
  918. (default mingetty))
  919. (tty mingetty-configuration-tty) ;string
  920. (auto-login mingetty-auto-login ;string | #f
  921. (default #f))
  922. (login-program mingetty-login-program ;gexp
  923. (default #f))
  924. (login-pause? mingetty-login-pause? ;Boolean
  925. (default #f)))
  926. (define mingetty-shepherd-service
  927. (match-lambda
  928. (($ <mingetty-configuration> mingetty tty auto-login login-program
  929. login-pause?)
  930. (list
  931. (shepherd-service
  932. (documentation "Run mingetty on an tty.")
  933. (provision (list (symbol-append 'term- (string->symbol tty))))
  934. ;; Since the login prompt shows the host name, wait for the 'host-name'
  935. ;; service to be done. Also wait for udev essentially so that the tty
  936. ;; text is not lost in the middle of kernel messages (XXX).
  937. (requirement '(user-processes host-name udev))
  938. (start #~(make-forkexec-constructor
  939. (list #$(file-append mingetty "/sbin/mingetty")
  940. "--noclear" #$tty
  941. #$@(if auto-login
  942. #~("--autologin" #$auto-login)
  943. #~())
  944. #$@(if login-program
  945. #~("--loginprog" #$login-program)
  946. #~())
  947. #$@(if login-pause?
  948. #~("--loginpause")
  949. #~()))))
  950. (stop #~(make-kill-destructor)))))))
  951. (define mingetty-service-type
  952. (service-type (name 'mingetty)
  953. (extensions (list (service-extension shepherd-root-service-type
  954. mingetty-shepherd-service)))
  955. (description
  956. "Provide console login using the @command{mingetty}
  957. program.")))
  958. (define* (mingetty-service config)
  959. "Return a service to run mingetty according to @var{config}, which specifies
  960. the tty to run, among other things."
  961. (service mingetty-service-type config))
  962. (define-record-type* <nscd-configuration> nscd-configuration
  963. make-nscd-configuration
  964. nscd-configuration?
  965. (log-file nscd-configuration-log-file ;string
  966. (default "/var/log/nscd.log"))
  967. (debug-level nscd-debug-level ;integer
  968. (default 0))
  969. ;; TODO: See nscd.conf in glibc for other options to add.
  970. (caches nscd-configuration-caches ;list of <nscd-cache>
  971. (default %nscd-default-caches))
  972. (name-services nscd-configuration-name-services ;list of <packages>
  973. (default '()))
  974. (glibc nscd-configuration-glibc ;<package>
  975. (default (canonical-package glibc))))
  976. (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
  977. nscd-cache?
  978. (database nscd-cache-database) ;symbol
  979. (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
  980. (negative-time-to-live nscd-cache-negative-time-to-live
  981. (default 20)) ;integer
  982. (suggested-size nscd-cache-suggested-size ;integer ("default module
  983. ;of hash table")
  984. (default 211))
  985. (check-files? nscd-cache-check-files? ;Boolean
  986. (default #t))
  987. (persistent? nscd-cache-persistent? ;Boolean
  988. (default #t))
  989. (shared? nscd-cache-shared? ;Boolean
  990. (default #t))
  991. (max-database-size nscd-cache-max-database-size ;integer
  992. (default (* 32 (expt 2 20))))
  993. (auto-propagate? nscd-cache-auto-propagate? ;Boolean
  994. (default #t)))
  995. (define %nscd-default-caches
  996. ;; Caches that we want to enable by default. Note that when providing an
  997. ;; empty nscd.conf, all caches are disabled.
  998. (list (nscd-cache (database 'hosts)
  999. ;; Aggressively cache the host name cache to improve
  1000. ;; privacy and resilience.
  1001. (positive-time-to-live (* 3600 12))
  1002. (negative-time-to-live 20)
  1003. (persistent? #t))
  1004. (nscd-cache (database 'services)
  1005. ;; Services are unlikely to change, so we can be even more
  1006. ;; aggressive.
  1007. (positive-time-to-live (* 3600 24))
  1008. (negative-time-to-live 3600)
  1009. (check-files? #t) ;check /etc/services changes
  1010. (persistent? #t))))
  1011. (define %nscd-default-configuration
  1012. ;; Default nscd configuration.
  1013. (nscd-configuration))
  1014. (define (nscd.conf-file config)
  1015. "Return the @file{nscd.conf} configuration file for @var{config}, an
  1016. @code{<nscd-configuration>} object."
  1017. (define cache->config
  1018. (match-lambda
  1019. (($ <nscd-cache> (= symbol->string database)
  1020. positive-ttl negative-ttl size check-files?
  1021. persistent? shared? max-size propagate?)
  1022. (string-append "\nenable-cache\t" database "\tyes\n"
  1023. "positive-time-to-live\t" database "\t"
  1024. (number->string positive-ttl) "\n"
  1025. "negative-time-to-live\t" database "\t"
  1026. (number->string negative-ttl) "\n"
  1027. "suggested-size\t" database "\t"
  1028. (number->string size) "\n"
  1029. "check-files\t" database "\t"
  1030. (if check-files? "yes\n" "no\n")
  1031. "persistent\t" database "\t"
  1032. (if persistent? "yes\n" "no\n")
  1033. "shared\t" database "\t"
  1034. (if shared? "yes\n" "no\n")
  1035. "max-db-size\t" database "\t"
  1036. (number->string max-size) "\n"
  1037. "auto-propagate\t" database "\t"
  1038. (if propagate? "yes\n" "no\n")))))
  1039. (match config
  1040. (($ <nscd-configuration> log-file debug-level caches)
  1041. (plain-file "nscd.conf"
  1042. (string-append "\
  1043. # Configuration of libc's name service cache daemon (nscd).\n\n"
  1044. (if log-file
  1045. (string-append "logfile\t" log-file)
  1046. "")
  1047. "\n"
  1048. (if debug-level
  1049. (string-append "debug-level\t"
  1050. (number->string debug-level))
  1051. "")
  1052. "\n"
  1053. (string-concatenate
  1054. (map cache->config caches)))))))
  1055. (define (nscd-shepherd-service config)
  1056. "Return a shepherd service for CONFIG, an <nscd-configuration> object."
  1057. (let ((nscd.conf (nscd.conf-file config))
  1058. (name-services (nscd-configuration-name-services config)))
  1059. (list (shepherd-service
  1060. (documentation "Run libc's name service cache daemon (nscd).")
  1061. (provision '(nscd))
  1062. (requirement '(user-processes))
  1063. (start #~(make-forkexec-constructor
  1064. (list #$(file-append (nscd-configuration-glibc config)
  1065. "/sbin/nscd")
  1066. "-f" #$nscd.conf "--foreground")
  1067. ;; Wait for the PID file. However, the PID file is
  1068. ;; written before nscd is actually listening on its
  1069. ;; socket (XXX).
  1070. #:pid-file "/var/run/nscd/nscd.pid"
  1071. #:environment-variables
  1072. (list (string-append "LD_LIBRARY_PATH="
  1073. (string-join
  1074. (map (lambda (dir)
  1075. (string-append dir "/lib"))
  1076. (list #$@name-services))
  1077. ":")))))
  1078. (stop #~(make-kill-destructor))))))
  1079. (define nscd-activation
  1080. ;; Actions to take before starting nscd.
  1081. #~(begin
  1082. (use-modules (guix build utils))
  1083. (mkdir-p "/var/run/nscd")
  1084. (mkdir-p "/var/db/nscd") ;for the persistent cache
  1085. ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
  1086. ;; that file exists when it is started. Thus create it here. Note: on
  1087. ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
  1088. ;; is a symlink, hence 'lstat'.
  1089. (unless (false-if-exception (lstat "/etc/resolv.conf"))
  1090. (call-with-output-file "/etc/resolv.conf"
  1091. (lambda (port)
  1092. (display "# This is a placeholder.\n" port))))))
  1093. (define nscd-service-type
  1094. (service-type (name 'nscd)
  1095. (extensions
  1096. (list (service-extension activation-service-type
  1097. (const nscd-activation))
  1098. (service-extension shepherd-root-service-type
  1099. nscd-shepherd-service)))
  1100. ;; This can be extended by providing additional name services
  1101. ;; such as nss-mdns.
  1102. (compose concatenate)
  1103. (extend (lambda (config name-services)
  1104. (nscd-configuration
  1105. (inherit config)
  1106. (name-services (append
  1107. (nscd-configuration-name-services config)
  1108. name-services)))))
  1109. (description
  1110. "Runs libc's @dfn{name service cache daemon} (nscd) with the
  1111. given configuration---an @code{<nscd-configuration>} object. @xref{Name
  1112. Service Switch}, for an example.")))
  1113. (define* (nscd-service #:optional (config %nscd-default-configuration))
  1114. "Return a service that runs libc's name service cache daemon (nscd) with the
  1115. given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
  1116. Service Switch}, for an example."
  1117. (service nscd-service-type config))
  1118. (define-record-type* <syslog-configuration>
  1119. syslog-configuration make-syslog-configuration
  1120. syslog-configuration?
  1121. (syslogd syslog-configuration-syslogd
  1122. (default (file-append inetutils "/libexec/syslogd")))
  1123. (config-file syslog-configuration-config-file
  1124. (default %default-syslog.conf)))
  1125. (define syslog-service-type
  1126. (shepherd-service-type
  1127. 'syslog
  1128. (lambda (config)
  1129. (shepherd-service
  1130. (documentation "Run the syslog daemon (syslogd).")
  1131. (provision '(syslogd))
  1132. (requirement '(user-processes))
  1133. (start #~(make-forkexec-constructor
  1134. (list #$(syslog-configuration-syslogd config)
  1135. "--rcfile" #$(syslog-configuration-config-file config))
  1136. #:pid-file "/var/run/syslog.pid"))
  1137. (stop #~(make-kill-destructor))))))
  1138. ;; Snippet adapted from the GNU inetutils manual.
  1139. (define %default-syslog.conf
  1140. (plain-file "syslog.conf" "
  1141. # Log all error messages, authentication messages of
  1142. # level notice or higher and anything of level err or
  1143. # higher to the console.
  1144. # Don't log private authentication messages!
  1145. *.alert;auth.notice;authpriv.none /dev/console
  1146. # Log anything (except mail) of level info or higher.
  1147. # Don't log private authentication messages!
  1148. *.info;mail.none;authpriv.none /var/log/messages
  1149. # Like /var/log/messages, but also including \"debug\"-level logs.
  1150. *.debug;mail.none;authpriv.none /var/log/debug
  1151. # Same, in a different place.
  1152. *.info;mail.none;authpriv.none /dev/tty12
  1153. # The authpriv file has restricted access.
  1154. authpriv.* /var/log/secure
  1155. # Log all the mail messages in one place.
  1156. mail.* /var/log/maillog
  1157. "))
  1158. (define* (syslog-service #:optional (config (syslog-configuration)))
  1159. "Return a service that runs @command{syslogd} and takes
  1160. @var{<syslog-configuration>} as a parameter.
  1161. @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
  1162. information on the configuration file syntax."
  1163. (service syslog-service-type config))
  1164. (define pam-limits-service-type
  1165. (let ((security-limits
  1166. ;; Create /etc/security containing the provided "limits.conf" file.
  1167. (lambda (limits-file)
  1168. `(("security"
  1169. ,(computed-file
  1170. "security"
  1171. #~(begin
  1172. (mkdir #$output)
  1173. (stat #$limits-file)
  1174. (symlink #$limits-file
  1175. (string-append #$output "/limits.conf"))))))))
  1176. (pam-extension
  1177. (lambda (pam)
  1178. (let ((pam-limits (pam-entry
  1179. (control "required")
  1180. (module "pam_limits.so")
  1181. (arguments '("conf=/etc/security/limits.conf")))))
  1182. (if (member (pam-service-name pam)
  1183. '("login" "su" "slim"))
  1184. (pam-service
  1185. (inherit pam)
  1186. (session (cons pam-limits
  1187. (pam-service-session pam))))
  1188. pam)))))
  1189. (service-type
  1190. (name 'limits)
  1191. (extensions
  1192. (list (service-extension etc-service-type security-limits)
  1193. (service-extension pam-root-service-type
  1194. (lambda _ (list pam-extension)))))
  1195. (description
  1196. "Install the specified resource usage limits by populating
  1197. @file{/etc/security/limits.conf} and using the @code{pam_limits}
  1198. authentication module."))))
  1199. (define* (pam-limits-service #:optional (limits '()))
  1200. "Return a service that makes selected programs respect the list of
  1201. pam-limits-entry specified in LIMITS via pam_limits.so."
  1202. (service pam-limits-service-type
  1203. (plain-file "limits.conf"
  1204. (string-join (map pam-limits-entry->string limits)
  1205. "\n"))))
  1206. ;;;
  1207. ;;; Guix services.
  1208. ;;;
  1209. (define* (guix-build-accounts count #:key
  1210. (group "guixbuild")
  1211. (first-uid 30001)
  1212. (shadow shadow))
  1213. "Return a list of COUNT user accounts for Guix build users, with UIDs
  1214. starting at FIRST-UID, and under GID."
  1215. (unfold (cut > <> count)
  1216. (lambda (n)
  1217. (user-account
  1218. (name (format #f "guixbuilder~2,'0d" n))
  1219. (system? #t)
  1220. (uid (+ first-uid n -1))
  1221. (group group)
  1222. ;; guix-daemon expects GROUP to be listed as a
  1223. ;; supplementary group too:
  1224. ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
  1225. (supplementary-groups (list group "kvm"))
  1226. (comment (format #f "Guix Build User ~2d" n))
  1227. (home-directory "/var/empty")
  1228. (shell (file-append shadow "/sbin/nologin"))))
  1229. 1+
  1230. 1))
  1231. (define (hydra-key-authorization key guix)
  1232. "Return a gexp with code to register KEY, a file containing a 'guix archive'
  1233. public key, with GUIX."
  1234. #~(unless (file-exists? "/etc/guix/acl")
  1235. (let ((pid (primitive-fork)))
  1236. (case pid
  1237. ((0)
  1238. (let* ((key #$key)
  1239. (port (open-file key "r0b")))
  1240. (format #t "registering public key '~a'...~%" key)
  1241. (close-port (current-input-port))
  1242. (dup port 0)
  1243. (execl #$(file-append guix "/bin/guix")
  1244. "guix" "archive" "--authorize")
  1245. (exit 1)))
  1246. (else
  1247. (let ((status (cdr (waitpid pid))))
  1248. (unless (zero? status)
  1249. (format (current-error-port) "warning: \
  1250. failed to register hydra.gnu.org public key: ~a~%" status))))))))
  1251. (define %default-authorized-guix-keys
  1252. ;; List of authorized substitute keys.
  1253. (list (file-append guix "/share/guix/hydra.gnu.org.pub")
  1254. (file-append guix "/share/guix/berlin.guixsd.org.pub")))
  1255. (define-record-type* <guix-configuration>
  1256. guix-configuration make-guix-configuration
  1257. guix-configuration?
  1258. (guix guix-configuration-guix ;<package>
  1259. (default guix))
  1260. (build-group guix-configuration-build-group ;string
  1261. (default "guixbuild"))
  1262. (build-accounts guix-configuration-build-accounts ;integer
  1263. (default 10))
  1264. (authorize-key? guix-configuration-authorize-key? ;Boolean
  1265. (default #t))
  1266. (authorized-keys guix-configuration-authorized-keys ;list of gexps
  1267. (default %default-authorized-guix-keys))
  1268. (use-substitutes? guix-configuration-use-substitutes? ;Boolean
  1269. (default #t))
  1270. (substitute-urls guix-configuration-substitute-urls ;list of strings
  1271. (default %default-substitute-urls))
  1272. (max-silent-time guix-configuration-max-silent-time ;integer
  1273. (default 0))
  1274. (timeout guix-configuration-timeout ;integer
  1275. (default 0))
  1276. (extra-options guix-configuration-extra-options ;list of strings
  1277. (default '()))
  1278. (log-file guix-configuration-log-file ;string
  1279. (default "/var/log/guix-daemon.log"))
  1280. (http-proxy guix-http-proxy ;string | #f
  1281. (default #f))
  1282. (tmpdir guix-tmpdir ;string | #f
  1283. (default #f)))
  1284. (define %default-guix-configuration
  1285. (guix-configuration))
  1286. (define (guix-shepherd-service config)
  1287. "Return a <shepherd-service> for the Guix daemon service with CONFIG."
  1288. (match config
  1289. (($ <guix-configuration> guix build-group build-accounts
  1290. authorize-key? keys
  1291. use-substitutes? substitute-urls
  1292. max-silent-time timeout
  1293. extra-options
  1294. log-file http-proxy tmpdir)
  1295. (list (shepherd-service
  1296. (documentation "Run the Guix daemon.")
  1297. (provision '(guix-daemon))
  1298. (requirement '(user-processes))
  1299. (start
  1300. #~(make-forkexec-constructor
  1301. (list #$(file-append guix "/bin/guix-daemon")
  1302. "--build-users-group" #$build-group
  1303. "--max-silent-time" #$(number->string max-silent-time)
  1304. "--timeout" #$(number->string timeout)
  1305. #$@(if use-substitutes?
  1306. '()
  1307. '("--no-substitutes"))
  1308. "--substitute-urls" #$(string-join substitute-urls)
  1309. #$@extra-options)
  1310. #:environment-variables
  1311. (list #$@(if http-proxy
  1312. (list (string-append "http_proxy=" http-proxy))
  1313. '())
  1314. #$@(if tmpdir
  1315. (list (string-append "TMPDIR=" tmpdir))
  1316. '()))
  1317. #:log-file #$log-file))
  1318. (stop #~(make-kill-destructor)))))))
  1319. (define (guix-accounts config)
  1320. "Return the user accounts and user groups for CONFIG."
  1321. (match config
  1322. (($ <guix-configuration> _ build-group build-accounts)
  1323. (cons (user-group
  1324. (name build-group)
  1325. (system? #t)
  1326. ;; Use a fixed GID so that we can create the store with the right
  1327. ;; owner.
  1328. (id 30000))
  1329. (guix-build-accounts build-accounts
  1330. #:group build-group)))))
  1331. (define (guix-activation config)
  1332. "Return the activation gexp for CONFIG."
  1333. (match config
  1334. (($ <guix-configuration> guix build-group build-accounts authorize-key? keys)
  1335. ;; Assume that the store has BUILD-GROUP as its group. We could
  1336. ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
  1337. ;; chown leads to an entire copy of the tree, which is a bad idea.
  1338. ;; Optionally authorize hydra.gnu.org's key.
  1339. (if authorize-key?
  1340. #~(begin
  1341. #$@(map (cut hydra-key-authorization <> guix) keys))
  1342. #~#f))))
  1343. (define guix-service-type
  1344. (service-type
  1345. (name 'guix)
  1346. (extensions
  1347. (list (service-extension shepherd-root-service-type guix-shepherd-service)
  1348. (service-extension account-service-type guix-accounts)
  1349. (service-extension activation-service-type guix-activation)
  1350. (service-extension profile-service-type
  1351. (compose list guix-configuration-guix))))
  1352. (default-value (guix-configuration))
  1353. (description
  1354. "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
  1355. (define* (guix-service #:optional (config %default-guix-configuration))
  1356. "Return a service that runs the Guix build daemon according to
  1357. @var{config}."
  1358. (service guix-service-type config))
  1359. (define-record-type* <guix-publish-configuration>
  1360. guix-publish-configuration make-guix-publish-configuration
  1361. guix-publish-configuration?
  1362. (guix guix-publish-configuration-guix ;package
  1363. (default guix))
  1364. (port guix-publish-configuration-port ;number
  1365. (default 80))
  1366. (host guix-publish-configuration-host ;string
  1367. (default "localhost"))
  1368. (compression-level guix-publish-configuration-compression-level ;integer
  1369. (default 3))
  1370. (nar-path guix-publish-configuration-nar-path ;string
  1371. (default "nar"))
  1372. (cache guix-publish-configuration-cache ;#f | string
  1373. (default #f))
  1374. (workers guix-publish-configuration-workers ;#f | integer
  1375. (default #f))
  1376. (ttl guix-publish-configuration-ttl ;#f | integer
  1377. (default #f)))
  1378. (define guix-publish-shepherd-service
  1379. (match-lambda
  1380. (($ <guix-publish-configuration> guix port host compression
  1381. nar-path cache workers ttl)
  1382. (list (shepherd-service
  1383. (provision '(guix-publish))
  1384. (requirement '(guix-daemon))
  1385. (start #~(make-forkexec-constructor
  1386. (list #$(file-append guix "/bin/guix")
  1387. "publish" "-u" "guix-publish"
  1388. "-p" #$(number->string port)
  1389. "-C" #$(number->string compression)
  1390. (string-append "--nar-path=" #$nar-path)
  1391. (string-append "--listen=" #$host)
  1392. #$@(if workers
  1393. #~((string-append "--workers="
  1394. #$(number->string
  1395. workers)))
  1396. #~())
  1397. #$@(if ttl
  1398. #~((string-append "--ttl="
  1399. #$(number->string ttl)
  1400. "s"))
  1401. #~())
  1402. #$@(if cache
  1403. #~((string-append "--cache=" #$cache))
  1404. #~()))
  1405. ;; Make sure we run in a UTF-8 locale so we can produce
  1406. ;; nars for packages that contain UTF-8 file names such
  1407. ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
  1408. #:environment-variables
  1409. (list (string-append "GUIX_LOCPATH="
  1410. #$glibc-utf8-locales "/lib/locale")
  1411. "LC_ALL=en_US.utf8")))
  1412. (stop #~(make-kill-destructor)))))))
  1413. (define %guix-publish-accounts
  1414. (list (user-group (name "guix-publish") (system? #t))
  1415. (user-account
  1416. (name "guix-publish")
  1417. (group "guix-publish")
  1418. (system? #t)
  1419. (comment "guix publish user")
  1420. (home-directory "/var/empty")
  1421. (shell (file-append shadow "/sbin/nologin")))))
  1422. (define (guix-publish-activation config)
  1423. (let ((cache (guix-publish-configuration-cache config)))
  1424. (if cache
  1425. (with-imported-modules '((guix build utils))
  1426. #~(begin
  1427. (use-modules (guix build utils))
  1428. (mkdir-p #$cache)
  1429. (let* ((pw (getpw "guix-publish"))
  1430. (uid (passwd:uid pw))
  1431. (gid (passwd:gid pw)))
  1432. (chown #$cache uid gid))))
  1433. #t)))
  1434. (define guix-publish-service-type
  1435. (service-type (name 'guix-publish)
  1436. (extensions
  1437. (list (service-extension shepherd-root-service-type
  1438. guix-publish-shepherd-service)
  1439. (service-extension account-service-type
  1440. (const %guix-publish-accounts))
  1441. (service-extension activation-service-type
  1442. guix-publish-activation)))
  1443. (default-value (guix-publish-configuration))
  1444. (description
  1445. "Add a Shepherd service running @command{guix publish}, a
  1446. command that allows you to share pre-built binaries with others over HTTP.")))
  1447. (define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
  1448. "Return a service that runs @command{guix publish} listening on @var{host}
  1449. and @var{port} (@pxref{Invoking guix publish}).
  1450. This assumes that @file{/etc/guix} already contains a signing key pair as
  1451. created by @command{guix archive --generate-key} (@pxref{Invoking guix
  1452. archive}). If that is not the case, the service will fail to start."
  1453. ;; Deprecated.
  1454. (service guix-publish-service-type
  1455. (guix-publish-configuration (guix guix) (port port) (host host))))
  1456. ;;;
  1457. ;;; Udev.
  1458. ;;;
  1459. (define-record-type* <udev-configuration>
  1460. udev-configuration make-udev-configuration
  1461. udev-configuration?
  1462. (udev udev-configuration-udev ;<package>
  1463. (default udev))
  1464. (rules udev-configuration-rules ;list of <package>
  1465. (default '())))
  1466. (define (udev-rules-union packages)
  1467. "Return the union of the @code{lib/udev/rules.d} directories found in each
  1468. item of @var{packages}."
  1469. (define build
  1470. (with-imported-modules '((guix build union)
  1471. (guix build utils))
  1472. #~(begin
  1473. (use-modules (guix build union)
  1474. (guix build utils)
  1475. (srfi srfi-1)
  1476. (srfi srfi-26))
  1477. (define %standard-locations
  1478. '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
  1479. (define (rules-sub-directory directory)
  1480. ;; Return the sub-directory of DIRECTORY containing udev rules, or
  1481. ;; #f if none was found.
  1482. (find directory-exists?
  1483. (map (cut string-append directory <>) %standard-locations)))
  1484. (mkdir-p (string-append #$output "/lib/udev"))
  1485. (union-build (string-append #$output "/lib/udev/rules.d")
  1486. (filter-map rules-sub-directory '#$packages)))))
  1487. (computed-file "udev-rules" build))
  1488. (define (udev-rule file-name contents)
  1489. "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
  1490. (computed-file file-name
  1491. (with-imported-modules '((guix build utils))
  1492. #~(begin
  1493. (use-modules (guix build utils))
  1494. (define rules.d
  1495. (string-append #$output "/lib/udev/rules.d"))
  1496. (mkdir-p rules.d)
  1497. (call-with-output-file
  1498. (string-append rules.d "/" #$file-name)
  1499. (lambda (port)
  1500. (display #$contents port)))))))
  1501. (define (file->udev-rule file-name file)
  1502. "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
  1503. (computed-file file-name
  1504. (with-imported-modules '((guix build utils))
  1505. #~(begin
  1506. (use-modules (guix build utils))
  1507. (define rules.d
  1508. (string-append #$output "/lib/udev/rules.d"))
  1509. (define file-copy-dest
  1510. (string-append rules.d "/" #$file-name))
  1511. (mkdir-p rules.d)
  1512. (copy-file #$file file-copy-dest)))))
  1513. (define kvm-udev-rule
  1514. ;; Return a directory with a udev rule that changes the group of /dev/kvm to
  1515. ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
  1516. ;; but now we have to add it by ourselves.
  1517. ;; Build users are part of the "kvm" group, so we can fearlessly make
  1518. ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
  1519. (udev-rule "90-kvm.rules"
  1520. "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
  1521. (define udev-shepherd-service
  1522. ;; Return a <shepherd-service> for UDEV with RULES.
  1523. (match-lambda
  1524. (($ <udev-configuration> udev rules)
  1525. (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
  1526. (udev.conf (computed-file "udev.conf"
  1527. #~(call-with-output-file #$output
  1528. (lambda (port)
  1529. (format port
  1530. "udev_rules=\"~a/lib/udev/rules.d\"\n"
  1531. #$rules))))))
  1532. (list
  1533. (shepherd-service
  1534. (provision '(udev))
  1535. ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
  1536. ;; be added: see
  1537. ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
  1538. (requirement '(root-file-system))
  1539. (documentation "Populate the /dev directory, dynamically.")
  1540. (start #~(lambda ()
  1541. (define find
  1542. (@ (srfi srfi-1) find))
  1543. (define udevd
  1544. ;; Choose the right 'udevd'.
  1545. (find file-exists?
  1546. (map (lambda (suffix)
  1547. (string-append #$udev suffix))
  1548. '("/libexec/udev/udevd" ;udev
  1549. "/sbin/udevd")))) ;eudev
  1550. (define (wait-for-udevd)
  1551. ;; Wait until someone's listening on udevd's control
  1552. ;; socket.
  1553. (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
  1554. (let try ()
  1555. (catch 'system-error
  1556. (lambda ()
  1557. (connect sock PF_UNIX "/run/udev/control")
  1558. (close-port sock))
  1559. (lambda args
  1560. (format #t "waiting for udevd...~%")
  1561. (usleep 500000)
  1562. (try))))))
  1563. ;; Allow udev to find the modules.
  1564. (setenv "LINUX_MODULE_DIRECTORY"
  1565. "/run/booted-system/kernel/lib/modules")
  1566. ;; The first one is for udev, the second one for eudev.
  1567. (setenv "UDEV_CONFIG_FILE" #$udev.conf)
  1568. (setenv "EUDEV_RULES_DIRECTORY"
  1569. #$(file-append rules "/lib/udev/rules.d"))
  1570. (let* ((kernel-release
  1571. (utsname:release (uname)))
  1572. (linux-module-directory
  1573. (getenv "LINUX_MODULE_DIRECTORY"))
  1574. (directory
  1575. (string-append linux-module-directory "/"
  1576. kernel-release))
  1577. (old-umask (umask #o022)))
  1578. (make-static-device-nodes directory)
  1579. (umask old-umask))
  1580. (let ((pid (primitive-fork)))
  1581. (case pid
  1582. ((0)
  1583. (exec-command (list udevd)))
  1584. (else
  1585. ;; Wait until udevd is up and running. This
  1586. ;; appears to be needed so that the events
  1587. ;; triggered below are actually handled.
  1588. (wait-for-udevd)
  1589. ;; Trigger device node creation.
  1590. (system* #$(file-append udev "/bin/udevadm")
  1591. "trigger" "--action=add")
  1592. ;; Wait for things to settle down.
  1593. (system* #$(file-append udev "/bin/udevadm")
  1594. "settle")
  1595. pid)))))
  1596. (stop #~(make-kill-destructor))
  1597. ;; When halting the system, 'udev' is actually killed by
  1598. ;; 'user-processes', i.e., before its own 'stop' method was called.
  1599. ;; Thus, make sure it is not respawned.
  1600. (respawn? #f)
  1601. ;; We need additional modules.
  1602. (modules `((gnu build linux-boot)
  1603. ,@%default-modules))))))))
  1604. (define udev-service-type
  1605. (service-type (name 'udev)
  1606. (extensions
  1607. (list (service-extension shepherd-root-service-type
  1608. udev-shepherd-service)))
  1609. (compose concatenate) ;concatenate the list of rules
  1610. (extend (lambda (config rules)
  1611. (match config
  1612. (($ <udev-configuration> udev initial-rules)
  1613. (udev-configuration
  1614. (udev udev)
  1615. (rules (append initial-rules rules)))))))
  1616. (description
  1617. "Run @command{udev}, which populates the @file{/dev}
  1618. directory dynamically. Get extra rules from the packages listed in the
  1619. @code{rules} field of its value, @code{udev-configuration} object.")))
  1620. (define* (udev-service #:key (udev eudev) (rules '()))
  1621. "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
  1622. extra rules from the packages listed in @var{rules}."
  1623. (service udev-service-type
  1624. (udev-configuration (udev udev) (rules rules))))
  1625. (define swap-service-type
  1626. (shepherd-service-type
  1627. 'swap
  1628. (lambda (device)
  1629. (define requirement
  1630. (if (string-prefix? "/dev/mapper/" device)
  1631. (list (symbol-append 'device-mapping-
  1632. (string->symbol (basename device))))
  1633. '()))
  1634. (shepherd-service
  1635. (provision (list (symbol-append 'swap- (string->symbol device))))
  1636. (requirement `(udev ,@requirement))
  1637. (documentation "Enable the given swap device.")
  1638. (start #~(lambda ()
  1639. (restart-on-EINTR (swapon #$device))
  1640. #t))
  1641. (stop #~(lambda _
  1642. (restart-on-EINTR (swapoff #$device))
  1643. #f))
  1644. (respawn? #f)))))
  1645. (define (swap-service device)
  1646. "Return a service that uses @var{device} as a swap device."
  1647. (service swap-service-type device))
  1648. (define-record-type* <gpm-configuration>
  1649. gpm-configuration make-gpm-configuration gpm-configuration?
  1650. (gpm gpm-configuration-gpm) ;package
  1651. (options gpm-configuration-options)) ;list of strings
  1652. (define gpm-shepherd-service
  1653. (match-lambda
  1654. (($ <gpm-configuration> gpm options)
  1655. (list (shepherd-service
  1656. (requirement '(udev))
  1657. (provision '(gpm))
  1658. (start #~(lambda ()
  1659. ;; 'gpm' runs in the background and sets a PID file.
  1660. ;; Note that it requires running as "root".
  1661. (false-if-exception (delete-file "/var/run/gpm.pid"))
  1662. (fork+exec-command (list #$(file-append gpm "/sbin/gpm")
  1663. #$@options))
  1664. ;; Wait for the PID file to appear; declare failure if
  1665. ;; it doesn't show up.
  1666. (let loop ((i 3))
  1667. (or (file-exists? "/var/run/gpm.pid")
  1668. (if (zero? i)
  1669. #f
  1670. (begin
  1671. (sleep 1)
  1672. (loop (1- i))))))))
  1673. (stop #~(lambda (_)
  1674. ;; Return #f if successfully stopped.
  1675. (not (zero? (system* #$(file-append gpm "/sbin/gpm")
  1676. "-k"))))))))))
  1677. (define gpm-service-type
  1678. (service-type (name 'gpm)
  1679. (extensions
  1680. (list (service-extension shepherd-root-service-type
  1681. gpm-shepherd-service)))
  1682. (description
  1683. "Run GPM, the general-purpose mouse daemon, with the given
  1684. command-line options. GPM allows users to use the mouse in the console,
  1685. notably to select, copy, and paste text. The default options use the
  1686. @code{ps2} protocol, which works for both USB and PS/2 mice.")))
  1687. (define* (gpm-service #:key (gpm gpm)
  1688. (options '("-m" "/dev/input/mice" "-t" "ps2")))
  1689. "Run @var{gpm}, the general-purpose mouse daemon, with the given
  1690. command-line @var{options}. GPM allows users to use the mouse in the console,
  1691. notably to select, copy, and paste text. The default value of @var{options}
  1692. uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
  1693. This service is not part of @var{%base-services}."
  1694. ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
  1695. ;; "info mice" and "mouse_set X" to use the right mouse.
  1696. (service gpm-service-type
  1697. (gpm-configuration (gpm gpm) (options options))))
  1698. (define-record-type* <kmscon-configuration>
  1699. kmscon-configuration make-kmscon-configuration
  1700. kmscon-configuration?
  1701. (kmscon kmscon-configuration-kmscon
  1702. (default kmscon))
  1703. (virtual-terminal kmscon-configuration-virtual-terminal)
  1704. (login-program kmscon-configuration-login-program
  1705. (default (file-append shadow "/bin/login")))
  1706. (login-arguments kmscon-configuration-login-arguments
  1707. (default '("-p")))
  1708. (hardware-acceleration? kmscon-configuration-hardware-acceleration?
  1709. (default #f))) ; #t causes failure
  1710. (define kmscon-service-type
  1711. (shepherd-service-type
  1712. 'kmscon
  1713. (lambda (config)
  1714. (let ((kmscon (kmscon-configuration-kmscon config))
  1715. (virtual-terminal (kmscon-configuration-virtual-terminal config))
  1716. (login-program (kmscon-configuration-login-program config))
  1717. (login-arguments (kmscon-configuration-login-arguments config))
  1718. (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config)))
  1719. (define kmscon-command
  1720. #~(list
  1721. #$(file-append kmscon "/bin/kmscon") "--login"
  1722. "--vt" #$virtual-terminal
  1723. #$@(if hardware-acceleration? '("--hwaccel") '())
  1724. "--" #$login-program #$@login-arguments))
  1725. (shepherd-service
  1726. (documentation "kmscon virtual terminal")
  1727. (requirement '(user-processes udev dbus-system))
  1728. (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
  1729. (start #~(make-forkexec-constructor #$kmscon-command))
  1730. (stop #~(make-kill-destructor)))))))
  1731. (define %base-services
  1732. ;; Convenience variable holding the basic services.
  1733. (list (login-service)
  1734. (service console-font-service-type
  1735. (map (lambda (tty)
  1736. (cons tty %default-console-font))
  1737. '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
  1738. (mingetty-service (mingetty-configuration
  1739. (tty "tty1")))
  1740. (mingetty-service (mingetty-configuration
  1741. (tty "tty2")))
  1742. (mingetty-service (mingetty-configuration
  1743. (tty "tty3")))
  1744. (mingetty-service (mingetty-configuration
  1745. (tty "tty4")))
  1746. (mingetty-service (mingetty-configuration
  1747. (tty "tty5")))
  1748. (mingetty-service (mingetty-configuration
  1749. (tty "tty6")))
  1750. (service static-networking-service-type
  1751. (list (static-networking (interface "lo")
  1752. (ip "127.0.0.1")
  1753. (requirement '())
  1754. (provision '(loopback)))))
  1755. (syslog-service)
  1756. (service urandom-seed-service-type)
  1757. (guix-service)
  1758. (nscd-service)
  1759. ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
  1760. ;; used, so enable them by default. The FUSE and ALSA rules are
  1761. ;; less critical, but handy.
  1762. (udev-service #:rules (list lvm2 fuse alsa-utils crda))
  1763. (service special-files-service-type
  1764. `(("/bin/sh" ,(file-append (canonical-package bash)
  1765. "/bin/sh"))))))
  1766. ;;; base.scm ends here