gnu: csound: Update to 6.16.2.
[jackhill/guix/guix.git] / gnu / installer / final.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
20 (define-module (gnu installer final)
21 #:use-module (gnu installer newt page)
22 #:use-module (gnu installer steps)
23 #:use-module (gnu installer utils)
24 #:use-module (gnu installer user)
25 #:use-module (gnu services herd)
26 #:use-module (guix build syscalls)
27 #:use-module (guix build utils)
28 #:use-module (gnu build accounts)
29 #:use-module (gnu build install)
30 #:use-module (gnu build linux-container)
31 #:use-module ((gnu system shadow) #:prefix sys:)
32 #:use-module (rnrs io ports)
33 #:use-module (srfi srfi-1)
34 #:use-module (ice-9 ftw)
35 #:use-module (ice-9 popen)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:use-module (ice-9 rdelim)
39 #:export (install-system))
40
41 (define %seed
42 (seed->random-state
43 (logxor (getpid) (car (gettimeofday)))))
44
45 (define (integer->alphanumeric-char n)
46 "Map N, an integer in the [0..62] range, to an alphanumeric character."
47 (cond ((< n 10)
48 (integer->char (+ (char->integer #\0) n)))
49 ((< n 36)
50 (integer->char (+ (char->integer #\A) (- n 10))))
51 ((< n 62)
52 (integer->char (+ (char->integer #\a) (- n 36))))
53 (else
54 (error "integer out of bounds" n))))
55
56 (define (random-string len)
57 "Compute a random string of size LEN where each character is alphanumeric."
58 (let loop ((chars '())
59 (len len))
60 (if (zero? len)
61 (list->string chars)
62 (let ((n (random 62 %seed)))
63 (loop (cons (integer->alphanumeric-char n) chars)
64 (- len 1))))))
65
66 (define (create-user-database users root)
67 "Create /etc/passwd, /etc/shadow, and /etc/group under ROOT for the given
68 USERS."
69 (define etc
70 (string-append root "/etc"))
71
72 (define (salt)
73 ;; "$6" gives us a SHA512 password hash; the random string must be taken
74 ;; from the './0-9A-Za-z' alphabet (info "(libc) Passphrase Storage").
75 (string-append "$6$" (random-string 10)))
76
77 (define users*
78 (map (lambda (user)
79 (define root?
80 (string=? "root" (user-name user)))
81
82 (sys:user-account (name (user-name user))
83 (comment (user-real-name user))
84 (group "users")
85 (uid (if root? 0 #f))
86 (home-directory
87 (user-home-directory user))
88 (password (crypt
89 (secret-content (user-password user))
90 (salt)))
91
92 ;; We need a string here, not a file-like, hence
93 ;; this choice.
94 (shell
95 "/run/current-system/profile/bin/bash")))
96 users))
97
98 (define-values (group password shadow)
99 (user+group-databases users* sys:%base-groups
100 #:current-passwd '()
101 #:current-groups '()
102 #:current-shadow '()))
103
104 (mkdir-p etc)
105 (write-group group (string-append etc "/group"))
106 (write-passwd password (string-append etc "/passwd"))
107 (write-shadow shadow (string-append etc "/shadow")))
108
109 (define (call-with-mnt-container thunk)
110 "This is a variant of call-with-container. Run THUNK in a new container
111 process, within a separate MNT namespace. The container is not jailed so that
112 it can interact with the rest of the system."
113 (let ((pid (run-container "/" '() '(mnt) 1 thunk)))
114 ;; Catch SIGINT and kill the container process.
115 (sigaction SIGINT
116 (lambda (signum)
117 (false-if-exception
118 (kill pid SIGKILL))))
119
120 (match (waitpid pid)
121 ((_ . status) status))))
122
123 (define (install-locale locale)
124 "Install the given LOCALE or the en_US.utf8 locale as a fallback."
125 (let ((supported? (false-if-exception
126 (setlocale LC_ALL locale))))
127 (if supported?
128 (begin
129 (installer-log-line "install supported locale ~a." locale)
130 (setenv "LC_ALL" locale))
131 (begin
132 ;; If the selected locale is not supported, install a default UTF-8
133 ;; locale. This is required to copy some files with UTF-8
134 ;; characters, in the nss-certs package notably. Set LANGUAGE
135 ;; anyways, to have translated messages if possible.
136 (installer-log-line "~a locale is not supported, installing \
137 en_US.utf8 locale instead." locale)
138 (setlocale LC_ALL "en_US.utf8")
139 (setenv "LC_ALL" "en_US.utf8")
140 (setenv "LANGUAGE"
141 (string-take locale
142 (or (string-index locale #\_)
143 (string-length locale))))))))
144
145 (define* (install-system locale #:key (users '()))
146 "Create /etc/shadow and /etc/passwd on the installation target for USERS.
147 Start COW-STORE service on target directory and launch guix install command in
148 a subshell. LOCALE must be the locale name under which that command will run,
149 or #f. Return #t on success and #f on failure."
150 (define backing-directory
151 ;; Sub-directory used as the backing store for copy-on-write.
152 "/tmp/guix-inst")
153
154 (define (assert-exit x)
155 (primitive-exit (if x 0 1)))
156
157 (let* ((options (catch 'system-error
158 (lambda ()
159 ;; If this file exists, it can provide
160 ;; additional command-line options.
161 (call-with-input-file
162 "/tmp/installer-system-init-options"
163 read))
164 (const '())))
165 (install-command (append (list "guix" "system" "init"
166 "--fallback")
167 options
168 (list (%installer-configuration-file)
169 (%installer-target-dir))))
170 (database-dir "/var/guix/db")
171 (database-file (string-append database-dir "/db.sqlite"))
172 (saved-database (string-append database-dir "/db.save"))
173 (ret #f))
174 (mkdir-p (%installer-target-dir))
175
176 ;; We want to initialize user passwords but we don't want to store them in
177 ;; the config file since the password hashes would end up world-readable
178 ;; in the store. Thus, create /etc/shadow & co. here such that, on the
179 ;; first boot, the activation snippet that creates accounts will reuse the
180 ;; passwords that we've put in there.
181 (create-user-database users (%installer-target-dir))
182
183 ;; When the store overlay is mounted, other processes such as kmscon, udev
184 ;; and guix-daemon may open files from the store, preventing the
185 ;; underlying install support from being umounted. See:
186 ;; https://lists.gnu.org/archive/html/guix-devel/2018-12/msg00161.html.
187 ;;
188 ;; To avoid this situation, mount the store overlay inside a container,
189 ;; and run the installation from within that container.
190 (zero?
191 (call-with-mnt-container
192 (lambda ()
193 (dynamic-wind
194 (lambda ()
195 ;; Install the locale before mounting the cow-store, otherwise
196 ;; the loaded cow-store locale files will prevent umounting.
197 (install-locale locale)
198
199 ;; Save the database, so that it can be restored once the
200 ;; cow-store is umounted.
201 (copy-file database-file saved-database)
202 (mount-cow-store (%installer-target-dir) backing-directory))
203 (lambda ()
204 ;; We need to drag the guix-daemon to the container MNT
205 ;; namespace, so that it can operate on the cow-store.
206 (stop-service 'guix-daemon)
207 (start-service 'guix-daemon (list (number->string (getpid))))
208
209 (setvbuf (current-output-port) 'none)
210 (setvbuf (current-error-port) 'none)
211
212 (setenv "PATH" "/run/current-system/profile/bin/")
213
214 (set! ret (run-command install-command)))
215 (lambda ()
216 ;; Restart guix-daemon so that it does no keep the MNT namespace
217 ;; alive.
218 (restart-service 'guix-daemon)
219 (copy-file saved-database database-file)
220
221 ;; Finally umount the cow-store and exit the container.
222 (unmount-cow-store (%installer-target-dir) backing-directory)
223 (assert-exit ret))))))))