system.scm 40 KB

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