system.scm 65 KB

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