system.scm 57 KB

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