base.scm 100 KB

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