installer.scm 14 KB

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