system.scm 60 KB

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