installer.scm 20 KB

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