system.scm 54 KB

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