1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
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 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
30 populate-single-profile-directory))
34 ;;; This module supports the installation of the GNU system on a hard disk.
35 ;;; It is meant to be used both in a build environment (in derivations that
36 ;;; build VM images), and on the bare metal (when really installing the
41 (define (install-boot-config bootcfg bootcfg-location mount-point)
42 "Atomically copy BOOTCFG into BOOTCFG-LOCATION on the MOUNT-POINT. Note
43 that the caller must make sure that BOOTCFG is registered as a GC root so
44 that the fonts, background images, etc. referred to by BOOTCFG are not GC'd."
45 (let* ((target (string-append mount-point bootcfg-location))
46 (pivot (string-append target ".new")))
47 (mkdir-p (dirname target))
49 ;; Copy BOOTCFG instead of just symlinking it, because symlinks won't
50 ;; work when /boot is on a separate partition. Do that atomically.
51 (copy-file bootcfg pivot)
52 (rename-file pivot target)))
54 (define (evaluate-populate-directive directive target)
55 "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
57 (let loop ((directive directive))
62 (mkdir-p (string-append target name)))
63 (('directory name uid gid)
64 (let ((dir (string-append target name)))
67 (('directory name uid gid mode)
68 (loop `(directory ,name ,uid ,gid))
69 (chmod (string-append target name) mode))
74 (symlink old (string-append target new)))
76 ;; When doing 'guix system init' on the current '/', some
77 ;; symlinks may already exists. Override them.
78 (if (= EEXIST (system-error-errno args))
80 (delete-file (string-append target new))
82 (apply throw args))))))))
84 ;; Usually we can only get here when installing to an existing root,
85 ;; as with 'guix system init foo.scm /'.
86 (format (current-error-port)
87 "error: failed to evaluate directive: ~s~%"
89 (apply throw args)))))
91 (define (directives store)
92 "Return a list of directives to populate the root file system that will host
94 `(;; Note: the store's GID is fixed precisely so we can set it here rather
95 ;; than at activation time.
96 (directory ,store 0 30000 #o1775)
99 (directory "/var/log") ; for shepherd
100 (directory "/var/guix/gcroots")
101 (directory "/var/empty") ; for no-login accounts
102 (directory "/var/db") ; for dhclient, etc.
103 (directory "/var/run")
106 (directory "/var/guix/profiles/per-user/root" 0 0)
108 ;; Link to the initial system generation.
109 ("/var/guix/profiles/system" -> "system-1-link")
111 ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
112 ("/var/guix/gcroots/current-system" -> "/run/current-system")
114 ;; XXX: 'guix-register' creates this symlink with a wrong target, so
115 ;; create it upfront to be sure.
116 ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
119 (directory "/tmp" 0 0 #o1777) ; sticky bit
120 (directory "/var/tmp" 0 0 #o1777)
121 (directory "/var/lock" 0 0 #o1777)
123 (directory "/root" 0 0) ; an exception
124 (directory "/home" 0 0)))
126 (define (populate-root-file-system system target)
127 "Make the essential non-store files and directories on TARGET. This
128 includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
129 (for-each (cut evaluate-populate-directive <> target)
130 (directives (%store-directory)))
132 ;; Add system generation 1.
133 (let ((generation-1 (string-append target
134 "/var/guix/profiles/system-1-link")))
138 (symlink system generation-1))
140 ;; If GENERATION-1 already exists, overwrite it.
141 (if (= EEXIST (system-error-errno args))
143 (delete-file generation-1)
145 (apply throw args)))))))
147 (define (reset-timestamps directory)
148 "Reset the timestamps of all the files under DIRECTORY, so that they appear
149 as created and modified at the Epoch."
150 (display "clearing file timestamps...\n")
151 (for-each (lambda (file)
152 (let ((s (lstat file)))
153 ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so
154 ;; the timestamp of symlinks cannot be changed, and there are
155 ;; symlinks here pointing to /gnu/store, which is the host,
157 (unless (eq? (stat:type s) 'symlink)
158 (utime file 0 0 0 0))))
159 (find-files directory #:directories? #t)))
161 (define* (register-closure store closure
162 #:key (deduplicate? #t))
163 "Register CLOSURE in STORE, where STORE is the directory name of the target
164 store and CLOSURE is the name of a file containing a reference graph as used
165 by 'guix-register'. As a side effect, this resets timestamps on store files
166 and, if DEDUPLICATE? is true, deduplicates files common to CLOSURE and the
168 (let ((status (apply system* "guix-register" "--prefix" store
169 (append (if deduplicate? '() '("--no-deduplication"))
171 (unless (zero? status)
172 (error "failed to register store items" closure))))
174 (define* (populate-single-profile-directory directory
175 #:key profile closure
178 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
179 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
180 is initialized to contain a single profile under /root pointing to PROFILE.
181 When REGISTER? is true, initialize DIRECTORY/var/guix/db to reflect the
182 contents of the store; DEDUPLICATE? determines whether to deduplicate files in
185 This is used to create the self-contained tarballs with 'guix pack'."
187 (string-append directory "/" file))
189 (define %root-profile
190 "/var/guix/profiles/per-user/root")
192 (define (mkdir-p* dir)
193 (mkdir-p (scope dir)))
195 (define (symlink* old new)
196 (symlink old (scope new)))
198 ;; Populate the store.
199 (populate-store (list closure) directory)
202 (register-closure (canonicalize-path directory) closure
203 #:deduplicate? deduplicate?)
205 ;; XXX: 'guix-register' registers profiles as GC roots but the symlink
206 ;; target uses $TMPDIR. Fix that.
207 (delete-file (scope "/var/guix/gcroots/profiles"))
208 (symlink* "/var/guix/profiles"
209 "/var/guix/gcroots/profiles"))
211 ;; Make root's profile, which makes it a GC root.
212 (mkdir-p* %root-profile)
214 (string-append %root-profile "/guix-profile-1-link"))
215 (symlink* (string-append %root-profile "/guix-profile-1-link")
216 (string-append %root-profile "/guix-profile"))
219 (symlink* (string-append %root-profile "/guix-profile")
220 "/root/.guix-profile"))
222 ;;; install.scm ends here