system.scm 42 KB

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