system.scm 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
  5. ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
  6. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  7. ;;; Copyright © 2019 Meiyo Peng <meiyo.peng@gmail.com>
  8. ;;; Copyright © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
  9. ;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
  10. ;;; Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
  11. ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
  12. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  13. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <jannek@gnu.org>
  14. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  15. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  16. ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
  17. ;;;
  18. ;;; This file is part of GNU Guix.
  19. ;;;
  20. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  21. ;;; under the terms of the GNU General Public License as published by
  22. ;;; the Free Software Foundation; either version 3 of the License, or (at
  23. ;;; your option) any later version.
  24. ;;;
  25. ;;; GNU Guix is distributed in the hope that it will be useful, but
  26. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. ;;; GNU General Public License for more details.
  29. ;;;
  30. ;;; You should have received a copy of the GNU General Public License
  31. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  32. (define-module (gnu system)
  33. #:use-module (guix inferior)
  34. #:use-module (guix store)
  35. #:use-module (guix monads)
  36. #:use-module (guix gexp)
  37. #:use-module (guix records)
  38. #:use-module (guix packages)
  39. #:use-module (guix derivations)
  40. #:use-module (guix profiles)
  41. #:use-module ((guix utils) #:select (substitute-keyword-arguments))
  42. #:use-module (guix i18n)
  43. #:use-module (guix diagnostics)
  44. #:use-module (gnu packages admin)
  45. #:use-module (gnu packages base)
  46. #:use-module (gnu packages bash)
  47. #:use-module (gnu packages compression)
  48. #:use-module (gnu packages cross-base)
  49. #:use-module (gnu packages cryptsetup)
  50. #:use-module (gnu packages disk)
  51. #:use-module (gnu packages file-systems)
  52. #:use-module (gnu packages firmware)
  53. #:use-module (gnu packages gawk)
  54. #:use-module (gnu packages guile)
  55. #:use-module (gnu packages guile-xyz)
  56. #:use-module (gnu packages hurd)
  57. #:use-module (gnu packages less)
  58. #:use-module (gnu packages linux)
  59. #:use-module (gnu packages man)
  60. #:use-module (gnu packages nano)
  61. #:use-module (gnu packages nvi)
  62. #:use-module (gnu packages package-management)
  63. #:use-module (gnu packages pciutils)
  64. #:use-module (gnu packages texinfo)
  65. #:use-module (gnu packages text-editors)
  66. #:use-module (gnu packages wget)
  67. #:use-module (gnu services)
  68. #:use-module (gnu services shepherd)
  69. #:use-module (gnu services base)
  70. #:use-module (gnu bootloader)
  71. #:use-module (gnu system shadow)
  72. #:use-module (gnu system nss)
  73. #:use-module (gnu system locale)
  74. #:use-module (gnu system pam)
  75. #:use-module (gnu system linux-initrd)
  76. #:use-module (gnu system setuid)
  77. #:use-module (gnu system uuid)
  78. #:use-module (gnu system file-systems)
  79. #:use-module (gnu system mapped-devices)
  80. #:use-module (ice-9 match)
  81. #:use-module (srfi srfi-1)
  82. #:use-module (srfi srfi-26)
  83. #:use-module (srfi srfi-34)
  84. #:use-module (srfi srfi-35)
  85. #:use-module (rnrs bytevectors)
  86. #:export (operating-system
  87. operating-system?
  88. this-operating-system
  89. operating-system-bootloader
  90. operating-system-services
  91. operating-system-essential-services
  92. operating-system-default-essential-services
  93. operating-system-user-services
  94. operating-system-packages
  95. operating-system-host-name
  96. operating-system-hosts-file
  97. operating-system-hurd
  98. operating-system-kernel
  99. operating-system-kernel-file
  100. operating-system-kernel-arguments
  101. operating-system-label
  102. operating-system-default-label
  103. operating-system-initrd-modules
  104. operating-system-initrd
  105. operating-system-users
  106. operating-system-groups
  107. operating-system-issue
  108. operating-system-timezone
  109. operating-system-locale
  110. operating-system-locale-definitions
  111. operating-system-locale-libcs
  112. operating-system-mapped-devices
  113. operating-system-file-systems
  114. operating-system-store-file-system
  115. operating-system-user-mapped-devices
  116. operating-system-boot-mapped-devices
  117. operating-system-bootloader-crypto-devices
  118. operating-system-activation-script
  119. operating-system-user-accounts
  120. operating-system-shepherd-service-names
  121. operating-system-user-kernel-arguments
  122. operating-system-firmware
  123. operating-system-keyboard-layout
  124. operating-system-name-service-switch
  125. operating-system-pam-services
  126. operating-system-setuid-programs
  127. operating-system-skeletons
  128. operating-system-sudoers-file
  129. operating-system-swap-devices
  130. operating-system-kernel-loadable-modules
  131. operating-system-location
  132. operating-system-derivation
  133. operating-system-profile
  134. operating-system-bootcfg
  135. operating-system-etc-directory
  136. operating-system-locale-directory
  137. operating-system-boot-script
  138. operating-system-uuid
  139. system-linux-image-file-name
  140. operating-system-with-gc-roots
  141. operating-system-with-provenance
  142. hurd-default-essential-services
  143. boot-parameters
  144. boot-parameters?
  145. boot-parameters-label
  146. boot-parameters-root-device
  147. boot-parameters-bootloader-name
  148. boot-parameters-bootloader-menu-entries
  149. boot-parameters-store-crypto-devices
  150. boot-parameters-store-device
  151. boot-parameters-store-directory-prefix
  152. boot-parameters-store-mount-point
  153. boot-parameters-locale
  154. boot-parameters-kernel
  155. boot-parameters-kernel-arguments
  156. boot-parameters-initrd
  157. boot-parameters-multiboot-modules
  158. read-boot-parameters
  159. read-boot-parameters-file
  160. boot-parameters->menu-entry
  161. local-host-aliases
  162. %root-account
  163. %setuid-programs
  164. %sudoers-specification
  165. %base-packages
  166. %base-packages-artwork
  167. %base-packages-interactive
  168. %base-packages-linux
  169. %base-packages-networking
  170. %base-packages-disk-utilities
  171. %base-packages-utils
  172. %base-firmware
  173. %default-kernel-arguments))
  174. ;;; Commentary:
  175. ;;;
  176. ;;; This module supports whole-system configuration.
  177. ;;;
  178. ;;; Code:
  179. (define (bootable-kernel-arguments system root-device)
  180. "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
  181. (list (string-append "--root="
  182. ;; Note: Always use the DCE format because that's what
  183. ;; (gnu build linux-boot) expects for the '--root'
  184. ;; kernel command-line option.
  185. (file-system-device->string root-device
  186. #:uuid-type 'dce))
  187. #~(string-append "--system=" #$system)
  188. #~(string-append "--load=" #$system "/boot")))
  189. ;; System-wide configuration.
  190. ;; TODO: Add per-field docstrings/stexi.
  191. (define-record-type* <operating-system> operating-system
  192. make-operating-system
  193. operating-system?
  194. this-operating-system
  195. (kernel operating-system-kernel ; package
  196. (default linux-libre))
  197. (kernel-loadable-modules operating-system-kernel-loadable-modules
  198. (default '())) ; list of packages
  199. (kernel-arguments operating-system-user-kernel-arguments
  200. (default %default-kernel-arguments)) ; list of gexps/strings
  201. (hurd operating-system-hurd
  202. (default #f)) ; package
  203. (bootloader operating-system-bootloader) ; <bootloader-configuration>
  204. (label operating-system-label ; string
  205. (thunked)
  206. (default (operating-system-default-label this-operating-system)))
  207. (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
  208. (default #f))
  209. (initrd operating-system-initrd ; (list fs) -> file-like
  210. (default base-initrd))
  211. (initrd-modules operating-system-initrd-modules ; list of strings
  212. (thunked) ; it's system-dependent
  213. (default %base-initrd-modules))
  214. (firmware operating-system-firmware ; list of packages
  215. (default %base-firmware))
  216. (host-name operating-system-host-name) ; string
  217. (hosts-file operating-system-hosts-file ; file-like | #f
  218. (default #f))
  219. (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
  220. (default '()))
  221. (file-systems operating-system-file-systems) ; list of fs
  222. (swap-devices operating-system-swap-devices ; list of strings
  223. (default '()))
  224. (users operating-system-users ; list of user accounts
  225. (default %base-user-accounts))
  226. (groups operating-system-groups ; list of user groups
  227. (default %base-groups))
  228. (skeletons operating-system-skeletons ; list of name/file-like value
  229. (default (default-skeletons)))
  230. (issue operating-system-issue ; string
  231. (default %default-issue))
  232. (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
  233. (default %base-packages)) ; or just PACKAGE
  234. (timezone operating-system-timezone) ; string
  235. (locale operating-system-locale ; string
  236. (default "en_US.utf8"))
  237. (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
  238. (default %default-locale-definitions))
  239. (locale-libcs operating-system-locale-libcs ; list of <packages>
  240. (default %default-locale-libcs))
  241. (name-service-switch operating-system-name-service-switch ; <name-service-switch>
  242. (default %default-nss))
  243. (essential-services operating-system-essential-services ; list of services
  244. (thunked)
  245. (default (operating-system-default-essential-services
  246. this-operating-system)))
  247. (services operating-system-user-services ; list of services
  248. (default %base-services))
  249. (pam-services operating-system-pam-services ; list of PAM services
  250. (default (base-pam-services)))
  251. (setuid-programs operating-system-setuid-programs
  252. (default %setuid-programs) ; list of <setuid-program>
  253. (sanitize ensure-setuid-program-list))
  254. (sudoers-file operating-system-sudoers-file ; file-like
  255. (default %sudoers-specification))
  256. (location operating-system-location ; <location>
  257. (default (and=> (current-source-location)
  258. source-properties->location))
  259. (innate)))
  260. (define (operating-system-kernel-arguments os root-device)
  261. "Return all the kernel arguments, including the ones not specified
  262. directly by the user."
  263. (append (bootable-kernel-arguments os root-device)
  264. (operating-system-user-kernel-arguments os)))
  265. ;;;
  266. ;;; Boot parameters
  267. ;;;
  268. (define-record-type* <boot-parameters>
  269. boot-parameters make-boot-parameters boot-parameters?
  270. (label boot-parameters-label)
  271. ;; Because we will use the 'store-device' to create the GRUB search command,
  272. ;; the 'store-device' has slightly different semantics than 'root-device'.
  273. ;; The 'store-device' can be a file system uuid, a file system label, or #f,
  274. ;; but it cannot be a device file name such as "/dev/sda3", since GRUB would
  275. ;; not understand that. The 'root-device', on the other hand, corresponds
  276. ;; exactly to the device field of the <file-system> object representing the
  277. ;; OS's root file system, so it might be a device file name like
  278. ;; "/dev/sda3". The 'store-directory-prefix' field contains #f or the store
  279. ;; file name inside the 'store-device' as it is seen by GRUB, e.g. it would
  280. ;; contain "/storefs" if the store is located in that subvolume of a btrfs
  281. ;; partition.
  282. (root-device boot-parameters-root-device)
  283. (bootloader-name boot-parameters-bootloader-name)
  284. (bootloader-menu-entries ;list of <menu-entry>
  285. boot-parameters-bootloader-menu-entries)
  286. (store-device boot-parameters-store-device)
  287. (store-mount-point boot-parameters-store-mount-point)
  288. (store-directory-prefix boot-parameters-store-directory-prefix)
  289. (store-crypto-devices boot-parameters-store-crypto-devices
  290. (default '()))
  291. (locale boot-parameters-locale)
  292. (kernel boot-parameters-kernel)
  293. (kernel-arguments boot-parameters-kernel-arguments)
  294. (initrd boot-parameters-initrd)
  295. (multiboot-modules boot-parameters-multiboot-modules))
  296. (define (ensure-not-/dev device)
  297. "If DEVICE starts with a slash, return #f. This is meant to filter out
  298. Linux device names such as /dev/sda, and to preserve GRUB device names and
  299. file system labels."
  300. (if (and (string? device) (string-prefix? "/" device))
  301. #f
  302. device))
  303. (define (read-boot-parameters port)
  304. "Read boot parameters from PORT and return the corresponding
  305. <boot-parameters> object or #f if the format is unrecognized."
  306. (define device-sexp->device
  307. (match-lambda
  308. (('uuid (? symbol? type) (? bytevector? bv))
  309. (bytevector->uuid bv type))
  310. (('file-system-label (? string? label))
  311. (file-system-label label))
  312. ((? bytevector? bv) ;old format
  313. (bytevector->uuid bv 'dce))
  314. ((? string? device)
  315. (if (string-contains device ":/")
  316. device ; nfs-root
  317. ;; It used to be that we would not distinguish between labels and
  318. ;; device names. Try to infer the right thing here.
  319. (if (string-prefix? "/" device)
  320. device
  321. (file-system-label device))))))
  322. (define uuid-sexp->uuid
  323. (match-lambda
  324. (('uuid (? symbol? type) (? bytevector? bv))
  325. (bytevector->uuid bv type))
  326. (x
  327. (warning (G_ "unrecognized uuid ~a at '~a'~%") x (port-filename port))
  328. #f)))
  329. (match (read port)
  330. (('boot-parameters ('version 0)
  331. ('label label) ('root-device root)
  332. ('kernel kernel)
  333. rest ...)
  334. (boot-parameters
  335. (label label)
  336. (root-device (device-sexp->device root))
  337. (bootloader-name
  338. (match (assq 'bootloader-name rest)
  339. ((_ args) args)
  340. (#f 'grub))) ; for compatibility reasons.
  341. (bootloader-menu-entries
  342. (match (assq 'bootloader-menu-entries rest)
  343. ((_ entries) (map sexp->menu-entry entries))
  344. (#f '())))
  345. ;; In the past, we would store the directory name of linux instead of
  346. ;; the absolute file name of its image. Detect that and correct it.
  347. (kernel (if (string=? kernel (direct-store-path kernel))
  348. (string-append kernel "/"
  349. (system-linux-image-file-name))
  350. kernel))
  351. (kernel-arguments
  352. (match (assq 'kernel-arguments rest)
  353. ((_ args) args)
  354. (#f '()))) ;the old format
  355. (initrd
  356. (match (assq 'initrd rest)
  357. (('initrd ('string-append directory file)) ;the old format
  358. (string-append directory file))
  359. (('initrd (? string? file))
  360. file)
  361. (#f #f)))
  362. (multiboot-modules
  363. (match (assq 'multiboot-modules rest)
  364. ((_ args) args)
  365. (#f '())))
  366. (locale
  367. (match (assq 'locale rest)
  368. ((_ locale) locale)
  369. (#f #f)))
  370. (store-device
  371. ;; Linux device names like "/dev/sda1" are not suitable GRUB device
  372. ;; identifiers, so we just filter them out.
  373. (ensure-not-/dev
  374. (match (assq 'store rest)
  375. (('store ('device #f) _ ...)
  376. root-device)
  377. (('store ('device device) _ ...)
  378. (device-sexp->device device))
  379. (_ ;the old format
  380. root-device))))
  381. (store-directory-prefix
  382. (match (assq 'store rest)
  383. (('store . store-data)
  384. (match (assq 'directory-prefix store-data)
  385. (('directory-prefix prefix) prefix)
  386. ;; No directory-prefix found.
  387. (_ #f)))
  388. (_
  389. ;; No store found, old format.
  390. #f)))
  391. (store-crypto-devices
  392. (match (assq 'store rest)
  393. (('store . store-data)
  394. (match (assq 'crypto-devices store-data)
  395. (('crypto-devices (devices ...))
  396. (map uuid-sexp->uuid devices))
  397. (('crypto-devices dev)
  398. (warning (G_ "unrecognized crypto-devices ~S at '~a'~%")
  399. dev (port-filename port))
  400. '())
  401. (_
  402. ;; No crypto-devices found.
  403. '())))
  404. (_
  405. ;; No store found, old format.
  406. '())))
  407. (store-mount-point
  408. (match (assq 'store rest)
  409. (('store ('device _) ('mount-point mount-point) _ ...)
  410. mount-point)
  411. (_ ;the old format
  412. "/")))))
  413. (x ;unsupported format
  414. (warning (G_ "unrecognized boot parameters at '~a'~%")
  415. (port-filename port))
  416. #f)))
  417. (define (read-boot-parameters-file system)
  418. "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
  419. file and returns the corresponding <boot-parameters> object or #f if the
  420. format is unrecognized.
  421. The object has its kernel-arguments extended in order to make it bootable."
  422. (let* ((file (string-append system "/parameters"))
  423. (params (call-with-input-file file read-boot-parameters))
  424. (root (boot-parameters-root-device params)))
  425. (boot-parameters
  426. (inherit params)
  427. (kernel-arguments (append (bootable-kernel-arguments system root)
  428. (boot-parameters-kernel-arguments params))))))
  429. (define (boot-parameters->menu-entry conf)
  430. (let* ((kernel (boot-parameters-kernel conf))
  431. (multiboot-modules (boot-parameters-multiboot-modules conf))
  432. (multiboot? (pair? multiboot-modules)))
  433. (menu-entry
  434. (label (boot-parameters-label conf))
  435. (device (boot-parameters-store-device conf))
  436. (device-mount-point (boot-parameters-store-mount-point conf))
  437. (linux (and (not multiboot?) kernel))
  438. (linux-arguments (if (not multiboot?)
  439. (boot-parameters-kernel-arguments conf)
  440. '()))
  441. (initrd (boot-parameters-initrd conf))
  442. (multiboot-kernel (and multiboot? kernel))
  443. (multiboot-arguments (if multiboot?
  444. (boot-parameters-kernel-arguments conf)
  445. '()))
  446. (multiboot-modules (if multiboot?
  447. (boot-parameters-multiboot-modules conf)
  448. '())))))
  449. ;;;
  450. ;;; Services.
  451. ;;;
  452. (define (non-boot-file-system-service os)
  453. "Return the file system service for the file systems of OS that are not
  454. marked as 'needed-for-boot'."
  455. (define file-systems
  456. (remove file-system-needed-for-boot?
  457. (operating-system-file-systems os)))
  458. (define mapped-devices-for-boot
  459. (operating-system-boot-mapped-devices os))
  460. (define (device-mappings fs)
  461. (let ((device (file-system-device fs)))
  462. (if (string? device) ;title is 'device
  463. (filter (lambda (md)
  464. (any (cut string=? device <>)
  465. (map (cut string-append "/dev/mapper" <>)
  466. (mapped-device-targets md))))
  467. (operating-system-mapped-devices os))
  468. '())))
  469. (define (add-dependencies fs)
  470. ;; Add the dependencies due to device mappings to FS.
  471. (file-system
  472. (inherit fs)
  473. (dependencies
  474. (delete-duplicates
  475. (remove (cut member <> mapped-devices-for-boot)
  476. (append (device-mappings fs)
  477. (file-system-dependencies fs)))
  478. eq?))))
  479. (service file-system-service-type
  480. (map add-dependencies file-systems)))
  481. (define (mapped-device-users device file-systems)
  482. "Return the subset of FILE-SYSTEMS that use DEVICE."
  483. (let ((targets (map (cut string-append "/dev/mapper/" <>)
  484. (mapped-device-targets device))))
  485. (filter (lambda (fs)
  486. (or (member device (file-system-dependencies fs))
  487. (and (string? (file-system-device fs))
  488. (any (cut string=? (file-system-device fs) <>) targets))))
  489. file-systems)))
  490. (define (operating-system-user-mapped-devices os)
  491. "Return the subset of mapped devices that can be installed in
  492. user-land--i.e., those not needed during boot."
  493. (let ((devices (operating-system-mapped-devices os))
  494. (file-systems (operating-system-file-systems os)))
  495. (filter (lambda (md)
  496. (let ((users (mapped-device-users md file-systems)))
  497. (not (any file-system-needed-for-boot? users))))
  498. devices)))
  499. (define (operating-system-boot-mapped-devices os)
  500. "Return the subset of mapped devices that must be installed during boot,
  501. from the initrd."
  502. (let ((devices (operating-system-mapped-devices os))
  503. (file-systems (operating-system-file-systems os)))
  504. (filter (lambda (md)
  505. (let ((users (mapped-device-users md file-systems)))
  506. (any file-system-needed-for-boot? users)))
  507. devices)))
  508. (define (operating-system-bootloader-crypto-devices os)
  509. "Return the subset of mapped devices that the bootloader must open.
  510. Only devices specified by uuid are supported."
  511. (define (valid-crypto-device? dev)
  512. (or (uuid? dev)
  513. (begin
  514. (warning (G_ "\
  515. mapped-device '~a' may not be mounted by the bootloader.~%")
  516. dev)
  517. #f)))
  518. (filter-map (match-lambda
  519. ((and (= mapped-device-type type)
  520. (= mapped-device-source source))
  521. (and (eq? luks-device-mapping type)
  522. (valid-crypto-device? source)
  523. source))
  524. (_ #f))
  525. ;; XXX: Ordering is important, we trust the returned one.
  526. (operating-system-boot-mapped-devices os)))
  527. (define (device-mapping-services os)
  528. "Return the list of device-mapping services for OS as a list."
  529. (map device-mapping-service
  530. (operating-system-user-mapped-devices os)))
  531. (define (swap-services os)
  532. "Return the list of swap services for OS."
  533. (map swap-service (operating-system-swap-devices os)))
  534. (define* (system-linux-image-file-name #:optional
  535. (target (or (%current-target-system)
  536. (%current-system))))
  537. "Return the basename of the kernel image file for TARGET."
  538. (cond
  539. ((string-prefix? "arm" target) "zImage")
  540. ((string-prefix? "mips" target) "vmlinuz")
  541. ((string-prefix? "aarch64" target) "Image")
  542. (else "bzImage")))
  543. (define (operating-system-kernel-file os)
  544. "Return an object representing the absolute file name of the kernel image of
  545. OS."
  546. (if (operating-system-hurd os)
  547. (file-append (operating-system-kernel os) "/boot/gnumach")
  548. (file-append (operating-system-kernel os)
  549. "/" (system-linux-image-file-name))))
  550. (define %default-modprobe-blacklist
  551. ;; List of kernel modules to blacklist by default.
  552. '("usbmouse" ;races with bcm5974, see <https://bugs.gnu.org/35574>
  553. "usbkbd")) ;races with usbhid, see <https://issues.guix.gnu.org/35574#18>
  554. (define %default-kernel-arguments
  555. ;; Default arguments passed to the kernel.
  556. (list (string-append "modprobe.blacklist="
  557. (string-join %default-modprobe-blacklist ","))
  558. "quiet"))
  559. (define* (operating-system-directory-base-entries os)
  560. "Return the basic entries of the 'system' directory of OS for use as the
  561. value of the SYSTEM-SERVICE-TYPE service."
  562. (let* ((locale (operating-system-locale-directory os))
  563. (kernel (operating-system-kernel os))
  564. (hurd (operating-system-hurd os))
  565. (initrd (and (not hurd) (operating-system-initrd-file os)))
  566. (params (operating-system-boot-parameters-file os)))
  567. `(,@(if hurd
  568. `(("hurd" ,hurd)
  569. ("kernel" ,kernel))
  570. '())
  571. ("parameters" ,params)
  572. ,@(if initrd `(("initrd" ,initrd)) '())
  573. ("locale" ,locale)))) ;used by libc
  574. (define (operating-system-default-essential-services os)
  575. "Return the list of essential services for OS. These are special services
  576. that implement part of what's declared in OS are responsible for low-level
  577. bookkeeping."
  578. (define known-fs
  579. (map file-system-mount-point (operating-system-file-systems os)))
  580. (let* ((mappings (device-mapping-services os))
  581. (root-fs (root-file-system-service))
  582. (other-fs (non-boot-file-system-service os))
  583. (swaps (swap-services os))
  584. (procs (service user-processes-service-type))
  585. (host-name (host-name-service (operating-system-host-name os)))
  586. (entries (operating-system-directory-base-entries os)))
  587. (cons* (service system-service-type entries)
  588. (service linux-builder-service-type
  589. (linux-builder-configuration
  590. (kernel (operating-system-kernel os))
  591. (modules (operating-system-kernel-loadable-modules os))))
  592. %boot-service
  593. ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
  594. ;; execs shepherd comes last in the boot script (XXX). Likewise,
  595. ;; the cleanup service must come first so that its gexp runs before
  596. ;; activation code.
  597. (service cleanup-service-type #f)
  598. %activation-service
  599. %shepherd-root-service
  600. (pam-root-service (operating-system-pam-services os))
  601. (account-service (append (operating-system-accounts os)
  602. (operating-system-groups os))
  603. (operating-system-skeletons os))
  604. (operating-system-etc-service os)
  605. (service fstab-service-type
  606. (filter file-system-needed-for-boot?
  607. (operating-system-file-systems os)))
  608. (session-environment-service
  609. (operating-system-environment-variables os))
  610. host-name procs root-fs
  611. (service setuid-program-service-type
  612. (operating-system-setuid-programs os))
  613. (service profile-service-type
  614. (operating-system-packages os))
  615. other-fs
  616. (append mappings swaps
  617. ;; Add the firmware service.
  618. (list %linux-bare-metal-service
  619. (service firmware-service-type
  620. (operating-system-firmware os)))))))
  621. (define (hurd-default-essential-services os)
  622. (let ((entries (operating-system-directory-base-entries os)))
  623. (list (service system-service-type entries)
  624. %boot-service
  625. %hurd-startup-service
  626. %activation-service
  627. %shepherd-root-service
  628. (service user-processes-service-type)
  629. (account-service (append (operating-system-accounts os)
  630. (operating-system-groups os))
  631. (operating-system-skeletons os))
  632. (root-file-system-service)
  633. (service file-system-service-type '())
  634. (service fstab-service-type
  635. (filter file-system-needed-for-boot?
  636. (operating-system-file-systems os)))
  637. (pam-root-service (operating-system-pam-services os))
  638. (operating-system-etc-service os)
  639. (service setuid-program-service-type
  640. (operating-system-setuid-programs os))
  641. (service profile-service-type (operating-system-packages os)))))
  642. (define* (operating-system-services os)
  643. "Return all the services of OS, including \"essential\" services."
  644. (instantiate-missing-services
  645. (append (operating-system-user-services os)
  646. (operating-system-essential-services os))))
  647. (define (operating-system-with-gc-roots os roots)
  648. "Return a variant of OS where ROOTS are registered as GC roots."
  649. (operating-system
  650. (inherit os)
  651. ;; We use this procedure for the installation OS, which already defines GC
  652. ;; roots. Add ROOTS to those.
  653. (services (cons (simple-service 'extra-root
  654. gc-root-service-type roots)
  655. (operating-system-user-services os)))))
  656. (define (operating-system-configuration-file os)
  657. "Return the configuration file of OS, based on its 'location' field, or #f
  658. if it could not be determined."
  659. (let ((file (and=> (operating-system-location os)
  660. location-file)))
  661. (and file
  662. (or (and (string-prefix? "/" file) file)
  663. (search-path %load-path file)))))
  664. (define* (operating-system-with-provenance os
  665. #:optional
  666. (config-file
  667. (operating-system-configuration-file
  668. os)))
  669. "Return a variant of OS that stores its own provenance information,
  670. including CONFIG-FILE, if available. This is achieved by adding an instance
  671. of PROVENANCE-SERVICE-TYPE to its services."
  672. (operating-system
  673. (inherit os)
  674. (services (cons (service provenance-service-type config-file)
  675. (operating-system-user-services os)))))
  676. ;;;
  677. ;;; /etc.
  678. ;;;
  679. (define %base-firmware
  680. ;; Firmware usable by default.
  681. (list ath9k-htc-firmware
  682. openfwwf-firmware))
  683. (define %base-packages-artwork
  684. ;; Default set of artwork packages.
  685. (list guix-icons))
  686. (define %base-packages-utils
  687. ;; Default set of utilities packages.
  688. (cons* procps psmisc which
  689. (@ (gnu packages admin) shadow) ;for 'passwd'
  690. guile-3.0-latest
  691. ;; The packages below are also in %FINAL-INPUTS, so take them from
  692. ;; there to avoid duplication.
  693. (list bash coreutils findutils grep sed
  694. diffutils patch gawk tar gzip bzip2 xz lzip)))
  695. (define %base-packages-linux
  696. ;; Default set of linux specific packages.
  697. (list pciutils usbutils
  698. util-linux+udev
  699. ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
  700. ;; already depends on it anyway.
  701. kmod eudev))
  702. (define %base-packages-interactive
  703. ;; Default set of common interactive packages.
  704. (list less mg nano
  705. nvi
  706. man-db
  707. info-reader ;the standalone Info reader (no Perl)
  708. bash-completion
  709. kbd
  710. ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
  711. ;; want the other commands and the man pages (notably because
  712. ;; auto-completion in Emacs shell relies on man pages.)
  713. sudo
  714. guile-readline guile-colorized))
  715. (define %base-packages-networking
  716. ;; Default set of networking packages.
  717. (list inetutils isc-dhcp
  718. iproute
  719. wget
  720. ;; wireless-tools is deprecated in favor of iw, but it's still what
  721. ;; many people are familiar with, so keep it around.
  722. iw wireless-tools))
  723. (define %base-packages-disk-utilities
  724. ;; A well-rounded set of packages for interacting with disks, partitions
  725. ;; and filesystems.
  726. (list parted gptfdisk ddrescue
  727. ;; We used to provide fdisk from GNU fdisk, but as of version 2.0.0a
  728. ;; it pulls Guile 1.8, which takes unreasonable space; furthermore
  729. ;; util-linux's fdisk is already available, in %base-packages-linux.
  730. cryptsetup mdadm
  731. dosfstools
  732. btrfs-progs
  733. f2fs-tools
  734. jfsutils))
  735. (define %base-packages
  736. ;; Default set of packages globally visible. It should include anything
  737. ;; required for basic administrator tasks.
  738. (append (list e2fsprogs)
  739. %base-packages-artwork
  740. %base-packages-interactive
  741. %base-packages-linux
  742. %base-packages-networking
  743. %base-packages-utils))
  744. (define %default-issue
  745. ;; Default contents for /etc/issue.
  746. "
  747. This is the GNU system. Welcome.\n")
  748. (define (local-host-aliases host-name)
  749. "Return aliases for HOST-NAME, to be used in /etc/hosts."
  750. (string-append "127.0.0.1 localhost " host-name "\n"
  751. "::1 localhost " host-name "\n"))
  752. (define (default-/etc/hosts host-name)
  753. "Return the default /etc/hosts file."
  754. (plain-file "hosts" (local-host-aliases host-name)))
  755. (define (validated-sudoers-file file)
  756. "Return a copy of FILE, a sudoers file, after checking that it is
  757. syntactically correct."
  758. (computed-file "sudoers"
  759. (with-imported-modules '((guix build utils))
  760. #~(begin
  761. (use-modules (guix build utils))
  762. (invoke #+(file-append sudo "/sbin/visudo")
  763. "--check" "--file" #$file)
  764. (copy-file #$file #$output)))))
  765. (define (os-release)
  766. (plain-file "os-release"
  767. "\
  768. NAME=\"Guix System\"
  769. ID=guix
  770. PRETTY_NAME=\"Guix System\"
  771. LOGO=guix-icon
  772. HOME_URL=\"https://guix.gnu.org\"
  773. DOCUMENTATION_URL=\"https://guix.gnu.org/en/manual\"
  774. SUPPORT_URL=\"https://guix.gnu.org/en/help\"
  775. BUG_REPORT_URL=\"https://lists.gnu.org/mailman/listinfo/bug-guix\"
  776. "))
  777. (define* (operating-system-etc-service os)
  778. "Return a <service> that builds a directory containing the static part of
  779. the /etc directory."
  780. (let* ((login.defs
  781. (plain-file "login.defs"
  782. (string-append
  783. "# Default paths for non-login shells started by su(1).\n"
  784. "ENV_PATH /run/setuid-programs:"
  785. "/run/current-system/profile/bin:"
  786. "/run/current-system/profile/sbin\n"
  787. "ENV_SUPATH /run/setuid-programs:"
  788. "/run/current-system/profile/bin:"
  789. "/run/current-system/profile/sbin\n")))
  790. (hurd (operating-system-hurd os))
  791. (issue (plain-file "issue" (operating-system-issue os)))
  792. (nsswitch (operating-system-name-service-switch os))
  793. (nsswitch (and nsswitch
  794. (plain-file "nsswitch.conf"
  795. (name-service-switch->string nsswitch))))
  796. (sudoers (operating-system-sudoers-file os))
  797. ;; Startup file for POSIX-compliant login shells, which set system-wide
  798. ;; environment variables.
  799. (profile (mixed-text-file "profile" "\
  800. # Crucial variables that could be missing in the profiles' 'etc/profile'
  801. # because they would require combining both profiles.
  802. # FIXME: See <http://bugs.gnu.org/20255>.
  803. export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
  804. export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
  805. export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
  806. export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
  807. # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
  808. export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
  809. # Ignore the default value of 'PATH'.
  810. unset PATH
  811. # Load the system profile's settings.
  812. GUIX_PROFILE=/run/current-system/profile ; \\
  813. . /run/current-system/profile/etc/profile
  814. # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
  815. # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
  816. # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
  817. # reading the user's 'etc/profile' to allow variables to be overridden.
  818. if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
  819. -a -z \"$LINUX_MODULE_DIRECTORY\" ]
  820. then
  821. . /etc/environment
  822. export `cat /etc/environment | cut -d= -f1`
  823. fi
  824. # Arrange so that ~/.config/guix/current comes first.
  825. for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
  826. do
  827. if [ -f \"$profile/etc/profile\" ]
  828. then
  829. # Load the user profile's settings.
  830. GUIX_PROFILE=\"$profile\" ; \\
  831. . \"$profile/etc/profile\"
  832. else
  833. # At least define this one so that basic things just work
  834. # when the user installs their first package.
  835. export PATH=\"$profile/bin:$PATH\"
  836. fi
  837. done
  838. # Prepend setuid programs.
  839. export PATH=/run/setuid-programs:$PATH
  840. # Arrange so that ~/.config/guix/current/share/info comes first.
  841. export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
  842. # Set the umask, notably for users logging in via 'lsh'.
  843. # See <http://bugs.gnu.org/22650>.
  844. umask 022
  845. # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
  846. # find dictionaries.
  847. export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
  848. # Allow GStreamer-based applications to find plugins.
  849. export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
  850. if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
  851. then
  852. # Load Bash-specific initialization code.
  853. . /etc/bashrc
  854. fi
  855. "))
  856. (bashrc (plain-file "bashrc" "\
  857. # Bash-specific initialization.
  858. # The 'bash-completion' package.
  859. if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
  860. then
  861. # Bash-completion sources ~/.bash_completion. It installs a dynamic
  862. # completion loader that searches its own completion files as well
  863. # as those in ~/.guix-profile and /run/current-system/profile.
  864. source /run/current-system/profile/etc/profile.d/bash_completion.sh
  865. fi\n")))
  866. (etc-service
  867. `(("os-release" ,#~#$(os-release))
  868. ("services" ,(file-append net-base "/etc/services"))
  869. ("protocols" ,(file-append net-base "/etc/protocols"))
  870. ("rpc" ,(file-append net-base "/etc/rpc"))
  871. ("login.defs" ,#~#$login.defs)
  872. ("issue" ,#~#$issue)
  873. ,@(if nsswitch `(("nsswitch.conf" ,#~#$nsswitch)) '())
  874. ("profile" ,#~#$profile)
  875. ("bashrc" ,#~#$bashrc)
  876. ("hosts" ,#~#$(or (operating-system-hosts-file os)
  877. (default-/etc/hosts (operating-system-host-name os))))
  878. ;; Write the operating-system-host-name to /etc/hostname to prevent
  879. ;; NetworkManager from changing the system's hostname when connecting
  880. ;; to certain networks. Some discussion at
  881. ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
  882. ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
  883. ;; Some programs (e.g., GLib) look at /etc/timezone to find the
  884. ;; name of the current timezone. For details, see
  885. ;; https://lists.gnu.org/archive/html/guix-devel/2019-07/msg00166.html
  886. ;; Some programs expect a terminating newline.
  887. ("timezone" ,(plain-file "timezone"
  888. (string-append
  889. (string-trim-both
  890. (operating-system-timezone os))
  891. "\n")))
  892. ("localtime" ,(file-append tzdata "/share/zoneinfo/"
  893. (operating-system-timezone os)))
  894. ,@(if sudoers
  895. `(("sudoers" ,(validated-sudoers-file sudoers)))
  896. '())
  897. ,@(if hurd
  898. `(("login" ,(file-append hurd "/etc/login"))
  899. ("motd" ,(file-append hurd "/etc/motd"))
  900. ("ttys" ,(file-append hurd "/etc/ttys")))
  901. '())))))
  902. (define %root-account
  903. ;; Default root account.
  904. (user-account
  905. (name "root")
  906. (password "")
  907. (uid 0) (group "root")
  908. (comment "System administrator")
  909. (home-directory "/root")))
  910. (define (operating-system-accounts os)
  911. "Return the user accounts for OS, including an obligatory 'root' account,
  912. and excluding accounts requested by services."
  913. ;; Make sure there's a root account.
  914. (if (find (lambda (user)
  915. (and=> (user-account-uid user) zero?))
  916. (operating-system-users os))
  917. (operating-system-users os)
  918. (cons %root-account (operating-system-users os))))
  919. (define (maybe-string->file file-name thing)
  920. "If THING is a string, return a <plain-file> with THING as its content.
  921. Otherwise just return THING.
  922. This is for backward-compatibility of fields that used to be strings and are
  923. now file-like objects.."
  924. (match thing
  925. ((? string?)
  926. (warning (G_ "using a string for file '~a' is deprecated; \
  927. use 'plain-file' instead~%")
  928. file-name)
  929. (plain-file file-name thing))
  930. (x
  931. x)))
  932. (define (maybe-file->monadic file-name thing)
  933. "If THING is a value in %STORE-MONAD, return it as is; otherwise return
  934. THING in the %STORE-MONAD.
  935. This is for backward-compatibility of fields that used to be monadic values
  936. and are now file-like objects."
  937. (with-monad %store-monad
  938. (match thing
  939. ((? procedure?)
  940. (warning (G_ "using a monadic value for '~a' is deprecated; \
  941. use 'plain-file' instead~%")
  942. file-name)
  943. thing)
  944. (x
  945. (return x)))))
  946. (define (operating-system-etc-directory os)
  947. "Return that static part of the /etc directory of OS."
  948. (etc-directory
  949. (fold-services (operating-system-services os)
  950. #:target-type etc-service-type)))
  951. (define (operating-system-environment-variables os)
  952. "Return the environment variables of OS for
  953. @var{session-environment-service-type}, to be used in @file{/etc/environment}."
  954. `(("LANG" . ,(operating-system-locale os))
  955. ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
  956. ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
  957. ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
  958. ;; Tell 'modprobe' & co. where to look for modules.
  959. ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
  960. ;; These variables are honored by OpenSSL (libssl) and Git.
  961. ("SSL_CERT_DIR" . "/etc/ssl/certs")
  962. ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
  963. ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
  964. ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
  965. ;; searched for.
  966. ("GTK_DATA_PREFIX" . "/run/current-system/profile")
  967. ;; By default, applications that use D-Bus, such as Emacs, abort at startup
  968. ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
  969. ("DBUS_FATAL_WARNINGS" . "0")
  970. ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
  971. ;; used to look things up in 'PREFIX/lib/locale' instead of
  972. ;; '/run/current-system/locale' as was intended. Keep this hack around so
  973. ;; that people who still have glibc@2.23-using packages in their profiles
  974. ;; can use them correctly.
  975. ;; TODO: Remove when glibc@2.23 is long gone.
  976. ("GUIX_LOCPATH" . "/run/current-system/locale")))
  977. (define-syntax-rule (ensure-setuid-program-list lst)
  978. "Ensure LST is a list of <setuid-program> records and warn otherwise."
  979. (%ensure-setuid-program-list lst (current-source-location)))
  980. (define (%ensure-setuid-program-list lst location)
  981. (define warned? #f)
  982. (define (warn-once)
  983. (unless warned?
  984. (warning (source-properties->location location)
  985. (G_ "representing setuid programs with file-like objects is \
  986. deprecated; use 'setuid-program' instead~%"))
  987. (set! warned? #t)))
  988. (map (match-lambda
  989. ((? setuid-program? program)
  990. program)
  991. (program
  992. ;; PROGRAM is a file-like or a gexp like #~(string-append #$foo
  993. ;; "/bin/bar").
  994. (warn-once)
  995. (setuid-program (program program))))
  996. lst))
  997. (define %setuid-programs
  998. ;; Default set of setuid-root programs.
  999. (let ((shadow (@ (gnu packages admin) shadow)))
  1000. (map file-like->setuid-program
  1001. (list (file-append shadow "/bin/passwd")
  1002. (file-append shadow "/bin/sg")
  1003. (file-append shadow "/bin/su")
  1004. (file-append shadow "/bin/newgrp")
  1005. (file-append shadow "/bin/newuidmap")
  1006. (file-append shadow "/bin/newgidmap")
  1007. (file-append inetutils "/bin/ping")
  1008. (file-append inetutils "/bin/ping6")
  1009. (file-append sudo "/bin/sudo")
  1010. (file-append sudo "/bin/sudoedit")
  1011. (file-append fuse "/bin/fusermount")
  1012. ;; To allow mounts with the "user" option, "mount" and "umount" must
  1013. ;; be setuid-root.
  1014. (file-append util-linux "/bin/mount")
  1015. (file-append util-linux "/bin/umount")))))
  1016. (define %sudoers-specification
  1017. ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
  1018. ;; group can do anything. See
  1019. ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
  1020. ;; TODO: Add a declarative API.
  1021. (plain-file "sudoers" "\
  1022. root ALL=(ALL) ALL
  1023. %wheel ALL=(ALL) ALL\n"))
  1024. (define* (operating-system-activation-script os)
  1025. "Return the activation script for OS---i.e., the code that \"activates\" the
  1026. stateful part of OS, including user accounts and groups, special directories,
  1027. etc."
  1028. (let* ((services (operating-system-services os))
  1029. (activation (fold-services services
  1030. #:target-type activation-service-type)))
  1031. (activation-service->script activation)))
  1032. (define* (operating-system-boot-script os)
  1033. "Return the boot script for OS---i.e., the code started by the initrd once
  1034. we're running in the final root."
  1035. (let* ((services (operating-system-services os))
  1036. (boot (fold-services services #:target-type boot-service-type)))
  1037. (service-value boot)))
  1038. (define (operating-system-user-accounts os)
  1039. "Return the list of user accounts of OS."
  1040. (let* ((services (operating-system-services os))
  1041. (account (fold-services services
  1042. #:target-type account-service-type)))
  1043. (filter user-account?
  1044. (service-value account))))
  1045. (define (operating-system-shepherd-service-names os)
  1046. "Return the list of Shepherd service names for OS."
  1047. (append-map shepherd-service-provision
  1048. (shepherd-configuration-services
  1049. (service-value
  1050. (fold-services (operating-system-services os)
  1051. #:target-type
  1052. shepherd-root-service-type)))))
  1053. (define* (operating-system-derivation os)
  1054. "Return a derivation that builds OS."
  1055. (let* ((services (operating-system-services os))
  1056. (system (fold-services services)))
  1057. ;; SYSTEM contains the derivation as a monadic value.
  1058. (service-value system)))
  1059. (define* (operating-system-profile os)
  1060. "Return a derivation that builds the system profile of OS."
  1061. (mlet* %store-monad
  1062. ((services -> (operating-system-services os))
  1063. (profile (fold-services services
  1064. #:target-type profile-service-type)))
  1065. (match profile
  1066. (("profile" profile)
  1067. (return profile)))))
  1068. (define (operating-system-root-file-system os)
  1069. "Return the root file system of OS."
  1070. (or (find (lambda (fs)
  1071. (string=? "/" (file-system-mount-point fs)))
  1072. (operating-system-file-systems os))
  1073. (raise (condition
  1074. (&message (message "missing root file system"))
  1075. (&error-location
  1076. (location (operating-system-location os)))))))
  1077. (define (operating-system-initrd-file os)
  1078. "Return a gexp denoting the initrd file of OS."
  1079. (define boot-file-systems
  1080. (filter file-system-needed-for-boot?
  1081. (operating-system-file-systems os)))
  1082. (define mapped-devices
  1083. (operating-system-boot-mapped-devices os))
  1084. (define make-initrd
  1085. (operating-system-initrd os))
  1086. (make-initrd boot-file-systems
  1087. #:linux (operating-system-kernel os)
  1088. #:linux-modules
  1089. (operating-system-initrd-modules os)
  1090. #:mapped-devices mapped-devices
  1091. #:keyboard-layout (operating-system-keyboard-layout os)))
  1092. (define* (operating-system-uuid os #:optional (type 'dce))
  1093. "Compute UUID object with a deterministic \"UUID\" for OS, of the given
  1094. TYPE (one of 'iso9660 or 'dce). Return a UUID object."
  1095. ;; Note: For this to be deterministic, we must not hash things that contains
  1096. ;; (directly or indirectly) procedures, for example. That rules out
  1097. ;; anything that contains gexps, thunk or delayed record fields, etc.
  1098. (define service-name
  1099. (compose service-type-name service-kind))
  1100. (define (file-system-digest fs)
  1101. ;; Return a hashable digest that does not contain 'dependencies' since
  1102. ;; this field can contain procedures.
  1103. (let ((device (file-system-device fs)))
  1104. (list (file-system-mount-point fs)
  1105. (file-system-type fs)
  1106. (file-system-device->string device)
  1107. (file-system-options fs))))
  1108. (if (eq? type 'iso9660)
  1109. (let ((pad (compose (cut string-pad <> 2 #\0)
  1110. number->string))
  1111. (h (hash (map service-name (operating-system-services os))
  1112. 3600)))
  1113. (bytevector->uuid
  1114. (string->iso9660-uuid
  1115. (string-append "1970-01-01-"
  1116. (pad (hash (operating-system-host-name os) 24)) "-"
  1117. (pad (quotient h 60)) "-"
  1118. (pad (modulo h 60)) "-"
  1119. (pad (hash (map file-system-digest
  1120. (operating-system-file-systems os))
  1121. 100))))
  1122. 'iso9660))
  1123. (bytevector->uuid
  1124. (uint-list->bytevector
  1125. (list (hash (map file-system-digest
  1126. (operating-system-file-systems os))
  1127. (- (expt 2 32) 1))
  1128. (hash (operating-system-host-name os)
  1129. (- (expt 2 32) 1))
  1130. (hash (map service-name (operating-system-services os))
  1131. (- (expt 2 32) 1))
  1132. (hash (map file-system-digest (operating-system-file-systems os))
  1133. (- (expt 2 32) 1)))
  1134. (endianness little)
  1135. 4)
  1136. type)))
  1137. (define (locale-name->definition* name)
  1138. "Variant of 'locale-name->definition' that raises an error upon failure."
  1139. (match (locale-name->definition name)
  1140. (#f
  1141. (raise (formatted-message (G_ "~a: invalid locale name") name)))
  1142. (def def)))
  1143. (define (operating-system-locale-directory os)
  1144. "Return the directory containing the locales compiled for the definitions
  1145. listed in OS. The C library expects to find it under
  1146. /run/current-system/locale."
  1147. (define name
  1148. (operating-system-locale os))
  1149. (define definitions
  1150. ;; While we're at it, check whether NAME is defined and add it if needed.
  1151. (if (member name (map locale-definition-name
  1152. (operating-system-locale-definitions os)))
  1153. (operating-system-locale-definitions os)
  1154. (cons (locale-name->definition* name)
  1155. (operating-system-locale-definitions os))))
  1156. (locale-directory definitions
  1157. #:libcs (operating-system-locale-libcs os)))
  1158. (define* (kernel->boot-label kernel #:key hurd)
  1159. "Return a label for the bootloader menu entry that boots KERNEL."
  1160. (cond ((package? hurd)
  1161. (string-append "GNU with the "
  1162. (string-titlecase (package-name hurd)) " "
  1163. (package-version hurd)))
  1164. ((package? kernel)
  1165. (string-append "GNU with "
  1166. (string-titlecase (package-name kernel)) " "
  1167. (package-version kernel)))
  1168. ((inferior-package? kernel)
  1169. (string-append "GNU with "
  1170. (string-titlecase (inferior-package-name kernel)) " "
  1171. (inferior-package-version kernel)))
  1172. (else "GNU")))
  1173. (define (operating-system-default-label os)
  1174. "Return the default label for OS, as it will appear in the bootloader menu
  1175. entry."
  1176. (kernel->boot-label (operating-system-kernel os)
  1177. #:hurd (operating-system-hurd os)))
  1178. (define (store-file-system file-systems)
  1179. "Return the file system object among FILE-SYSTEMS that contains the store."
  1180. (match (filter (lambda (fs)
  1181. (and (file-system-mount? fs)
  1182. (not (memq 'bind-mount (file-system-flags fs)))
  1183. (string-prefix? (file-system-mount-point fs)
  1184. (%store-prefix))))
  1185. file-systems)
  1186. ((and candidates (head . tail))
  1187. (reduce (lambda (fs1 fs2)
  1188. (if (> (string-length (file-system-mount-point fs1))
  1189. (string-length (file-system-mount-point fs2)))
  1190. fs1
  1191. fs2))
  1192. head
  1193. candidates))))
  1194. (define (operating-system-store-file-system os)
  1195. "Return the file system that contains the store of OS."
  1196. (store-file-system (operating-system-file-systems os)))
  1197. (define* (operating-system-bootcfg os #:optional (old-entries '()))
  1198. "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
  1199. a list of <menu-entry>, to populate the \"old entries\" menu."
  1200. (let* ((file-systems (operating-system-file-systems os))
  1201. (root-fs (operating-system-root-file-system os))
  1202. (root-device (file-system-device root-fs))
  1203. (locale (operating-system-locale os))
  1204. (crypto-devices (operating-system-bootloader-crypto-devices os))
  1205. (params (operating-system-boot-parameters
  1206. os root-device
  1207. #:system-kernel-arguments? #t))
  1208. (entry (boot-parameters->menu-entry params))
  1209. (bootloader-conf (operating-system-bootloader os)))
  1210. (define generate-config-file
  1211. (bootloader-configuration-file-generator
  1212. (bootloader-configuration-bootloader bootloader-conf)))
  1213. (generate-config-file bootloader-conf (list entry)
  1214. #:old-entries old-entries
  1215. #:locale locale
  1216. #:store-crypto-devices crypto-devices
  1217. #:store-directory-prefix
  1218. (btrfs-store-subvolume-file-name file-systems))))
  1219. (define (operating-system-multiboot-modules os)
  1220. (if (operating-system-hurd os) (hurd-multiboot-modules os) '()))
  1221. (define (hurd-multiboot-modules os)
  1222. (let* ((hurd (operating-system-hurd os))
  1223. (root-file-system-command
  1224. (list (file-append hurd "/hurd/ext2fs.static")
  1225. "ext2fs"
  1226. "--multiboot-command-line='${kernel-command-line}'"
  1227. "--host-priv-port='${host-port}'"
  1228. "--device-master-port='${device-port}'"
  1229. "--exec-server-task='${exec-task}'"
  1230. "--store-type=typed"
  1231. "--x-xattr-translator-records"
  1232. "'${root}'" "'$(task-create)'" "'$(task-resume)'"))
  1233. (target (%current-target-system))
  1234. (libc (if target
  1235. (with-parameters ((%current-target-system #f))
  1236. ;; TODO: cross-libc has extra patches for the Hurd;
  1237. ;; remove in next rebuild cycle
  1238. (cross-libc target))
  1239. glibc))
  1240. (exec-server-command
  1241. (list (file-append libc "/lib/ld.so.1") "exec"
  1242. (file-append hurd "/hurd/exec") "'$(exec-task=task-create)'")))
  1243. (list root-file-system-command exec-server-command)))
  1244. (define* (operating-system-boot-parameters os root-device
  1245. #:key system-kernel-arguments?)
  1246. "Return a monadic <boot-parameters> record that describes the boot
  1247. parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
  1248. such as '--root' and '--load' to <boot-parameters>."
  1249. (let* ((initrd (and (not (operating-system-hurd os))
  1250. (operating-system-initrd-file os)))
  1251. (store (operating-system-store-file-system os))
  1252. (file-systems (operating-system-file-systems os))
  1253. (crypto-devices (operating-system-bootloader-crypto-devices os))
  1254. (locale (operating-system-locale os))
  1255. (bootloader (bootloader-configuration-bootloader
  1256. (operating-system-bootloader os)))
  1257. (bootloader-name (bootloader-name bootloader))
  1258. (label (operating-system-label os))
  1259. (multiboot-modules (operating-system-multiboot-modules os)))
  1260. (boot-parameters
  1261. (label label)
  1262. (root-device root-device)
  1263. (kernel (operating-system-kernel-file os))
  1264. (kernel-arguments
  1265. (if system-kernel-arguments?
  1266. (operating-system-kernel-arguments os root-device)
  1267. (operating-system-user-kernel-arguments os)))
  1268. (initrd initrd)
  1269. (multiboot-modules multiboot-modules)
  1270. (bootloader-name bootloader-name)
  1271. (bootloader-menu-entries
  1272. (bootloader-configuration-menu-entries (operating-system-bootloader os)))
  1273. (locale locale)
  1274. (store-device (ensure-not-/dev (file-system-device store)))
  1275. (store-directory-prefix (btrfs-store-subvolume-file-name file-systems))
  1276. (store-crypto-devices crypto-devices)
  1277. (store-mount-point (file-system-mount-point store)))))
  1278. (define (device->sexp device)
  1279. "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
  1280. (match device
  1281. ((? uuid? uuid)
  1282. `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
  1283. ((? file-system-label? label)
  1284. `(file-system-label ,(file-system-label->string label)))
  1285. (_
  1286. device)))
  1287. (define* (operating-system-boot-parameters-file os
  1288. #:key system-kernel-arguments?)
  1289. "Return a file that describes the boot parameters of OS. The primary use of
  1290. this file is the reconstruction of GRUB menu entries for old configurations.
  1291. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
  1292. and '--load' to the returned file (since the returned file is then usually
  1293. stored into the content-addressed \"system\" directory, it's usually not a
  1294. good idea to give it because the content hash would change by the content hash
  1295. being stored into the \"parameters\" file)."
  1296. (let* ((root (operating-system-root-file-system os))
  1297. (device (file-system-device root))
  1298. (params (operating-system-boot-parameters
  1299. os device
  1300. #:system-kernel-arguments?
  1301. system-kernel-arguments?)))
  1302. (scheme-file "parameters"
  1303. #~(boot-parameters
  1304. (version 0)
  1305. (label #$(boot-parameters-label params))
  1306. (root-device
  1307. #$(device->sexp
  1308. (boot-parameters-root-device params)))
  1309. (kernel #$(boot-parameters-kernel params))
  1310. (kernel-arguments
  1311. #$(boot-parameters-kernel-arguments params))
  1312. #$@(if (boot-parameters-initrd params)
  1313. #~((initrd #$(boot-parameters-initrd params)))
  1314. #~())
  1315. #$@(if (pair? (boot-parameters-multiboot-modules params))
  1316. #~((multiboot-modules
  1317. #$(boot-parameters-multiboot-modules params)))
  1318. #~())
  1319. (bootloader-name #$(boot-parameters-bootloader-name params))
  1320. (bootloader-menu-entries
  1321. #$(map menu-entry->sexp
  1322. (or (and=> (operating-system-bootloader os)
  1323. bootloader-configuration-menu-entries)
  1324. '())))
  1325. (locale #$(boot-parameters-locale params))
  1326. (store
  1327. (device
  1328. #$(device->sexp (boot-parameters-store-device params)))
  1329. (mount-point #$(boot-parameters-store-mount-point
  1330. params))
  1331. (directory-prefix
  1332. #$(boot-parameters-store-directory-prefix params))
  1333. (crypto-devices
  1334. #$(map device->sexp
  1335. (boot-parameters-store-crypto-devices params)))))
  1336. #:set-load-path? #f)))
  1337. (define-gexp-compiler (operating-system-compiler (os <operating-system>)
  1338. system target)
  1339. ((store-lift
  1340. (lambda (store)
  1341. ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
  1342. ;; 'operating-system-derivation'.
  1343. (run-with-store store (operating-system-derivation os)
  1344. #:system system
  1345. #:target target)))))
  1346. ;;; system.scm ends here