system.scm 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  4. ;;; Copyright © 2016, 2017, 2018 Chris Marusich <cmmarusich@gmail.com>
  5. ;;; Copyright © 2017, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  7. ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
  8. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  9. ;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
  10. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  11. ;;;
  12. ;;; This file is part of GNU Guix.
  13. ;;;
  14. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  15. ;;; under the terms of the GNU General Public License as published by
  16. ;;; the Free Software Foundation; either version 3 of the License, or (at
  17. ;;; your option) any later version.
  18. ;;;
  19. ;;; GNU Guix is distributed in the hope that it will be useful, but
  20. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;;; GNU General Public License for more details.
  23. ;;;
  24. ;;; You should have received a copy of the GNU General Public License
  25. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  26. (define-module (guix scripts system)
  27. #:use-module (guix config)
  28. #:use-module (guix ui)
  29. #:use-module ((guix status) #:select (with-status-verbosity))
  30. #:use-module (guix store)
  31. #:autoload (guix base16) (bytevector->base16-string)
  32. #:autoload (guix store database)
  33. (sqlite-register store-database-file call-with-database)
  34. #:autoload (guix build store-copy) (copy-store-item)
  35. #:use-module (guix describe)
  36. #:use-module (guix grafts)
  37. #:use-module (guix gexp)
  38. #:use-module (guix derivations)
  39. #:use-module (guix packages)
  40. #:use-module (guix utils)
  41. #:use-module (guix monads)
  42. #:use-module (guix records)
  43. #:use-module (guix profiles)
  44. #:use-module (guix scripts)
  45. #:use-module (guix channels)
  46. #:use-module (guix scripts build)
  47. #:autoload (guix scripts package) (delete-generations
  48. delete-matching-generations)
  49. #:autoload (guix scripts pull) (channel-commit-hyperlink)
  50. #:autoload (guix graph) (export-graph node-type
  51. graph-backend-name %graph-backends)
  52. #:use-module (guix scripts graph)
  53. #:use-module (guix scripts system reconfigure)
  54. #:use-module (guix build utils)
  55. #:use-module (guix progress)
  56. #:use-module ((guix build syscalls) #:select (terminal-columns))
  57. #:use-module (gnu build install)
  58. #:autoload (gnu build file-systems)
  59. (find-partition-by-label find-partition-by-uuid)
  60. #:autoload (gnu build linux-modules)
  61. (device-module-aliases matching-modules)
  62. #:use-module (gnu system linux-initrd)
  63. #:use-module (gnu image)
  64. #:use-module (gnu system)
  65. #:use-module (gnu bootloader)
  66. #:use-module (gnu system file-systems)
  67. #:use-module (gnu system image)
  68. #:use-module (gnu system mapped-devices)
  69. #:use-module (gnu system linux-container)
  70. #:use-module (gnu system uuid)
  71. #:use-module (gnu system vm)
  72. #:use-module (gnu services)
  73. #:use-module (gnu services shepherd)
  74. #:use-module (gnu services herd)
  75. #:use-module (srfi srfi-1)
  76. #:use-module (srfi srfi-11)
  77. #:use-module (srfi srfi-19)
  78. #:use-module (srfi srfi-26)
  79. #:use-module (srfi srfi-34)
  80. #:use-module (srfi srfi-35)
  81. #:use-module (srfi srfi-37)
  82. #:use-module (ice-9 format)
  83. #:use-module (ice-9 match)
  84. #:use-module (rnrs bytevectors)
  85. #:export (guix-system
  86. read-operating-system))
  87. ;;;
  88. ;;; Operating system declaration.
  89. ;;;
  90. (define %user-module
  91. ;; Module in which the machine description file is loaded.
  92. (make-user-module '((gnu system)
  93. (gnu services)
  94. (gnu system shadow))))
  95. (define (read-operating-system file)
  96. "Read the operating-system declaration from FILE and return it."
  97. (load* file %user-module))
  98. ;;;
  99. ;;; Installation.
  100. ;;;
  101. (define-syntax-rule (save-load-path-excursion body ...)
  102. "Save the current values of '%load-path' and '%load-compiled-path', run
  103. BODY..., and restore them."
  104. (let ((path %load-path)
  105. (cpath %load-compiled-path))
  106. (dynamic-wind
  107. (const #t)
  108. (lambda ()
  109. body ...)
  110. (lambda ()
  111. (set! %load-path path)
  112. (set! %load-compiled-path cpath)))))
  113. (define-syntax-rule (save-environment-excursion body ...)
  114. "Save the current environment variables, run BODY..., and restore them."
  115. (let ((env (environ)))
  116. (dynamic-wind
  117. (const #t)
  118. (lambda ()
  119. body ...)
  120. (lambda ()
  121. (environ env)))))
  122. (define topologically-sorted*
  123. (store-lift topologically-sorted))
  124. (define* (copy-item item info target db
  125. #:key (log-port (current-error-port)))
  126. "Copy ITEM to the store under root directory TARGET and populate DB with the
  127. given INFO, a <path-info> record."
  128. (let ((dest (string-append target item)))
  129. (format log-port "copying '~a'...~%" item)
  130. ;; Remove DEST if it exists to make sure that (1) we do not fail badly
  131. ;; while trying to overwrite it (see <http://bugs.gnu.org/20722>), and
  132. ;; (2) we end up with the right contents.
  133. (when (false-if-exception (lstat dest))
  134. (for-each make-file-writable
  135. (find-files dest (lambda (file stat)
  136. (eq? 'directory (stat:type stat)))
  137. #:directories? #t))
  138. (delete-file-recursively dest))
  139. (copy-store-item item target
  140. #:deduplicate? #t)
  141. (sqlite-register db
  142. #:path item
  143. #:references (path-info-references info)
  144. #:deriver (path-info-deriver info)
  145. #:hash (string-append
  146. "sha256:"
  147. (bytevector->base16-string (path-info-hash info)))
  148. #:nar-size (path-info-nar-size info))))
  149. (define* (copy-closure item target
  150. #:key (log-port (current-error-port)))
  151. "Copy ITEM and all its dependencies to the store under root directory
  152. TARGET, and register them."
  153. (mlet* %store-monad ((to-copy (topologically-sorted* (list item)))
  154. (info (mapm %store-monad query-path-info* to-copy))
  155. (size -> (reduce + 0 (map path-info-nar-size info))))
  156. (define progress-bar
  157. (progress-reporter/bar (length to-copy)
  158. (format #f (G_ "copying to '~a'...")
  159. target)))
  160. (define state
  161. (string-append target "/var/guix"))
  162. (check-available-space size target)
  163. ;; Explicitly use "TARGET/var/guix" as the state directory to avoid
  164. ;; reproducing the user's current settings; see
  165. ;; <http://bugs.gnu.org/18049>.
  166. (call-with-database (store-database-file #:prefix target
  167. #:state-directory state)
  168. (lambda (db)
  169. (call-with-progress-reporter progress-bar
  170. (lambda (report)
  171. (let ((void (%make-void-port "w")))
  172. (for-each (lambda (item info)
  173. (copy-item item info target db #:log-port void)
  174. (report))
  175. to-copy info))))))
  176. (return *unspecified*)))
  177. (define* (install os-drv target
  178. #:key (log-port (current-output-port))
  179. install-bootloader? bootloader bootcfg)
  180. "Copy the closure of BOOTCFG, which includes the output of OS-DRV, to
  181. directory TARGET. TARGET must be an absolute directory name since that's what
  182. 'register-path' expects.
  183. When INSTALL-BOOTLOADER? is true, install bootloader using BOOTCFG."
  184. (define (maybe-copy to-copy)
  185. (with-monad %store-monad
  186. (if (string=? target "/")
  187. (begin
  188. (warning (G_ "initializing the current root file system~%"))
  189. (return #t))
  190. (begin
  191. ;; Make sure the target store exists.
  192. (mkdir-p (string-append target (%store-prefix)))
  193. ;; Copy items to the new store.
  194. (copy-closure to-copy target #:log-port log-port)))))
  195. ;; Make sure TARGET is root-owned when running as root, but still allow
  196. ;; non-root uses (useful for testing.) See
  197. ;; <http://lists.gnu.org/archive/html/guix-devel/2015-05/msg00452.html>.
  198. (if (zero? (geteuid))
  199. (chown target 0 0)
  200. (warning (G_ "not running as 'root', so \
  201. the ownership of '~a' may be incorrect!~%")
  202. target))
  203. ;; If a previous installation was attempted, make sure we start anew; in
  204. ;; particular, we don't want to keep a store database that might not
  205. ;; correspond to what we're actually putting in the store.
  206. (let ((state (string-append target "/var/guix")))
  207. (when (file-exists? state)
  208. (delete-file-recursively state)))
  209. (chmod target #o755)
  210. (let ((os-dir (derivation->output-path os-drv))
  211. (format (lift format %store-monad))
  212. (populate (lift2 populate-root-file-system %store-monad)))
  213. (mlet %store-monad ((bootcfg (lower-object bootcfg)))
  214. (mbegin %store-monad
  215. ;; Copy the closure of BOOTCFG, which includes OS-DIR,
  216. ;; eventual background image and so on.
  217. (maybe-copy (derivation->output-path bootcfg))
  218. ;; Create a bunch of additional files.
  219. (format log-port "populating '~a'...~%" target)
  220. (populate os-dir target)
  221. (mwhen install-bootloader?
  222. (install-bootloader local-eval bootloader bootcfg
  223. #:target target)
  224. (return
  225. (info (G_ "bootloader successfully installed on '~a'~%")
  226. (bootloader-configuration-target bootloader))))))))
  227. ;;;
  228. ;;; Reconfiguration.
  229. ;;;
  230. (define %system-profile
  231. ;; The system profile.
  232. (string-append %state-directory "/profiles/system"))
  233. (define-syntax-rule (with-shepherd-error-handling mbody ...)
  234. "Catch and report Shepherd errors that arise when binding MBODY, a monadic
  235. expression in %STORE-MONAD."
  236. (lambda (store)
  237. (catch 'system-error
  238. (lambda ()
  239. (guard (c ((shepherd-error? c)
  240. (values (report-shepherd-error c) store)))
  241. (values (run-with-store store (mbegin %store-monad mbody ...))
  242. store)))
  243. (lambda (key proc format-string format-args errno . rest)
  244. (warning (G_ "while talking to shepherd: ~a~%")
  245. (apply format #f format-string format-args))
  246. (values #f store)))))
  247. (define (report-shepherd-error error)
  248. "Report ERROR, a '&shepherd-error' error condition object."
  249. (when error
  250. (cond ((service-not-found-error? error)
  251. (warning (G_ "service '~a' could not be found~%")
  252. (service-not-found-error-service error)))
  253. ((action-not-found-error? error)
  254. (warning (G_ "service '~a' does not have an action '~a'~%")
  255. (action-not-found-error-service error)
  256. (action-not-found-error-action error)))
  257. ((action-exception-error? error)
  258. (warning (G_ "exception caught while executing '~a' \
  259. on service '~a':~%")
  260. (action-exception-error-action error)
  261. (action-exception-error-service error))
  262. (print-exception (current-error-port) #f
  263. (action-exception-error-key error)
  264. (action-exception-error-arguments error)))
  265. ((unknown-shepherd-error? error)
  266. (warning (G_ "something went wrong: ~s~%")
  267. (unknown-shepherd-error-sexp error)))
  268. ((shepherd-error? error)
  269. (warning (G_ "shepherd error~%"))))
  270. ;; Don't leave users out in the cold and explain what that means and what
  271. ;; they can do.
  272. (warning (G_ "some services could not be upgraded~%"))
  273. (display-hint (G_ "To allow changes to all the system services to take
  274. effect, you will need to reboot."))))
  275. (define-syntax-rule (unless-file-not-found exp)
  276. (catch 'system-error
  277. (lambda ()
  278. exp)
  279. (lambda args
  280. (if (= ENOENT (system-error-errno args))
  281. #f
  282. (apply throw args)))))
  283. (define (seconds->string seconds)
  284. "Return a string representing the date for SECONDS."
  285. (let ((time (make-time time-utc 0 seconds)))
  286. (date->string (time-utc->date time)
  287. "~Y-~m-~d ~H:~M")))
  288. (define* (profile-boot-parameters #:optional (profile %system-profile)
  289. (numbers
  290. (reverse (generation-numbers profile))))
  291. "Return a list of 'boot-parameters' for the generations of PROFILE specified
  292. by NUMBERS, which is a list of generation numbers. The list is ordered from
  293. the most recent to the oldest profiles."
  294. (define (system->boot-parameters system number time)
  295. (unless-file-not-found
  296. (let* ((params (read-boot-parameters-file system))
  297. (label (boot-parameters-label params)))
  298. (boot-parameters
  299. (inherit params)
  300. (label (string-append label " (#"
  301. (number->string number) ", "
  302. (seconds->string time) ")"))))))
  303. (let* ((systems (map (cut generation-file-name profile <>)
  304. numbers))
  305. (times (map (lambda (system)
  306. (unless-file-not-found
  307. (stat:mtime (lstat system))))
  308. systems)))
  309. (filter-map system->boot-parameters systems numbers times)))
  310. ;;;
  311. ;;; Roll-back.
  312. ;;;
  313. (define (roll-back-system store)
  314. "Roll back the system profile to its previous generation. STORE is an open
  315. connection to the store."
  316. (switch-to-system-generation store "-1"))
  317. ;;;
  318. ;;; Switch generations.
  319. ;;;
  320. (define (switch-to-system-generation store spec)
  321. "Switch the system profile to the generation specified by SPEC, and
  322. re-install bootloader with a configuration file that uses the specified system
  323. generation as its default entry. STORE is an open connection to the store."
  324. (let ((number (relative-generation-spec->number %system-profile spec)))
  325. (if number
  326. (begin
  327. (reinstall-bootloader store number)
  328. (switch-to-generation* %system-profile number))
  329. (leave (G_ "cannot switch to system generation '~a'~%") spec))))
  330. (define* (system-bootloader-name #:optional (system %system-profile))
  331. "Return the bootloader name stored in SYSTEM's \"parameters\" file."
  332. (let ((params (unless-file-not-found
  333. (read-boot-parameters-file system))))
  334. (boot-parameters-bootloader-name params)))
  335. (define (reinstall-bootloader store number)
  336. "Re-install bootloader for existing system profile generation NUMBER.
  337. STORE is an open connection to the store."
  338. (let* ((generation (generation-file-name %system-profile number))
  339. ;; Detect the bootloader used in %system-profile.
  340. (bootloader (lookup-bootloader-by-name (system-bootloader-name)))
  341. ;; Use the detected bootloader with default configuration.
  342. ;; It will be enough to allow the system to boot.
  343. (bootloader-config (bootloader-configuration
  344. (bootloader bootloader)))
  345. ;; Make the specified system generation the default entry.
  346. (params (first (profile-boot-parameters %system-profile
  347. (list number))))
  348. (locale (boot-parameters-locale params))
  349. (store-crypto-devices (boot-parameters-store-crypto-devices params))
  350. (store-directory-prefix
  351. (boot-parameters-store-directory-prefix params))
  352. (old-generations
  353. (delv number (reverse (generation-numbers %system-profile))))
  354. (old-params (profile-boot-parameters
  355. %system-profile old-generations))
  356. (entries (cons (boot-parameters->menu-entry params)
  357. (boot-parameters-bootloader-menu-entries params)))
  358. (old-entries (map boot-parameters->menu-entry old-params)))
  359. (run-with-store store
  360. (mlet* %store-monad
  361. ((bootcfg (lower-object
  362. ((bootloader-configuration-file-generator bootloader)
  363. bootloader-config entries
  364. #:locale locale
  365. #:store-crypto-devices store-crypto-devices
  366. #:store-directory-prefix store-directory-prefix
  367. #:old-entries old-entries)))
  368. (drvs -> (list bootcfg)))
  369. (mbegin %store-monad
  370. (built-derivations drvs)
  371. ;; Only install bootloader configuration file.
  372. (install-bootloader local-eval bootloader-config bootcfg
  373. #:run-installer? #f))))))
  374. ;;;
  375. ;;; Graphs.
  376. ;;;
  377. (define (service-node-label service)
  378. "Return a label to represent SERVICE."
  379. (let ((type (service-kind service))
  380. (value (service-value service)))
  381. (string-append (symbol->string (service-type-name type))
  382. (cond ((or (number? value) (symbol? value))
  383. (string-append " " (object->string value)))
  384. ((string? value)
  385. (string-append " " value))
  386. ((file-system? value)
  387. (string-append " " (file-system-mount-point value)))
  388. (else
  389. "")))))
  390. (define (service-node-type services)
  391. "Return a node type for SERVICES. Since <service> instances are not
  392. self-contained (they express dependencies on service types, not on services),
  393. we have to create the 'edges' procedure dynamically as a function of the full
  394. list of services."
  395. (node-type
  396. (name "service")
  397. (description "the DAG of services")
  398. (identifier (lift1 object-address %store-monad))
  399. (label service-node-label)
  400. (edges (lift1 (service-back-edges services) %store-monad))))
  401. (define (shepherd-service-node-label service)
  402. "Return a label for a node representing a <shepherd-service>."
  403. (string-join (map symbol->string (shepherd-service-provision service))))
  404. (define (shepherd-service-node-type services)
  405. "Return a node type for SERVICES, a list of <shepherd-service>."
  406. (node-type
  407. (name "shepherd-service")
  408. (description "the dependency graph of shepherd services")
  409. (identifier (lift1 shepherd-service-node-label %store-monad))
  410. (label shepherd-service-node-label)
  411. (edges (lift1 (shepherd-service-back-edges services) %store-monad))))
  412. ;;;
  413. ;;; Generations.
  414. ;;;
  415. (define* (display-system-generation number
  416. #:optional (profile %system-profile))
  417. "Display a summary of system generation NUMBER in a human-readable format."
  418. (define (display-channel channel)
  419. (format #t " ~a:~%" (channel-name channel))
  420. (format #t (G_ " repository URL: ~a~%") (channel-url channel))
  421. (when (channel-branch channel)
  422. (format #t (G_ " branch: ~a~%") (channel-branch channel)))
  423. (format #t (G_ " commit: ~a~%")
  424. (if (supports-hyperlinks?)
  425. (channel-commit-hyperlink channel)
  426. (channel-commit channel))))
  427. (unless (zero? number)
  428. (let* ((generation (generation-file-name profile number))
  429. (params (read-boot-parameters-file generation))
  430. (label (boot-parameters-label params))
  431. (bootloader-name (boot-parameters-bootloader-name params))
  432. (root (boot-parameters-root-device params))
  433. (root-device (if (bytevector? root)
  434. (uuid->string root)
  435. root))
  436. (kernel (boot-parameters-kernel params))
  437. (multiboot-modules (boot-parameters-multiboot-modules params)))
  438. (define-values (channels config-file)
  439. (system-provenance generation))
  440. (display-generation profile number)
  441. (format #t (G_ " file name: ~a~%") generation)
  442. (format #t (G_ " canonical file name: ~a~%") (readlink* generation))
  443. ;; TRANSLATORS: Please preserve the two-space indentation.
  444. (format #t (G_ " label: ~a~%") label)
  445. (format #t (G_ " bootloader: ~a~%") bootloader-name)
  446. ;; TRANSLATORS: The '~[', '~;', and '~]' sequences in this string must
  447. ;; be preserved. They denote conditionals, such that the result will
  448. ;; look like:
  449. ;; root device: UUID: 12345-678
  450. ;; or:
  451. ;; root device: label: "my-root"
  452. ;; or just:
  453. ;; root device: /dev/sda3
  454. (format #t (G_ " root device: ~[UUID: ~a~;label: ~s~;~a~]~%")
  455. (cond ((uuid? root-device) 0)
  456. ((file-system-label? root-device) 1)
  457. (else 2))
  458. (file-system-device->string root-device))
  459. (format #t (G_ " kernel: ~a~%") kernel)
  460. (match multiboot-modules
  461. (() #f)
  462. (((modules . _) ...)
  463. (format #t (G_ " multiboot: ~a~%")
  464. (string-join modules "\n "))))
  465. (unless (null? channels)
  466. ;; TRANSLATORS: Here "channel" is the same terminology as used in
  467. ;; "guix describe" and "guix pull --channels".
  468. (format #t (G_ " channels:~%"))
  469. (for-each display-channel channels))
  470. (when config-file
  471. (format #t (G_ " configuration file: ~a~%")
  472. (if (supports-hyperlinks?)
  473. (file-hyperlink config-file)
  474. config-file))))))
  475. (define* (list-generations pattern #:optional (profile %system-profile))
  476. "Display in a human-readable format all the system generations matching
  477. PATTERN, a string. When PATTERN is #f, display all the system generations."
  478. (cond ((not (file-exists? profile)) ; XXX: race condition
  479. (raise (condition (&profile-not-found-error
  480. (profile profile)))))
  481. ((not pattern)
  482. (for-each display-system-generation (profile-generations profile)))
  483. ((matching-generations pattern profile)
  484. =>
  485. (lambda (numbers)
  486. (if (null-list? numbers)
  487. (exit 1)
  488. (leave-on-EPIPE
  489. (for-each display-system-generation numbers)))))))
  490. ;;;
  491. ;;; File system declaration checks.
  492. ;;;
  493. (define (check-file-system-availability file-systems)
  494. "Check whether the UUIDs or partition labels that FILE-SYSTEMS refer to, if
  495. any, are available. Raise an error if they're not."
  496. (define relevant
  497. (filter (lambda (fs)
  498. (and (file-system-mount? fs)
  499. (not (member (file-system-type fs)
  500. %pseudo-file-system-types))
  501. ;; Don't try to validate network file systems.
  502. (not (string-prefix? "nfs" (file-system-type fs)))
  503. (not (memq 'bind-mount (file-system-flags fs)))))
  504. file-systems))
  505. (define labeled
  506. (filter (lambda (fs)
  507. (file-system-label? (file-system-device fs)))
  508. relevant))
  509. (define literal
  510. (filter (lambda (fs)
  511. (string? (file-system-device fs)))
  512. relevant))
  513. (define uuid
  514. (filter (lambda (fs)
  515. (uuid? (file-system-device fs)))
  516. relevant))
  517. (define fail? #f)
  518. (define (file-system-location* fs)
  519. (and=> (file-system-location fs)
  520. source-properties->location))
  521. (let-syntax ((error (syntax-rules ()
  522. ((_ args ...)
  523. (begin
  524. (set! fail? #t)
  525. (report-error args ...))))))
  526. (for-each (lambda (fs)
  527. (catch 'system-error
  528. (lambda ()
  529. (stat (file-system-device fs)))
  530. (lambda args
  531. (let ((errno (system-error-errno args))
  532. (device (file-system-device fs)))
  533. (error (file-system-location* fs)
  534. (G_ "device '~a' not found: ~a~%")
  535. device (strerror errno))
  536. (unless (string-prefix? "/" device)
  537. (display-hint (format #f (G_ "If '~a' is a file system
  538. label, write @code{(file-system-label ~s)} in your @code{device} field.")
  539. device device)))))))
  540. literal)
  541. (for-each (lambda (fs)
  542. (let ((label (file-system-label->string
  543. (file-system-device fs))))
  544. (unless (find-partition-by-label label)
  545. (error (file-system-location* fs)
  546. (G_ "file system with label '~a' not found~%")
  547. label))))
  548. labeled)
  549. (for-each (lambda (fs)
  550. (unless (find-partition-by-uuid (file-system-device fs))
  551. (error (file-system-location* fs)
  552. (G_ "file system with UUID '~a' not found~%")
  553. (uuid->string (file-system-device fs)))))
  554. uuid)
  555. (when fail?
  556. ;; Better be safe than sorry.
  557. (exit 1))))
  558. (define (check-mapped-devices os)
  559. "Check that each of MAPPED-DEVICES is valid according to the 'check'
  560. procedure of its type."
  561. (define boot-mapped-devices
  562. (operating-system-boot-mapped-devices os))
  563. (define (needed-for-boot? md)
  564. (memq md boot-mapped-devices))
  565. (define initrd-modules
  566. (operating-system-initrd-modules os))
  567. (for-each (lambda (md)
  568. (let ((check (mapped-device-kind-check
  569. (mapped-device-type md))))
  570. ;; We expect CHECK to raise an exception with a detailed
  571. ;; '&message' if something goes wrong.
  572. (check md
  573. #:needed-for-boot? (needed-for-boot? md)
  574. #:initrd-modules initrd-modules)))
  575. (operating-system-mapped-devices os)))
  576. (define (check-initrd-modules os)
  577. "Check that modules needed by 'needed-for-boot' file systems in OS are
  578. available in the initrd. Note that mapped devices are responsible for
  579. checking this by themselves in their 'check' procedure."
  580. (define (file-system-/dev fs)
  581. (let ((device (file-system-device fs)))
  582. (match device
  583. ((? string?)
  584. device)
  585. ((? uuid?)
  586. (find-partition-by-uuid device))
  587. ((? file-system-label?)
  588. (find-partition-by-label (file-system-label->string device))))))
  589. (define file-systems
  590. (filter file-system-needed-for-boot?
  591. (operating-system-file-systems os)))
  592. (for-each (lambda (fs)
  593. (check-device-initrd-modules (file-system-/dev fs)
  594. (operating-system-initrd-modules os)
  595. (source-properties->location
  596. (file-system-location fs))))
  597. file-systems))
  598. ;;;
  599. ;;; Action.
  600. ;;;
  601. (define* (system-derivation-for-action os action
  602. #:key image-size image-type
  603. full-boot? container-shared-network?
  604. mappings label
  605. volatile-root?)
  606. "Return as a monadic value the derivation for OS according to ACTION."
  607. (mlet %store-monad ((target (current-target-system)))
  608. (case action
  609. ((build init reconfigure)
  610. (operating-system-derivation os))
  611. ((container)
  612. (container-script
  613. os
  614. #:mappings mappings
  615. #:shared-network? container-shared-network?))
  616. ((vm-image)
  617. (system-qemu-image os #:disk-image-size image-size))
  618. ((vm)
  619. (system-qemu-image/shared-store-script os
  620. #:full-boot? full-boot?
  621. #:disk-image-size
  622. (if full-boot?
  623. image-size
  624. (* 70 (expt 2 20)))
  625. #:mappings mappings))
  626. ((image disk-image)
  627. (let* ((base-image (os->image os #:type image-type))
  628. (base-target (image-target base-image)))
  629. (when (eq? action 'disk-image)
  630. (warning (G_ "'disk-image' is deprecated: use 'image' instead~%")))
  631. (lower-object
  632. (system-image
  633. (image
  634. (inherit (if label
  635. (image-with-label base-image label)
  636. base-image))
  637. (target (or base-target target))
  638. (size image-size)
  639. (operating-system os)
  640. (volatile-root? volatile-root?))))))
  641. ((docker-image)
  642. (system-docker-image os
  643. #:shared-network? container-shared-network?)))))
  644. (define (maybe-suggest-running-guix-pull)
  645. "Suggest running 'guix pull' if this has never been done before."
  646. ;; Check whether we're running a 'guix pull'-provided 'guix' command. When
  647. ;; 'current-profile' returns #f, we may be running the globally-installed
  648. ;; 'guix' and thus run the risk of deploying an older 'guix'. See
  649. ;; <https://lists.gnu.org/archive/html/guix-devel/2014-08/msg00057.html>
  650. (unless (or (current-profile) (getenv "GUIX_UNINSTALLED"))
  651. (warning (G_ "Consider running 'guix pull' before 'reconfigure'.~%"))
  652. (warning (G_ "Failing to do that may downgrade your system!~%"))))
  653. (define (bootloader-installer-script installer
  654. bootloader device target)
  655. "Return a file calling INSTALLER gexp with given BOOTLOADER, DEVICE
  656. and TARGET arguments."
  657. (scheme-file "bootloader-installer"
  658. (with-imported-modules '((gnu build bootloader)
  659. (guix build utils))
  660. #~(begin
  661. (use-modules (gnu build bootloader)
  662. (guix build utils)
  663. (ice-9 binary-ports)
  664. (srfi srfi-34)
  665. (srfi srfi-35))
  666. (guard (c ((message-condition? c) ;XXX: i18n
  667. (format (current-error-port) "error: ~a~%"
  668. (condition-message c))
  669. (exit 1)))
  670. (#$installer #$bootloader #$device #$target)
  671. (info (G_ "bootloader successfully installed on '~a'~%")
  672. #$device))))))
  673. (define (local-eval exp)
  674. "Evaluate EXP, a G-Expression, in-place."
  675. (mlet* %store-monad ((lowered (lower-gexp exp))
  676. (_ (built-derivations (lowered-gexp-inputs lowered))))
  677. (save-load-path-excursion
  678. (set! %load-path (lowered-gexp-load-path lowered))
  679. (set! %load-compiled-path (lowered-gexp-load-compiled-path lowered))
  680. (return (primitive-eval (lowered-gexp-sexp lowered))))))
  681. (define* (perform-action action os
  682. #:key
  683. (validate-reconfigure ensure-forward-reconfigure)
  684. save-provenance?
  685. skip-safety-checks?
  686. install-bootloader?
  687. dry-run? derivations-only?
  688. use-substitutes? bootloader-target target
  689. image-size image-type
  690. volatile-root?
  691. full-boot? label container-shared-network?
  692. (mappings '())
  693. (gc-root #f))
  694. "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install
  695. bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
  696. target root directory; IMAGE-SIZE is the size of the image to be built, for
  697. the 'vm-image' and 'image' actions. IMAGE-TYPE is the type of image to
  698. be built. When VOLATILE-ROOT? is #t, the root file system is mounted
  699. volatile.
  700. FULL-BOOT? is used for the 'vm' action; it determines whether to
  701. boot directly to the kernel or to the bootloader. CONTAINER-SHARED-NETWORK?
  702. determines if the container will use a separate network namespace.
  703. When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
  704. building anything.
  705. When GC-ROOT is a path, also make that path an indirect root of the build
  706. output when building a system derivation, such as a disk image.
  707. When SKIP-SAFETY-CHECKS? is true, skip the file system and initrd module
  708. static checks."
  709. (define println
  710. (cut format #t "~a~%" <>))
  711. (define menu-entries
  712. (if (eq? 'init action)
  713. '()
  714. (map boot-parameters->menu-entry (profile-boot-parameters))))
  715. (define bootloader
  716. (operating-system-bootloader os))
  717. (define bootcfg
  718. (and (memq action '(init reconfigure))
  719. (operating-system-bootcfg os menu-entries)))
  720. (when (eq? action 'reconfigure)
  721. (maybe-suggest-running-guix-pull)
  722. (check-forward-update validate-reconfigure))
  723. ;; Check whether the declared file systems exist. This is better than
  724. ;; instantiating a broken configuration. Assume that we can only check if
  725. ;; running as root.
  726. (when (and (not skip-safety-checks?)
  727. (memq action '(init reconfigure)))
  728. (check-mapped-devices os)
  729. (when (zero? (getuid))
  730. (check-file-system-availability (operating-system-file-systems os))
  731. (check-initrd-modules os)))
  732. (mlet* %store-monad
  733. ((sys (system-derivation-for-action os action
  734. #:label label
  735. #:image-type image-type
  736. #:image-size image-size
  737. #:volatile-root? volatile-root?
  738. #:full-boot? full-boot?
  739. #:container-shared-network? container-shared-network?
  740. #:mappings mappings))
  741. ;; For 'init' and 'reconfigure', always build BOOTCFG, even if
  742. ;; --no-bootloader is passed, because we then use it as a GC root.
  743. ;; See <http://bugs.gnu.org/21068>.
  744. (drvs (mapm/accumulate-builds lower-object
  745. (if (memq action '(init reconfigure))
  746. (list sys bootcfg)
  747. (list sys))))
  748. (% (if derivations-only?
  749. (return (for-each (compose println derivation-file-name)
  750. drvs))
  751. (built-derivations drvs))))
  752. (if (or dry-run? derivations-only?)
  753. (return #f)
  754. (begin
  755. (for-each (compose println derivation->output-path)
  756. drvs)
  757. (case action
  758. ((reconfigure)
  759. (newline)
  760. (format #t (G_ "activating system...~%"))
  761. (mbegin %store-monad
  762. (switch-to-system local-eval os)
  763. (mwhen install-bootloader?
  764. (install-bootloader local-eval bootloader bootcfg
  765. #:target (or target "/"))
  766. (return
  767. (info (G_ "bootloader successfully installed on '~a'~%")
  768. (bootloader-configuration-target bootloader))))
  769. (with-shepherd-error-handling
  770. (upgrade-shepherd-services local-eval os)
  771. (return (format #t (G_ "\
  772. To complete the upgrade, run 'herd restart SERVICE' to stop,
  773. upgrade, and restart each service that was not automatically restarted.\n")))
  774. (return (format #t (G_ "\
  775. Run 'herd status' to view the list of services on your system.\n"))))))
  776. ((init)
  777. (newline)
  778. (format #t (G_ "initializing operating system under '~a'...~%")
  779. target)
  780. (install sys (canonicalize-path target)
  781. #:install-bootloader? install-bootloader?
  782. #:bootloader bootloader
  783. #:bootcfg bootcfg))
  784. (else
  785. ;; All we had to do was to build SYS and maybe register an
  786. ;; indirect GC root.
  787. (let ((output (derivation->output-path sys)))
  788. (mbegin %store-monad
  789. (mwhen gc-root
  790. (register-root* (list output) gc-root))
  791. (return output)))))))))
  792. (define (lookup-backend name) ;TODO: factorize
  793. "Return the graph backend called NAME. Raise an error if it is not found."
  794. (or (find (lambda (backend)
  795. (string=? (graph-backend-name backend) name))
  796. %graph-backends)
  797. (leave (G_ "~a: unknown backend~%") name)))
  798. (define* (export-extension-graph os port
  799. #:key (backend (lookup-backend "graphviz")))
  800. "Export the service extension graph of OS to PORT using BACKEND."
  801. (let* ((services (operating-system-services os))
  802. (system (find (lambda (service)
  803. (eq? (service-kind service) system-service-type))
  804. services)))
  805. (export-graph (list system) (current-output-port)
  806. #:backend backend
  807. #:node-type (service-node-type services)
  808. #:reverse-edges? #t)))
  809. (define* (export-shepherd-graph os port
  810. #:key (backend (lookup-backend "graphviz")))
  811. "Export the graph of shepherd services of OS to PORT using BACKEND."
  812. (let* ((services (operating-system-services os))
  813. (pid1 (fold-services services
  814. #:target-type shepherd-root-service-type))
  815. (shepherds (service-value pid1)) ;list of <shepherd-service>
  816. (sinks (filter (lambda (service)
  817. (null? (shepherd-service-requirement service)))
  818. shepherds)))
  819. (export-graph sinks (current-output-port)
  820. #:backend backend
  821. #:node-type (shepherd-service-node-type shepherds)
  822. #:reverse-edges? #t)))
  823. ;;;
  824. ;;; Images.
  825. ;;;
  826. (define (list-image-types)
  827. "Print the available image types."
  828. (display (G_ "The available image types are:\n"))
  829. (newline)
  830. (format #t "~{ - ~a ~%~}" (map image-type-name (force %image-types))))
  831. ;;;
  832. ;;; Options.
  833. ;;;
  834. (define (show-help)
  835. (display (G_ "Usage: guix system [OPTION ...] ACTION [ARG ...] [FILE]
  836. Build the operating system declared in FILE according to ACTION.
  837. Some ACTIONS support additional ARGS.\n"))
  838. (newline)
  839. (display (G_ "The valid values for ACTION are:\n"))
  840. (newline)
  841. (display (G_ "\
  842. search search for existing service types\n"))
  843. (display (G_ "\
  844. reconfigure switch to a new operating system configuration\n"))
  845. (display (G_ "\
  846. roll-back switch to the previous operating system configuration\n"))
  847. (display (G_ "\
  848. describe describe the current system\n"))
  849. (display (G_ "\
  850. list-generations list the system generations\n"))
  851. (display (G_ "\
  852. switch-generation switch to an existing operating system configuration\n"))
  853. (display (G_ "\
  854. delete-generations delete old system generations\n"))
  855. (display (G_ "\
  856. build build the operating system without installing anything\n"))
  857. (display (G_ "\
  858. container build a container that shares the host's store\n"))
  859. (display (G_ "\
  860. vm build a virtual machine image that shares the host's store\n"))
  861. (display (G_ "\
  862. vm-image build a freestanding virtual machine image\n"))
  863. (display (G_ "\
  864. image build a Guix System image\n"))
  865. (display (G_ "\
  866. docker-image build a Docker image\n"))
  867. (display (G_ "\
  868. init initialize a root file system to run GNU\n"))
  869. (display (G_ "\
  870. extension-graph emit the service extension graph in Dot format\n"))
  871. (display (G_ "\
  872. shepherd-graph emit the graph of shepherd services in Dot format\n"))
  873. (show-build-options-help)
  874. (display (G_ "
  875. -d, --derivation return the derivation of the given system"))
  876. (display (G_ "
  877. -e, --expression=EXPR consider the operating-system EXPR evaluates to
  878. instead of reading FILE, when applicable"))
  879. (display (G_ "
  880. --allow-downgrades for 'reconfigure', allow downgrades to earlier
  881. channel revisions"))
  882. (display (G_ "
  883. --on-error=STRATEGY
  884. apply STRATEGY (one of nothing-special, backtrace,
  885. or debug) when an error occurs while reading FILE"))
  886. (display (G_ "
  887. --list-image-types list available image types"))
  888. (display (G_ "
  889. -t, --image-type=TYPE for 'image', produce an image of TYPE"))
  890. (display (G_ "
  891. --image-size=SIZE for 'vm-image', produce an image of SIZE"))
  892. (display (G_ "
  893. --no-bootloader for 'init', do not install a bootloader"))
  894. (display (G_ "
  895. --volatile for 'image', make the root file system volatile"))
  896. (display (G_ "
  897. --label=LABEL for 'image', label disk image with LABEL"))
  898. (display (G_ "
  899. --save-provenance save provenance information"))
  900. (display (G_ "
  901. --share=SPEC for 'vm' and 'container', share host file system with
  902. read/write access according to SPEC"))
  903. (display (G_ "
  904. --expose=SPEC for 'vm' and 'container', expose host file system
  905. directory as read-only according to SPEC"))
  906. (display (G_ "
  907. -N, --network for 'container', allow containers to access the network"))
  908. (display (G_ "
  909. -r, --root=FILE for 'vm', 'vm-image', 'image', 'container',
  910. and 'build', make FILE a symlink to the result, and
  911. register it as a garbage collector root"))
  912. (display (G_ "
  913. --full-boot for 'vm', make a full boot sequence"))
  914. (display (G_ "
  915. --skip-checks skip file system and initrd module safety checks"))
  916. (display (G_ "
  917. --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
  918. (display (G_ "
  919. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  920. (newline)
  921. (display (G_ "
  922. --graph-backend=BACKEND
  923. use BACKEND for 'extension-graphs' and 'shepherd-graph'"))
  924. (newline)
  925. (display (G_ "
  926. -h, --help display this help and exit"))
  927. (display (G_ "
  928. -V, --version display version information and exit"))
  929. (newline)
  930. (show-bug-report-information))
  931. (define %options
  932. ;; Specifications of the command-line options.
  933. (cons* (option '(#\h "help") #f #f
  934. (lambda args
  935. (show-help)
  936. (exit 0)))
  937. (option '(#\V "version") #f #f
  938. (lambda args
  939. (show-version-and-exit "guix system")))
  940. (option '(#\e "expression") #t #f
  941. (lambda (opt name arg result)
  942. (alist-cons 'expression arg result)))
  943. (option '(#\d "derivation") #f #f
  944. (lambda (opt name arg result)
  945. (alist-cons 'derivations-only? #t result)))
  946. (option '("allow-downgrades") #f #f
  947. (lambda (opt name arg result)
  948. (alist-cons 'validate-reconfigure
  949. warn-about-backward-reconfigure
  950. result)))
  951. (option '("on-error") #t #f
  952. (lambda (opt name arg result)
  953. (alist-cons 'on-error (string->symbol arg)
  954. result)))
  955. (option '(#\t "image-type") #t #f
  956. (lambda (opt name arg result)
  957. (alist-cons 'image-type (string->symbol arg)
  958. result)))
  959. (option '("list-image-types") #f #f
  960. (lambda (opt name arg result)
  961. (list-image-types)
  962. (exit 0)))
  963. (option '("image-size") #t #f
  964. (lambda (opt name arg result)
  965. (alist-cons 'image-size (size->number arg)
  966. result)))
  967. (option '(#\N "network") #f #f
  968. (lambda (opt name arg result)
  969. (alist-cons 'container-shared-network? #t result)))
  970. (option '("no-bootloader" "no-grub") #f #f
  971. (lambda (opt name arg result)
  972. (alist-cons 'install-bootloader? #f result)))
  973. (option '("volatile") #f #f
  974. (lambda (opt name arg result)
  975. (alist-cons 'volatile-root? #t result)))
  976. (option '("label") #t #f
  977. (lambda (opt name arg result)
  978. (alist-cons 'label arg result)))
  979. (option '("full-boot") #f #f
  980. (lambda (opt name arg result)
  981. (alist-cons 'full-boot? #t result)))
  982. (option '("save-provenance") #f #f
  983. (lambda (opt name arg result)
  984. (alist-cons 'save-provenance? #t result)))
  985. (option '("skip-checks") #f #f
  986. (lambda (opt name arg result)
  987. (alist-cons 'skip-safety-checks? #t result)))
  988. (option '("share") #t #f
  989. (lambda (opt name arg result)
  990. (alist-cons 'file-system-mapping
  991. (specification->file-system-mapping arg #t)
  992. result)))
  993. (option '("expose") #t #f
  994. (lambda (opt name arg result)
  995. (alist-cons 'file-system-mapping
  996. (specification->file-system-mapping arg #f)
  997. result)))
  998. (option '(#\n "dry-run") #f #f
  999. (lambda (opt name arg result)
  1000. (alist-cons 'dry-run? #t result)))
  1001. (option '(#\v "verbosity") #t #f
  1002. (lambda (opt name arg result)
  1003. (let ((level (string->number* arg)))
  1004. (alist-cons 'verbosity level
  1005. (alist-delete 'verbosity result)))))
  1006. (option '(#\s "system") #t #f
  1007. (lambda (opt name arg result)
  1008. (alist-cons 'system arg
  1009. (alist-delete 'system result eq?))))
  1010. (option '("target") #t #f
  1011. (lambda (opt name arg result)
  1012. (alist-cons 'target arg
  1013. (alist-delete 'target result eq?))))
  1014. (option '(#\r "root") #t #f
  1015. (lambda (opt name arg result)
  1016. (alist-cons 'gc-root arg result)))
  1017. (option '("graph-backend") #t #f
  1018. (lambda (opt name arg result)
  1019. (alist-cons 'graph-backend arg result)))
  1020. %standard-build-options))
  1021. (define %default-options
  1022. ;; Alist of default option values.
  1023. `((system . ,(%current-system))
  1024. (target . #f)
  1025. (substitutes? . #t)
  1026. (offload? . #t)
  1027. (print-build-trace? . #t)
  1028. (print-extended-build-trace? . #t)
  1029. (multiplexed-build-output? . #t)
  1030. (graft? . #t)
  1031. (debug . 0)
  1032. (verbosity . #f) ;default
  1033. (validate-reconfigure . ,ensure-forward-reconfigure)
  1034. (image-type . efi-raw)
  1035. (image-size . guess)
  1036. (install-bootloader? . #t)
  1037. (label . #f)
  1038. (volatile-root? . #f)
  1039. (graph-backend . "graphviz")))
  1040. (define (verbosity-level opts)
  1041. "Return the verbosity level based on OPTS, the alist of parsed options."
  1042. (or (assoc-ref opts 'verbosity)
  1043. (if (eq? (assoc-ref opts 'action) 'build)
  1044. 2 1)))
  1045. ;;;
  1046. ;;; Entry point.
  1047. ;;;
  1048. (define (process-action action args opts)
  1049. "Process ACTION, a sub-command, with the arguments are listed in ARGS.
  1050. ACTION must be one of the sub-commands that takes an operating system
  1051. declaration as an argument (a file name.) OPTS is the raw alist of options
  1052. resulting from command-line parsing."
  1053. (define (ensure-operating-system file-or-exp obj)
  1054. (unless (operating-system? obj)
  1055. (leave (G_ "'~a' does not return an operating system~%")
  1056. file-or-exp))
  1057. obj)
  1058. (define save-provenance?
  1059. (or (assoc-ref opts 'save-provenance?)
  1060. (memq action '(init reconfigure))))
  1061. (let* ((file (match args
  1062. (() #f)
  1063. ((x . _) x)))
  1064. (expr (assoc-ref opts 'expression))
  1065. (system (assoc-ref opts 'system))
  1066. (target (assoc-ref opts 'target))
  1067. (transform (if save-provenance?
  1068. (cut operating-system-with-provenance <> file)
  1069. identity))
  1070. (os (transform
  1071. (ensure-operating-system
  1072. (or file expr)
  1073. (cond
  1074. ((and expr file)
  1075. (leave
  1076. (G_ "both file and expression cannot be specified~%")))
  1077. (expr
  1078. (read/eval expr))
  1079. (file
  1080. (load* file %user-module
  1081. #:on-error (assoc-ref opts 'on-error)))
  1082. (else
  1083. (leave (G_ "no configuration specified~%")))))))
  1084. (dry? (assoc-ref opts 'dry-run?))
  1085. (bootloader? (assoc-ref opts 'install-bootloader?))
  1086. (label (assoc-ref opts 'label))
  1087. (target-file (match args
  1088. ((first second) second)
  1089. (_ #f)))
  1090. (bootloader-target
  1091. (and bootloader?
  1092. (bootloader-configuration-target
  1093. (operating-system-bootloader os)))))
  1094. (define (graph-backend)
  1095. (lookup-backend (assoc-ref opts 'graph-backend)))
  1096. (with-store store
  1097. (set-build-options-from-command-line store opts)
  1098. (with-build-handler (build-notifier #:use-substitutes?
  1099. (assoc-ref opts 'substitutes?)
  1100. #:verbosity
  1101. (verbosity-level opts)
  1102. #:dry-run?
  1103. (assoc-ref opts 'dry-run?))
  1104. (run-with-store store
  1105. (mbegin %store-monad
  1106. (set-guile-for-build (default-guile))
  1107. (case action
  1108. ((extension-graph)
  1109. (export-extension-graph os (current-output-port)
  1110. #:backend (graph-backend)))
  1111. ((shepherd-graph)
  1112. (export-shepherd-graph os (current-output-port)
  1113. #:backend (graph-backend)))
  1114. (else
  1115. (unless (memq action '(build init))
  1116. (warn-about-old-distro #:suggested-command
  1117. "guix system reconfigure"))
  1118. (perform-action action os
  1119. #:dry-run? dry?
  1120. #:derivations-only? (assoc-ref opts
  1121. 'derivations-only?)
  1122. #:use-substitutes? (assoc-ref opts 'substitutes?)
  1123. #:skip-safety-checks?
  1124. (assoc-ref opts 'skip-safety-checks?)
  1125. #:validate-reconfigure
  1126. (assoc-ref opts 'validate-reconfigure)
  1127. #:image-type (lookup-image-type-by-name
  1128. (assoc-ref opts 'image-type))
  1129. #:image-size (assoc-ref opts 'image-size)
  1130. #:volatile-root?
  1131. (assoc-ref opts 'volatile-root?)
  1132. #:full-boot? (assoc-ref opts 'full-boot?)
  1133. #:container-shared-network?
  1134. (assoc-ref opts 'container-shared-network?)
  1135. #:mappings (filter-map (match-lambda
  1136. (('file-system-mapping . m)
  1137. m)
  1138. (_ #f))
  1139. opts)
  1140. #:install-bootloader? bootloader?
  1141. #:label label
  1142. #:target target-file
  1143. #:bootloader-target bootloader-target
  1144. #:gc-root (assoc-ref opts 'gc-root)))))
  1145. #:target target
  1146. #:system system)))
  1147. (warn-about-disk-space)))
  1148. (define (resolve-subcommand name)
  1149. (let ((module (resolve-interface
  1150. `(guix scripts system ,(string->symbol name))))
  1151. (proc (string->symbol (string-append "guix-system-" name))))
  1152. (module-ref module proc)))
  1153. (define (process-command command args opts)
  1154. "Process COMMAND, one of the 'guix system' sub-commands. ARGS is its
  1155. argument list and OPTS is the option alist."
  1156. (define-syntax-rule (with-store* store exp ...)
  1157. (with-store store
  1158. (set-build-options-from-command-line store opts)
  1159. exp ...))
  1160. (case command
  1161. ;; The following commands do not need to use the store, and they do not need
  1162. ;; an operating system configuration file.
  1163. ((list-generations)
  1164. (let ((pattern (match args
  1165. (() #f)
  1166. ((pattern) pattern)
  1167. (x (leave (G_ "wrong number of arguments~%"))))))
  1168. (list-generations pattern)))
  1169. ((describe)
  1170. (match (generation-number %system-profile)
  1171. (0
  1172. (error (G_ "no system generation, nothing to describe~%")))
  1173. (generation
  1174. (display-system-generation generation))))
  1175. ((search)
  1176. (apply (resolve-subcommand "search") args))
  1177. ;; The following commands need to use the store, but they do not need an
  1178. ;; operating system configuration file.
  1179. ((delete-generations)
  1180. (let ((pattern (match args
  1181. (() #f)
  1182. ((pattern) pattern)
  1183. (x (leave (G_ "wrong number of arguments~%"))))))
  1184. (with-store* store
  1185. (delete-matching-generations store %system-profile pattern)
  1186. (reinstall-bootloader store (generation-number %system-profile)))))
  1187. ((switch-generation)
  1188. (let ((pattern (match args
  1189. ((pattern) pattern)
  1190. (x (leave (G_ "wrong number of arguments~%"))))))
  1191. (with-store* store
  1192. (switch-to-system-generation store pattern))))
  1193. ((roll-back)
  1194. (let ((pattern (match args
  1195. (() "")
  1196. (x (leave (G_ "wrong number of arguments~%"))))))
  1197. (with-store* store
  1198. (roll-back-system store))))
  1199. ;; The following commands need to use the store, and they also
  1200. ;; need an operating system configuration file.
  1201. (else (process-action command args opts))))
  1202. (define-command (guix-system . args)
  1203. (synopsis "build and deploy full operating systems")
  1204. (define (parse-sub-command arg result)
  1205. ;; Parse sub-command ARG and augment RESULT accordingly.
  1206. (if (assoc-ref result 'action)
  1207. (alist-cons 'argument arg result)
  1208. (let ((action (string->symbol arg)))
  1209. (case action
  1210. ((build container vm vm-image image disk-image reconfigure init
  1211. extension-graph shepherd-graph
  1212. list-generations describe
  1213. delete-generations roll-back
  1214. switch-generation search docker-image)
  1215. (alist-cons 'action action result))
  1216. (else (leave (G_ "~a: unknown action~%") action))))))
  1217. (define (match-pair car)
  1218. ;; Return a procedure that matches a pair with CAR.
  1219. (match-lambda
  1220. ((head . tail)
  1221. (and (eq? car head) tail))
  1222. (_ #f)))
  1223. (define (option-arguments opts)
  1224. ;; Extract the plain arguments from OPTS.
  1225. (let* ((args (reverse (filter-map (match-pair 'argument) opts)))
  1226. (count (length args))
  1227. (action (assoc-ref opts 'action))
  1228. (expr (assoc-ref opts 'expression)))
  1229. (define (fail)
  1230. (leave (G_ "wrong number of arguments for action '~a'~%")
  1231. action))
  1232. (unless action
  1233. (format (current-error-port)
  1234. (G_ "guix system: missing command name~%"))
  1235. (format (current-error-port)
  1236. (G_ "Try 'guix system --help' for more information.~%"))
  1237. (exit 1))
  1238. (case action
  1239. ((build container vm vm-image image disk-image docker-image
  1240. reconfigure)
  1241. (unless (or (= count 1)
  1242. (and expr (= count 0)))
  1243. (fail)))
  1244. ((init)
  1245. (unless (= count 2)
  1246. (fail))))
  1247. args))
  1248. (with-error-handling
  1249. (let* ((opts (parse-command-line args %options
  1250. (list %default-options)
  1251. #:argument-handler
  1252. parse-sub-command))
  1253. (args (option-arguments opts))
  1254. (command (assoc-ref opts 'action)))
  1255. (parameterize ((%graft? (assoc-ref opts 'graft?)))
  1256. (with-status-verbosity (verbosity-level opts)
  1257. (process-command command args opts))))))
  1258. ;;; Local Variables:
  1259. ;;; eval: (put 'with-store* 'scheme-indent-function 1)
  1260. ;;; End:
  1261. ;;; system.scm ends here