system.scm 60 KB

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