Commit | Line | Data |
---|---|---|
dc5f3275 MO |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com> | |
898677ed | 3 | ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org> |
dc5f3275 MO |
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) | |
898677ed | 24 | #:use-module (gnu installer user) |
dc5f3275 MO |
25 | #:use-module (gnu services herd) |
26 | #:use-module (guix build utils) | |
898677ed LC |
27 | #:use-module (gnu build accounts) |
28 | #:use-module ((gnu system shadow) #:prefix sys:) | |
29 | #:use-module (rnrs io ports) | |
dc5f3275 MO |
30 | #:export (install-system)) |
31 | ||
898677ed LC |
32 | (define %seed |
33 | (seed->random-state | |
34 | (logxor (getpid) (car (gettimeofday))))) | |
35 | ||
36 | (define (integer->alphanumeric-char n) | |
37 | "Map N, an integer in the [0..62] range, to an alphanumeric character." | |
38 | (cond ((< n 10) | |
39 | (integer->char (+ (char->integer #\0) n))) | |
40 | ((< n 36) | |
41 | (integer->char (+ (char->integer #\A) (- n 10)))) | |
42 | ((< n 62) | |
43 | (integer->char (+ (char->integer #\a) (- n 36)))) | |
44 | (else | |
45 | (error "integer out of bounds" n)))) | |
46 | ||
47 | (define (random-string len) | |
48 | "Compute a random string of size LEN where each character is alphanumeric." | |
49 | (let loop ((chars '()) | |
50 | (len len)) | |
51 | (if (zero? len) | |
52 | (list->string chars) | |
53 | (let ((n (random 62 %seed))) | |
54 | (loop (cons (integer->alphanumeric-char n) chars) | |
55 | (- len 1)))))) | |
56 | ||
57 | (define (create-user-database users root) | |
58 | "Create /etc/passwd, /etc/shadow, and /etc/group under ROOT for the given | |
59 | USERS." | |
60 | (define etc | |
61 | (string-append root "/etc")) | |
62 | ||
63 | (define (salt) | |
64 | ;; "$6" gives us a SHA512 password hash; the random string must be taken | |
65 | ;; from the './0-9A-Za-z' alphabet (info "(libc) Passphrase Storage"). | |
66 | (string-append "$6$" (random-string 10))) | |
67 | ||
68 | (define users* | |
69 | (map (lambda (user) | |
91a7c499 LC |
70 | (define root? |
71 | (string=? "root" (user-name user))) | |
72 | ||
898677ed | 73 | (sys:user-account (name (user-name user)) |
0e8e963d | 74 | (comment (user-real-name user)) |
898677ed | 75 | (group "users") |
91a7c499 | 76 | (uid (if root? 0 #f)) |
898677ed LC |
77 | (home-directory |
78 | (user-home-directory user)) | |
79 | (password (crypt (user-password user) | |
80 | (salt))) | |
81 | ||
82 | ;; We need a string here, not a file-like, hence | |
83 | ;; this choice. | |
84 | (shell | |
85 | "/run/current-system/profile/bin/bash"))) | |
86 | users)) | |
87 | ||
88 | (define-values (group password shadow) | |
89 | (user+group-databases users* sys:%base-groups | |
90 | #:current-passwd '() | |
91 | #:current-groups '() | |
92 | #:current-shadow '())) | |
93 | ||
94 | (mkdir-p etc) | |
95 | (write-group group (string-append etc "/group")) | |
96 | (write-passwd password (string-append etc "/passwd")) | |
97 | (write-shadow shadow (string-append etc "/shadow"))) | |
98 | ||
99 | (define* (install-system locale #:key (users '())) | |
100 | "Create /etc/shadow and /etc/passwd on the installation target for USERS. | |
101 | Start COW-STORE service on target directory and launch guix install command in | |
102 | a subshell. LOCALE must be the locale name under which that command will run, | |
9529f785 | 103 | or #f. Return #t on success and #f on failure." |
dc5f3275 MO |
104 | (let ((install-command |
105 | (format #f "guix system init ~a ~a" | |
106 | (%installer-configuration-file) | |
107 | (%installer-target-dir)))) | |
108 | (mkdir-p (%installer-target-dir)) | |
898677ed LC |
109 | |
110 | ;; We want to initialize user passwords but we don't want to store them in | |
111 | ;; the config file since the password hashes would end up world-readable | |
112 | ;; in the store. Thus, create /etc/shadow & co. here such that, on the | |
113 | ;; first boot, the activation snippet that creates accounts will reuse the | |
114 | ;; passwords that we've put in there. | |
115 | (create-user-database users (%installer-target-dir)) | |
116 | ||
dc5f3275 | 117 | (start-service 'cow-store (list (%installer-target-dir))) |
9529f785 | 118 | (run-shell-command install-command #:locale locale))) |