installer.scm 17 KB

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