image: Add a new API.
[jackhill/guix/guix.git] / gnu / build / install.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
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 build install)
21 #:use-module (guix build utils)
22 #:use-module (guix build store-copy)
23 #:use-module (srfi srfi-26)
24 #:use-module (ice-9 match)
25 #:export (install-boot-config
26 evaluate-populate-directive
27 populate-root-file-system
28 install-database-and-gc-roots
29 populate-single-profile-directory))
30
31 ;;; Commentary:
32 ;;;
33 ;;; This module supports the installation of the GNU system on a hard disk.
34 ;;; It is meant to be used both in a build environment (in derivations that
35 ;;; build VM images), and on the bare metal (when really installing the
36 ;;; system.)
37 ;;;
38 ;;; Code:
39
40 (define (install-boot-config bootcfg bootcfg-location mount-point)
41 "Atomically copy BOOTCFG into BOOTCFG-LOCATION on the MOUNT-POINT. Note
42 that the caller must make sure that BOOTCFG is registered as a GC root so
43 that the fonts, background images, etc. referred to by BOOTCFG are not GC'd."
44 (let* ((target (string-append mount-point bootcfg-location))
45 (pivot (string-append target ".new")))
46 (mkdir-p (dirname target))
47
48 ;; Copy BOOTCFG instead of just symlinking it, because symlinks won't
49 ;; work when /boot is on a separate partition. Do that atomically.
50 (copy-file bootcfg pivot)
51 (rename-file pivot target)))
52
53 (define* (evaluate-populate-directive directive target
54 #:key
55 (default-gid 0)
56 (default-uid 0))
57 "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
58 directory TARGET. DEFAULT-UID and DEFAULT-GID are the default UID and GID in
59 the context of the caller. If the directive matches those defaults then,
60 'chown' won't be run."
61 (let loop ((directive directive))
62 (catch 'system-error
63 (lambda ()
64 (match directive
65 (('directory name)
66 (mkdir-p (string-append target name)))
67 (('directory name uid gid)
68 (let ((dir (string-append target name)))
69 (mkdir-p dir)
70 ;; If called from a context without "root" permissions, "chown"
71 ;; to root will fail. In that case, do not try to run "chown"
72 ;; and assume that the file will be chowned elsewhere (when
73 ;; interned in the store for instance).
74 (or (and (= uid default-uid) (= gid default-gid))
75 (chown dir uid gid))))
76 (('directory name uid gid mode)
77 (loop `(directory ,name ,uid ,gid))
78 (chmod (string-append target name) mode))
79 ((new '-> old)
80 (let try ()
81 (catch 'system-error
82 (lambda ()
83 (symlink old (string-append target new)))
84 (lambda args
85 ;; When doing 'guix system init' on the current '/', some
86 ;; symlinks may already exists. Override them.
87 (if (= EEXIST (system-error-errno args))
88 (begin
89 (delete-file (string-append target new))
90 (try))
91 (apply throw args))))))))
92 (lambda args
93 ;; Usually we can only get here when installing to an existing root,
94 ;; as with 'guix system init foo.scm /'.
95 (format (current-error-port)
96 "error: failed to evaluate directive: ~s~%"
97 directive)
98 (apply throw args)))))
99
100 (define (directives store)
101 "Return a list of directives to populate the root file system that will host
102 STORE."
103 `((directory ,store 0 0 #o1775)
104
105 (directory "/etc")
106 (directory "/var/log") ; for shepherd
107 (directory "/var/guix/gcroots")
108 (directory "/var/empty") ; for no-login accounts
109 (directory "/var/db") ; for dhclient, etc.
110 (directory "/var/run")
111 (directory "/run")
112 (directory "/mnt")
113 (directory "/var/guix/profiles/per-user/root" 0 0)
114
115 ;; Link to the initial system generation.
116 ("/var/guix/profiles/system" -> "system-1-link")
117
118 ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
119 ("/var/guix/gcroots/current-system" -> "/run/current-system")
120 ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
121
122 (directory "/bin")
123 (directory "/tmp" 0 0 #o1777) ; sticky bit
124 (directory "/var/tmp" 0 0 #o1777)
125 (directory "/var/lock" 0 0 #o1777)
126
127 (directory "/home" 0 0)))
128
129 (define (populate-root-file-system system target)
130 "Make the essential non-store files and directories on TARGET. This
131 includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
132 (for-each (cut evaluate-populate-directive <> target)
133 (directives (%store-directory)))
134
135 ;; Add system generation 1.
136 (let ((generation-1 (string-append target
137 "/var/guix/profiles/system-1-link")))
138 (let try ()
139 (catch 'system-error
140 (lambda ()
141 (symlink system generation-1))
142 (lambda args
143 ;; If GENERATION-1 already exists, overwrite it.
144 (if (= EEXIST (system-error-errno args))
145 (begin
146 (delete-file generation-1)
147 (try))
148 (apply throw args)))))))
149
150 (define %root-profile
151 "/var/guix/profiles/per-user/root")
152
153 (define* (install-database-and-gc-roots root database profile
154 #:key (profile-name "guix-profile"))
155 "Install DATABASE, the store database, under directory ROOT. Create
156 PROFILE-NAME and have it link to PROFILE, a store item."
157 (define (scope file)
158 (string-append root "/" file))
159
160 (define (mkdir-p* dir)
161 (mkdir-p (scope dir)))
162
163 (define (symlink* old new)
164 (symlink old (scope new)))
165
166 (install-file database (scope "/var/guix/db/"))
167 (chmod (scope "/var/guix/db/db.sqlite") #o644)
168 (mkdir-p* "/var/guix/profiles")
169 (mkdir-p* "/var/guix/gcroots")
170 (symlink* "/var/guix/profiles" "/var/guix/gcroots/profiles")
171
172 ;; Make root's profile, which makes it a GC root.
173 (mkdir-p* %root-profile)
174 (symlink* profile
175 (string-append %root-profile "/" profile-name "-1-link"))
176 (symlink* (string-append profile-name "-1-link")
177 (string-append %root-profile "/" profile-name)))
178
179 (define* (populate-single-profile-directory directory
180 #:key profile closure
181 (profile-name "guix-profile")
182 database)
183 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
184 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
185 is initialized to contain a single profile under /root pointing to PROFILE.
186
187 When DATABASE is true, copy it to DIRECTORY/var/guix/db and create
188 DIRECTORY/var/guix/gcroots and friends.
189
190 PROFILE-NAME is the name of the profile being created under
191 /var/guix/profiles, typically either \"guix-profile\" or \"current-guix\".
192
193 This is used to create the self-contained tarballs with 'guix pack'."
194 (define (scope file)
195 (string-append directory "/" file))
196
197 (define (mkdir-p* dir)
198 (mkdir-p (scope dir)))
199
200 (define (symlink* old new)
201 (symlink old (scope new)))
202
203 ;; Populate the store.
204 (populate-store (list closure) directory)
205
206 (when database
207 (install-database-and-gc-roots directory database profile
208 #:profile-name profile-name))
209
210 (match profile-name
211 ("guix-profile"
212 (mkdir-p* "/root")
213 (symlink* (string-append %root-profile "/guix-profile")
214 "/root/.guix-profile"))
215 ("current-guix"
216 (mkdir-p* "/root/.config/guix")
217 (symlink* (string-append %root-profile "/current-guix")
218 "/root/.config/guix/current"))
219 (_
220 #t)))
221
222 ;;; install.scm ends here