code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt services)
  #:use-module (gnu installer services)
  #:use-module (gnu installer steps)
  #:use-module (gnu installer newt page)
  #:use-module (gnu installer newt utils)
  #:use-module (guix i18n)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (newt)
  #:export (run-services-page))

(define (run-desktop-environments-cbt-page)
  "Run a page allowing the user to choose between various desktop
environments."
  (let ((items (filter desktop-system-service? %system-services)))
    (run-checkbox-tree-page
     #:info-text (G_ "Please select the desktop environment(s) you wish to \
install.  If you select multiple desktop environments here, you will be able \
to choose from them later when you log in.")
     #:title (G_ "Desktop environment")
     #:items items
     #:selection (map system-service-recommended? items)
     #:item->text system-service-name             ;no i18n for DE names
     #:checkbox-tree-height 9
     #:exit-button-callback-procedure
     (lambda ()
       (raise
        (condition
         (&installer-step-abort)))))))

(define (run-networking-cbt-page)
  "Run a page allowing the user to select networking services."
  (let ((items (filter (lambda (service)
                         (eq? 'networking (system-service-type service)))
                       %system-services)))
    (run-checkbox-tree-page
     #:info-text (G_ "You can now select networking services to run on your \
system.")
     #:title (G_ "Network service")
     #:items items
     #:selection (map system-service-recommended? items)
     #:item->text (compose G_ system-service-name)
     #:checkbox-tree-height 5
     #:exit-button-callback-procedure
     (lambda ()
       (raise
        (condition
         (&installer-step-abort)))))))

(define (run-other-services-cbt-page)
  "Run a page allowing the user to select other services."
  (let ((items (filter (lambda (service)
                         (not (member (system-service-type service)
                                      '(desktop
                                        network-management
                                        networking))))
                       %system-services)))
    (run-checkbox-tree-page
     #:info-text (G_ "You can now select other services to run on your \
system.")
     #:title (G_ "Other services")
     #:items items
     #:selection (map system-service-recommended? items)
     #:item->text (compose G_ system-service-name)
     #:checkbox-tree-height 9
     #:exit-button-callback-procedure
     (lambda ()
       (raise
        (condition
         (&installer-step-abort)))))))

(define (run-network-management-page)
  "Run a page to select among several network management methods."
  (let ((title (G_ "Network management")))
    (run-listbox-selection-page
     #:title title
     #:info-text (G_ "Choose the method to manage network connections.

We recommend NetworkManager or Connman for a WiFi-capable laptop; the DHCP \
client may be enough for a server.")
     #:info-textbox-width 70
     #:listbox-height 7
     #:listbox-items (filter (lambda (service)
                               (eq? 'network-management
                                    (system-service-type service)))
                             %system-services)
     #:listbox-item->text (compose G_ system-service-name)
     #:sort-listbox-items? #f
     #:button-text (G_ "Exit")
     #:button-callback-procedure
     (lambda _
       (raise
        (condition
         (&installer-step-abort)))))))

(define (run-services-page)
  (let ((desktop (run-desktop-environments-cbt-page)))
    ;; When the user did not select any desktop services, and thus didn't get
    ;; '%desktop-services', offer network management services.
    (append desktop
            (run-networking-cbt-page)
            (if (null? desktop)
                (list (run-network-management-page))
                '())
            (run-other-services-cbt-page))))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt substitutes)
  #:use-module (gnu installer substitutes)
  #:use-module (gnu installer utils)
  #:use-module (guix i18n)
  #:use-module (newt)
  #:use-module (ice-9 match)
  #:export (run-substitutes-page))

(define* (run-substitutes-page)
  (match (current-clients)
    (()
     (case (choice-window
            (G_ "Substitute server discovery.")
            (G_ "Enable") (G_ "Disable")
            (G_ " By turning this option on, you allow Guix to fetch \
substitutes (pre-built binaries) during installation from servers \
discovered on your local area network (LAN) in addition to the official \
server.  This can increase download throughput.

 There are no security risks: only genuine substitutes may be retrieved from \
those servers.  However, eavesdroppers on your LAN may be able to see what \
software you are installing."))
       ((1) (enable-discovery))
       ((2) (disable-discovery))))
    (_ #f)))

repo_name: maximed/guix

license: mit


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt timezone)
  #:use-module (gnu installer steps)
  #:use-module (gnu installer timezone)
  #:use-module (gnu installer newt page)
  #:use-module (guix i18n)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (ice-9 match)
  #:use-module (ice-9 receive)
  #:use-module (newt)
  #:export (run-timezone-page))

;; Information textbox width.
(define info-textbox-width (make-parameter 40))

(define (fill-timezones listbox timezones)
  "Fill the given LISTBOX with TIMEZONES. Return an association list
correlating listbox keys with timezones."
  (map (lambda (timezone)
         (let ((key (append-entry-to-listbox listbox timezone)))
           (cons key timezone)))
       timezones))

(define (run-timezone-page zonetab)
  "Run a page displaying available timezones, grouped by regions. The user is
invited to select a timezone. The selected timezone, under Posix format is
returned."
  (define (all-but-last list)
    (reverse (cdr (reverse list))))

  (define (run-page timezone-tree)
    (define (loop path)
      ;; XXX: Translation of time zones isn't perfect here because the
      ;; "iso_3166-1" domain contains translation for "territories" (like
      ;; "Antarctic") but not for continents (like "Africa").
      (let ((timezones (locate-children timezone-tree path)))
        (run-listbox-selection-page
         #:title (G_ "Timezone")
         #:info-text (G_ "Please select a timezone.")
         #:listbox-items timezones
         #:listbox-item->text (cut gettext <> "iso_3166-1")
         #:button-text (if (null? path)
                           (G_ "Exit")
                           (G_ "Back"))
         #:button-callback-procedure
         (if (null? path)
             (lambda _
               (raise
                (condition
                 (&installer-step-abort))))
             (lambda _
               (loop (all-but-last path))))
         #:listbox-callback-procedure
         (lambda (timezone)
           (let* ((timezone* (append path (list timezone)))
                  (tz (timezone->posix-tz timezone*)))
             (if (timezone-has-child? timezone-tree timezone*)
                 (loop timezone*)
                 tz))))))
    (loop '()))

  (let ((timezone-tree (zonetab->timezone-tree zonetab)))
    (run-page timezone-tree)))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt user)
  #:use-module (gnu installer user)
  #:use-module ((gnu installer steps) #:select (&installer-step-abort))
  #:use-module (gnu installer newt page)
  #:use-module (gnu installer newt utils)
  #:use-module (gnu installer utils)
  #:use-module (guix i18n)
  #:use-module (newt)
  #:use-module (ice-9 match)
  #:use-module (ice-9 receive)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:export (run-user-page))

(define* (run-user-add-page #:key (name "") (real-name "")
                            (home-directory ""))
  "Run a form to enter the user name, home directory, and password.  Use NAME,
REAL-NAME, and HOME-DIRECTORY as the initial values in the form."
  (define (pad-label label)
    (string-pad-right label 25))

  (let* ((label-name
          (make-label -1 -1 (pad-label (G_ "Name"))))
         (label-real-name
          (make-label -1 -1 (pad-label (G_ "Real name"))))
         (label-home-directory
          (make-label -1 -1 (pad-label (G_ "Home directory"))))
         (label-password
          (make-label -1 -1 (pad-label (G_ "Password"))))
         (entry-width 35)
         (entry-name (make-entry -1 -1 entry-width
                                 #:initial-value name))
         (entry-real-name (make-entry -1 -1 entry-width
                                      #:initial-value real-name))
         (entry-home-directory (make-entry -1 -1 entry-width
                                           #:initial-value home-directory))
         (password-visible-cb
          (make-checkbox -1 -1 (G_ "Show") #\space "x "))
         (entry-password (make-entry -1 -1 entry-width
                                     #:flags (logior FLAG-PASSWORD
                                                     FLAG-SCROLL)))
         (entry-grid (make-grid 3 5))
         (button-grid (make-grid 1 1))
         (ok-button (make-button -1 -1 (G_ "OK")))
         (grid (make-grid 1 2))
         (title (G_ "User creation"))
         (set-entry-grid-field
          (cut set-grid-field entry-grid <> <> GRID-ELEMENT-COMPONENT <>))
         (form (make-form)))

    (set-entry-grid-field 0 0 label-name)
    (set-entry-grid-field 1 0 entry-name)
    (set-entry-grid-field 0 1 label-real-name)
    (set-entry-grid-field 1 1 entry-real-name)
    (set-entry-grid-field 0 2 label-home-directory)
    (set-entry-grid-field 1 2 entry-home-directory)
    (set-entry-grid-field 0 3 label-password)
    (set-entry-grid-field 1 3 entry-password)

    (set-grid-field entry-grid
                    2 3
                    GRID-ELEMENT-COMPONENT
                    password-visible-cb
                    #:pad-left 1)

    (set-grid-field button-grid 0 0 GRID-ELEMENT-COMPONENT ok-button)

    (add-component-callback
     entry-name
     (lambda ()
       (set-entry-text entry-home-directory
                       (string-append "/home/" (entry-value entry-name)))

       (when (string-null? (entry-value entry-real-name))
         (set-entry-text entry-real-name
                         (string-titlecase (entry-value entry-name))))))

    (add-component-callback
     password-visible-cb
     (lambda ()
       (set-entry-flags entry-password
                        FLAG-PASSWORD
                        FLAG-ROLE-TOGGLE)))

    (add-components-to-form form
                            label-name label-real-name
                            label-home-directory label-password
                            entry-name entry-real-name
                            entry-home-directory entry-password
                            password-visible-cb
                            ok-button)

    (make-wrapped-grid-window (vertically-stacked-grid
                               GRID-ELEMENT-SUBGRID entry-grid
                               GRID-ELEMENT-SUBGRID button-grid)
                              title)

    (let ((error-page
           (lambda ()
             (run-error-page (G_ "Empty inputs are not allowed.")
                             (G_ "Empty input")))))
      (receive (exit-reason argument)
          (run-form form)
        (dynamic-wind
          (const #t)
          (lambda ()
            (when (eq? exit-reason 'exit-component)
              (cond
               ((components=? argument ok-button)
                (let ((name           (entry-value entry-name))
                      (real-name      (entry-value entry-real-name))
                      (home-directory (entry-value entry-home-directory))
                      (password       (entry-value entry-password)))
                  (if (or (string=? name "")
                          (string=? home-directory ""))
                      (begin
                        (error-page)
                        (run-user-add-page))
                      (let ((password (confirm-password password)))
                        (if password
                            (user
                             (name name)
                             (real-name real-name)
                             (home-directory home-directory)
                             (password password))
                            (run-user-add-page #:name name
                                               #:real-name real-name
                                               #:home-directory
                                               home-directory)))))))))
          (lambda ()
            (destroy-form-and-pop form)))))))

(define* (confirm-password password #:optional (try-again (const #f)))
  "Ask the user to confirm PASSWORD, a possibly empty string.  Call TRY-AGAIN,
a thunk, if the confirmation doesn't match PASSWORD, and return its result."
  (define confirmation
    (run-input-page (G_ "Please confirm the password.")
                    (G_ "Password confirmation required")
                    #:allow-empty-input? #t
                    #:input-visibility-checkbox? #t))

  (if (string=? password confirmation)
      password
      (begin
        (run-error-page
         (G_ "Password mismatch, please try again.")
         (G_ "Password error"))
        (try-again))))

(define (run-root-password-page)
  (define password
    ;; TRANSLATORS: Leave "root" untranslated: it refers to the name of the
    ;; system administrator account.
    (run-input-page (G_ "Please choose a password for the system \
administrator (\"root\").")
                    (G_ "System administrator password")
                    #:input-visibility-checkbox? #t))

  (confirm-password password run-root-password-page))

(define (run-user-page)
  (define (run users)
    (let* ((listbox (make-listbox
                     -1 -1 10
                     (logior FLAG-SCROLL FLAG-BORDER)))
           (info-textbox
            (make-reflowed-textbox
             -1 -1
             (G_ "Please add at least one user to system\
 using the 'Add' button.")
             40 #:flags FLAG-BORDER))
           (add-button (make-compact-button -1 -1 (G_ "Add")))
           (del-button (make-compact-button -1 -1 (G_ "Delete")))
           (listbox-button-grid
            (apply
             vertically-stacked-grid
             GRID-ELEMENT-COMPONENT add-button
             `(,@(if (null? users)
                     '()
                     (list GRID-ELEMENT-COMPONENT del-button)))))
           (ok-button (make-button -1 -1 (G_ "OK")))
           (exit-button (make-button -1 -1 (G_ "Exit")))
           (title (G_ "User creation"))
           (grid
            (vertically-stacked-grid
             GRID-ELEMENT-COMPONENT info-textbox
             GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
                                   GRID-ELEMENT-COMPONENT listbox
                                   GRID-ELEMENT-SUBGRID listbox-button-grid)
             GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
                                   GRID-ELEMENT-COMPONENT ok-button
                                   GRID-ELEMENT-COMPONENT exit-button)))
           (sorted-users (sort users (lambda (a b)
                                       (string<= (user-name a)
                                                 (user-name b)))))
           (listbox-elements
            (map
             (lambda (user)
               `((key . ,(append-entry-to-listbox listbox
                                                  (user-name user)))
                 (user . ,user)))
             sorted-users))
           (form (make-form)))

      (add-form-to-grid grid form #t)
      (make-wrapped-grid-window grid title)
      (if (null? users)
          (set-current-component form add-button)
          (set-current-component form ok-button))

      (receive (exit-reason argument)
          (run-form-with-clients form '(add-users))
        (dynamic-wind
          (const #t)
          (lambda ()
            (match exit-reason
              ('exit-component
               (cond
                ((components=? argument add-button)
                 (run (cons (run-user-add-page) users)))
                ((components=? argument del-button)
                 (let* ((current-user-key (current-listbox-entry listbox))
                        (users
                         (map (cut assoc-ref <> 'user)
                              (remove (lambda (element)
                                        (equal? (assoc-ref element 'key)
                                                current-user-key))
                                      listbox-elements))))
                   (run users)))
                ((components=? argument ok-button)
                 (when (null? users)
                   (run-error-page (G_ "Please create at least one user.")
                                   (G_ "No user"))
                   (run users))
                 (reverse users))
                ((components=? argument exit-button)
                 (raise
                  (condition
                   (&installer-step-abort))))))
              ('exit-fd-ready
               ;; Read the complete user list at once.
               (match argument
                 ((('user ('name names) ('real-name real-names)
                          ('home-directory homes) ('password passwords))
                   ..1)
                  (map (lambda (name real-name home password)
                         (user (name name) (real-name real-name)
                               (home-directory home)
                               (password password)))
                       names real-names homes passwords))))))
          (lambda ()
            (destroy-form-and-pop form))))))

  ;; Add a "root" user simply to convey the root password.
  (cons (user (name "root")
              (home-directory "/root")
              (password (run-root-password-page)))
        (run '())))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt utils)
  #:use-module (ice-9 receive)
  #:use-module (newt)
  #:export (screen-columns
            screen-rows

            destroy-form-and-pop
            set-screen-size!))

;; Number of columns and rows of the terminal.
(define screen-columns (make-parameter 0))
(define screen-rows    (make-parameter 0))

(define (destroy-form-and-pop form)
  "Destroy the given FORM and pop the current window."
  (destroy-form form)
  (pop-window))

(define (set-screen-size!)
  "Set the parameters 'screen-columns' and 'screen-rows' to the number of
columns and rows respectively of the current terminal."
  (receive (columns rows)
      (screen-size)
    (screen-columns columns)
    (screen-rows rows)))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt welcome)
  #:use-module (gnu installer steps)
  #:use-module (gnu installer utils)
  #:use-module (gnu installer newt page)
  #:use-module (gnu installer newt utils)
  #:use-module (guix build syscalls)
  #:use-module (guix i18n)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (ice-9 match)
  #:use-module (ice-9 receive)
  #:use-module (newt)
  #:export (run-welcome-page))

;; Expected width and height for the logo.
(define logo-width (make-parameter 43))
(define logo-height (make-parameter 19))

(define info-textbox-width (make-parameter 70))
(define options-listbox-height (make-parameter 5))

(define (display-logo?)
  (> (screen-rows) 35))

(define* (run-menu-page title info-text logo
                        #:key
                        listbox-items
                        listbox-item->text)
  "Run a page with the given TITLE, to ask the user to choose between
LISTBOX-ITEMS displayed in a listbox. The listbox items are converted to text
using LISTBOX-ITEM->TEXT procedure. Display the textual LOGO in the center of
the page. Contrary to other pages, we cannot resort to grid layouts, because
we want this page to occupy all the screen space available."
  (define (fill-listbox listbox items)
    (map (lambda (item)
           (let* ((text (listbox-item->text item))
                  (key (append-entry-to-listbox listbox text)))
             (cons key item)))
         items))

  (let* ((logo-textbox
          (make-textbox -1 -1
                        (if (display-logo?) (logo-width) 0)
                        (if (display-logo?) (logo-height) 0)
                        0))
         (info-textbox
          (make-reflowed-textbox -1 -1
                                 info-text
                                 (info-textbox-width)))
         (options-listbox
          (make-listbox -1 -1
                        (options-listbox-height)
                        (logior FLAG-BORDER FLAG-RETURNEXIT)))
         (keys (fill-listbox options-listbox listbox-items))
         (grid (vertically-stacked-grid
                GRID-ELEMENT-COMPONENT logo-textbox
                GRID-ELEMENT-COMPONENT info-textbox
                GRID-ELEMENT-COMPONENT options-listbox))
         (form (make-form)))

    (define (choice->item str)
      ;; Return the item that corresponds to STR.
      (match (find (match-lambda
                     ((key . item)
                      (string=? str (listbox-item->text item))))
                   keys)
        ((key . item) item)
        (#f (raise (condition (&installer-step-abort))))))

    (set-textbox-text logo-textbox (read-all logo))

    (add-form-to-grid grid form #t)
    (make-wrapped-grid-window grid title)

    (receive (exit-reason argument)
        (run-form-with-clients form
                               `(menu (title ,title)
                                      (text ,info-text)
                                      (items
                                       ,(map listbox-item->text
                                             listbox-items))))
      (dynamic-wind
        (const #t)
        (lambda ()
          (match exit-reason
            ('exit-component
             (let* ((entry (current-listbox-entry options-listbox))
                    (item (assoc-ref keys entry)))
               (match item
                 ((text . proc)
                  (proc)))))
            ('exit-fd-ready
             (let* ((choice argument)
                    (item   (choice->item choice)))
               (match item
                 ((text . proc)
                  (proc)))))))
        (lambda ()
          (destroy-form-and-pop form))))))

(define (run-welcome-page logo)
  "Run a welcome page with the given textual LOGO displayed at the center of
the page. Ask the user to choose between manual installation, graphical
installation and reboot."
  (run-menu-page
   (G_ "GNU Guix install")
   (G_ "Welcome to GNU Guix system installer!

You will be guided through a graphical installation program.

If you are familiar with GNU/Linux and you want tight control over \
the installation process, you can instead choose manual installation.  \
Documentation is accessible at any time by pressing Ctrl-Alt-F2.")
   logo
   #:listbox-items
   `((,(G_ "Graphical install using a terminal based interface")
      .
      ,(const #t))
     (,(G_ "Install using the shell based process")
      .
      ,(lambda ()
         ;; Switch to TTY3, where a root shell is available for shell based
         ;; install. The other root TTY's would have been ok too.
         (system* "chvt" "3")
         (run-welcome-page logo)))
     (,(G_ "Reboot")
      .
      ,(lambda ()
         (newt-finish)
         (reboot))))
   #:listbox-item->text car))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019 Meiyo Peng <meiyo@riseup.net>
;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt wifi)
  #:use-module (gnu installer connman)
  #:use-module (gnu installer steps)
  #:use-module (gnu installer newt utils)
  #:use-module (gnu installer newt page)
  #:use-module (guix i18n)
  #:use-module (guix records)
  #:use-module (ice-9 format)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 rdelim)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (newt)
  #:export (run-wifi-page))

;; This record associates a connman service to its key the listbox.
(define-record-type* <service-item>
  service-item make-service-item
  service-item?
  (service   service-item-service) ; connman <service>
  (key       service-item-key)) ; newt listbox-key

(define (strength->string strength)
  "Convert STRENGTH as an integer percentage into a text printable strength
bar using unicode characters. Taken from NetworkManager's
nmc_wifi_strength_bars."
  (let ((quarter #\x2582)
        (half #\x2584)
        (three-quarter #\x2586)
        (full #\x2588))
    (cond
     ((> strength 80)
      ;; ▂▄▆█
      (string quarter half three-quarter full))
     ((> strength 55)
      ;; ▂▄▆_
      (string quarter half three-quarter #\_))
     ((> strength 30)
      ;; ▂▄__
      (string quarter half #\_ #\_))
     ((> strength 5)
      ;; ▂___
      (string quarter #\_ #\_ #\_))
     (else
      ;; ____
      (string quarter #\_ #\_ #\_ #\_)))))

(define (force-wifi-scan)
  "Force a wifi scan. Raise a condition if no wifi technology is available."
  (let* ((technologies (connman-technologies))
         (wifi-technology
          (find (lambda (technology)
                  (string=? (technology-type technology) "wifi"))
                technologies)))
    (if wifi-technology
        (connman-scan-technology wifi-technology)
        (raise (condition
                (&message
                 (message (G_ "Unable to find a wifi technology"))))))))

(define (draw-scanning-page)
  "Draw a page to indicate a wifi scan in progress."
  (draw-info-page (G_ "Scanning wifi for available networks, please wait.")
                  (G_ "Scan in progress")))

(define (run-wifi-password-page)
  "Run a page prompting user for a password and return it."
  (run-input-page (G_ "Please enter the wifi password.")
                  (G_ "Password required")
                  #:input-visibility-checkbox? #t))

(define (run-wrong-password-page service-name)
  "Run a page to inform user of a wrong password input."
  (run-error-page
   (format #f (G_ "The password you entered for ~a is incorrect.")
           service-name)
   (G_ "Wrong password")))

(define (run-unknown-error-page service-name)
  "Run a page to inform user that a connection error happened."
  (run-error-page
   (format #f
           (G_ "An error occurred while trying to connect to ~a, please retry.")
           service-name)
   (G_ "Connection error")))

(define (password-callback)
  (run-wifi-password-page))

(define (connect-wifi-service listbox service-items)
  "Connect to the wifi service selected in LISTBOX. SERVICE-ITEMS is the list
of <service-item> records present in LISTBOX."
  (let* ((listbox-key (current-listbox-entry listbox))
         (item (find (lambda (item)
                       (eq? (service-item-key item) listbox-key))
                     service-items))
         (service (service-item-service item))
         (service-name (service-name service))
         (form (draw-connecting-page service-name)))
    (dynamic-wind
      (const #t)
      (lambda ()
        (guard (c ((connman-password-error? c)
                   (run-wrong-password-page service-name)
                   #f)
                  ((connman-already-connected-error? c)
                   #t)
                  ((connman-connection-error? c)
                   (run-unknown-error-page service-name)
                   #f))
          (connman-connect-with-auth service password-callback)))
      (lambda ()
        (destroy-form-and-pop form)))))

(define (run-wifi-scan-page)
  "Force a wifi scan and draw a page during the operation."
  (let ((form (draw-scanning-page)))
    (force-wifi-scan)
    (destroy-form-and-pop form)))

(define (wifi-services)
  "Return all the connman services of wifi type."
  (let ((services (connman-services)))
    (filter (lambda (service)
              (and (string=? (service-type service) "wifi")
                   (service-name service)
                   (not (string-null? (service-name service)))))
            services)))

(define* (fill-wifi-services listbox wifi-services)
  "Append all the services in WIFI-SERVICES to the given LISTBOX."
  (clear-listbox listbox)
  (map (lambda (service)
         (let* ((text (service->text service))
                (key (append-entry-to-listbox listbox text)))
           (service-item
            (service service)
            (key key))))
       wifi-services))

;; Maximum length of a wifi service name.
(define service-name-max-length (make-parameter 20))

;; Height of the listbox displaying wifi services.
(define wifi-listbox-height (make-parameter
                             (default-listbox-height)))

;; Information textbox width.
(define info-textbox-width (make-parameter 40))

(define (service->text service)
  "Return a string composed of the name and the strength of the given
SERVICE. A '*' preceding the service name indicates that it is connected."
  (let* ((name (service-name service))
         (padded-name (string-pad-right name
                                        (service-name-max-length)))
         (strength (service-strength service))
         (strength-string (strength->string strength))
         (state (service-state service))
         (connected? (or (string=? state "online")
                         (string=? state "ready"))))
    (format #f "~c ~a ~a~%"
            (if connected? #\* #\ )
            padded-name
            strength-string)))

(define (run-wifi-page)
  "Run a page displaying available wifi networks in a listbox. Connect to the
network when the corresponding listbox entry is selected. A button allow to
force a wifi scan."
  (let* ((listbox (make-listbox
                   -1 -1
                   (wifi-listbox-height)
                   (logior FLAG-SCROLL FLAG-BORDER FLAG-RETURNEXIT)))
         (form (make-form))
         (buttons-grid (make-grid 1 1))
         (middle-grid (make-grid 2 1))
         (info-text (G_ "Please select a wifi network."))
         (info-textbox
          (make-reflowed-textbox -1 -1 info-text
                                 (info-textbox-width)
                                 #:flags FLAG-BORDER))
         (exit-button (make-button -1 -1 (G_ "Exit")))
         (scan-button (make-button -1 -1 (G_ "Scan")))
         (services (wifi-services))
         (service-items '()))

    (if (null? services)
        (append-entry-to-listbox listbox (G_ "No wifi detected"))
        (set! service-items (fill-wifi-services listbox services)))

    (set-grid-field middle-grid 0 0 GRID-ELEMENT-COMPONENT listbox)
    (set-grid-field middle-grid 1 0 GRID-ELEMENT-COMPONENT scan-button
                    #:anchor ANCHOR-TOP
                    #:pad-left 2)
    (set-grid-field buttons-grid 0 0 GRID-ELEMENT-COMPONENT exit-button)

    (add-components-to-form form
                            info-textbox
                            listbox scan-button
                            exit-button)
    (make-wrapped-grid-window
     (basic-window-grid info-textbox middle-grid buttons-grid)
     (G_ "Wifi"))

    (receive (exit-reason argument)
        (run-form form)
      (dynamic-wind
        (const #t)
        (lambda ()
          (when (eq? exit-reason 'exit-component)
            (cond
             ((components=? argument scan-button)
              (run-wifi-scan-page)
              (run-wifi-page))
             ((components=? argument exit-button)
              (raise
               (condition
                (&installer-step-abort))))
             ((components=? argument listbox)
              (let ((result (connect-wifi-service listbox service-items)))
                (unless result
                  (run-wifi-page)))))))
        (lambda ()
          (destroy-form-and-pop form))))))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer parted)
  #:use-module (gnu installer steps)
  #:use-module (gnu installer utils)
  #:use-module (gnu installer newt page)
  #:use-module (gnu system uuid)
  #:use-module ((gnu build file-systems)
                #:select (canonicalize-device-spec
                          find-partition-by-label
                          read-partition-uuid
                          read-luks-partition-uuid))
  #:use-module ((gnu build linux-boot)
                #:select (linux-command-line
                          find-long-option))
  #:use-module ((gnu build linux-modules)
                #:select (missing-modules))
  #:use-module ((gnu system linux-initrd)
                #:select (%base-initrd-modules))
  #:use-module (guix build syscalls)
  #:use-module (guix build utils)
  #:use-module (guix records)
  #:use-module (guix utils)
  #:use-module (guix i18n)
  #:use-module (parted)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (rnrs io ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:export (<user-partition>
            user-partition
            make-user-partition
            user-partition?
            user-partition-name
            user-partition-type
            user-partition-file-name
            user-partition-disk-file-name
            user-partition-crypt-label
            user-partition-crypt-password
            user-partition-fs-type
            user-partition-bootable?
            user-partition-esp?
            user-partition-bios-grub?
            user-partition-size
            user-partition-start
            user-partition-end
            user-partition-mount-point
            user-partition-need-formatting?
            user-partition-parted-object

            find-esp-partition
            small-freespace-partition?
            esp-partition?
            boot-partition?
            efi-installation?
            default-esp-mount-point

            with-delay-device-in-use?
            force-device-sync
            non-install-devices
            partition-user-type
            user-fs-type-name
            partition-filesystem-user-type
            partition-get-flags
            partition->user-partition
            create-special-user-partitions
            find-user-partition-by-parted-object

            device-description
            partition-end-formatted
            partition-print-number
            partition-description
            partitions-descriptions
            user-partition-description

            &max-primary-exceeded
            max-primary-exceeded?
            &extended-creation-error
            extended-creation-error?
            &logical-creation-error
            logical-creation-error?

            can-create-partition?
            mklabel
            mkpart
            rmpart

            auto-partition!

            &no-root-mount-point
            no-root-mount-point?
            &cannot-read-uuid
            cannot-read-uuid?
            cannot-read-uuid-partition

            check-user-partitions
            set-user-partitions-file-name
            format-user-partitions
            mount-user-partitions
            umount-user-partitions
            with-mounted-partitions
            user-partitions->file-systems
            user-partitions->configuration

            init-parted
            free-parted))

;;;
;;; Partition record.
;;;

(define-record-type* <user-partition>
  user-partition make-user-partition
  user-partition?
  (name                 user-partition-name ;string
                        (default #f))
  (type                 user-partition-type
                        (default 'normal)) ; 'normal | 'logical | 'extended
  (file-name            user-partition-file-name
                        (default #f))
  (disk-file-name       user-partition-disk-file-name
                        (default #f))
  (crypt-label          user-partition-crypt-label
                        (default #f))
  (crypt-password       user-partition-crypt-password
                        (default #f))
  (fs-type              user-partition-fs-type
                        (default 'ext4))
  (bootable?            user-partition-bootable?
                        (default #f))
  (esp?                 user-partition-esp?
                        (default #f))
  (bios-grub?           user-partition-bios-grub?
                        (default #f))
  (size                 user-partition-size
                        (default #f))
  (start                user-partition-start ;start as string (e.g. '11MB')
                        (default #f))
  (end                  user-partition-end ;same as start
                        (default #f))
  (mount-point          user-partition-mount-point ;string
                        (default #f))
  (need-formatting?     user-partition-need-formatting? ; boolean
                        (default #f))
  (parted-object        user-partition-parted-object ; <partition> from parted
                        (default #f)))

;;
;; Utilities.
;;

(define (find-esp-partition partitions)
  "Find and return the ESP partition among PARTITIONS."
  (find esp-partition? partitions))

(define* (small-freespace-partition? device
                                     partition
                                     #:key (max-size MEBIBYTE-SIZE))
  "Return #t is PARTITION is a free-space partition with less a size strictly
inferior to MAX-SIZE, #f otherwise."
  (let ((size (partition-length partition))
        (max-sector-size (/ max-size
                            (device-sector-size device))))
    (< size max-sector-size)))

(define (partition-user-type partition)
  "Return the type of PARTITION, to be stored in the TYPE field of
<user-partition> record. It can be 'normal, 'extended or 'logical."
  (cond ((normal-partition? partition)
         'normal)
        ((extended-partition? partition)
         'extended)
        ((logical-partition? partition)
         'logical)
        (else #f)))

(define (esp-partition? partition)
  "Return #t if partition has the ESP flag, return #f otherwise."
  (let* ((disk (partition-disk partition))
         (disk-type (disk-disk-type disk)))
    (and (data-partition? partition)
         (partition-is-flag-available? partition PARTITION-FLAG-ESP)
         (partition-get-flag partition PARTITION-FLAG-ESP))))

(define (boot-partition? partition)
  "Return #t if partition has the boot flag, return #f otherwise."
  (and (data-partition? partition)
       (partition-is-flag-available? partition PARTITION-FLAG-BOOT)
       (partition-get-flag partition PARTITION-FLAG-BOOT)))

;; The default mount point for ESP partitions.
(define default-esp-mount-point
  (make-parameter "/boot/efi"))

(define (efi-installation?)
  "Return #t if an EFI installation should be performed, #f otherwise."
  (file-exists? "/sys/firmware/efi"))

(define (user-fs-type-name fs-type)
  "Return the name of FS-TYPE as specified by libparted."
  (case fs-type
    ((ext4)  "ext4")
    ((btrfs) "btrfs")
    ((fat16) "fat16")
    ((fat32) "fat32")
    ((jfs)   "jfs")
    ((ntfs)  "ntfs")
    ((swap)  "linux-swap")))

(define (user-fs-type->mount-type fs-type)
  "Return the mount type of FS-TYPE."
  (case fs-type
    ((ext4)  "ext4")
    ((btrfs) "btrfs")
    ((fat16) "vfat")
    ((fat32) "vfat")
    ((jfs)   "jfs")
    ((ntfs)  "ntfs")))

(define (partition-filesystem-user-type partition)
  "Return the filesystem type of PARTITION, to be stored in the FS-TYPE field
of <user-partition> record."
  (let ((fs-type (partition-fs-type partition)))
    (and fs-type
         (let ((name (filesystem-type-name fs-type)))
           (cond
            ((string=? name "ext4") 'ext4)
            ((string=? name "btrfs") 'btrfs)
            ((string=? name "fat16") 'fat16)
            ((string=? name "fat32") 'fat32)
            ((string=? name "jfs") 'jfs)
            ((string=? name "ntfs") 'ntfs)
            ((or (string=? name "swsusp")
                 (string=? name "linux-swap(v0)")
                 (string=? name "linux-swap(v1)"))
             'swap)
            (else
             (error (format #f "Unhandled ~a fs-type~%" name))))))))

(define (partition-get-flags partition)
  "Return the list of flags supported by the given PARTITION."
  (filter-map (lambda (flag)
                (and (partition-get-flag partition flag)
                     flag))
              (partition-flags partition)))

(define (partition->user-partition partition)
  "Convert PARTITION into a <user-partition> record and return it."
  (let* ((disk (partition-disk partition))
         (device (disk-device disk))
         (disk-type (disk-disk-type disk))
         (has-name? (disk-type-check-feature
                     disk-type
                     DISK-TYPE-FEATURE-PARTITION-NAME))
         (name (and has-name?
                    (data-partition? partition)
                    (partition-get-name partition))))
    (user-partition
     (name (and (and name
                     (not (string=? name "")))
                name))
     (type (or (partition-user-type partition)
               'normal))
     (file-name (partition-get-path partition))
     (disk-file-name (device-path device))
     (fs-type (or (partition-filesystem-user-type partition)
                  'ext4))
     (mount-point (and (esp-partition? partition)
                       (default-esp-mount-point)))
     (bootable? (boot-partition? partition))
     (esp? (esp-partition? partition))
     (parted-object partition))))

(define (create-special-user-partitions partitions)
  "Return a list with a <user-partition> record describing the ESP partition
found in PARTITIONS, if any."
  (filter-map (lambda (partition)
                (and (esp-partition? partition)
                     (partition->user-partition partition)))
              partitions))

(define (find-user-partition-by-parted-object user-partitions
                                              partition)
  "Find and return the <user-partition> record in USER-PARTITIONS list which
PARTED-OBJECT field equals PARTITION, return #f if not found."
  (find (lambda (user-partition)
          (equal? (user-partition-parted-object user-partition)
                  partition))
        user-partitions))

;;
;; Devices
;;

(define (with-delay-device-in-use? file-name)
  "Call DEVICE-IN-USE? with a few retries, as the first re-read will often
fail. See rereadpt function in wipefs.c of util-linux for an explanation."
  ;; Kernel always return EINVAL for BLKRRPART on loopdevices.
  (and (not (string-match "/dev/loop*" file-name))
       (let loop ((try 16))
         (usleep 250000)
         (let ((in-use? (device-in-use? file-name)))
           (if (and in-use? (> try 0))
               (loop (- try 1))
               in-use?)))))

(define* (force-device-sync device)
  "Force a flushing of the given DEVICE."
  (device-open device)
  (device-sync device)
  (device-close device))

(define (remove-logical-devices)
  "Remove all active logical devices."
  (with-null-output-ports
   (invoke "dmsetup" "remove_all")))

(define (installation-device)
  "Return the installation device path."
  (let* ((cmdline (linux-command-line))
         (root (find-long-option "--root" cmdline)))
    (and root
         (canonicalize-device-spec (uuid root)))))

(define (non-install-devices)
  "Return all the available devices, except the install device."
  (define (read-only? device)
    (dynamic-wind
    (lambda ()
      (device-open device))
    (lambda ()
      (device-read-only? device))
    (lambda ()
      (device-close device))))

  ;; If parted reports that a device is read-only it is probably the
  ;; installation device. However, as this detection does not always work,
  ;; compare the device path to the installation device path read from the
  ;; command line.
  (let ((install-device (installation-device)))
    (remove (lambda (device)
              (let ((file-name (device-path device)))
                (or (read-only? device)
                    (and install-device
                         (string=? file-name install-device)))))
            (devices))))

;;
;; Disk and partition printing.
;;

(define* (device-description device #:optional disk)
  "Return a string describing the given DEVICE."
  (let* ((type (device-type device))
         (file-name (device-path device))
         (model (device-model device))
         (type-str (device-type->string type))
         (disk-type (if disk
                        (disk-disk-type disk)
                        (disk-probe device)))
         (length (device-length device))
         (sector-size (device-sector-size device))
         (end (unit-format-custom-byte device
                                       (* length sector-size)
                                       UNIT-GIGABYTE)))
    (string-join
     `(,@(if (string=? model "")
             `(,type-str)
             `(,model ,(string-append "(" type-str ")")))
       ,file-name
       ,end
       ,@(if disk-type
             `(,(disk-type-name disk-type))
             '()))
     " ")))

(define (partition-end-formatted device partition)
  "Return as a string the end of PARTITION with the relevant unit."
  (unit-format-byte
   device
   (-
    (* (+ (partition-end partition) 1)
       (device-sector-size device))
    1)))

(define (partition-print-number partition)
  "Convert the given partition NUMBER to string."
  (let ((number (partition-number partition)))
    (number->string number)))

(define (partition-description partition user-partition)
  "Return a string describing the given PARTITION, located on the DISK of
DEVICE."

  (define (partition-print-type partition)
    "Return the type of PARTITION as a string."
    (if (freespace-partition? partition)
        (G_ "Free space")
        (let ((type (partition-type partition)))
          (match type
            ((type-symbol)
             (symbol->string type-symbol))))))

  (define (partition-print-flags partition)
    "Return the flags of PARTITION as a string of comma separated flags."
    (string-join
     (filter-map
      (lambda (flag)
        (and (partition-get-flag partition flag)
             (partition-flag-get-name flag)))
      (partition-flags partition))
     ","))

  (define (maybe-string-pad string length)
    "Returned a string formatted by padding STRING of LENGTH characters to the
right. If STRING is #f use an empty string."
    (if (and string (not (string=? string "")))
        (string-pad-right string length)
        ""))

  (let* ((disk (partition-disk partition))
         (device (disk-device disk))
         (disk-type (disk-disk-type disk))
         (has-name? (disk-type-check-feature
                     disk-type
                     DISK-TYPE-FEATURE-PARTITION-NAME))
         (has-extended? (disk-type-check-feature
                         disk-type
                         DISK-TYPE-FEATURE-EXTENDED))
         (part-type (partition-print-type partition))
         (number (and (not (freespace-partition? partition))
                      (partition-print-number partition)))
         (name (and has-name?
                    (if (freespace-partition? partition)
                        (G_ "Free space")
                        (partition-get-name partition))))
         (start (unit-format device
                             (partition-start partition)))
         (end (partition-end-formatted device partition))
         (size (unit-format device (partition-length partition)))
         (fs-type (partition-fs-type partition))
         (fs-type-name (and fs-type
                            (filesystem-type-name fs-type)))
         (crypt-label (and user-partition
                           (user-partition-crypt-label user-partition)))
         (flags (and (not (freespace-partition? partition))
                     (partition-print-flags partition)))
         (mount-point (and user-partition
                           (user-partition-mount-point user-partition))))
    `(,(or number "")
      ,@(if has-extended?
            (list part-type)
            '())
      ,size
      ,(or fs-type-name "")
      ,(or flags "")
      ,(or mount-point "")
      ,(or crypt-label "")
      ,(maybe-string-pad name 30))))

(define (partitions-descriptions partitions user-partitions)
  "Return a list of strings describing all the partitions found on
DEVICE. METADATA partitions are not described. The strings are padded to the
right so that they can be displayed as a table."

  (define (max-length-column lists column-index)
    "Return the maximum length of the string at position COLUMN-INDEX in the
list of string lists LISTS."
    (apply max
           (map (lambda (list)
                  (string-length
                   (list-ref list column-index)))
                lists)))

  (define (pad-descriptions descriptions)
    "Return a padded version of the list of string lists DESCRIPTIONS. The
strings are padded to the length of the longer string in a same column, as
determined by MAX-LENGTH-COLUMN procedure."
    (let* ((description-length (length (car descriptions)))
           (paddings (map (lambda (index)
                            (max-length-column descriptions index))
                          (iota description-length))))
      (map (lambda (description)
             (map string-pad-right description paddings))
           descriptions)))

  (let* ((descriptions
          (map
           (lambda (partition)
             (let ((user-partition
                    (find-user-partition-by-parted-object user-partitions
                                                          partition)))
               (partition-description partition user-partition)))
           partitions))
         (padded-descriptions (if (null? partitions)
                                  '()
                                  (pad-descriptions descriptions))))
    (map (cut string-join <> " ") padded-descriptions)))

(define (user-partition-description user-partition)
  "Return a string describing the given USER-PARTITION record."
  (let* ((partition (user-partition-parted-object user-partition))
         (disk (partition-disk partition))
         (disk-type (disk-disk-type disk))
         (device (disk-device disk))
         (has-name? (disk-type-check-feature
                     disk-type
                     DISK-TYPE-FEATURE-PARTITION-NAME))
         (has-extended? (disk-type-check-feature
                         disk-type
                         DISK-TYPE-FEATURE-EXTENDED))
         (name (user-partition-name user-partition))
         (type (user-partition-type user-partition))
         (type-name (symbol->string type))
         (fs-type (user-partition-fs-type user-partition))
         (fs-type-name (user-fs-type-name fs-type))
         (bootable? (user-partition-bootable? user-partition))
         (esp? (user-partition-esp? user-partition))
         (need-formatting? (user-partition-need-formatting? user-partition))
         (crypt-label (user-partition-crypt-label user-partition))
         (size (user-partition-size user-partition))
         (mount-point (user-partition-mount-point user-partition)))
    `(,@(if has-name?
            `((name . ,(format #f (G_ "Name: ~a")
                               (or name (G_ "None")))))
            '())
      ,@(if (and has-extended?
                 (freespace-partition? partition)
                 (not (eq? type 'logical)))
            `((type . ,(format #f (G_ "Type: ~a") type-name)))
            '())
      ,@(if (eq? type 'extended)
            '()
            `((fs-type . ,(format #f (G_ "File system type: ~a")
                                  fs-type-name))))
      ,@(if (or (eq? type 'extended)
                (eq? fs-type 'swap)
                (not has-extended?))
            '()
            `((bootable . ,(format #f (G_ "Bootable flag: ~:[off~;on~]")
                                   bootable?))))
      ,@(if (and (not has-extended?)
                 (not (eq? fs-type 'swap)))
            `((esp? . ,(format #f (G_ "ESP flag: ~:[off~;on~]") esp?)))
            '())
      ,@(if (freespace-partition? partition)
            (let ((size-formatted
                   (or size (unit-format device   ;XXX: i18n
                                         (partition-length partition)))))
              `((size . ,(format #f (G_ "Size: ~a") size-formatted))))
            '())
      ,@(if (or (eq? type 'extended)
                (eq? fs-type 'swap))
            '()
            `((crypt-label
               . ,(format #f (G_ "Encryption: ~:[No~a~;Yes (label '~a')~]")
                          crypt-label (or crypt-label "")))))
      ,@(if (or (freespace-partition? partition)
                (eq? fs-type 'swap))
            '()
            `((need-formatting?
               . ,(format #f (G_ "Format the partition? ~:[No~;Yes~]")
                          need-formatting?))))
      ,@(if (or (eq? type 'extended)
                (eq? fs-type 'swap))
            '()
            `((mount-point
               . ,(format #f (G_ "Mount point: ~a")
                          (or mount-point
                              (and esp? (default-esp-mount-point))
                              (G_ "None")))))))))

;;
;; Partition table creation.
;;

(define (mklabel device type-name)
  "Create a partition table on DEVICE. TYPE-NAME is the type of the partition
table, \"msdos\" or \"gpt\"."
  (let ((type (disk-type-get type-name)))
    (disk-new-fresh device type)))

;;
;; Partition creation.
;;

;; The maximum count of primary partitions is exceeded.
(define-condition-type &max-primary-exceeded &condition
  max-primary-exceeded?)

;; It is not possible to create an extended partition.
(define-condition-type &extended-creation-error &condition
  extended-creation-error?)

;; It is not possible to create a logical partition.
(define-condition-type &logical-creation-error &condition
  logical-creation-error?)

(define (can-create-primary? disk)
  "Return #t if it is possible to create a primary partition on DISK, return
#f otherwise."
  (let ((max-primary (disk-get-max-primary-partition-count disk)))
    (find (lambda (number)
            (not (disk-get-partition disk number)))
          (iota max-primary 1))))

(define (can-create-extended? disk)
  "Return #t if it is possible to create an extended partition on DISK, return
#f otherwise."
  (let* ((disk-type (disk-disk-type disk))
         (has-extended? (disk-type-check-feature
                         disk-type
                         DISK-TYPE-FEATURE-EXTENDED)))
    (and (can-create-primary? disk)
         has-extended?
         (not (disk-extended-partition disk)))))

(define (can-create-logical? disk)
  "Return #t is it is possible to create a logical partition on DISK, return
#f otherwise."
  (let* ((disk-type (disk-disk-type disk))
         (has-extended? (disk-type-check-feature
                         disk-type
                         DISK-TYPE-FEATURE-EXTENDED)))
    (and has-extended?
         (disk-extended-partition disk))))

(define (can-create-partition? user-part)
  "Return #t if it is possible to create the given USER-PART record, return #f
otherwise."
  (let* ((type (user-partition-type user-part))
         (partition (user-partition-parted-object user-part))
         (disk (partition-disk partition)))
    (case type
      ((normal)
       (or (can-create-primary? disk)
           (raise
            (condition (&max-primary-exceeded)))))
      ((extended)
       (or (can-create-extended? disk)
           (raise
            (condition (&extended-creation-error)))))
      ((logical)
       (or (can-create-logical? disk)
           (raise
            (condition (&logical-creation-error))))))))

(define* (mkpart disk user-partition
                 #:key (previous-partition #f))
  "Create the given USER-PARTITION on DISK. The PREVIOUS-PARTITION argument as
to be set to the partition preceding USER-PARTITION if any."

  (define (parse-start-end start end)
    "Parse start and end strings as positions on DEVICE expressed with a unit,
like '100GB' or '12.2KiB'. Return a list of 4 elements, the start sector, its
range (1 unit large area centered on start sector), the end sector and its
range."
    (let ((device (disk-device disk)))
      (call-with-values
          (lambda ()
            (unit-parse start device))
        (lambda (start-sector start-range)
          (call-with-values
              (lambda ()
                (unit-parse end device))
            (lambda (end-sector end-range)
              (list start-sector start-range
                    end-sector end-range)))))))

  (define* (extend-ranges! start-range end-range
                           #:key (offset 0))
    "Try to extend START-RANGE by 1 MEBIBYTE to the right and END-RANGE by 1
MEBIBYTE to the left. This way, if the disk is aligned on 2048 sectors of
512KB (like frequently), we will have a chance for the
'optimal-align-constraint' to succeed. Do not extend ranges if that would
cause them to cross."
    (let* ((device (disk-device disk))
           (start-range-end (geometry-end start-range))
           (end-range-start (geometry-start end-range))
           (mebibyte-sector-size (/ MEBIBYTE-SIZE
                                    (device-sector-size device)))
           (new-start-range-end
            (+ start-range-end mebibyte-sector-size offset))
           (new-end-range-start
            (- end-range-start mebibyte-sector-size offset)))
      (when (< new-start-range-end new-end-range-start)
        (geometry-set-end start-range new-start-range-end)
        (geometry-set-start end-range new-end-range-start))))

  (match (parse-start-end (user-partition-start user-partition)
                          (user-partition-end user-partition))
    ((start-sector start-range end-sector end-range)
     (let* ((prev-end (if previous-partition
                          (partition-end previous-partition)
                          0))
            (start-distance (- start-sector prev-end))
            (type (user-partition-type user-partition))
            ;; There should be at least 2 unallocated sectors in front of each
            ;; logical partition, otherwise parted will fail badly:
            ;; https://gparted.org/h2-fix-msdos-pt.php#apply-action-fail.
            (start-offset (if previous-partition
                              (- 3 start-distance)
                              0))
            (start-sector* (if (and (eq? type 'logical)
                                    (< start-distance 3))
                               (+ start-sector start-offset)
                               start-sector)))
       ;; This is a hack.  Parted almost always fails to create optimally
       ;; aligned partitions (unless specifying percentages) because the
       ;; default range of 1MB centered on the start sector is not enough when
       ;; the optimal alignment is 2048 sectors of 512KB.
       (extend-ranges! start-range end-range #:offset start-offset)

       (let* ((device (disk-device disk))
              (disk-type (disk-disk-type disk))
              (length (device-length device))
              (name (user-partition-name user-partition))
              (filesystem-type
               (filesystem-type-get
                (user-fs-type-name
                 (user-partition-fs-type user-partition))))
              (flags `(,@(if (user-partition-bootable? user-partition)
                             `(,PARTITION-FLAG-BOOT)
                             '())
                       ,@(if (user-partition-esp? user-partition)
                             `(,PARTITION-FLAG-ESP)
                             '())
                       ,@(if (user-partition-bios-grub? user-partition)
                             `(,PARTITION-FLAG-BIOS-GRUB)
                             '())))
              (has-name? (disk-type-check-feature
                          disk-type
                          DISK-TYPE-FEATURE-PARTITION-NAME))
              (partition-type (partition-type->int type))
              (partition (partition-new disk
                                        #:type partition-type
                                        #:filesystem-type filesystem-type
                                        #:start start-sector*
                                        #:end end-sector))
              (user-constraint (constraint-new
                                #:start-align 'any
                                #:end-align 'any
                                #:start-range start-range
                                #:end-range end-range
                                #:min-size 1
                                #:max-size length))
              (dev-constraint
               (device-get-optimal-aligned-constraint device))
              (final-constraint (constraint-intersect user-constraint
                                                      dev-constraint))
              (no-constraint (constraint-any device))
              ;; Try to create a partition with an optimal alignment
              ;; constraint. If it fails, fallback to creating a partition
              ;; with no specific constraint.
              (partition-constraint-ok?
               (disk-add-partition disk partition final-constraint))
              (partition-no-contraint-ok?
               (or partition-constraint-ok?
                   (disk-add-partition disk partition no-constraint)))
              (partition-ok?
               (or partition-constraint-ok? partition-no-contraint-ok?)))
         (syslog "Creating partition:
~/type: ~a
~/filesystem-type: ~a
~/start: ~a
~/end: ~a
~/start-range: [~a, ~a]
~/end-range: [~a, ~a]
~/constraint: ~a
~/no-constraint: ~a
"
                 partition-type
                 (filesystem-type-name filesystem-type)
                 start-sector*
                 end-sector
                 (geometry-start start-range) (geometry-end start-range)
                 (geometry-start end-range) (geometry-end end-range)
                 partition-constraint-ok?
                 partition-no-contraint-ok?)
         ;; Set the partition name if supported.
         (when (and partition-ok? has-name? name)
           (partition-set-name partition name))

         ;; Set flags is required.
         (for-each (lambda (flag)
                     (and (partition-is-flag-available? partition flag)
                          (partition-set-flag partition flag 1)))
                   flags)

         (and partition-ok?
              (partition-set-system partition filesystem-type)
              partition))))))

;;
;; Partition destruction.
;;

(define (rmpart disk number)
  "Remove the partition with the given NUMBER on DISK."
  (let ((partition (disk-get-partition disk number)))
    (disk-remove-partition* disk partition)))

;;
;; Auto partitionning.
;;

(define* (create-adjacent-partitions! disk partitions
                                      #:key (last-partition-end 0))
  "Create the given PARTITIONS on DISK. LAST-PARTITION-END is the sector from
which we want to start creating partitions. The START and END of each created
partition are computed from its SIZE value and the position of the last
partition."
  (let ((device (disk-device disk)))
    (let loop ((partitions partitions)
               (remaining-space (- (device-length device)
                                   last-partition-end))
               (start last-partition-end))
      (match partitions
        (() '())
        ((partition . rest)
         (let* ((size (user-partition-size partition))
                (percentage-size (and (string? size)
                                      (read-percentage size)))
                (sector-size (device-sector-size device))
                (partition-size (if percentage-size
                                    (exact->inexact
                                     (* (/ percentage-size 100)
                                        remaining-space))
                                    size))
                (end-partition (min (- (device-length device) 1)
                                    (nearest-exact-integer
                                     (+ start partition-size 1))))
                (name (user-partition-name partition))
                (type (user-partition-type partition))
                (fs-type (user-partition-fs-type partition))
                (start-formatted (unit-format-custom device
                                                     start
                                                     UNIT-SECTOR))
                (end-formatted (unit-format-custom device
                                                   end-partition
                                                   UNIT-SECTOR))
                (new-user-partition (user-partition
                                     (inherit partition)
                                     (start start-formatted)
                                     (end end-formatted)))
                (new-partition
                 (mkpart disk new-user-partition)))
           (if new-partition
               (cons (user-partition
                      (inherit new-user-partition)
                      (file-name (partition-get-path new-partition))
                      (disk-file-name (device-path device))
                      (parted-object new-partition))
                     (loop rest
                           (if (eq? type 'extended)
                               remaining-space
                               (- remaining-space
                                  (partition-length new-partition)))
                           (if (eq? type 'extended)
                               (+ start 1)
                               (+ (partition-end new-partition) 1))))
               (error
                (format #f "Unable to create partition ~a~%" name)))))))))

(define (force-user-partitions-formatting user-partitions)
  "Set the NEED-FORMATING? fields to #t on all <user-partition> records of
USER-PARTITIONS list and return the updated list."
  (map (lambda (p)
         (user-partition
          (inherit p)
          (need-formatting? #t)))
       user-partitions))

(define* (auto-partition! disk
                          #:key
                          (scheme 'entire-root))
  "Automatically create partitions on DISK. All the previous
partitions (except the ESP on a GPT disk, if present) are wiped. SCHEME is the
desired partitioning scheme. It can be 'entire-root or
'entire-root-home. 'entire-root will create a swap partition and a root
partition occupying all the remaining space. 'entire-root-home will create a
swap partition, a root partition and a home partition.

Return the complete list of partitions on DISK, including the ESP when it
exists."
  (let* ((device (disk-device disk))
         (disk-type (disk-disk-type disk))
         (has-extended? (disk-type-check-feature
                         disk-type
                         DISK-TYPE-FEATURE-EXTENDED))
         (partitions (filter data-partition? (disk-partitions disk)))
         (esp-partition (find-esp-partition partitions))
         ;; According to
         ;; https://wiki.archlinux.org/index.php/EFI_system_partition, the ESP
         ;; size should be at least 550MiB.
         (new-esp-size (nearest-exact-integer
                        (/ (* 550 MEBIBYTE-SIZE)
                           (device-sector-size device))))
         (end-esp-partition (and esp-partition
                                 (partition-end esp-partition)))
         (non-boot-partitions (remove esp-partition? partitions))
         (bios-grub-size (/ (* 3 MEBIBYTE-SIZE)
                            (device-sector-size device)))
         (five-percent-disk (nearest-exact-integer
                             (* 0.05 (device-length device))))
         (default-swap-size (nearest-exact-integer
                             (/ (* 4 GIGABYTE-SIZE)
                                (device-sector-size device))))
         ;; Use a 4GB size for the swap if it represents less than 5% of the
         ;; disk space. Otherwise, set the swap size to 5% of the disk space.
         (swap-size (min default-swap-size five-percent-disk)))

    ;; Remove everything but esp if it exists.
    (for-each
     (lambda (partition)
       (and (data-partition? partition)
            (disk-remove-partition* disk partition)))
     non-boot-partitions)

    (let* ((start-partition
            (if (efi-installation?)
                (and (not esp-partition)
                     (user-partition
                      (fs-type 'fat32)
                      (esp? #t)
                      (size new-esp-size)
                      (mount-point (default-esp-mount-point))))
                (user-partition
                 (fs-type 'ext4)
                 (bootable? #t)
                 (bios-grub? #t)
                 (size bios-grub-size))))
           (new-partitions
            (cond
             ((or (eq? scheme 'entire-root)
                  (eq? scheme 'entire-encrypted-root))
              (let ((encrypted? (eq? scheme 'entire-encrypted-root)))
                `(,@(if start-partition
                        `(,start-partition)
                        '())
                  ,@(if encrypted?
                        '()
                        `(,(user-partition
                            (fs-type 'swap)
                            (size swap-size))))
                  ,(user-partition
                    (fs-type 'ext4)
                    (bootable? has-extended?)
                    (crypt-label (and encrypted? "cryptroot"))
                    (size "100%")
                    (mount-point "/")))))
             ((or (eq? scheme 'entire-root-home)
                  (eq? scheme 'entire-encrypted-root-home))
              (let ((encrypted? (eq? scheme 'entire-encrypted-root-home)))
                `(,@(if start-partition
                        `(,start-partition)
                        '())
                  ,(user-partition
                    (fs-type 'ext4)
                    (bootable? has-extended?)
                    (crypt-label (and encrypted? "cryptroot"))
                    (size "33%")
                    (mount-point "/"))
                  ,@(if has-extended?
                        `(,(user-partition
                            (type 'extended)
                            (size "100%")))
                        '())
                  ,@(if encrypted?
                        '()
                        `(,(user-partition
                            (type (if has-extended?
                                      'logical
                                      'normal))
                            (fs-type 'swap)
                            (size swap-size))))
                  ,(user-partition
                    (type (if has-extended?
                              'logical
                              'normal))
                    (fs-type 'ext4)
                    (crypt-label (and encrypted? "crypthome"))
                    (size "100%")
                    (mount-point "/home")))))))
           (new-partitions* (force-user-partitions-formatting
                             new-partitions)))
      (append (if esp-partition
                  (list (partition->user-partition esp-partition))
                  '())
              (create-adjacent-partitions! disk
                                           new-partitions*
                                           #:last-partition-end
                                           (or end-esp-partition 0))))))

;;
;; Convert user-partitions.
;;

;; No root mount point found.
(define-condition-type &no-root-mount-point &condition
  no-root-mount-point?)

;; Cannot not read the partition UUID.
(define-condition-type &cannot-read-uuid &condition
  cannot-read-uuid?
  (partition cannot-read-uuid-partition))

(define (check-user-partitions user-partitions)
  "Check the following statements:

The USER-PARTITIONS list contains one <user-partition> record with a
mount-point set to '/'.  Raise &no-root-mount-point condition otherwise.

All the USER-PARTITIONS with a mount point and that will not be formatted have
a valid UUID.  Raise a &cannot-read-uuid condition specifying the faulty
partition otherwise.

Return #t if all the statements are valid."
  (define (check-root)
    (let ((mount-points
           (map user-partition-mount-point user-partitions)))
      (or (member "/" mount-points)
          (raise
           (condition (&no-root-mount-point))))))

  (define (check-uuid)
    (let ((mount-partitions
           (filter user-partition-mount-point user-partitions)))
      (every
       (lambda (user-partition)
         (let ((file-name (user-partition-file-name user-partition))
               (need-formatting?
                (user-partition-need-formatting? user-partition)))
           (or need-formatting?
               (read-partition-uuid file-name)
               (raise
                (condition
                 (&cannot-read-uuid
                  (partition file-name)))))))
       mount-partitions)))

  (and (check-root)
       (check-uuid)
       #t))

(define (set-user-partitions-file-name user-partitions)
  "Set the partition file-name of <user-partition> records in USER-PARTITIONS
list and return the updated list."
  (map (lambda (p)
         (let* ((partition (user-partition-parted-object p))
                (file-name (partition-get-path partition)))
           (user-partition
            (inherit p)
            (file-name file-name))))
       user-partitions))

(define-syntax-rule (with-null-output-ports exp ...)
  "Evaluate EXP with both the output port and the error port pointing to the
bit bucket."
  (with-output-to-port (%make-void-port "w")
    (lambda ()
      (with-error-to-port (%make-void-port "w")
        (lambda () exp ...)))))

(define (create-btrfs-file-system partition)
  "Create a btrfs file-system for PARTITION file-name."
  (with-null-output-ports
   (invoke "mkfs.btrfs" "-f" partition)))

(define (create-ext4-file-system partition)
  "Create an ext4 file-system for PARTITION file-name."
  (with-null-output-ports
   (invoke "mkfs.ext4" "-F" partition)))

(define (create-fat16-file-system partition)
  "Create a fat16 file-system for PARTITION file-name."
  (with-null-output-ports
   (invoke "mkfs.fat" "-F16" partition)))

(define (create-fat32-file-system partition)
  "Create a fat32 file-system for PARTITION file-name."
  (with-null-output-ports
   (invoke "mkfs.fat" "-F32" partition)))

(define (create-jfs-file-system partition)
  "Create a JFS file-system for PARTITION file-name."
  (with-null-output-ports
   (invoke "jfs_mkfs" "-f" partition)))

(define (create-ntfs-file-system partition)
  "Create a JFS file-system for PARTITION file-name."
  (with-null-output-ports
   (invoke "mkfs.ntfs" "-F" "-f" partition)))

(define (create-swap-partition partition)
  "Set up swap area on PARTITION file-name."
  (with-null-output-ports
   (invoke "mkswap" "-f" partition)))

(define (call-with-luks-key-file password proc)
  "Write PASSWORD in a temporary file and pass it to PROC as argument."
  (call-with-temporary-output-file
   (lambda (file port)
     (put-string port password)
     (close port)
     (proc file))))

(define (user-partition-upper-file-name user-partition)
  "Return the file-name of the virtual block device corresponding to
USER-PARTITION if it is encrypted, or the plain file-name otherwise."
  (let ((crypt-label (user-partition-crypt-label user-partition))
        (file-name (user-partition-file-name user-partition)))
    (if crypt-label
        (string-append "/dev/mapper/" crypt-label)
        file-name)))

(define (luks-format-and-open user-partition)
  "Format and open the encrypted partition pointed by USER-PARTITION."
  (let* ((file-name (user-partition-file-name user-partition))
         (label (user-partition-crypt-label user-partition))
         (password (user-partition-crypt-password user-partition)))
    (call-with-luks-key-file
     password
     (lambda (key-file)
       (syslog "formatting and opening LUKS entry ~s at ~s~%"
               label file-name)
       (system* "cryptsetup" "-q" "luksFormat" file-name key-file)
       (system* "cryptsetup" "open" "--type" "luks"
                "--key-file" key-file file-name label)))))

(define (luks-close user-partition)
  "Close the encrypted partition pointed by USER-PARTITION."
  (let ((label (user-partition-crypt-label user-partition)))
    (syslog "closing LUKS entry ~s~%" label)
    (system* "cryptsetup" "close" label)))

(define (format-user-partitions user-partitions)
  "Format the <user-partition> records in USER-PARTITIONS list with
NEED-FORMATING? field set to #t."
  (for-each
   (lambda (user-partition)
     (let* ((need-formatting?
             (user-partition-need-formatting? user-partition))
            (type (user-partition-type user-partition))
            (crypt-label (user-partition-crypt-label user-partition))
            (file-name (user-partition-upper-file-name user-partition))
            (fs-type (user-partition-fs-type user-partition)))
       (when crypt-label
         (luks-format-and-open user-partition))

       (case fs-type
         ((btrfs)
          (and need-formatting?
               (not (eq? type 'extended))
               (create-btrfs-file-system file-name)))
         ((ext4)
          (and need-formatting?
               (not (eq? type 'extended))
               (create-ext4-file-system file-name)))
         ((fat16)
          (and need-formatting?
               (not (eq? type 'extended))
               (create-fat16-file-system file-name)))
         ((fat32)
          (and need-formatting?
               (not (eq? type 'extended))
               (create-fat32-file-system file-name)))
         ((jfs)
          (and need-formatting?
               (not (eq? type 'extended))
               (create-jfs-file-system file-name)))
         ((ntfs)
          (and need-formatting?
               (not (eq? type 'extended))
               (create-ntfs-file-system file-name)))
         ((swap)
          (create-swap-partition file-name))
         (else
          ;; TODO: Add support for other file-system types.
          #t))))
   user-partitions))

(define (sort-partitions user-partitions)
  "Sort USER-PARTITIONS by mount-points, so that the more nested mount-point
comes last. This is useful to mount/umount partitions in a coherent order."
  (sort user-partitions
        (lambda (a b)
          (let ((mount-point-a (user-partition-mount-point a))
                (mount-point-b (user-partition-mount-point b)))
            (string-prefix? mount-point-a mount-point-b)))))

(define (mount-user-partitions user-partitions)
  "Mount the <user-partition> records in USER-PARTITIONS list on their
respective mount-points."
  (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
         (sorted-partitions (sort-partitions mount-partitions)))
    (for-each (lambda (user-partition)
                (let* ((mount-point
                        (user-partition-mount-point user-partition))
                       (target
                        (string-append (%installer-target-dir)
                                       mount-point))
                       (fs-type
                        (user-partition-fs-type user-partition))
                       (crypt-label
                        (user-partition-crypt-label user-partition))
                       (mount-type
                        (user-fs-type->mount-type fs-type))
                       (file-name
                        (user-partition-upper-file-name user-partition)))
                  (mkdir-p target)
                  (syslog "mounting ~s on ~s~%" file-name target)
                  (mount file-name target mount-type)))
              sorted-partitions)))

(define (umount-user-partitions user-partitions)
  "Unmount all the <user-partition> records in USER-PARTITIONS list."
  (let* ((mount-partitions (filter user-partition-mount-point user-partitions))
         (sorted-partitions (sort-partitions mount-partitions)))
    (for-each (lambda (user-partition)
                (let* ((mount-point
                        (user-partition-mount-point user-partition))
                       (crypt-label
                        (user-partition-crypt-label user-partition))
                       (target
                        (string-append (%installer-target-dir)
                                       mount-point)))
                  (syslog "unmounting ~s~%" target)
                  (umount target)
                  (when crypt-label
                    (luks-close user-partition))))
              (reverse sorted-partitions))))

(define (find-swap-user-partitions user-partitions)
  "Return the subset of <user-partition> records in USER-PARTITIONS list with
the FS-TYPE field set to 'swap, return the empty list if none found."
  (filter (lambda (user-partition)
            (let ((fs-type (user-partition-fs-type user-partition)))
              (eq? fs-type 'swap)))
          user-partitions))

(define (start-swapping user-partitions)
  "Start swapping on <user-partition> records with FS-TYPE equal to 'swap."
  (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
         (swap-devices (map user-partition-file-name swap-user-partitions)))
    (for-each swapon swap-devices)))

(define (stop-swapping user-partitions)
  "Stop swapping on <user-partition> records with FS-TYPE equal to 'swap."
  (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
         (swap-devices (map user-partition-file-name swap-user-partitions)))
    (for-each swapoff swap-devices)))

(define-syntax-rule (with-mounted-partitions user-partitions exp ...)
  "Mount USER-PARTITIONS and start swapping within the dynamic extent of EXP."
  (dynamic-wind
    (lambda ()
      (mount-user-partitions user-partitions)
      (start-swapping user-partitions))
    (lambda ()
      exp ...)
    (lambda ()
      (umount-user-partitions user-partitions)
      (stop-swapping user-partitions)
      #f)))

(define (user-partition->file-system user-partition)
  "Convert the given USER-PARTITION record in a FILE-SYSTEM record from
(gnu system file-systems) module and return it."
  (let* ((mount-point (user-partition-mount-point user-partition))
         (fs-type (user-partition-fs-type user-partition))
         (crypt-label (user-partition-crypt-label user-partition))
         (mount-type (user-fs-type->mount-type fs-type))
         (file-name (user-partition-file-name user-partition))
         (upper-file-name (user-partition-upper-file-name user-partition))
         ;; Only compute uuid if partition is not encrypted.
         (uuid (or crypt-label
                   (uuid->string (read-partition-uuid file-name) fs-type))))
    `(file-system
       (mount-point ,mount-point)
       (device ,@(if crypt-label
                     `(,upper-file-name)
                     `((uuid ,uuid (quote ,fs-type)))))
       (type ,mount-type)
       ,@(if crypt-label
             '((dependencies mapped-devices))
             '()))))

(define (user-partitions->file-systems user-partitions)
  "Convert the given USER-PARTITIONS list of <user-partition> records into a
list of <file-system> records."
  (filter-map
   (lambda (user-partition)
     (let ((mount-point
            (user-partition-mount-point user-partition)))
       (and mount-point
            (user-partition->file-system user-partition))))
   user-partitions))

(define (user-partition->mapped-device user-partition)
  "Convert the given USER-PARTITION record into a MAPPED-DEVICE record
from (gnu system mapped-devices) and return it."
  (let ((label (user-partition-crypt-label user-partition))
        (file-name (user-partition-file-name user-partition)))
    `(mapped-device
      (source (uuid ,(uuid->string
                      (read-luks-partition-uuid file-name)
                      'luks)))
      (target ,label)
      (type luks-device-mapping))))

(define (root-user-partition? partition)
  "Return true if PARTITION is the root partition."
  (let ((mount-point (user-partition-mount-point partition)))
    (and mount-point
         (string=? mount-point "/"))))

(define (bootloader-configuration user-partitions)
  "Return the bootloader configuration field for USER-PARTITIONS."
  (let* ((root-partition (find root-user-partition?
                               user-partitions))
         (root-partition-disk (user-partition-disk-file-name root-partition)))
    `((bootloader-configuration
       ,@(if (efi-installation?)
             `((bootloader grub-efi-bootloader)
               (target ,(default-esp-mount-point)))
             `((bootloader grub-bootloader)
               (target ,root-partition-disk)))

       ;; XXX: Assume we defined the 'keyboard-layout' field of
       ;; <operating-system> right above.
       (keyboard-layout keyboard-layout)))))

(define (user-partition-missing-modules user-partitions)
  "Return the list of kernel modules missing from the default set of kernel
modules to access USER-PARTITIONS."
  (let ((devices (filter user-partition-crypt-label user-partitions))
        (root    (find root-user-partition? user-partitions)))
    (delete-duplicates
     (append-map (lambda (device)
                   (catch 'system-error
                     (lambda ()
                       (missing-modules device %base-initrd-modules))
                     (const '())))
                 (delete-duplicates
                  (map user-partition-file-name
                       (cons root devices)))))))

(define (initrd-configuration user-partitions)
  "Return an 'initrd-modules' field with everything needed for
USER-PARTITIONS, or return nothing."
  (match (user-partition-missing-modules user-partitions)
    (()
     '())
    ((modules ...)
     `((initrd-modules (append ',modules
                               %base-initrd-modules))))))

(define (user-partitions->configuration user-partitions)
  "Return the configuration field for USER-PARTITIONS."
  (let* ((swap-user-partitions (find-swap-user-partitions user-partitions))
         (swap-devices (map user-partition-file-name swap-user-partitions))
         (encrypted-partitions
          (filter user-partition-crypt-label user-partitions)))
    `((bootloader ,@(bootloader-configuration user-partitions))
      ,@(initrd-configuration user-partitions)
      ,@(if (null? swap-devices)
            '()
            (let* ((uuids (map (lambda (file)
                                 (uuid->string (read-partition-uuid file)))
                               swap-devices)))
              `((swap-devices (list ,@(map (lambda (uuid)
                                             `(uuid ,uuid))
                                           uuids))))))
      ,@(if (null? encrypted-partitions)
            '()
            `((mapped-devices
               (list ,@(map user-partition->mapped-device
                            encrypted-partitions)))))
      (file-systems (cons*
                     ,@(user-partitions->file-systems user-partitions)
                     %base-file-systems)))))

;;
;; Initialization.
;;

(define (init-parted)
  "Initialize libparted support."
  (probe-all-devices!)
  ;; Remove all logical devices, otherwise "device-is-busy?" will report true
  ;; on all devices containaing active logical volumes.
  (remove-logical-devices)
  (exception-set-handler (lambda (exception)
                           EXCEPTION-OPTION-UNHANDLED)))

(define (free-parted devices)
  "Deallocate memory used for DEVICES in parted, force sync them and wait for
the devices not to be used before returning."
  ;; XXX: Formatting and further operations on disk partition table may fail
  ;; because the partition table changes are not synced, or because the device
  ;; is still in use, even if parted should have finished editing
  ;; partitions. This is not well understood, but syncing devices and waiting
  ;; them to stop returning EBUSY to BLKRRPART ioctl seems to be enough. The
  ;; same kind of issue is described here:
  ;; https://mail.gnome.org/archives/commits-list/2013-March/msg18423.html.
  (let ((device-file-names (map device-path devices)))
    (for-each force-device-sync devices)
    (for-each (lambda (file-name)
                (let/time ((time in-use?
                                 (with-delay-device-in-use? file-name)))
                  (if in-use?
                      (error
                       (format #f (G_ "Device ~a is still in use.")
                               file-name))
                      (syslog "Syncing ~a took ~a seconds.~%"
                              file-name (time-second time)))))
              device-file-names)))


code:
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer proxy)
  #:use-module (gnu installer utils)
  #:use-module (gnu services herd)
  #:export (set-http-proxy
            clear-http-proxy))

(define (set-http-proxy proxy)
  (with-silent-shepherd
    (with-shepherd-action 'guix-daemon
        ('set-http-proxy proxy)
        result
      result)))

(define (clear-http-proxy)
  (with-silent-shepherd
    (with-shepherd-action 'guix-daemon
        ('set-http-proxy)
        result
      result)))

;; Local Variables:
;; eval: (put 'with-silent-shepherd 'scheme-indent-function 0)
;; End: