system.scm 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. #:memory-size 1024
  639. #:shared-network? container-shared-network?)))))
  640. (define (maybe-suggest-running-guix-pull)
  641. "Suggest running 'guix pull' if this has never been done before."
  642. ;; Check whether we're running a 'guix pull'-provided 'guix' command. When
  643. ;; 'current-profile' returns #f, we may be running the globally-installed
  644. ;; 'guix' and thus run the risk of deploying an older 'guix'. See
  645. ;; <https://lists.gnu.org/archive/html/guix-devel/2014-08/msg00057.html>
  646. (unless (or (current-profile) (getenv "GUIX_UNINSTALLED"))
  647. (warning (G_ "Consider running 'guix pull' before 'reconfigure'.~%"))
  648. (warning (G_ "Failing to do that may downgrade your system!~%"))))
  649. (define (bootloader-installer-script installer
  650. bootloader device target)
  651. "Return a file calling INSTALLER gexp with given BOOTLOADER, DEVICE
  652. and TARGET arguments."
  653. (scheme-file "bootloader-installer"
  654. (with-imported-modules '((gnu build bootloader)
  655. (guix build utils))
  656. #~(begin
  657. (use-modules (gnu build bootloader)
  658. (guix build utils)
  659. (ice-9 binary-ports)
  660. (srfi srfi-34)
  661. (srfi srfi-35))
  662. (guard (c ((message-condition? c) ;XXX: i18n
  663. (format (current-error-port) "error: ~a~%"
  664. (condition-message c))
  665. (exit 1)))
  666. (#$installer #$bootloader #$device #$target)
  667. (info (G_ "bootloader successfully installed on '~a'~%")
  668. #$device))))))
  669. (define (local-eval exp)
  670. "Evaluate EXP, a G-Expression, in-place."
  671. (mlet* %store-monad ((lowered (lower-gexp exp))
  672. (_ (built-derivations (lowered-gexp-inputs lowered))))
  673. (save-load-path-excursion
  674. (set! %load-path (lowered-gexp-load-path lowered))
  675. (set! %load-compiled-path (lowered-gexp-load-compiled-path lowered))
  676. (return (primitive-eval (lowered-gexp-sexp lowered))))))
  677. (define* (perform-action action image
  678. #:key
  679. (validate-reconfigure ensure-forward-reconfigure)
  680. save-provenance?
  681. skip-safety-checks?
  682. install-bootloader?
  683. dry-run? derivations-only?
  684. use-substitutes? bootloader-target target
  685. full-boot?
  686. container-shared-network?
  687. (mappings '())
  688. (gc-root #f))
  689. "Perform ACTION for IMAGE. INSTALL-BOOTLOADER? specifies whether to install
  690. bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
  691. target root directory.
  692. FULL-BOOT? is used for the 'vm' action; it determines whether to
  693. boot directly to the kernel or to the bootloader. CONTAINER-SHARED-NETWORK?
  694. determines if the container will use a separate network namespace.
  695. When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
  696. building anything.
  697. When GC-ROOT is a path, also make that path an indirect root of the build
  698. output when building a system derivation, such as a disk image.
  699. When SKIP-SAFETY-CHECKS? is true, skip the file system and initrd module
  700. static checks."
  701. (define println
  702. (cut format #t "~a~%" <>))
  703. (define menu-entries
  704. (if (eq? 'init action)
  705. '()
  706. (map boot-parameters->menu-entry (profile-boot-parameters))))
  707. (define os
  708. (image-operating-system image))
  709. (define bootloader
  710. (operating-system-bootloader os))
  711. (define bootcfg
  712. (and (memq action '(init reconfigure))
  713. (operating-system-bootcfg os menu-entries)))
  714. (when (eq? action 'reconfigure)
  715. (maybe-suggest-running-guix-pull)
  716. (check-forward-update validate-reconfigure))
  717. ;; Check whether the declared file systems exist. This is better than
  718. ;; instantiating a broken configuration. Assume that we can only check if
  719. ;; running as root.
  720. (when (and (not skip-safety-checks?)
  721. (memq action '(init reconfigure)))
  722. (check-mapped-devices os)
  723. (when (zero? (getuid))
  724. (check-file-system-availability (operating-system-file-systems os))
  725. (check-initrd-modules os)))
  726. (mlet* %store-monad
  727. ((sys (system-derivation-for-action image action
  728. #:full-boot? full-boot?
  729. #:container-shared-network? container-shared-network?
  730. #:mappings mappings))
  731. ;; For 'init' and 'reconfigure', always build BOOTCFG, even if
  732. ;; --no-bootloader is passed, because we then use it as a GC root.
  733. ;; See <http://bugs.gnu.org/21068>.
  734. (drvs (mapm/accumulate-builds lower-object
  735. (if (memq action '(init reconfigure))
  736. (list sys bootcfg)
  737. (list sys))))
  738. (% (if derivations-only?
  739. (return (for-each (compose println derivation-file-name)
  740. drvs))
  741. (built-derivations drvs))))
  742. (if (or dry-run? derivations-only?)
  743. (return #f)
  744. (begin
  745. (for-each (compose println derivation->output-path)
  746. drvs)
  747. (case action
  748. ((reconfigure)
  749. (newline)
  750. (format #t (G_ "activating system...~%"))
  751. (mbegin %store-monad
  752. (switch-to-system local-eval os)
  753. (mwhen install-bootloader?
  754. (install-bootloader local-eval bootloader bootcfg
  755. #:target (or target "/"))
  756. (return
  757. (info (G_ "bootloader successfully installed on '~a'~%")
  758. (bootloader-configuration-target bootloader))))
  759. (with-shepherd-error-handling
  760. (upgrade-shepherd-services local-eval os)
  761. (return (format #t (G_ "\
  762. To complete the upgrade, run 'herd restart SERVICE' to stop,
  763. upgrade, and restart each service that was not automatically restarted.\n")))
  764. (return (format #t (G_ "\
  765. Run 'herd status' to view the list of services on your system.\n"))))))
  766. ((init)
  767. (newline)
  768. (format #t (G_ "initializing operating system under '~a'...~%")
  769. target)
  770. (install sys (canonicalize-path target)
  771. #:install-bootloader? install-bootloader?
  772. #:bootloader bootloader
  773. #:bootcfg bootcfg))
  774. (else
  775. ;; All we had to do was to build SYS and maybe register an
  776. ;; indirect GC root.
  777. (let ((output (derivation->output-path sys)))
  778. (mbegin %store-monad
  779. (mwhen gc-root
  780. (register-root* (list output) gc-root))
  781. (return output)))))))))
  782. (define (lookup-backend name) ;TODO: factorize
  783. "Return the graph backend called NAME. Raise an error if it is not found."
  784. (or (find (lambda (backend)
  785. (string=? (graph-backend-name backend) name))
  786. %graph-backends)
  787. (leave (G_ "~a: unknown backend~%") name)))
  788. (define* (export-extension-graph os port
  789. #:key (backend (lookup-backend "graphviz")))
  790. "Export the service extension graph of OS to PORT using BACKEND."
  791. (let* ((services (operating-system-services os))
  792. (system (find (lambda (service)
  793. (eq? (service-kind service) system-service-type))
  794. services)))
  795. (export-graph (list system) (current-output-port)
  796. #:backend backend
  797. #:node-type (service-node-type services)
  798. #:reverse-edges? #t)))
  799. (define* (export-shepherd-graph os port
  800. #:key (backend (lookup-backend "graphviz")))
  801. "Export the graph of shepherd services of OS to PORT using BACKEND."
  802. (let* ((services (operating-system-services os))
  803. (pid1 (fold-services services
  804. #:target-type shepherd-root-service-type))
  805. ;; Get the list of <shepherd-service>.
  806. (shepherds (shepherd-configuration-services (service-value pid1)))
  807. (sinks (filter (lambda (service)
  808. (null? (shepherd-service-requirement service)))
  809. shepherds)))
  810. (export-graph sinks (current-output-port)
  811. #:backend backend
  812. #:node-type (shepherd-service-node-type shepherds)
  813. #:reverse-edges? #t)))
  814. ;;;
  815. ;;; Images.
  816. ;;;
  817. (define (list-image-types)
  818. "Print the available image types."
  819. (display (G_ "The available image types are:\n"))
  820. (newline)
  821. (format #t "~{ - ~a ~%~}" (map image-type-name (force %image-types))))
  822. ;;;
  823. ;;; Options.
  824. ;;;
  825. (define (show-help)
  826. (display (G_ "Usage: guix system [OPTION ...] ACTION [ARG ...] [FILE]
  827. Build the operating system declared in FILE according to ACTION.
  828. Some ACTIONS support additional ARGS.\n"))
  829. (newline)
  830. (display (G_ "The valid values for ACTION are:\n"))
  831. (newline)
  832. (display (G_ "\
  833. search search for existing service types\n"))
  834. (display (G_ "\
  835. reconfigure switch to a new operating system configuration\n"))
  836. (display (G_ "\
  837. roll-back switch to the previous operating system configuration\n"))
  838. (display (G_ "\
  839. describe describe the current system\n"))
  840. (display (G_ "\
  841. list-generations list the system generations\n"))
  842. (display (G_ "\
  843. switch-generation switch to an existing operating system configuration\n"))
  844. (display (G_ "\
  845. delete-generations delete old system generations\n"))
  846. (display (G_ "\
  847. build build the operating system without installing anything\n"))
  848. (display (G_ "\
  849. container build a container that shares the host's store\n"))
  850. (display (G_ "\
  851. vm build a virtual machine image that shares the host's store\n"))
  852. (display (G_ "\
  853. image build a Guix System image\n"))
  854. (display (G_ "\
  855. docker-image build a Docker image\n"))
  856. (display (G_ "\
  857. init initialize a root file system to run GNU\n"))
  858. (display (G_ "\
  859. extension-graph emit the service extension graph in Dot format\n"))
  860. (display (G_ "\
  861. shepherd-graph emit the graph of shepherd services in Dot format\n"))
  862. (show-build-options-help)
  863. (display (G_ "
  864. -d, --derivation return the derivation of the given system"))
  865. (display (G_ "
  866. -e, --expression=EXPR consider the operating-system EXPR evaluates to
  867. instead of reading FILE, when applicable"))
  868. (display (G_ "
  869. --allow-downgrades for 'reconfigure', allow downgrades to earlier
  870. channel revisions"))
  871. (display (G_ "
  872. --on-error=STRATEGY
  873. apply STRATEGY (one of nothing-special, backtrace,
  874. or debug) when an error occurs while reading FILE"))
  875. (display (G_ "
  876. --list-image-types list available image types"))
  877. (display (G_ "
  878. -t, --image-type=TYPE for 'image', produce an image of TYPE"))
  879. (display (G_ "
  880. --image-size=SIZE for 'image', produce an image of SIZE"))
  881. (display (G_ "
  882. --no-bootloader for 'init', do not install a bootloader"))
  883. (display (G_ "
  884. --volatile for 'image', make the root file system volatile"))
  885. (display (G_ "
  886. --label=LABEL for 'image', label disk image with LABEL"))
  887. (display (G_ "
  888. --save-provenance save provenance information"))
  889. (display (G_ "
  890. --share=SPEC for 'vm' and 'container', share host file system with
  891. read/write access according to SPEC"))
  892. (display (G_ "
  893. --expose=SPEC for 'vm' and 'container', expose host file system
  894. directory as read-only according to SPEC"))
  895. (display (G_ "
  896. -N, --network for 'container', allow containers to access the network"))
  897. (display (G_ "
  898. -r, --root=FILE for 'vm', 'image', 'container' and 'build',
  899. make FILE a symlink to the result, and
  900. register it as a garbage collector root"))
  901. (display (G_ "
  902. --full-boot for 'vm', make a full boot sequence"))
  903. (display (G_ "
  904. --skip-checks skip file system and initrd module safety checks"))
  905. (display (G_ "
  906. --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
  907. (display (G_ "
  908. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  909. (newline)
  910. (display (G_ "
  911. --graph-backend=BACKEND
  912. use BACKEND for 'extension-graphs' and 'shepherd-graph'"))
  913. (newline)
  914. (display (G_ "
  915. -h, --help display this help and exit"))
  916. (display (G_ "
  917. -V, --version display version information and exit"))
  918. (newline)
  919. (show-bug-report-information))
  920. (define %options
  921. ;; Specifications of the command-line options.
  922. (cons* (option '(#\h "help") #f #f
  923. (lambda args
  924. (show-help)
  925. (exit 0)))
  926. (option '(#\V "version") #f #f
  927. (lambda args
  928. (show-version-and-exit "guix system")))
  929. (option '(#\e "expression") #t #f
  930. (lambda (opt name arg result)
  931. (alist-cons 'expression arg result)))
  932. (option '(#\d "derivation") #f #f
  933. (lambda (opt name arg result)
  934. (alist-cons 'derivations-only? #t result)))
  935. (option '("allow-downgrades") #f #f
  936. (lambda (opt name arg result)
  937. (alist-cons 'validate-reconfigure
  938. warn-about-backward-reconfigure
  939. result)))
  940. (option '("on-error") #t #f
  941. (lambda (opt name arg result)
  942. (alist-cons 'on-error (string->symbol arg)
  943. result)))
  944. (option '(#\t "image-type") #t #f
  945. (lambda (opt name arg result)
  946. (alist-cons 'image-type (string->symbol arg)
  947. result)))
  948. (option '("list-image-types") #f #f
  949. (lambda (opt name arg result)
  950. (list-image-types)
  951. (exit 0)))
  952. (option '("image-size") #t #f
  953. (lambda (opt name arg result)
  954. (alist-cons 'image-size (size->number arg)
  955. result)))
  956. (option '(#\N "network") #f #f
  957. (lambda (opt name arg result)
  958. (alist-cons 'container-shared-network? #t result)))
  959. (option '("no-bootloader" "no-grub") #f #f
  960. (lambda (opt name arg result)
  961. (alist-cons 'install-bootloader? #f result)))
  962. (option '("volatile") #f #f
  963. (lambda (opt name arg result)
  964. (alist-cons 'volatile-root? #t result)))
  965. (option '("label") #t #f
  966. (lambda (opt name arg result)
  967. (alist-cons 'label arg result)))
  968. (option '("full-boot") #f #f
  969. (lambda (opt name arg result)
  970. (alist-cons 'full-boot? #t result)))
  971. (option '("save-provenance") #f #f
  972. (lambda (opt name arg result)
  973. (alist-cons 'save-provenance? #t result)))
  974. (option '("skip-checks") #f #f
  975. (lambda (opt name arg result)
  976. (alist-cons 'skip-safety-checks? #t result)))
  977. (option '("share") #t #f
  978. (lambda (opt name arg result)
  979. (alist-cons 'file-system-mapping
  980. (specification->file-system-mapping arg #t)
  981. result)))
  982. (option '("expose") #t #f
  983. (lambda (opt name arg result)
  984. (alist-cons 'file-system-mapping
  985. (specification->file-system-mapping arg #f)
  986. result)))
  987. (option '(#\n "dry-run") #f #f
  988. (lambda (opt name arg result)
  989. (alist-cons 'dry-run? #t result)))
  990. (option '(#\v "verbosity") #t #f
  991. (lambda (opt name arg result)
  992. (let ((level (string->number* arg)))
  993. (alist-cons 'verbosity level
  994. (alist-delete 'verbosity result)))))
  995. (option '(#\s "system") #t #f
  996. (lambda (opt name arg result)
  997. (alist-cons 'system arg
  998. (alist-delete 'system result eq?))))
  999. (option '("target") #t #f
  1000. (lambda (opt name arg result)
  1001. (alist-cons 'target arg
  1002. (alist-delete 'target result eq?))))
  1003. (option '(#\r "root") #t #f
  1004. (lambda (opt name arg result)
  1005. (alist-cons 'gc-root arg result)))
  1006. (option '("graph-backend") #t #f
  1007. (lambda (opt name arg result)
  1008. (alist-cons 'graph-backend arg result)))
  1009. %standard-build-options))
  1010. (define %default-options
  1011. ;; Alist of default option values.
  1012. `((system . ,(%current-system))
  1013. (target . #f)
  1014. (substitutes? . #t)
  1015. (offload? . #t)
  1016. (print-build-trace? . #t)
  1017. (print-extended-build-trace? . #t)
  1018. (multiplexed-build-output? . #t)
  1019. (graft? . #t)
  1020. (debug . 0)
  1021. (verbosity . #f) ;default
  1022. (validate-reconfigure . ,ensure-forward-reconfigure)
  1023. (image-type . efi-raw)
  1024. (image-size . guess)
  1025. (install-bootloader? . #t)
  1026. (label . #f)
  1027. (volatile-root? . #f)
  1028. (graph-backend . "graphviz")))
  1029. (define (verbosity-level opts)
  1030. "Return the verbosity level based on OPTS, the alist of parsed options."
  1031. (or (assoc-ref opts 'verbosity)
  1032. (if (eq? (assoc-ref opts 'action) 'build)
  1033. 3 1)))
  1034. ;;;
  1035. ;;; Entry point.
  1036. ;;;
  1037. (define (process-action action args opts)
  1038. "Process ACTION, a sub-command, with the arguments are listed in ARGS.
  1039. ACTION must be one of the sub-commands that takes an operating system
  1040. declaration as an argument (a file name.) OPTS is the raw alist of options
  1041. resulting from command-line parsing."
  1042. (define (ensure-operating-system-or-image file-or-exp obj)
  1043. (unless (or (operating-system? obj) (image? obj))
  1044. (leave (G_ "'~a' does not return an operating system or an image~%")
  1045. file-or-exp))
  1046. obj)
  1047. (define save-provenance?
  1048. (or (assoc-ref opts 'save-provenance?)
  1049. (memq action '(init reconfigure))))
  1050. (let* ((file (match args
  1051. (() #f)
  1052. ((x . _) x)))
  1053. (expr (assoc-ref opts 'expression))
  1054. (system (assoc-ref opts 'system))
  1055. (target (assoc-ref opts 'target))
  1056. (transform (lambda (obj)
  1057. (if (and save-provenance? (operating-system? obj))
  1058. (operating-system-with-provenance obj file)
  1059. obj)))
  1060. (obj (transform
  1061. (ensure-operating-system-or-image
  1062. (or file expr)
  1063. (cond
  1064. ((and expr file)
  1065. (leave
  1066. (G_ "both file and expression cannot be specified~%")))
  1067. (expr
  1068. (read/eval expr))
  1069. (file
  1070. (load* file %user-module
  1071. #:on-error (assoc-ref opts 'on-error)))
  1072. (else
  1073. (leave (G_ "no configuration specified~%")))))))
  1074. (dry? (assoc-ref opts 'dry-run?))
  1075. (bootloader? (assoc-ref opts 'install-bootloader?))
  1076. (label (assoc-ref opts 'label))
  1077. (image-type (lookup-image-type-by-name
  1078. (assoc-ref opts 'image-type)))
  1079. (image (let* ((image-type (if (eq? action 'vm-image)
  1080. qcow2-image-type
  1081. image-type))
  1082. (image-size (assoc-ref opts 'image-size))
  1083. (volatile? (assoc-ref opts 'volatile-root?))
  1084. (base-image (if (operating-system? obj)
  1085. (os->image obj
  1086. #:type image-type)
  1087. obj))
  1088. (base-target (image-target base-image)))
  1089. (image
  1090. (inherit (if label
  1091. (image-with-label base-image label)
  1092. base-image))
  1093. (target (or base-target target))
  1094. (size image-size)
  1095. (volatile-root? volatile?))))
  1096. (os (image-operating-system image))
  1097. (target-file (match args
  1098. ((first second) second)
  1099. (_ #f)))
  1100. (bootloader-target
  1101. (and bootloader?
  1102. (bootloader-configuration-target
  1103. (operating-system-bootloader os)))))
  1104. (define (graph-backend)
  1105. (lookup-backend (assoc-ref opts 'graph-backend)))
  1106. (with-store store
  1107. (set-build-options-from-command-line store opts)
  1108. (with-build-handler (build-notifier #:use-substitutes?
  1109. (assoc-ref opts 'substitutes?)
  1110. #:verbosity
  1111. (verbosity-level opts)
  1112. #:dry-run?
  1113. (assoc-ref opts 'dry-run?))
  1114. (run-with-store store
  1115. (mbegin %store-monad
  1116. (set-guile-for-build (default-guile))
  1117. (case action
  1118. ((extension-graph)
  1119. (export-extension-graph os (current-output-port)
  1120. #:backend (graph-backend)))
  1121. ((shepherd-graph)
  1122. (export-shepherd-graph os (current-output-port)
  1123. #:backend (graph-backend)))
  1124. (else
  1125. (unless (memq action '(build init))
  1126. (warn-about-old-distro #:suggested-command
  1127. "guix system reconfigure"))
  1128. (perform-action action image
  1129. #:dry-run? dry?
  1130. #:derivations-only? (assoc-ref opts
  1131. 'derivations-only?)
  1132. #:use-substitutes? (assoc-ref opts 'substitutes?)
  1133. #:skip-safety-checks?
  1134. (assoc-ref opts 'skip-safety-checks?)
  1135. #:validate-reconfigure
  1136. (assoc-ref opts 'validate-reconfigure)
  1137. #:full-boot? (assoc-ref opts 'full-boot?)
  1138. #:container-shared-network?
  1139. (assoc-ref opts 'container-shared-network?)
  1140. #:mappings (filter-map (match-lambda
  1141. (('file-system-mapping . m)
  1142. m)
  1143. (_ #f))
  1144. opts)
  1145. #:install-bootloader? bootloader?
  1146. #:target target-file
  1147. #:bootloader-target bootloader-target
  1148. #:gc-root (assoc-ref opts 'gc-root)))))
  1149. #:target target
  1150. #:system system)))
  1151. (warn-about-disk-space)))
  1152. (define (resolve-subcommand name)
  1153. (let ((module (resolve-interface
  1154. `(guix scripts system ,(string->symbol name))))
  1155. (proc (string->symbol (string-append "guix-system-" name))))
  1156. (module-ref module proc)))
  1157. (define (process-command command args opts)
  1158. "Process COMMAND, one of the 'guix system' sub-commands. ARGS is its
  1159. argument list and OPTS is the option alist."
  1160. (define-syntax-rule (with-store* store exp ...)
  1161. (with-store store
  1162. (set-build-options-from-command-line store opts)
  1163. exp ...))
  1164. (case command
  1165. ;; The following commands do not need to use the store, and they do not need
  1166. ;; an operating system configuration file.
  1167. ((list-generations)
  1168. (let ((pattern (match args
  1169. (() #f)
  1170. ((pattern) pattern)
  1171. (x (leave (G_ "wrong number of arguments~%"))))))
  1172. (list-generations pattern)))
  1173. ((describe)
  1174. (match (generation-number %system-profile)
  1175. (0
  1176. (error (G_ "no system generation, nothing to describe~%")))
  1177. (generation
  1178. (display-system-generation generation))))
  1179. ((search)
  1180. (apply (resolve-subcommand "search") args))
  1181. ;; The following commands need to use the store, but they do not need an
  1182. ;; operating system configuration file.
  1183. ((delete-generations)
  1184. (let ((pattern (match args
  1185. (() #f)
  1186. ((pattern) pattern)
  1187. (x (leave (G_ "wrong number of arguments~%"))))))
  1188. (with-store* store
  1189. (delete-matching-generations store %system-profile pattern)
  1190. (reinstall-bootloader store (generation-number %system-profile)))))
  1191. ((switch-generation)
  1192. (let ((pattern (match args
  1193. ((pattern) pattern)
  1194. (x (leave (G_ "wrong number of arguments~%"))))))
  1195. (with-store* store
  1196. (switch-to-system-generation store pattern))))
  1197. ((roll-back)
  1198. (let ((pattern (match args
  1199. (() "")
  1200. (x (leave (G_ "wrong number of arguments~%"))))))
  1201. (with-store* store
  1202. (roll-back-system store))))
  1203. ;; The following commands need to use the store, and they also
  1204. ;; need an operating system configuration file.
  1205. (else (process-action command args opts))))
  1206. (define-command (guix-system . args)
  1207. (synopsis "build and deploy full operating systems")
  1208. (define (parse-sub-command arg result)
  1209. ;; Parse sub-command ARG and augment RESULT accordingly.
  1210. (if (assoc-ref result 'action)
  1211. (alist-cons 'argument arg result)
  1212. (let ((action (string->symbol arg)))
  1213. (case action
  1214. ((build container vm vm-image image disk-image reconfigure init
  1215. extension-graph shepherd-graph
  1216. list-generations describe
  1217. delete-generations roll-back
  1218. switch-generation search docker-image)
  1219. (alist-cons 'action action result))
  1220. (else (leave (G_ "~a: unknown action~%") action))))))
  1221. (define (match-pair car)
  1222. ;; Return a procedure that matches a pair with CAR.
  1223. (match-lambda
  1224. ((head . tail)
  1225. (and (eq? car head) tail))
  1226. (_ #f)))
  1227. (define (option-arguments opts)
  1228. ;; Extract the plain arguments from OPTS.
  1229. (let* ((args (reverse (filter-map (match-pair 'argument) opts)))
  1230. (count (length args))
  1231. (action (assoc-ref opts 'action))
  1232. (expr (assoc-ref opts 'expression)))
  1233. (define (fail)
  1234. (leave (G_ "wrong number of arguments for action '~a'~%")
  1235. action))
  1236. (unless action
  1237. (format (current-error-port)
  1238. (G_ "guix system: missing command name~%"))
  1239. (format (current-error-port)
  1240. (G_ "Try 'guix system --help' for more information.~%"))
  1241. (exit 1))
  1242. (case action
  1243. ((build container vm vm-image image disk-image docker-image
  1244. reconfigure)
  1245. (unless (or (= count 1)
  1246. (and expr (= count 0)))
  1247. (fail)))
  1248. ((init)
  1249. (unless (= count 2)
  1250. (fail))))
  1251. args))
  1252. (with-error-handling
  1253. (let* ((opts (parse-command-line args %options
  1254. (list %default-options)
  1255. #:argument-handler
  1256. parse-sub-command))
  1257. (args (option-arguments opts))
  1258. (command (assoc-ref opts 'action)))
  1259. (parameterize ((%graft? (assoc-ref opts 'graft?)))
  1260. (with-status-verbosity (verbosity-level opts)
  1261. (process-command command args opts))))))
  1262. ;;; Local Variables:
  1263. ;;; eval: (put 'with-store* 'scheme-indent-function 1)
  1264. ;;; End:
  1265. ;;; system.scm ends here