system.scm 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 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. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (gnu system)
  24. #:use-module (guix inferior)
  25. #:use-module (guix store)
  26. #:use-module (guix monads)
  27. #:use-module (guix gexp)
  28. #:use-module (guix records)
  29. #:use-module (guix packages)
  30. #:use-module (guix derivations)
  31. #:use-module (guix profiles)
  32. #:use-module (guix ui)
  33. #:use-module (gnu packages base)
  34. #:use-module (gnu packages bash)
  35. #:use-module (gnu packages guile)
  36. #:use-module (gnu packages guile-xyz)
  37. #:use-module (gnu packages admin)
  38. #:use-module (gnu packages linux)
  39. #:use-module (gnu packages pciutils)
  40. #:use-module (gnu packages package-management)
  41. #:use-module (gnu packages less)
  42. #:use-module (gnu packages zile)
  43. #:use-module (gnu packages nano)
  44. #:use-module (gnu packages gawk)
  45. #:use-module (gnu packages man)
  46. #:use-module (gnu packages texinfo)
  47. #:use-module (gnu packages compression)
  48. #:use-module (gnu packages firmware)
  49. #:use-module (gnu services)
  50. #:use-module (gnu services shepherd)
  51. #:use-module (gnu services base)
  52. #:use-module (gnu bootloader)
  53. #:use-module (gnu system shadow)
  54. #:use-module (gnu system nss)
  55. #:use-module (gnu system locale)
  56. #:use-module (gnu system pam)
  57. #:use-module (gnu system linux-initrd)
  58. #:use-module (gnu system uuid)
  59. #:use-module (gnu system file-systems)
  60. #:use-module (gnu system mapped-devices)
  61. #:use-module (ice-9 match)
  62. #:use-module (srfi srfi-1)
  63. #:use-module (srfi srfi-26)
  64. #:use-module (srfi srfi-34)
  65. #:use-module (srfi srfi-35)
  66. #:use-module (rnrs bytevectors)
  67. #:export (operating-system
  68. operating-system?
  69. this-operating-system
  70. operating-system-bootloader
  71. operating-system-services
  72. operating-system-essential-services
  73. operating-system-default-essential-services
  74. operating-system-user-services
  75. operating-system-packages
  76. operating-system-host-name
  77. operating-system-hosts-file
  78. operating-system-kernel
  79. operating-system-kernel-file
  80. operating-system-kernel-arguments
  81. operating-system-label
  82. operating-system-default-label
  83. operating-system-initrd-modules
  84. operating-system-initrd
  85. operating-system-users
  86. operating-system-groups
  87. operating-system-issue
  88. operating-system-timezone
  89. operating-system-locale
  90. operating-system-locale-definitions
  91. operating-system-locale-libcs
  92. operating-system-mapped-devices
  93. operating-system-file-systems
  94. operating-system-store-file-system
  95. operating-system-user-mapped-devices
  96. operating-system-boot-mapped-devices
  97. operating-system-activation-script
  98. operating-system-user-accounts
  99. operating-system-shepherd-service-names
  100. operating-system-user-kernel-arguments
  101. operating-system-derivation
  102. operating-system-profile
  103. operating-system-bootcfg
  104. operating-system-etc-directory
  105. operating-system-locale-directory
  106. operating-system-boot-script
  107. system-linux-image-file-name
  108. operating-system-with-gc-roots
  109. operating-system-with-provenance
  110. boot-parameters
  111. boot-parameters?
  112. boot-parameters-label
  113. boot-parameters-root-device
  114. boot-parameters-bootloader-name
  115. boot-parameters-bootloader-menu-entries
  116. boot-parameters-store-device
  117. boot-parameters-store-mount-point
  118. boot-parameters-kernel
  119. boot-parameters-kernel-arguments
  120. boot-parameters-initrd
  121. read-boot-parameters
  122. read-boot-parameters-file
  123. boot-parameters->menu-entry
  124. local-host-aliases
  125. %root-account
  126. %setuid-programs
  127. %base-packages
  128. %base-firmware))
  129. ;;; Commentary:
  130. ;;;
  131. ;;; This module supports whole-system configuration.
  132. ;;;
  133. ;;; Code:
  134. (define (bootable-kernel-arguments system root-device)
  135. "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
  136. (list (string-append "--root="
  137. (cond ((uuid? root-device)
  138. ;; Note: Always use the DCE format because that's
  139. ;; what (gnu build linux-boot) expects for the
  140. ;; '--root' kernel command-line option.
  141. (uuid->string (uuid-bytevector root-device)
  142. 'dce))
  143. ((file-system-label? root-device)
  144. (file-system-label->string root-device))
  145. (else root-device)))
  146. #~(string-append "--system=" #$system)
  147. #~(string-append "--load=" #$system "/boot")))
  148. ;; System-wide configuration.
  149. ;; TODO: Add per-field docstrings/stexi.
  150. (define-record-type* <operating-system> operating-system
  151. make-operating-system
  152. operating-system?
  153. this-operating-system
  154. (kernel operating-system-kernel ; package
  155. (default linux-libre))
  156. (kernel-arguments operating-system-user-kernel-arguments
  157. (default '("quiet"))) ; list of gexps/strings
  158. (bootloader operating-system-bootloader) ; <bootloader-configuration>
  159. (label operating-system-label ; string
  160. (thunked)
  161. (default (operating-system-default-label this-operating-system)))
  162. (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
  163. (default #f))
  164. (initrd operating-system-initrd ; (list fs) -> file-like
  165. (default base-initrd))
  166. (initrd-modules operating-system-initrd-modules ; list of strings
  167. (thunked) ; it's system-dependent
  168. (default %base-initrd-modules))
  169. (firmware operating-system-firmware ; list of packages
  170. (default %base-firmware))
  171. (host-name operating-system-host-name) ; string
  172. (hosts-file operating-system-hosts-file ; file-like | #f
  173. (default #f))
  174. (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
  175. (default '()))
  176. (file-systems operating-system-file-systems) ; list of fs
  177. (swap-devices operating-system-swap-devices ; list of strings
  178. (default '()))
  179. (users operating-system-users ; list of user accounts
  180. (default %base-user-accounts))
  181. (groups operating-system-groups ; list of user groups
  182. (default %base-groups))
  183. (skeletons operating-system-skeletons ; list of name/file-like value
  184. (default (default-skeletons)))
  185. (issue operating-system-issue ; string
  186. (default %default-issue))
  187. (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
  188. (default %base-packages)) ; or just PACKAGE
  189. (timezone operating-system-timezone) ; string
  190. (locale operating-system-locale ; string
  191. (default "en_US.utf8"))
  192. (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
  193. (default %default-locale-definitions))
  194. (locale-libcs operating-system-locale-libcs ; list of <packages>
  195. (default %default-locale-libcs))
  196. (name-service-switch operating-system-name-service-switch ; <name-service-switch>
  197. (default %default-nss))
  198. (essential-services operating-system-essential-services ; list of services
  199. (thunked)
  200. (default (operating-system-default-essential-services
  201. this-operating-system)))
  202. (services operating-system-user-services ; list of services
  203. (default %base-services))
  204. (pam-services operating-system-pam-services ; list of PAM services
  205. (default (base-pam-services)))
  206. (setuid-programs operating-system-setuid-programs
  207. (default %setuid-programs)) ; list of string-valued gexps
  208. (sudoers-file operating-system-sudoers-file ; file-like
  209. (default %sudoers-specification)))
  210. (define (operating-system-kernel-arguments os root-device)
  211. "Return all the kernel arguments, including the ones not specified
  212. directly by the user."
  213. (append (bootable-kernel-arguments os root-device)
  214. (operating-system-user-kernel-arguments os)))
  215. ;;;
  216. ;;; Boot parameters
  217. ;;;
  218. (define-record-type* <boot-parameters>
  219. boot-parameters make-boot-parameters boot-parameters?
  220. (label boot-parameters-label)
  221. ;; Because we will use the 'store-device' to create the GRUB search command,
  222. ;; the 'store-device' has slightly different semantics than 'root-device'.
  223. ;; The 'store-device' can be a file system uuid, a file system label, or #f,
  224. ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
  225. ;; understand that. The 'root-device', on the other hand, corresponds
  226. ;; exactly to the device field of the <file-system> object representing the
  227. ;; OS's root file system, so it might be a device path like "/dev/sda3".
  228. (root-device boot-parameters-root-device)
  229. (bootloader-name boot-parameters-bootloader-name)
  230. (bootloader-menu-entries ;list of <menu-entry>
  231. boot-parameters-bootloader-menu-entries)
  232. (store-device boot-parameters-store-device)
  233. (store-mount-point boot-parameters-store-mount-point)
  234. (kernel boot-parameters-kernel)
  235. (kernel-arguments boot-parameters-kernel-arguments)
  236. (initrd boot-parameters-initrd))
  237. (define (ensure-not-/dev device)
  238. "If DEVICE starts with a slash, return #f. This is meant to filter out
  239. Linux device names such as /dev/sda, and to preserve GRUB device names and
  240. file system labels."
  241. (if (and (string? device) (string-prefix? "/" device))
  242. #f
  243. device))
  244. (define (read-boot-parameters port)
  245. "Read boot parameters from PORT and return the corresponding
  246. <boot-parameters> object or #f if the format is unrecognized."
  247. (define device-sexp->device
  248. (match-lambda
  249. (('uuid (? symbol? type) (? bytevector? bv))
  250. (bytevector->uuid bv type))
  251. (('file-system-label (? string? label))
  252. (file-system-label label))
  253. ((? bytevector? bv) ;old format
  254. (bytevector->uuid bv 'dce))
  255. ((? string? device)
  256. ;; It used to be that we would not distinguish between labels and
  257. ;; device names. Try to infer the right thing here.
  258. (if (string-prefix? "/dev/" device)
  259. device
  260. (file-system-label device)))))
  261. (match (read port)
  262. (('boot-parameters ('version 0)
  263. ('label label) ('root-device root)
  264. ('kernel linux)
  265. rest ...)
  266. (boot-parameters
  267. (label label)
  268. (root-device (device-sexp->device root))
  269. (bootloader-name
  270. (match (assq 'bootloader-name rest)
  271. ((_ args) args)
  272. (#f 'grub))) ; for compatibility reasons.
  273. (bootloader-menu-entries
  274. (match (assq 'bootloader-menu-entries rest)
  275. ((_ entries) (map sexp->menu-entry entries))
  276. (#f '())))
  277. ;; In the past, we would store the directory name of the kernel instead
  278. ;; of the absolute file name of its image. Detect that and correct it.
  279. (kernel (if (string=? linux (direct-store-path linux))
  280. (string-append linux "/"
  281. (system-linux-image-file-name))
  282. linux))
  283. (kernel-arguments
  284. (match (assq 'kernel-arguments rest)
  285. ((_ args) args)
  286. (#f '()))) ;the old format
  287. (initrd
  288. (match (assq 'initrd rest)
  289. (('initrd ('string-append directory file)) ;the old format
  290. (string-append directory file))
  291. (('initrd (? string? file))
  292. file)))
  293. (store-device
  294. ;; Linux device names like "/dev/sda1" are not suitable GRUB device
  295. ;; identifiers, so we just filter them out.
  296. (ensure-not-/dev
  297. (match (assq 'store rest)
  298. (('store ('device #f) _ ...)
  299. root-device)
  300. (('store ('device device) _ ...)
  301. (device-sexp->device device))
  302. (_ ;the old format
  303. root-device))))
  304. (store-mount-point
  305. (match (assq 'store rest)
  306. (('store ('device _) ('mount-point mount-point) _ ...)
  307. mount-point)
  308. (_ ;the old format
  309. "/")))))
  310. (x ;unsupported format
  311. (warning (G_ "unrecognized boot parameters at '~a'~%")
  312. (port-filename port))
  313. #f)))
  314. (define (read-boot-parameters-file system)
  315. "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
  316. file and returns the corresponding <boot-parameters> object or #f if the
  317. format is unrecognized.
  318. The object has its kernel-arguments extended in order to make it bootable."
  319. (let* ((file (string-append system "/parameters"))
  320. (params (call-with-input-file file read-boot-parameters))
  321. (root (boot-parameters-root-device params)))
  322. (boot-parameters
  323. (inherit params)
  324. (kernel-arguments (append (bootable-kernel-arguments system root)
  325. (boot-parameters-kernel-arguments params))))))
  326. (define (boot-parameters->menu-entry conf)
  327. (menu-entry
  328. (label (boot-parameters-label conf))
  329. (device (boot-parameters-store-device conf))
  330. (device-mount-point (boot-parameters-store-mount-point conf))
  331. (linux (boot-parameters-kernel conf))
  332. (linux-arguments (boot-parameters-kernel-arguments conf))
  333. (initrd (boot-parameters-initrd conf))))
  334. ;;;
  335. ;;; Services.
  336. ;;;
  337. (define (non-boot-file-system-service os)
  338. "Return the file system service for the file systems of OS that are not
  339. marked as 'needed-for-boot'."
  340. (define file-systems
  341. (remove file-system-needed-for-boot?
  342. (operating-system-file-systems os)))
  343. (define mapped-devices-for-boot
  344. (operating-system-boot-mapped-devices os))
  345. (define (device-mappings fs)
  346. (let ((device (file-system-device fs)))
  347. (if (string? device) ;title is 'device
  348. (filter (lambda (md)
  349. (string=? (string-append "/dev/mapper/"
  350. (mapped-device-target md))
  351. device))
  352. (operating-system-mapped-devices os))
  353. '())))
  354. (define (add-dependencies fs)
  355. ;; Add the dependencies due to device mappings to FS.
  356. (file-system
  357. (inherit fs)
  358. (dependencies
  359. (delete-duplicates
  360. (remove (cut member <> mapped-devices-for-boot)
  361. (append (device-mappings fs)
  362. (file-system-dependencies fs)))
  363. eq?))))
  364. (service file-system-service-type
  365. (map add-dependencies file-systems)))
  366. (define (mapped-device-users device file-systems)
  367. "Return the subset of FILE-SYSTEMS that use DEVICE."
  368. (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
  369. (filter (lambda (fs)
  370. (or (member device (file-system-dependencies fs))
  371. (and (string? (file-system-device fs))
  372. (string=? (file-system-device fs) target))))
  373. file-systems)))
  374. (define (operating-system-user-mapped-devices os)
  375. "Return the subset of mapped devices that can be installed in
  376. user-land--i.e., those not needed during boot."
  377. (let ((devices (operating-system-mapped-devices os))
  378. (file-systems (operating-system-file-systems os)))
  379. (filter (lambda (md)
  380. (let ((users (mapped-device-users md file-systems)))
  381. (not (any file-system-needed-for-boot? users))))
  382. devices)))
  383. (define (operating-system-boot-mapped-devices os)
  384. "Return the subset of mapped devices that must be installed during boot,
  385. from the initrd."
  386. (let ((devices (operating-system-mapped-devices os))
  387. (file-systems (operating-system-file-systems os)))
  388. (filter (lambda (md)
  389. (let ((users (mapped-device-users md file-systems)))
  390. (any file-system-needed-for-boot? users)))
  391. devices)))
  392. (define (device-mapping-services os)
  393. "Return the list of device-mapping services for OS as a list."
  394. (map device-mapping-service
  395. (operating-system-user-mapped-devices os)))
  396. (define (swap-services os)
  397. "Return the list of swap services for OS."
  398. (map swap-service (operating-system-swap-devices os)))
  399. (define* (system-linux-image-file-name)
  400. "Return the basename of the kernel image file for SYSTEM."
  401. ;; FIXME: Evaluate the conditional based on the actual current system.
  402. (let ((target (or (%current-target-system) (%current-system))))
  403. (cond
  404. ((string-prefix? "arm" target) "zImage")
  405. ((string-prefix? "mips" target) "vmlinuz")
  406. ((string-prefix? "aarch64" target) "Image")
  407. (else "bzImage"))))
  408. (define (operating-system-kernel-file os)
  409. "Return an object representing the absolute file name of the kernel image of
  410. OS."
  411. (file-append (operating-system-kernel os)
  412. "/" (system-linux-image-file-name)))
  413. (define* (operating-system-directory-base-entries os)
  414. "Return the basic entries of the 'system' directory of OS for use as the
  415. value of the SYSTEM-SERVICE-TYPE service."
  416. (let ((locale (operating-system-locale-directory os)))
  417. (mlet %store-monad ((kernel -> (operating-system-kernel os))
  418. (initrd -> (operating-system-initrd-file os))
  419. (params (operating-system-boot-parameters-file os)))
  420. (return `(("kernel" ,kernel)
  421. ("parameters" ,params)
  422. ("initrd" ,initrd)
  423. ("locale" ,locale)))))) ;used by libc
  424. (define (operating-system-default-essential-services os)
  425. "Return the list of essential services for OS. These are special services
  426. that implement part of what's declared in OS are responsible for low-level
  427. bookkeeping."
  428. (define known-fs
  429. (map file-system-mount-point (operating-system-file-systems os)))
  430. (let* ((mappings (device-mapping-services os))
  431. (root-fs (root-file-system-service))
  432. (other-fs (non-boot-file-system-service os))
  433. (swaps (swap-services os))
  434. (procs (service user-processes-service-type))
  435. (host-name (host-name-service (operating-system-host-name os)))
  436. (entries (operating-system-directory-base-entries os)))
  437. (cons* (service system-service-type entries)
  438. %boot-service
  439. ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
  440. ;; execs shepherd comes last in the boot script (XXX). Likewise,
  441. ;; the cleanup service must come first so that its gexp runs before
  442. ;; activation code.
  443. (service cleanup-service-type #f)
  444. %activation-service
  445. %shepherd-root-service
  446. (pam-root-service (operating-system-pam-services os))
  447. (account-service (append (operating-system-accounts os)
  448. (operating-system-groups os))
  449. (operating-system-skeletons os))
  450. (operating-system-etc-service os)
  451. (service fstab-service-type
  452. (filter file-system-needed-for-boot?
  453. (operating-system-file-systems os)))
  454. (session-environment-service
  455. (operating-system-environment-variables os))
  456. host-name procs root-fs
  457. (service setuid-program-service-type
  458. (operating-system-setuid-programs os))
  459. (service profile-service-type
  460. (operating-system-packages os))
  461. other-fs
  462. (append mappings swaps
  463. ;; Add the firmware service.
  464. (list %linux-bare-metal-service
  465. (service firmware-service-type
  466. (operating-system-firmware os)))))))
  467. (define* (operating-system-services os)
  468. "Return all the services of OS, including \"essential\" services."
  469. (instantiate-missing-services
  470. (append (operating-system-user-services os)
  471. (operating-system-essential-services os))))
  472. (define (operating-system-with-gc-roots os roots)
  473. "Return a variant of OS where ROOTS are registered as GC roots."
  474. (operating-system
  475. (inherit os)
  476. ;; We use this procedure for the installation OS, which already defines GC
  477. ;; roots. Add ROOTS to those.
  478. (services (cons (simple-service 'extra-root
  479. gc-root-service-type roots)
  480. (operating-system-user-services os)))))
  481. (define* (operating-system-with-provenance os #:optional config-file)
  482. "Return a variant of OS that stores its own provenance information,
  483. including CONFIG-FILE, if available. This is achieved by adding an instance
  484. of PROVENANCE-SERVICE-TYPE to its services."
  485. (operating-system
  486. (inherit os)
  487. (services (cons (service provenance-service-type config-file)
  488. (operating-system-user-services os)))))
  489. ;;;
  490. ;;; /etc.
  491. ;;;
  492. (define %base-firmware
  493. ;; Firmware usable by default.
  494. (list ath9k-htc-firmware
  495. openfwwf-firmware))
  496. (define %base-packages
  497. ;; Default set of packages globally visible. It should include anything
  498. ;; required for basic administrator tasks.
  499. (cons* procps psmisc which less zile nano
  500. pciutils usbutils
  501. util-linux
  502. inetutils isc-dhcp
  503. (@ (gnu packages admin) shadow) ;for 'passwd'
  504. ;; wireless-tools is deprecated in favor of iw, but it's still what
  505. ;; many people are familiar with, so keep it around.
  506. iw wireless-tools
  507. iproute
  508. net-tools ; XXX: remove when Inetutils suffices
  509. man-db
  510. info-reader ;the standalone Info reader (no Perl)
  511. ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
  512. ;; want the other commands and the man pages (notably because
  513. ;; auto-completion in Emacs shell relies on man pages.)
  514. sudo
  515. ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
  516. ;; already depends on it anyway.
  517. kmod eudev
  518. e2fsprogs kbd
  519. bash-completion
  520. ;; XXX: We don't use (canonical-package guile-2.2) here because that
  521. ;; would create a collision in the global profile between the GMP
  522. ;; variant propagated by 'guile-final' and the GMP variant propagated
  523. ;; by 'gnutls', itself propagated by 'guix'.
  524. guile-2.2
  525. guile-readline guile-colorized
  526. ;; The packages below are also in %FINAL-INPUTS, so take them from
  527. ;; there to avoid duplication.
  528. (map canonical-package
  529. (list bash coreutils findutils grep sed
  530. diffutils patch gawk tar gzip bzip2 xz lzip))))
  531. (define %default-issue
  532. ;; Default contents for /etc/issue.
  533. "
  534. This is the GNU system. Welcome.\n")
  535. (define (local-host-aliases host-name)
  536. "Return aliases for HOST-NAME, to be used in /etc/hosts."
  537. (string-append "127.0.0.1 localhost " host-name "\n"
  538. "::1 localhost " host-name "\n"))
  539. (define (default-/etc/hosts host-name)
  540. "Return the default /etc/hosts file."
  541. (plain-file "hosts" (local-host-aliases host-name)))
  542. (define* (operating-system-etc-service os)
  543. "Return a <service> that builds containing the static part of the /etc
  544. directory."
  545. (let ((login.defs
  546. (plain-file "login.defs"
  547. (string-append
  548. "# Default paths for non-login shells started by su(1).\n"
  549. "ENV_PATH /run/setuid-programs:"
  550. "/run/current-system/profile/bin:"
  551. "/run/current-system/profile/sbin\n"
  552. "ENV_SUPATH /run/setuid-programs:"
  553. "/run/current-system/profile/bin:"
  554. "/run/current-system/profile/sbin\n")))
  555. (issue (plain-file "issue" (operating-system-issue os)))
  556. (nsswitch (plain-file "nsswitch.conf"
  557. (name-service-switch->string
  558. (operating-system-name-service-switch os))))
  559. ;; Startup file for POSIX-compliant login shells, which set system-wide
  560. ;; environment variables.
  561. (profile (mixed-text-file "profile" "\
  562. # Crucial variables that could be missing in the profiles' 'etc/profile'
  563. # because they would require combining both profiles.
  564. # FIXME: See <http://bugs.gnu.org/20255>.
  565. export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
  566. export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
  567. export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
  568. export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
  569. # Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
  570. export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
  571. # Ignore the default value of 'PATH'.
  572. unset PATH
  573. # Load the system profile's settings.
  574. GUIX_PROFILE=/run/current-system/profile ; \\
  575. . /run/current-system/profile/etc/profile
  576. # Since 'lshd' does not use pam_env, /etc/environment must be explicitly
  577. # loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
  578. # We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
  579. # reading the user's 'etc/profile' to allow variables to be overridden.
  580. if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
  581. -a -z \"$LINUX_MODULE_DIRECTORY\" ]
  582. then
  583. . /etc/environment
  584. export `cat /etc/environment | cut -d= -f1`
  585. fi
  586. # Arrange so that ~/.config/guix/current comes first.
  587. for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
  588. do
  589. if [ -f \"$profile/etc/profile\" ]
  590. then
  591. # Load the user profile's settings.
  592. GUIX_PROFILE=\"$profile\" ; \\
  593. . \"$profile/etc/profile\"
  594. else
  595. # At least define this one so that basic things just work
  596. # when the user installs their first package.
  597. export PATH=\"$profile/bin:$PATH\"
  598. fi
  599. done
  600. # Prepend setuid programs.
  601. export PATH=/run/setuid-programs:$PATH
  602. # Arrange so that ~/.config/guix/current/share/info comes first.
  603. export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
  604. # Set the umask, notably for users logging in via 'lsh'.
  605. # See <http://bugs.gnu.org/22650>.
  606. umask 022
  607. # Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
  608. # find dictionaries.
  609. export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
  610. # Allow GStreamer-based applications to find plugins.
  611. export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
  612. if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
  613. then
  614. # Load Bash-specific initialization code.
  615. . /etc/bashrc
  616. fi
  617. "))
  618. (bashrc (plain-file "bashrc" "\
  619. # Bash-specific initialization.
  620. # The 'bash-completion' package.
  621. if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
  622. then
  623. # Bash-completion sources ~/.bash_completion. It installs a dynamic
  624. # completion loader that searches its own completion files as well
  625. # as those in ~/.guix-profile and /run/current-system/profile.
  626. source /run/current-system/profile/etc/profile.d/bash_completion.sh
  627. fi\n")))
  628. (etc-service
  629. `(("services" ,(file-append net-base "/etc/services"))
  630. ("protocols" ,(file-append net-base "/etc/protocols"))
  631. ("rpc" ,(file-append net-base "/etc/rpc"))
  632. ("login.defs" ,#~#$login.defs)
  633. ("issue" ,#~#$issue)
  634. ("nsswitch.conf" ,#~#$nsswitch)
  635. ("profile" ,#~#$profile)
  636. ("bashrc" ,#~#$bashrc)
  637. ("hosts" ,#~#$(or (operating-system-hosts-file os)
  638. (default-/etc/hosts (operating-system-host-name os))))
  639. ;; Write the operating-system-host-name to /etc/hostname to prevent
  640. ;; NetworkManager from changing the system's hostname when connecting
  641. ;; to certain networks. Some discussion at
  642. ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
  643. ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
  644. ;; Some programs (e.g., GLib) look at /etc/timezone to find the
  645. ;; name of the current timezone. For details, see
  646. ;; https://lists.gnu.org/archive/html/guix-devel/2019-07/msg00166.html
  647. ("timezone" ,(plain-file "timezone" (operating-system-timezone os)))
  648. ("localtime" ,(file-append tzdata "/share/zoneinfo/"
  649. (operating-system-timezone os)))
  650. ("sudoers" ,(operating-system-sudoers-file os))))))
  651. (define %root-account
  652. ;; Default root account.
  653. (user-account
  654. (name "root")
  655. (password "")
  656. (uid 0) (group "root")
  657. (comment "System administrator")
  658. (home-directory "/root")))
  659. (define (operating-system-accounts os)
  660. "Return the user accounts for OS, including an obligatory 'root' account,
  661. and excluding accounts requested by services."
  662. ;; Make sure there's a root account.
  663. (if (find (lambda (user)
  664. (and=> (user-account-uid user) zero?))
  665. (operating-system-users os))
  666. (operating-system-users os)
  667. (cons %root-account (operating-system-users os))))
  668. (define (maybe-string->file file-name thing)
  669. "If THING is a string, return a <plain-file> with THING as its content.
  670. Otherwise just return THING.
  671. This is for backward-compatibility of fields that used to be strings and are
  672. now file-like objects.."
  673. (match thing
  674. ((? string?)
  675. (warning (G_ "using a string for file '~a' is deprecated; \
  676. use 'plain-file' instead~%")
  677. file-name)
  678. (plain-file file-name thing))
  679. (x
  680. x)))
  681. (define (maybe-file->monadic file-name thing)
  682. "If THING is a value in %STORE-MONAD, return it as is; otherwise return
  683. THING in the %STORE-MONAD.
  684. This is for backward-compatibility of fields that used to be monadic values
  685. and are now file-like objects."
  686. (with-monad %store-monad
  687. (match thing
  688. ((? procedure?)
  689. (warning (G_ "using a monadic value for '~a' is deprecated; \
  690. use 'plain-file' instead~%")
  691. file-name)
  692. thing)
  693. (x
  694. (return x)))))
  695. (define (operating-system-etc-directory os)
  696. "Return that static part of the /etc directory of OS."
  697. (etc-directory
  698. (fold-services (operating-system-services os)
  699. #:target-type etc-service-type)))
  700. (define (operating-system-environment-variables os)
  701. "Return the environment variables of OS for
  702. @var{session-environment-service-type}, to be used in @file{/etc/environment}."
  703. `(("LANG" . ,(operating-system-locale os))
  704. ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
  705. ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
  706. ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
  707. ;; Tell 'modprobe' & co. where to look for modules.
  708. ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
  709. ;; These variables are honored by OpenSSL (libssl) and Git.
  710. ("SSL_CERT_DIR" . "/etc/ssl/certs")
  711. ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
  712. ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
  713. ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
  714. ;; searched for.
  715. ("GTK_DATA_PREFIX" . "/run/current-system/profile")
  716. ;; By default, applications that use D-Bus, such as Emacs, abort at startup
  717. ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
  718. ("DBUS_FATAL_WARNINGS" . "0")
  719. ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
  720. ;; used to look things up in 'PREFIX/lib/locale' instead of
  721. ;; '/run/current-system/locale' as was intended. Keep this hack around so
  722. ;; that people who still have glibc@2.23-using packages in their profiles
  723. ;; can use them correctly.
  724. ;; TODO: Remove when glibc@2.23 is long gone.
  725. ("GUIX_LOCPATH" . "/run/current-system/locale")))
  726. (define %setuid-programs
  727. ;; Default set of setuid-root programs.
  728. (let ((shadow (@ (gnu packages admin) shadow)))
  729. (list (file-append shadow "/bin/passwd")
  730. (file-append shadow "/bin/su")
  731. (file-append shadow "/bin/newuidmap")
  732. (file-append shadow "/bin/newgidmap")
  733. (file-append inetutils "/bin/ping")
  734. (file-append inetutils "/bin/ping6")
  735. (file-append sudo "/bin/sudo")
  736. (file-append sudo "/bin/sudoedit")
  737. (file-append fuse "/bin/fusermount")
  738. ;; To allow mounts with the "user" option, "mount" and "umount" must
  739. ;; be setuid-root.
  740. (file-append util-linux "/bin/mount")
  741. (file-append util-linux "/bin/umount"))))
  742. (define %sudoers-specification
  743. ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
  744. ;; group can do anything. See
  745. ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
  746. ;; TODO: Add a declarative API.
  747. (plain-file "sudoers" "\
  748. root ALL=(ALL) ALL
  749. %wheel ALL=(ALL) ALL\n"))
  750. (define* (operating-system-activation-script os)
  751. "Return the activation script for OS---i.e., the code that \"activates\" the
  752. stateful part of OS, including user accounts and groups, special directories,
  753. etc."
  754. (let* ((services (operating-system-services os))
  755. (activation (fold-services services
  756. #:target-type activation-service-type)))
  757. (activation-service->script activation)))
  758. (define* (operating-system-boot-script os)
  759. "Return the boot script for OS---i.e., the code started by the initrd once
  760. we're running in the final root."
  761. (let* ((services (operating-system-services os))
  762. (boot (fold-services services #:target-type boot-service-type)))
  763. (service-value boot)))
  764. (define (operating-system-user-accounts os)
  765. "Return the list of user accounts of OS."
  766. (let* ((services (operating-system-services os))
  767. (account (fold-services services
  768. #:target-type account-service-type)))
  769. (filter user-account?
  770. (service-value account))))
  771. (define (operating-system-shepherd-service-names os)
  772. "Return the list of Shepherd service names for OS."
  773. (append-map shepherd-service-provision
  774. (service-value
  775. (fold-services (operating-system-services os)
  776. #:target-type
  777. shepherd-root-service-type))))
  778. (define* (operating-system-derivation os)
  779. "Return a derivation that builds OS."
  780. (let* ((services (operating-system-services os))
  781. (system (fold-services services)))
  782. ;; SYSTEM contains the derivation as a monadic value.
  783. (service-value system)))
  784. (define* (operating-system-profile os)
  785. "Return a derivation that builds the system profile of OS."
  786. (mlet* %store-monad
  787. ((services -> (operating-system-services os))
  788. (profile (fold-services services
  789. #:target-type profile-service-type)))
  790. (match profile
  791. (("profile" profile)
  792. (return profile)))))
  793. (define (operating-system-root-file-system os)
  794. "Return the root file system of OS."
  795. (find (lambda (fs)
  796. (string=? "/" (file-system-mount-point fs)))
  797. (operating-system-file-systems os)))
  798. (define (operating-system-initrd-file os)
  799. "Return a gexp denoting the initrd file of OS."
  800. (define boot-file-systems
  801. (filter file-system-needed-for-boot?
  802. (operating-system-file-systems os)))
  803. (define mapped-devices
  804. (operating-system-boot-mapped-devices os))
  805. (define make-initrd
  806. (operating-system-initrd os))
  807. (make-initrd boot-file-systems
  808. #:linux (operating-system-kernel os)
  809. #:linux-modules
  810. (operating-system-initrd-modules os)
  811. #:mapped-devices mapped-devices
  812. #:keyboard-layout (operating-system-keyboard-layout os)))
  813. (define (locale-name->definition* name)
  814. "Variant of 'locale-name->definition' that raises an error upon failure."
  815. (match (locale-name->definition name)
  816. (#f
  817. (raise (condition
  818. (&message
  819. (message (format #f (G_ "~a: invalid locale name") name))))))
  820. (def def)))
  821. (define (operating-system-locale-directory os)
  822. "Return the directory containing the locales compiled for the definitions
  823. listed in OS. The C library expects to find it under
  824. /run/current-system/locale."
  825. (define name
  826. (operating-system-locale os))
  827. (define definitions
  828. ;; While we're at it, check whether NAME is defined and add it if needed.
  829. (if (member name (map locale-definition-name
  830. (operating-system-locale-definitions os)))
  831. (operating-system-locale-definitions os)
  832. (cons (locale-name->definition* name)
  833. (operating-system-locale-definitions os))))
  834. (locale-directory definitions
  835. #:libcs (operating-system-locale-libcs os)))
  836. (define (kernel->boot-label kernel)
  837. "Return a label for the bootloader menu entry that boots KERNEL."
  838. (cond ((package? kernel)
  839. (string-append "GNU with "
  840. (string-titlecase (package-name kernel)) " "
  841. (package-version kernel)))
  842. ((inferior-package? kernel)
  843. (string-append "GNU with "
  844. (string-titlecase (inferior-package-name kernel)) " "
  845. (inferior-package-version kernel)))
  846. (else "GNU")))
  847. (define (operating-system-default-label os)
  848. "Return the default label for OS, as it will appear in the bootloader menu
  849. entry."
  850. (kernel->boot-label (operating-system-kernel os)))
  851. (define (store-file-system file-systems)
  852. "Return the file system object among FILE-SYSTEMS that contains the store."
  853. (match (filter (lambda (fs)
  854. (and (file-system-mount? fs)
  855. (not (memq 'bind-mount (file-system-flags fs)))
  856. (string-prefix? (file-system-mount-point fs)
  857. (%store-prefix))))
  858. file-systems)
  859. ((and candidates (head . tail))
  860. (reduce (lambda (fs1 fs2)
  861. (if (> (string-length (file-system-mount-point fs1))
  862. (string-length (file-system-mount-point fs2)))
  863. fs1
  864. fs2))
  865. head
  866. candidates))))
  867. (define (operating-system-store-file-system os)
  868. "Return the file system that contains the store of OS."
  869. (store-file-system (operating-system-file-systems os)))
  870. (define* (operating-system-bootcfg os #:optional (old-entries '()))
  871. "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
  872. a list of <menu-entry>, to populate the \"old entries\" menu."
  873. (let* ((root-fs (operating-system-root-file-system os))
  874. (root-device (file-system-device root-fs))
  875. (params (operating-system-boot-parameters
  876. os root-device
  877. #:system-kernel-arguments? #t))
  878. (entry (boot-parameters->menu-entry params))
  879. (bootloader-conf (operating-system-bootloader os)))
  880. (define generate-config-file
  881. (bootloader-configuration-file-generator
  882. (bootloader-configuration-bootloader bootloader-conf)))
  883. (generate-config-file bootloader-conf (list entry)
  884. #:old-entries old-entries)))
  885. (define* (operating-system-boot-parameters os root-device
  886. #:key system-kernel-arguments?)
  887. "Return a monadic <boot-parameters> record that describes the boot
  888. parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
  889. such as '--root' and '--load' to <boot-parameters>."
  890. (let* ((initrd (operating-system-initrd-file os))
  891. (store (operating-system-store-file-system os))
  892. (bootloader (bootloader-configuration-bootloader
  893. (operating-system-bootloader os)))
  894. (bootloader-name (bootloader-name bootloader))
  895. (label (operating-system-label os)))
  896. (boot-parameters
  897. (label label)
  898. (root-device root-device)
  899. (kernel (operating-system-kernel-file os))
  900. (kernel-arguments
  901. (if system-kernel-arguments?
  902. (operating-system-kernel-arguments os root-device)
  903. (operating-system-user-kernel-arguments os)))
  904. (initrd initrd)
  905. (bootloader-name bootloader-name)
  906. (bootloader-menu-entries
  907. (bootloader-configuration-menu-entries (operating-system-bootloader os)))
  908. (store-device (ensure-not-/dev (file-system-device store)))
  909. (store-mount-point (file-system-mount-point store)))))
  910. (define (device->sexp device)
  911. "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
  912. (match device
  913. ((? uuid? uuid)
  914. `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
  915. ((? file-system-label? label)
  916. `(file-system-label ,(file-system-label->string label)))
  917. (_
  918. device)))
  919. (define* (operating-system-boot-parameters-file os
  920. #:key system-kernel-arguments?)
  921. "Return a file that describes the boot parameters of OS. The primary use of
  922. this file is the reconstruction of GRUB menu entries for old configurations.
  923. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
  924. and '--load' to the returned file (since the returned file is then usually
  925. stored into the content-addressed \"system\" directory, it's usually not a
  926. good idea to give it because the content hash would change by the content hash
  927. being stored into the \"parameters\" file)."
  928. (let* ((root (operating-system-root-file-system os))
  929. (device (file-system-device root))
  930. (params (operating-system-boot-parameters
  931. os device
  932. #:system-kernel-arguments?
  933. system-kernel-arguments?)))
  934. (gexp->file "parameters"
  935. #~(boot-parameters
  936. (version 0)
  937. (label #$(boot-parameters-label params))
  938. (root-device
  939. #$(device->sexp
  940. (boot-parameters-root-device params)))
  941. (kernel #$(boot-parameters-kernel params))
  942. (kernel-arguments
  943. #$(boot-parameters-kernel-arguments params))
  944. (initrd #$(boot-parameters-initrd params))
  945. (bootloader-name #$(boot-parameters-bootloader-name params))
  946. (bootloader-menu-entries
  947. #$(map menu-entry->sexp
  948. (or (and=> (operating-system-bootloader os)
  949. bootloader-configuration-menu-entries)
  950. '())))
  951. (store
  952. (device
  953. #$(device->sexp (boot-parameters-store-device params)))
  954. (mount-point #$(boot-parameters-store-mount-point params))))
  955. #:set-load-path? #f)))
  956. (define-gexp-compiler (operating-system-compiler (os <operating-system>)
  957. system target)
  958. ((store-lift
  959. (lambda (store)
  960. ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
  961. ;; 'operating-system-derivation'.
  962. (run-with-store store (operating-system-derivation os)
  963. #:system system
  964. #:target target)))))
  965. ;;; system.scm ends here