system.scm 64 KB

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