1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu installer newt user)
21 #:use-module (gnu installer user)
22 #:use-module (gnu installer newt page)
23 #:use-module (gnu installer newt utils)
24 #:use-module (guix i18n)
26 #:use-module (ice-9 match)
27 #:use-module (ice-9 receive)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
30 #:export (run-user-page))
32 (define* (run-user-add-page #:key (name "") (real-name "")
34 "Run a form to enter the user name, home directory, and password. Use NAME,
35 REAL-NAME, and HOME-DIRECTORY as the initial values in the form."
36 (define (pad-label label)
37 (string-pad-right label 20))
40 (make-label -1 -1 (pad-label (G_ "Name"))))
42 (make-label -1 -1 (pad-label (G_ "Real name"))))
44 (make-label -1 -1 (pad-label (G_ "Home directory"))))
46 (make-label -1 -1 (pad-label (G_ "Password"))))
48 (entry-name (make-entry -1 -1 entry-width
49 #:initial-value name))
50 (entry-real-name (make-entry -1 -1 entry-width
51 #:initial-value real-name))
52 (entry-home-directory (make-entry -1 -1 entry-width
53 #:initial-value home-directory))
54 (entry-password (make-entry -1 -1 entry-width
55 #:flags FLAG-PASSWORD))
56 (entry-grid (make-grid 2 5))
57 (button-grid (make-grid 1 1))
58 (ok-button (make-button -1 -1 (G_ "OK")))
59 (grid (make-grid 1 2))
60 (title (G_ "User creation"))
62 (cut set-grid-field entry-grid <> <> GRID-ELEMENT-COMPONENT <>))
65 (set-entry-grid-field 0 0 label-name)
66 (set-entry-grid-field 1 0 entry-name)
67 (set-entry-grid-field 0 1 label-real-name)
68 (set-entry-grid-field 1 1 entry-real-name)
69 (set-entry-grid-field 0 2 label-home-directory)
70 (set-entry-grid-field 1 2 entry-home-directory)
71 (set-entry-grid-field 0 3 label-password)
72 (set-entry-grid-field 1 3 entry-password)
74 (set-grid-field button-grid 0 0 GRID-ELEMENT-COMPONENT ok-button)
76 (add-component-callback
79 (set-entry-text entry-home-directory
80 (string-append "/home/" (entry-value entry-name)))
82 (when (string-null? (entry-value entry-real-name))
83 (set-entry-text entry-real-name
84 (string-titlecase (entry-value entry-name))))))
86 (add-components-to-form form
87 label-name label-real-name
88 label-home-directory label-password
89 entry-name entry-real-name
90 entry-home-directory entry-password
93 (make-wrapped-grid-window (vertically-stacked-grid
94 GRID-ELEMENT-SUBGRID entry-grid
95 GRID-ELEMENT-SUBGRID button-grid)
99 (run-error-page (G_ "Empty inputs are not allowed.")
100 (G_ "Empty input")))))
101 (receive (exit-reason argument)
106 (when (eq? exit-reason 'exit-component)
108 ((components=? argument ok-button)
109 (let ((name (entry-value entry-name))
110 (real-name (entry-value entry-real-name))
111 (home-directory (entry-value entry-home-directory))
112 (password (entry-value entry-password)))
113 (if (or (string=? name "")
114 (string=? home-directory ""))
118 (let ((password (confirm-password password)))
122 (real-name real-name)
123 (home-directory home-directory)
125 (run-user-add-page #:name name
126 #:real-name real-name
128 home-directory)))))))))
130 (destroy-form-and-pop form)))))))
132 (define* (confirm-password password #:optional (try-again (const #f)))
133 "Ask the user to confirm PASSWORD, a possibly empty string. Call TRY-AGAIN,
134 a thunk, if the confirmation doesn't match PASSWORD, and return its result."
136 (run-input-page (G_ "Please confirm the password.")
137 (G_ "Password confirmation required")
138 #:allow-empty-input? #t
139 #:input-flags FLAG-PASSWORD))
141 (if (string=? password confirmation)
145 (G_ "Password mismatch, please try again.")
146 (G_ "Password error"))
149 (define (run-root-password-page)
150 ;; TRANSLATORS: Leave "root" untranslated: it refers to the name of the
151 ;; system administrator account.
153 (run-input-page (G_ "Please choose a password for the system \
154 administrator (\"root\").")
155 (G_ "System administrator password")
156 #:input-flags FLAG-PASSWORD))
158 (confirm-password password run-root-password-page))
160 (define (run-user-page)
162 (let* ((listbox (make-listbox
164 (logior FLAG-SCROLL FLAG-BORDER)))
166 (make-reflowed-textbox
168 (G_ "Please add at least one user to system\
169 using the 'Add' button.")
170 40 #:flags FLAG-BORDER))
171 (add-button (make-compact-button -1 -1 (G_ "Add")))
172 (del-button (make-compact-button -1 -1 (G_ "Delete")))
175 vertically-stacked-grid
176 GRID-ELEMENT-COMPONENT add-button
177 `(,@(if (null? users)
179 (list GRID-ELEMENT-COMPONENT del-button)))))
180 (ok-button (make-button -1 -1 (G_ "OK")))
181 (exit-button (make-button -1 -1 (G_ "Exit")))
182 (title "User creation")
184 (vertically-stacked-grid
185 GRID-ELEMENT-COMPONENT info-textbox
186 GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
187 GRID-ELEMENT-COMPONENT listbox
188 GRID-ELEMENT-SUBGRID listbox-button-grid)
189 GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
190 GRID-ELEMENT-COMPONENT ok-button
191 GRID-ELEMENT-COMPONENT exit-button)))
192 (sorted-users (sort users (lambda (a b)
193 (string<= (user-name a)
198 `((key . ,(append-entry-to-listbox listbox
205 (add-form-to-grid grid form #t)
206 (make-wrapped-grid-window grid title)
208 (set-current-component form add-button)
209 (set-current-component form ok-button))
211 (receive (exit-reason argument)
216 (when (eq? exit-reason 'exit-component)
218 ((components=? argument add-button)
219 (run (cons (run-user-add-page) users)))
220 ((components=? argument del-button)
221 (let* ((current-user-key (current-listbox-entry listbox))
223 (map (cut assoc-ref <> 'user)
224 (remove (lambda (element)
225 (equal? (assoc-ref element 'key)
229 ((components=? argument ok-button)
231 (run-error-page (G_ "Please create at least one user.")
236 (destroy-form-and-pop form))))))
238 ;; Add a "root" user simply to convey the root password.
239 (cons (user (name "root")
240 (home-directory "/root")
241 (password (run-root-password-page)))