install.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. (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-boot-config
  25. evaluate-populate-directive
  26. populate-root-file-system
  27. register-closure
  28. install-database-and-gc-roots
  29. populate-single-profile-directory))
  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. (define (install-boot-config bootcfg bootcfg-location mount-point)
  39. "Atomically copy BOOTCFG into BOOTCFG-LOCATION on the MOUNT-POINT. Note
  40. that the caller must make sure that BOOTCFG is registered as a GC root so
  41. that the fonts, background images, etc. referred to by BOOTCFG are not GC'd."
  42. (let* ((target (string-append mount-point bootcfg-location))
  43. (pivot (string-append target ".new")))
  44. (mkdir-p (dirname target))
  45. ;; Copy BOOTCFG instead of just symlinking it, because symlinks won't
  46. ;; work when /boot is on a separate partition. Do that atomically.
  47. (copy-file bootcfg pivot)
  48. (rename-file pivot target)))
  49. (define (evaluate-populate-directive directive target)
  50. "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
  51. directory TARGET."
  52. (let loop ((directive directive))
  53. (catch 'system-error
  54. (lambda ()
  55. (match directive
  56. (('directory name)
  57. (mkdir-p (string-append target name)))
  58. (('directory name uid gid)
  59. (let ((dir (string-append target name)))
  60. (mkdir-p dir)
  61. (chown dir uid gid)))
  62. (('directory name uid gid mode)
  63. (loop `(directory ,name ,uid ,gid))
  64. (chmod (string-append target name) mode))
  65. ((new '-> old)
  66. (let try ()
  67. (catch 'system-error
  68. (lambda ()
  69. (symlink old (string-append target new)))
  70. (lambda args
  71. ;; When doing 'guix system init' on the current '/', some
  72. ;; symlinks may already exists. Override them.
  73. (if (= EEXIST (system-error-errno args))
  74. (begin
  75. (delete-file (string-append target new))
  76. (try))
  77. (apply throw args))))))))
  78. (lambda args
  79. ;; Usually we can only get here when installing to an existing root,
  80. ;; as with 'guix system init foo.scm /'.
  81. (format (current-error-port)
  82. "error: failed to evaluate directive: ~s~%"
  83. directive)
  84. (apply throw args)))))
  85. (define (directives store)
  86. "Return a list of directives to populate the root file system that will host
  87. STORE."
  88. `(;; Note: the store's GID is fixed precisely so we can set it here rather
  89. ;; than at activation time.
  90. (directory ,store 0 30000 #o1775)
  91. (directory "/etc")
  92. (directory "/var/log") ; for shepherd
  93. (directory "/var/guix/gcroots")
  94. (directory "/var/empty") ; for no-login accounts
  95. (directory "/var/db") ; for dhclient, etc.
  96. (directory "/var/run")
  97. (directory "/run")
  98. (directory "/mnt")
  99. (directory "/var/guix/profiles/per-user/root" 0 0)
  100. ;; Link to the initial system generation.
  101. ("/var/guix/profiles/system" -> "system-1-link")
  102. ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
  103. ("/var/guix/gcroots/current-system" -> "/run/current-system")
  104. ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
  105. (directory "/bin")
  106. (directory "/tmp" 0 0 #o1777) ; sticky bit
  107. (directory "/var/tmp" 0 0 #o1777)
  108. (directory "/var/lock" 0 0 #o1777)
  109. (directory "/home" 0 0)))
  110. (define (populate-root-file-system system target)
  111. "Make the essential non-store files and directories on TARGET. This
  112. includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
  113. (for-each (cut evaluate-populate-directive <> target)
  114. (directives (%store-directory)))
  115. ;; Add system generation 1.
  116. (let ((generation-1 (string-append target
  117. "/var/guix/profiles/system-1-link")))
  118. (let try ()
  119. (catch 'system-error
  120. (lambda ()
  121. (symlink system generation-1))
  122. (lambda args
  123. ;; If GENERATION-1 already exists, overwrite it.
  124. (if (= EEXIST (system-error-errno args))
  125. (begin
  126. (delete-file generation-1)
  127. (try))
  128. (apply throw args)))))))
  129. (define %root-profile
  130. "/var/guix/profiles/per-user/root")
  131. (define* (install-database-and-gc-roots root database profile
  132. #:key (profile-name "guix-profile"))
  133. "Install DATABASE, the store database, under directory ROOT. Create
  134. PROFILE-NAME and have it link to PROFILE, a store item."
  135. (define (scope file)
  136. (string-append root "/" file))
  137. (define (mkdir-p* dir)
  138. (mkdir-p (scope dir)))
  139. (define (symlink* old new)
  140. (symlink old (scope new)))
  141. (install-file database (scope "/var/guix/db/"))
  142. (chmod (scope "/var/guix/db/db.sqlite") #o644)
  143. (mkdir-p* "/var/guix/profiles")
  144. (mkdir-p* "/var/guix/gcroots")
  145. (symlink* "/var/guix/profiles" "/var/guix/gcroots/profiles")
  146. ;; Make root's profile, which makes it a GC root.
  147. (mkdir-p* %root-profile)
  148. (symlink* profile
  149. (string-append %root-profile "/" profile-name "-1-link"))
  150. (symlink* (string-append profile-name "-1-link")
  151. (string-append %root-profile "/" profile-name)))
  152. (define* (populate-single-profile-directory directory
  153. #:key profile closure
  154. (profile-name "guix-profile")
  155. database)
  156. "Populate DIRECTORY with a store containing PROFILE, whose closure is given
  157. in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
  158. is initialized to contain a single profile under /root pointing to PROFILE.
  159. When DATABASE is true, copy it to DIRECTORY/var/guix/db and create
  160. DIRECTORY/var/guix/gcroots and friends.
  161. PROFILE-NAME is the name of the profile being created under
  162. /var/guix/profiles, typically either \"guix-profile\" or \"current-guix\".
  163. This is used to create the self-contained tarballs with 'guix pack'."
  164. (define (scope file)
  165. (string-append directory "/" file))
  166. (define (mkdir-p* dir)
  167. (mkdir-p (scope dir)))
  168. (define (symlink* old new)
  169. (symlink old (scope new)))
  170. ;; Populate the store.
  171. (populate-store (list closure) directory)
  172. (when database
  173. (install-database-and-gc-roots directory database profile
  174. #:profile-name profile-name))
  175. (match profile-name
  176. ("guix-profile"
  177. (mkdir-p* "/root")
  178. (symlink* (string-append %root-profile "/guix-profile")
  179. "/root/.guix-profile"))
  180. ("current-guix"
  181. (mkdir-p* "/root/.config/guix")
  182. (symlink* (string-append %root-profile "/current-guix")
  183. "/root/.config/guix/current"))
  184. (_
  185. #t)))
  186. ;;; install.scm ends here