1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 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
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)
54 "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
56 (let loop ((directive directive))
61 (mkdir-p (string-append target name)))
62 (('directory name uid gid)
63 (let ((dir (string-append target name)))
66 (('directory name uid gid mode)
67 (loop `(directory ,name ,uid ,gid))
68 (chmod (string-append target name) mode))
73 (symlink old (string-append target new)))
75 ;; When doing 'guix system init' on the current '/', some
76 ;; symlinks may already exists. Override them.
77 (if (= EEXIST (system-error-errno args))
79 (delete-file (string-append target new))
81 (apply throw args))))))))
83 ;; Usually we can only get here when installing to an existing root,
84 ;; as with 'guix system init foo.scm /'.
85 (format (current-error-port)
86 "error: failed to evaluate directive: ~s~%"
88 (apply throw args)))))
90 (define (directives store)
91 "Return a list of directives to populate the root file system that will host
93 `(;; Note: the store's GID is fixed precisely so we can set it here rather
94 ;; than at activation time.
95 (directory ,store 0 30000 #o1775)
98 (directory "/var/log") ; for shepherd
99 (directory "/var/guix/gcroots")
100 (directory "/var/empty") ; for no-login accounts
101 (directory "/var/db") ; for dhclient, etc.
102 (directory "/var/run")
105 (directory "/var/guix/profiles/per-user/root" 0 0)
107 ;; Link to the initial system generation.
108 ("/var/guix/profiles/system" -> "system-1-link")
110 ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
111 ("/var/guix/gcroots/current-system" -> "/run/current-system")
112 ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
115 (directory "/tmp" 0 0 #o1777) ; sticky bit
116 (directory "/var/tmp" 0 0 #o1777)
117 (directory "/var/lock" 0 0 #o1777)
119 (directory "/root" 0 0) ; an exception
120 (directory "/home" 0 0)))
122 (define (populate-root-file-system system target)
123 "Make the essential non-store files and directories on TARGET. This
124 includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
125 (for-each (cut evaluate-populate-directive <> target)
126 (directives (%store-directory)))
128 ;; Add system generation 1.
129 (let ((generation-1 (string-append target
130 "/var/guix/profiles/system-1-link")))
134 (symlink system generation-1))
136 ;; If GENERATION-1 already exists, overwrite it.
137 (if (= EEXIST (system-error-errno args))
139 (delete-file generation-1)
141 (apply throw args)))))))
143 (define* (populate-single-profile-directory directory
144 #:key profile closure
145 (profile-name "guix-profile")
147 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
148 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
149 is initialized to contain a single profile under /root pointing to PROFILE.
151 When DATABASE is true, copy it to DIRECTORY/var/guix/db and create
152 DIRECTORY/var/guix/gcroots and friends.
154 PROFILE-NAME is the name of the profile being created under
155 /var/guix/profiles, typically either \"guix-profile\" or \"current-guix\".
157 This is used to create the self-contained tarballs with 'guix pack'."
159 (string-append directory "/" file))
161 (define %root-profile
162 "/var/guix/profiles/per-user/root")
164 (define (mkdir-p* dir)
165 (mkdir-p (scope dir)))
167 (define (symlink* old new)
168 (symlink old (scope new)))
170 ;; Populate the store.
171 (populate-store (list closure) directory)
174 (install-file database (scope "/var/guix/db/"))
175 (chmod (scope "/var/guix/db/db.sqlite") #o644)
176 (mkdir-p* "/var/guix/profiles")
177 (mkdir-p* "/var/guix/gcroots")
178 (symlink* "/var/guix/profiles"
179 "/var/guix/gcroots/profiles"))
181 ;; Make root's profile, which makes it a GC root.
182 (mkdir-p* %root-profile)
184 (string-append %root-profile "/" profile-name "-1-link"))
185 (symlink* (string-append profile-name "-1-link")
186 (string-append %root-profile "/" profile-name))
191 (symlink* (string-append %root-profile "/guix-profile")
192 "/root/.guix-profile"))
194 (mkdir-p* "/root/.config/guix")
195 (symlink* (string-append %root-profile "/current-guix")
196 "/root/.config/guix/current"))
200 ;;; install.scm ends here