installer.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2019, 2020 Tobias Geerinckx-Rice <me@tobias.gr>
  5. ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu installer)
  22. #:use-module (guix discovery)
  23. #:use-module (guix packages)
  24. #:use-module (guix gexp)
  25. #:use-module (guix modules)
  26. #:use-module (guix utils)
  27. #:use-module (guix ui)
  28. #:use-module ((guix self) #:select (make-config.scm))
  29. #:use-module (guix packages)
  30. #:use-module (guix git-download)
  31. #:use-module (gnu installer utils)
  32. #:use-module (gnu packages admin)
  33. #:use-module (gnu packages base)
  34. #:use-module (gnu packages bash)
  35. #:use-module (gnu packages compression)
  36. #:use-module (gnu packages connman)
  37. #:use-module (gnu packages cryptsetup)
  38. #:use-module (gnu packages disk)
  39. #:use-module (gnu packages file-systems)
  40. #:use-module (gnu packages guile)
  41. #:use-module (gnu packages guile-xyz)
  42. #:autoload (gnu packages gnupg) (guile-gcrypt)
  43. #:use-module (gnu packages iso-codes)
  44. #:use-module (gnu packages linux)
  45. #:use-module (gnu packages nano)
  46. #:use-module (gnu packages ncurses)
  47. #:use-module (gnu packages package-management)
  48. #:use-module (gnu packages tls)
  49. #:use-module (gnu packages xorg)
  50. #:use-module (gnu system locale)
  51. #:use-module (ice-9 match)
  52. #:use-module (srfi srfi-1)
  53. #:export (installer-program))
  54. (define module-to-import?
  55. ;; Return true for modules that should be imported. For (gnu system …) and
  56. ;; (gnu packages …) modules, we simply add the whole 'guix' package via
  57. ;; 'with-extensions' (to avoid having to rebuild it all), which is why these
  58. ;; modules are excluded here.
  59. (match-lambda
  60. (('guix 'config) #f)
  61. (('gnu 'installer _ ...) #t)
  62. (('gnu 'build _ ...) #t)
  63. (('guix 'build _ ...) #t)
  64. (_ #f)))
  65. (define not-config?
  66. ;; Select (guix …) and (gnu …) modules, except (guix config).
  67. (match-lambda
  68. (('guix 'config) #f)
  69. (('guix _ ...) #t)
  70. (('gnu _ ...) #t)
  71. (_ #f)))
  72. (define* (build-compiled-file name locale-builder)
  73. "Return a file-like object that evaluates the gexp LOCALE-BUILDER and store
  74. its result in the scheme file NAME. The derivation will also build a compiled
  75. version of this file."
  76. (define set-utf8-locale
  77. #~(begin
  78. (setenv "LOCPATH"
  79. #$(file-append glibc-utf8-locales "/lib/locale/"
  80. (version-major+minor
  81. (package-version glibc-utf8-locales))))
  82. (setlocale LC_ALL "en_US.utf8")))
  83. (define builder
  84. (with-extensions (list guile-json-3)
  85. (with-imported-modules `(,@(source-module-closure
  86. '((gnu installer locale))
  87. #:select? not-config?)
  88. ((guix config) => ,(make-config.scm)))
  89. #~(begin
  90. (use-modules (gnu installer locale))
  91. ;; The locale files contain non-ASCII characters.
  92. #$set-utf8-locale
  93. (mkdir #$output)
  94. (let ((locale-file
  95. (string-append #$output "/" #$name ".scm"))
  96. (locale-compiled-file
  97. (string-append #$output "/" #$name ".go")))
  98. (call-with-output-file locale-file
  99. (lambda (port)
  100. (write #$locale-builder port)))
  101. (compile-file locale-file
  102. #:output-file locale-compiled-file))))))
  103. (computed-file name builder))
  104. (define apply-locale
  105. ;; Install the specified locale.
  106. (with-imported-modules (source-module-closure '((gnu services herd)))
  107. #~(lambda (locale)
  108. (false-if-exception
  109. (setlocale LC_ALL locale))
  110. ;; Restart the documentation viewer so it displays the manual in
  111. ;; language that corresponds to LOCALE. Make sure that nothing is
  112. ;; printed on the console.
  113. (parameterize ((shepherd-message-port
  114. (%make-void-port "w")))
  115. (stop-service 'term-tty2)
  116. (start-service 'term-tty2 (list locale))))))
  117. (define* (compute-locale-step #:key
  118. locales-name
  119. iso639-languages-name
  120. iso3166-territories-name)
  121. "Return a gexp that run the locale-page of INSTALLER, and install the
  122. selected locale. The list of locales, languages and territories passed to
  123. locale-page are computed in derivations named respectively LOCALES-NAME,
  124. ISO639-LANGUAGES-NAME and ISO3166-TERRITORIES-NAME. Those lists are compiled,
  125. so that when the installer is run, all the lengthy operations have already
  126. been performed at build time."
  127. (define (compiled-file-loader file name)
  128. #~(load-compiled
  129. (string-append #$file "/" #$name ".go")))
  130. (let* ((supported-locales #~(supported-locales->locales
  131. #+(glibc-supported-locales)))
  132. (iso-codes #~(string-append #$iso-codes "/share/iso-codes/json/"))
  133. (iso639-3 #~(string-append #$iso-codes "iso_639-3.json"))
  134. (iso639-5 #~(string-append #$iso-codes "iso_639-5.json"))
  135. (iso3166 #~(string-append #$iso-codes "iso_3166-1.json"))
  136. (locales-file (build-compiled-file
  137. locales-name
  138. #~`(quote ,#$supported-locales)))
  139. (iso639-file (build-compiled-file
  140. iso639-languages-name
  141. #~`(quote ,(iso639->iso639-languages
  142. #$supported-locales
  143. #$iso639-3 #$iso639-5))))
  144. (iso3166-file (build-compiled-file
  145. iso3166-territories-name
  146. #~`(quote ,(iso3166->iso3166-territories #$iso3166))))
  147. (locales-loader (compiled-file-loader locales-file
  148. locales-name))
  149. (iso639-loader (compiled-file-loader iso639-file
  150. iso639-languages-name))
  151. (iso3166-loader (compiled-file-loader iso3166-file
  152. iso3166-territories-name)))
  153. #~(lambda (current-installer)
  154. (let ((result
  155. ((installer-locale-page current-installer)
  156. #:supported-locales #$locales-loader
  157. #:iso639-languages #$iso639-loader
  158. #:iso3166-territories #$iso3166-loader)))
  159. (#$apply-locale result)
  160. result))))
  161. (define apply-keymap
  162. ;; Apply the specified keymap. Use the default keyboard model.
  163. #~(match-lambda
  164. ((layout variant options)
  165. (kmscon-update-keymap (default-keyboard-model)
  166. layout variant options))))
  167. (define* (compute-keymap-step context)
  168. "Return a gexp that runs the keymap-page of INSTALLER and install the
  169. selected keymap."
  170. #~(lambda (current-installer)
  171. (let ((result
  172. (call-with-values
  173. (lambda ()
  174. (xkb-rules->models+layouts
  175. (string-append #$xkeyboard-config
  176. "/share/X11/xkb/rules/base.xml")))
  177. (lambda (models layouts)
  178. ((installer-keymap-page current-installer)
  179. layouts '#$context)))))
  180. (and result (#$apply-keymap result))
  181. result)))
  182. (define (installer-steps)
  183. (let ((locale-step (compute-locale-step
  184. #:locales-name "locales"
  185. #:iso639-languages-name "iso639-languages"
  186. #:iso3166-territories-name "iso3166-territories"))
  187. (timezone-data #~(string-append #$tzdata
  188. "/share/zoneinfo/zone.tab")))
  189. #~(lambda (current-installer)
  190. ((installer-parameters-menu current-installer)
  191. (lambda ()
  192. ((installer-parameters-page current-installer)
  193. (lambda _
  194. (#$(compute-keymap-step 'param)
  195. current-installer)))))
  196. (list
  197. ;; Ask the user to choose a locale among those supported by
  198. ;; the glibc. Install the selected locale right away, so that
  199. ;; the user may benefit from any available translation for the
  200. ;; installer messages.
  201. (installer-step
  202. (id 'locale)
  203. (description (G_ "Locale"))
  204. (compute (lambda _
  205. (#$locale-step current-installer)))
  206. (configuration-formatter locale->configuration))
  207. ;; Welcome the user and ask them to choose between manual
  208. ;; installation and graphical install.
  209. (installer-step
  210. (id 'welcome)
  211. (compute (lambda _
  212. ((installer-welcome-page current-installer)
  213. #$(local-file "installer/aux-files/logo.txt")))))
  214. ;; Ask the user to select a timezone under glibc format.
  215. (installer-step
  216. (id 'timezone)
  217. (description (G_ "Timezone"))
  218. (compute (lambda _
  219. ((installer-timezone-page current-installer)
  220. #$timezone-data)))
  221. (configuration-formatter posix-tz->configuration))
  222. ;; The installer runs in a kmscon virtual terminal where loadkeys
  223. ;; won't work. kmscon uses libxkbcommon as a backend for keyboard
  224. ;; input. It is possible to update kmscon current keymap by sending
  225. ;; it a keyboard model, layout, variant and options, in a somehow
  226. ;; similar way as what is done with setxkbmap utility.
  227. ;;
  228. ;; So ask for a keyboard model, layout and variant to update the
  229. ;; current kmscon keymap. For non-Latin layouts, we add an
  230. ;; appropriate second layout and toggle via Alt+Shift.
  231. (installer-step
  232. (id 'keymap)
  233. (description (G_ "Keyboard mapping selection"))
  234. (compute (lambda _
  235. (#$(compute-keymap-step 'default)
  236. current-installer)))
  237. (configuration-formatter keyboard-layout->configuration))
  238. ;; Ask the user to input a hostname for the system.
  239. (installer-step
  240. (id 'hostname)
  241. (description (G_ "Hostname"))
  242. (compute (lambda _
  243. ((installer-hostname-page current-installer))))
  244. (configuration-formatter hostname->configuration))
  245. ;; Provide an interface above connmanctl, so that the user can select
  246. ;; a network susceptible to acces Internet.
  247. (installer-step
  248. (id 'network)
  249. (description (G_ "Network selection"))
  250. (compute (lambda _
  251. ((installer-network-page current-installer)))))
  252. ;; Ask whether to enable substitute server discovery.
  253. (installer-step
  254. (id 'substitutes)
  255. (description (G_ "Substitute server discovery"))
  256. (compute (lambda _
  257. ((installer-substitutes-page current-installer)))))
  258. ;; Prompt for users (name, group and home directory).
  259. (installer-step
  260. (id 'user)
  261. (description (G_ "User creation"))
  262. (compute (lambda _
  263. ((installer-user-page current-installer))))
  264. (configuration-formatter users->configuration))
  265. ;; Ask the user to choose one or many desktop environment(s).
  266. (installer-step
  267. (id 'services)
  268. (description (G_ "Services"))
  269. (compute (lambda _
  270. ((installer-services-page current-installer))))
  271. (configuration-formatter system-services->configuration))
  272. ;; Run a partitioning tool allowing the user to modify
  273. ;; partition tables, partitions and their mount points.
  274. ;; Do this last so the user has something to boot if any
  275. ;; of the previous steps didn't go as expected.
  276. (installer-step
  277. (id 'partition)
  278. (description (G_ "Partitioning"))
  279. (compute (lambda _
  280. ((installer-partition-page current-installer))))
  281. (configuration-formatter user-partitions->configuration))
  282. (installer-step
  283. (id 'final)
  284. (description (G_ "Configuration file"))
  285. (compute
  286. (lambda (result prev-steps)
  287. ((installer-final-page current-installer)
  288. result prev-steps))))))))
  289. (define (installer-program)
  290. "Return a file-like object that runs the given INSTALLER."
  291. (define init-gettext
  292. ;; Initialize gettext support, so that installer messages can be
  293. ;; translated.
  294. #~(begin
  295. (bindtextdomain "guix" (string-append #$guix "/share/locale"))
  296. (textdomain "guix")
  297. (setlocale LC_ALL "")))
  298. (define set-installer-path
  299. ;; Add the specified binary to PATH for later use by the installer.
  300. #~(let* ((inputs
  301. '#$(list bash ;start subshells
  302. connman ;call connmanctl
  303. cryptsetup
  304. dosfstools ;mkfs.fat
  305. e2fsprogs ;mkfs.ext4
  306. lvm2-static ;dmsetup
  307. btrfs-progs
  308. jfsutils ;jfs_mkfs
  309. ntfs-3g ;mkfs.ntfs
  310. xfsprogs ;mkfs.xfs
  311. kbd ;chvt
  312. util-linux ;mkwap
  313. nano
  314. shadow
  315. tar ;dump
  316. gzip ;dump
  317. coreutils)))
  318. (with-output-to-port (%make-void-port "w")
  319. (lambda ()
  320. (set-path-environment-variable "PATH" '("bin" "sbin") inputs)))))
  321. (define steps (installer-steps))
  322. (define modules
  323. (scheme-modules*
  324. (string-append (current-source-directory) "/..")
  325. "gnu/installer"))
  326. (define installer-builder
  327. ;; Note: Include GUIX as an extension to get all the (gnu system …), (gnu
  328. ;; packages …), etc. modules.
  329. (with-extensions (list guile-gcrypt guile-newt
  330. guile-parted guile-bytestructures
  331. guile-json-3 guile-git guile-webutils
  332. guix gnutls)
  333. (with-imported-modules `(,@(source-module-closure
  334. `(,@modules
  335. (gnu services herd)
  336. (guix build utils))
  337. #:select? module-to-import?)
  338. ((guix config) => ,(make-config.scm)))
  339. #~(begin
  340. (use-modules (gnu installer record)
  341. (gnu installer keymap)
  342. (gnu installer steps)
  343. (gnu installer dump)
  344. (gnu installer final)
  345. (gnu installer hostname)
  346. (gnu installer locale)
  347. (gnu installer parted)
  348. (gnu installer services)
  349. (gnu installer timezone)
  350. (gnu installer user)
  351. (gnu installer utils)
  352. (gnu installer newt)
  353. ((gnu installer newt keymap)
  354. #:select (keyboard-layout->configuration))
  355. (gnu services herd)
  356. (guix i18n)
  357. (guix build utils)
  358. ((system repl debug)
  359. #:select (terminal-width))
  360. (ice-9 match)
  361. (ice-9 textual-ports))
  362. ;; Initialize gettext support so that installers can use
  363. ;; (guix i18n) module.
  364. #$init-gettext
  365. ;; Add some binaries used by the installers to PATH.
  366. #$set-installer-path
  367. ;; Arrange for language and territory name translations to be
  368. ;; available. We need them at run time, not just compile time,
  369. ;; because some territories have several corresponding languages
  370. ;; (e.g., "French" is always displayed as "français", but
  371. ;; "Belgium" could be translated to Dutch, French, or German.)
  372. (bindtextdomain "iso_639-3" ;languages
  373. #+(file-append iso-codes "/share/locale"))
  374. (bindtextdomain "iso_3166-1" ;territories
  375. #+(file-append iso-codes "/share/locale"))
  376. ;; Likewise for XKB keyboard layout names.
  377. (bindtextdomain "xkeyboard-config"
  378. #+(file-append xkeyboard-config "/share/locale"))
  379. ;; Initialize 'terminal-width' in (system repl debug)
  380. ;; to a large-enough value to make backtrace more
  381. ;; verbose.
  382. (terminal-width 200)
  383. (define current-installer newt-installer)
  384. (define steps (#$steps current-installer))
  385. (dynamic-wind
  386. (installer-init current-installer)
  387. (lambda ()
  388. (parameterize
  389. ((run-command-in-installer
  390. (installer-run-command current-installer)))
  391. (catch #t
  392. (lambda ()
  393. (define results
  394. (run-installer-steps
  395. #:rewind-strategy 'menu
  396. #:menu-proc (installer-menu-page current-installer)
  397. #:steps steps))
  398. (match (result-step results 'final)
  399. ('success
  400. ;; We did it! Let's reboot!
  401. (sync)
  402. (stop-service 'root))
  403. (_
  404. ;; The installation failed, exit so that it is
  405. ;; restarted by login.
  406. #f)))
  407. (const #f)
  408. (lambda (key . args)
  409. (installer-log-line "crashing due to uncaught exception: ~s ~s"
  410. key args)
  411. (define dump-dir
  412. (prepare-dump key args #:result %current-result))
  413. (define action
  414. ((installer-exit-error current-installer)
  415. (get-string-all
  416. (open-input-file
  417. (string-append dump-dir "/installer-backtrace")))))
  418. (match action
  419. ('dump
  420. (let* ((dump-files
  421. ((installer-dump-page current-installer)
  422. dump-dir))
  423. (dump-archive
  424. (make-dump dump-dir dump-files)))
  425. ((installer-report-page current-installer)
  426. dump-archive)))
  427. (_ #f))
  428. (exit 1)))))
  429. (installer-exit current-installer))))))
  430. (program-file
  431. "installer"
  432. #~(begin
  433. ;; Set the default locale to install unicode support. For
  434. ;; some reason, unicode support is not correctly installed
  435. ;; when calling this in 'installer-builder'.
  436. (setenv "LANG" "en_US.UTF-8")
  437. (execl #$(program-file "installer-real" installer-builder
  438. #:guile guile-3.0-latest)
  439. "installer-real"))))