install: Really overwrite TARGET/var/guix/profiles/system-1-link.
[jackhill/guix/guix.git] / gnu / build / install.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu build install)
20 #:use-module (guix build utils)
21 #:use-module (guix build store-copy)
22 #:use-module (srfi srfi-26)
23 #:use-module (ice-9 match)
24 #:export (install-grub
25 populate-root-file-system
26 reset-timestamps
27 register-closure
28 populate-single-profile-directory))
29
30 ;;; Commentary:
31 ;;;
32 ;;; This module supports the installation of the GNU system on a hard disk.
33 ;;; It is meant to be used both in a build environment (in derivations that
34 ;;; build VM images), and on the bare metal (when really installing the
35 ;;; system.)
36 ;;;
37 ;;; Code:
38
39 (define* (install-grub grub.cfg device mount-point)
40 "Install GRUB with GRUB.CFG on DEVICE, which is assumed to be mounted on
41 MOUNT-POINT.
42
43 Note that the caller must make sure that GRUB.CFG is registered as a GC root
44 so that the fonts, background images, etc. referred to by GRUB.CFG are not
45 GC'd."
46 (let* ((target (string-append mount-point "/boot/grub/grub.cfg"))
47 (pivot (string-append target ".new")))
48 (mkdir-p (dirname target))
49
50 ;; Copy GRUB.CFG instead of just symlinking it, because symlinks won't
51 ;; work when /boot is on a separate partition. Do that atomically.
52 (copy-file grub.cfg pivot)
53 (rename-file pivot target)
54
55 (unless (zero? (system* "grub-install" "--no-floppy"
56 "--boot-directory"
57 (string-append mount-point "/boot")
58 device))
59 (error "failed to install GRUB"))))
60
61 (define (evaluate-populate-directive directive target)
62 "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
63 directory TARGET."
64 (let loop ((directive directive))
65 (catch 'system-error
66 (lambda ()
67 (match directive
68 (('directory name)
69 (mkdir-p (string-append target name)))
70 (('directory name uid gid)
71 (let ((dir (string-append target name)))
72 (mkdir-p dir)
73 (chown dir uid gid)))
74 (('directory name uid gid mode)
75 (loop `(directory ,name ,uid ,gid))
76 (chmod (string-append target name) mode))
77 ((new '-> old)
78 (let try ()
79 (catch 'system-error
80 (lambda ()
81 (symlink old (string-append target new)))
82 (lambda args
83 ;; When doing 'guix system init' on the current '/', some
84 ;; symlinks may already exists. Override them.
85 (if (= EEXIST (system-error-errno args))
86 (begin
87 (delete-file (string-append target new))
88 (try))
89 (apply throw args))))))))
90 (lambda args
91 ;; Usually we can only get here when installing to an existing root,
92 ;; as with 'guix system init foo.scm /'.
93 (format (current-error-port)
94 "error: failed to evaluate directive: ~s~%"
95 directive)
96 (apply throw args)))))
97
98 (define (directives store)
99 "Return a list of directives to populate the root file system that will host
100 STORE."
101 `(;; Note: the store's GID is fixed precisely so we can set it here rather
102 ;; than at activation time.
103 (directory ,store 0 30000 #o1775)
104
105 (directory "/etc")
106 (directory "/var/log") ; for dmd
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
121 (directory "/bin")
122 (directory "/tmp" 0 0 #o1777) ; sticky bit
123 (directory "/var/tmp" 0 0 #o1777)
124 (directory "/var/lock" 0 0 #o1777)
125
126 (directory "/root" 0 0) ; an exception
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 (reset-timestamps directory)
151 "Reset the timestamps of all the files under DIRECTORY, so that they appear
152 as created and modified at the Epoch."
153 (display "clearing file timestamps...\n")
154 (for-each (lambda (file)
155 (let ((s (lstat file)))
156 ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so
157 ;; the timestamp of symlinks cannot be changed, and there are
158 ;; symlinks here pointing to /gnu/store, which is the host,
159 ;; read-only store.
160 (unless (eq? (stat:type s) 'symlink)
161 (utime file 0 0 0 0))))
162 (find-files directory "")))
163
164 (define (register-closure store closure)
165 "Register CLOSURE in STORE, where STORE is the directory name of the target
166 store and CLOSURE is the name of a file containing a reference graph as used
167 by 'guix-register'. As a side effect, this resets timestamps on store files."
168 (let ((status (system* "guix-register" "--prefix" store
169 closure)))
170 (unless (zero? status)
171 (error "failed to register store items" closure))))
172
173 (define* (populate-single-profile-directory directory
174 #:key profile closure)
175 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
176 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
177 is initialized to contain a single profile under /root pointing to PROFILE.
178 This is used to create the self-contained Guix tarball."
179 (define (scope file)
180 (string-append directory "/" file))
181
182 (define %root-profile
183 "/var/guix/profiles/per-user/root")
184
185 (define (mkdir-p* dir)
186 (mkdir-p (scope dir)))
187
188 (define (symlink* old new)
189 (symlink old (scope new)))
190
191 ;; Populate the store.
192 (populate-store (list closure) directory)
193 (register-closure (canonicalize-path directory) closure)
194
195 ;; XXX: 'guix-register' registers profiles as GC roots but the symlink
196 ;; target uses $TMPDIR. Fix that.
197 (delete-file (scope "/var/guix/gcroots/profiles"))
198 (symlink* "/var/guix/profiles"
199 "/var/guix/gcroots/profiles")
200
201 ;; Make root's profile, which makes it a GC root.
202 (mkdir-p* %root-profile)
203 (symlink* profile
204 (string-append %root-profile "/guix-profile-1-link"))
205 (symlink* (string-append %root-profile "/guix-profile-1-link")
206 (string-append %root-profile "/guix-profile"))
207
208 (mkdir-p* "/root")
209 (symlink* (string-append %root-profile "/guix-profile")
210 "/root/.guix-profile"))
211
212 ;;; install.scm ends here