1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu installer steps)
20 #:use-module (guix records)
21 #:use-module (guix build utils)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 pretty-print)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-34)
26 #:use-module (srfi srfi-35)
27 #:use-module (rnrs io ports)
28 #:export (&installer-step-abort
39 installer-step-description
40 installer-step-compute
41 installer-step-configuration-formatter
49 %installer-configuration-file
51 %configuration-file-width
55 ;; This condition may be raised to abort the current step.
56 (define-condition-type &installer-step-abort &condition
57 installer-step-abort?)
59 ;; This condition may be raised to break out from the steps execution.
60 (define-condition-type &installer-step-break &condition
61 installer-step-break?)
63 ;; An installer-step record is basically an id associated to a compute
64 ;; procedure. The COMPUTE procedure takes exactly one argument, an association
65 ;; list containing the results of previously executed installer-steps (see
66 ;; RUN-INSTALLER-STEPS description). The value returned by the COMPUTE
67 ;; procedure will be stored in the results list passed to the next
68 ;; installer-step and so on.
69 (define-record-type* <installer-step>
70 installer-step make-installer-step
72 (id installer-step-id) ;symbol
73 (description installer-step-description ;string
76 ;; Make it thunked so that 'G_' is called at the
77 ;; right time, as opposed to being called once
78 ;; when the installer starts.
80 (compute installer-step-compute) ;procedure
81 (configuration-formatter installer-step-configuration-formatter ;procedure
84 (define* (run-installer-steps #:key
86 (rewind-strategy 'previous)
87 (menu-proc (const #f)))
88 "Run the COMPUTE procedure of all <installer-step> records in STEPS
89 sequencially. If the &installer-step-abort condition is raised, fallback to a
90 previous install-step, accordingly to the specified REWIND-STRATEGY.
92 REWIND-STRATEGY possible values are 'previous, 'menu and 'start. If 'previous
93 is selected, the execution will resume at the previous installer-step. If
94 'menu is selected, the MENU-PROC procedure will be called. Its return value
95 has to be an installer-step ID to jump to. The ID has to be the one of a
96 previously executed step. It is impossible to jump forward. Finally if 'start
97 is selected, the execution will resume at the first installer-step.
99 The result of every COMPUTE procedures is stored in an association list, under
102 '((STEP-ID . COMPUTE-RESULT) ...)
104 where STEP-ID is the ID field of the installer-step and COMPUTE-RESULT the
105 result of the associated COMPUTE procedure. This result association list is
106 passed as argument of every COMPUTE procedure. It is finally returned when the
109 If the &installer-step-break condition is raised, stop the computation and
110 return the accumalated result so far."
111 (define (pop-result list)
114 (define (first-step? steps step)
116 ((first-step . rest-steps)
117 (equal? first-step step))))
119 (define* (skip-to-step step result
120 #:key todo-steps done-steps)
123 (let ((found? (eq? (installer-step-id todo)
124 (installer-step-id step))))
128 #:todo-steps todo-steps
129 #:done-steps done-steps))
132 (error (format #f "Step ~a not found" (installer-step-id step))))
135 ((prev-done ... last-done)
136 (skip-to-step step (pop-result result)
137 #:todo-steps (cons last-done todo-steps)
138 #:done-steps prev-done)))))))))
140 (define* (run result #:key todo-steps done-steps)
142 (() (reverse result))
144 (guard (c ((installer-step-abort? c)
145 (case rewind-strategy
149 ;; We cannot go previous the first step. So re-raise
150 ;; the exception. It might be useful in the case of
151 ;; nested run-installer-steps. Abort to 'raise-above
152 ;; prompt to prevent the condition from being catched
153 ;; by one of the previously installed guard.
154 (abort-to-prompt 'raise-above c))
155 ((prev-done ... last-done)
156 (run (pop-result result)
157 #:todo-steps (cons last-done todo-steps)
158 #:done-steps prev-done))))
160 (let ((goto-step (menu-proc
161 (append done-steps (list step)))))
162 (if (eq? goto-step step)
164 #:todo-steps todo-steps
165 #:done-steps done-steps)
166 (skip-to-step goto-step result
167 #:todo-steps todo-steps
168 #:done-steps done-steps))))
170 (if (null? done-steps)
171 ;; Same as above, it makes no sense to jump to start
172 ;; when we are at the first installer-step. Abort to
173 ;; 'raise-above prompt to re-raise the condition.
174 (abort-to-prompt 'raise-above c)
177 #:done-steps '())))))
178 ((installer-step-break? c)
180 (let* ((id (installer-step-id step))
181 (compute (installer-step-compute step))
182 (res (compute result done-steps)))
183 (run (alist-cons id res result)
184 #:todo-steps rest-steps
185 #:done-steps (append done-steps (list step))))))))
187 (call-with-prompt 'raise-above
192 (lambda (k condition)
195 (define (find-step-by-id steps id)
196 "Find and return the step in STEPS whose id is equal to ID."
198 (eq? (installer-step-id step) id))
201 (define (result-step results step-id)
202 "Return the result of the installer-step specified by STEP-ID in
204 (assoc-ref results step-id))
206 (define (result-step-done? results step-id)
207 "Return #t if the installer-step specified by STEP-ID has a COMPUTE value
208 stored in RESULTS. Return #f otherwise."
209 (and (assoc step-id results) #t))
211 (define %installer-configuration-file (make-parameter "/mnt/etc/config.scm"))
212 (define %installer-target-dir (make-parameter "/mnt"))
213 (define %configuration-file-width (make-parameter 79))
215 (define (format-configuration steps results)
216 "Return the list resulting from the application of the procedure defined in
217 CONFIGURATION-FORMATTER field of <installer-step> on the associated result
222 (let* ((step-id (installer-step-id step))
224 (installer-step-configuration-formatter step))
225 (result-step (result-step results step-id)))
226 (if (and result-step conf-formatter)
227 (conf-formatter result-step)
230 (modules '((use-modules (gnu))
231 (use-service-modules desktop networking ssh xorg))))
234 (operating-system ,@configuration))))
236 (define* (configuration->file configuration
237 #:key (filename (%installer-configuration-file)))
238 "Write the given CONFIGURATION to FILENAME."
239 (mkdir-p (dirname filename))
240 (call-with-output-file filename
242 (format port ";; This is an operating system configuration generated~%")
243 (format port ";; by the graphical installer.~%")
245 (for-each (lambda (part)
248 (pretty-print part port)))
250 (flush-output-port port))))