1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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
28 install-database-and-gc-roots
29 populate-single-profile-directory))
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
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))
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)))
53 (define* (evaluate-populate-directive directive target
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))
66 (mkdir-p (string-append target name)))
67 (('directory name uid gid)
68 (let ((dir (string-append target name)))
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))
80 (call-with-output-file (string-append target name)
82 (('file name (? string? content))
83 (call-with-output-file (string-append target name)
85 (display content port))))
90 (symlink old (string-append target new)))
92 ;; When doing 'guix system init' on the current '/', some
93 ;; symlinks may already exists. Override them.
94 (if (= EEXIST (system-error-errno args))
96 (delete-file (string-append target new))
98 (apply throw args))))))))
100 ;; Usually we can only get here when installing to an existing root,
101 ;; as with 'guix system init foo.scm /'.
102 (format (current-error-port)
103 "error: failed to evaluate directive: ~s~%"
105 (apply throw args)))))
107 (define (directives store)
108 "Return a list of directives to populate the root file system that will host
110 `((directory ,store 0 0 #o1775)
113 (directory "/var/log") ; for shepherd
114 (directory "/var/guix/gcroots")
115 (directory "/var/empty") ; for no-login accounts
116 (directory "/var/db") ; for dhclient, etc.
117 (directory "/var/run")
120 (directory "/var/guix/profiles/per-user/root" 0 0)
122 ;; Link to the initial system generation.
123 ("/var/guix/profiles/system" -> "system-1-link")
125 ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
126 ("/var/guix/gcroots/current-system" -> "/run/current-system")
127 ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
130 (directory "/tmp" 0 0 #o1777) ; sticky bit
131 (directory "/var/tmp" 0 0 #o1777)
132 (directory "/var/lock" 0 0 #o1777)
134 (directory "/home" 0 0)))
136 (define* (populate-root-file-system system target
138 "Make the essential non-store files and directories on TARGET. This
139 includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM.
140 EXTRAS is a list of directives appended to the built-in directives to populate
142 (for-each (cut evaluate-populate-directive <> target)
143 (append (directives (%store-directory)) extras))
145 ;; Add system generation 1.
146 (let ((generation-1 (string-append target
147 "/var/guix/profiles/system-1-link")))
151 (symlink system generation-1))
153 ;; If GENERATION-1 already exists, overwrite it.
154 (if (= EEXIST (system-error-errno args))
156 (delete-file generation-1)
158 (apply throw args)))))))
160 (define %root-profile
161 "/var/guix/profiles/per-user/root")
163 (define* (install-database-and-gc-roots root database profile
164 #:key (profile-name "guix-profile"))
165 "Install DATABASE, the store database, under directory ROOT. Create
166 PROFILE-NAME and have it link to PROFILE, a store item."
168 (string-append root "/" file))
170 (define (mkdir-p* dir)
171 (mkdir-p (scope dir)))
173 (define (symlink* old new)
174 (symlink old (scope new)))
176 (install-file database (scope "/var/guix/db/"))
177 (chmod (scope "/var/guix/db/db.sqlite") #o644)
178 (mkdir-p* "/var/guix/profiles")
179 (mkdir-p* "/var/guix/gcroots")
180 (symlink* "/var/guix/profiles" "/var/guix/gcroots/profiles")
182 ;; Make root's profile, which makes it a GC root.
183 (mkdir-p* %root-profile)
185 (string-append %root-profile "/" profile-name "-1-link"))
186 (symlink* (string-append profile-name "-1-link")
187 (string-append %root-profile "/" profile-name)))
189 (define* (populate-single-profile-directory directory
190 #:key profile closure
191 (profile-name "guix-profile")
193 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
194 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
195 is initialized to contain a single profile under /root pointing to PROFILE.
197 When DATABASE is true, copy it to DIRECTORY/var/guix/db and create
198 DIRECTORY/var/guix/gcroots and friends.
200 PROFILE-NAME is the name of the profile being created under
201 /var/guix/profiles, typically either \"guix-profile\" or \"current-guix\".
203 This is used to create the self-contained tarballs with 'guix pack'."
205 (string-append directory "/" file))
207 (define (mkdir-p* dir)
208 (mkdir-p (scope dir)))
210 (define (symlink* old new)
211 (symlink old (scope new)))
213 ;; Populate the store.
214 (populate-store (list closure) directory)
217 (install-database-and-gc-roots directory database profile
218 #:profile-name profile-name))
223 (symlink* (string-append %root-profile "/guix-profile")
224 "/root/.guix-profile"))
226 (mkdir-p* "/root/.config/guix")
227 (symlink* (string-append %root-profile "/current-guix")
228 "/root/.config/guix/current"))
232 ;;; install.scm ends here