2015-05-06 14:14:58 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2019-01-20 01:33:25 +01:00
|
|
|
;;; Copyright © 2015, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
|
2015-05-06 14:14:58 +02:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix build profiles)
|
|
|
|
#:use-module (guix build union)
|
2015-05-06 17:08:00 +02:00
|
|
|
#:use-module (guix build utils)
|
|
|
|
#:use-module (guix search-paths)
|
|
|
|
#:use-module (srfi srfi-26)
|
2015-05-08 15:39:45 +02:00
|
|
|
#:use-module (ice-9 ftw)
|
2015-05-06 17:08:00 +02:00
|
|
|
#:use-module (ice-9 match)
|
2015-05-06 14:14:58 +02:00
|
|
|
#:use-module (ice-9 pretty-print)
|
2018-04-28 22:20:36 +02:00
|
|
|
#:re-export (symlink-relative) ;for convenience
|
2015-05-27 22:36:52 +02:00
|
|
|
#:export (ensure-writable-directory
|
|
|
|
build-profile))
|
2015-05-06 14:14:58 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; Build a user profile (essentially the union of all the installed packages)
|
|
|
|
;;; with its associated meta-data.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2015-05-06 17:08:00 +02:00
|
|
|
(define (abstract-profile profile)
|
|
|
|
"Return a procedure that replaces PROFILE in VALUE with a reference to the
|
|
|
|
'GUIX_PROFILE' environment variable. This allows users to specify what the
|
|
|
|
user-friendly name of the profile is, for instance ~/.guix-profile rather than
|
|
|
|
/gnu/store/...-profile."
|
2017-01-22 22:42:57 +01:00
|
|
|
(let ((replacement (string-append "${GUIX_PROFILE:-" profile "}"))
|
|
|
|
(crop (cute string-drop <> (string-length profile))))
|
2015-05-06 17:08:00 +02:00
|
|
|
(match-lambda
|
|
|
|
((search-path . value)
|
2017-01-22 22:42:57 +01:00
|
|
|
(match (search-path-specification-separator search-path)
|
|
|
|
(#f
|
|
|
|
(cons search-path
|
|
|
|
(string-append replacement (crop value))))
|
|
|
|
((? string? separator)
|
|
|
|
(let ((items (string-tokenize* value separator)))
|
|
|
|
(cons search-path
|
|
|
|
(string-join (map (lambda (str)
|
|
|
|
(string-append replacement (crop str)))
|
|
|
|
items)
|
|
|
|
separator)))))))))
|
2015-05-06 17:08:00 +02:00
|
|
|
|
|
|
|
(define (write-environment-variable-definition port)
|
|
|
|
"Write the given environment variable definition to PORT."
|
|
|
|
(match-lambda
|
|
|
|
((search-path . value)
|
|
|
|
(display (search-path-definition search-path value #:kind 'prefix)
|
|
|
|
port)
|
|
|
|
(newline port))))
|
|
|
|
|
2015-05-08 15:39:45 +02:00
|
|
|
(define (build-etc/profile output search-paths)
|
|
|
|
"Build the 'OUTPUT/etc/profile' shell file containing environment variable
|
|
|
|
definitions for all the SEARCH-PATHS."
|
2019-01-20 01:33:25 +01:00
|
|
|
(define file
|
|
|
|
(string-append output "/etc/profile"))
|
|
|
|
|
|
|
|
(mkdir-p (dirname file))
|
|
|
|
(when (file-exists? file)
|
|
|
|
(delete-file file))
|
|
|
|
|
|
|
|
(call-with-output-file file
|
2015-05-06 17:08:00 +02:00
|
|
|
(lambda (port)
|
|
|
|
;; The use of $GUIX_PROFILE described below is not great. Another
|
|
|
|
;; option would have been to use "$1" and have users run:
|
|
|
|
;;
|
|
|
|
;; source ~/.guix-profile/etc/profile ~/.guix-profile
|
|
|
|
;;
|
|
|
|
;; However, when 'source' is used with no arguments, $1 refers to the
|
2017-04-07 22:26:12 +02:00
|
|
|
;; first positional parameter of the calling script, so we cannot rely
|
|
|
|
;; on it.
|
2015-05-06 17:08:00 +02:00
|
|
|
(display "\
|
|
|
|
# Source this file to define all the relevant environment variables in Bash
|
|
|
|
# for this profile. You may want to define the 'GUIX_PROFILE' environment
|
|
|
|
# variable to point to the \"visible\" name of the profile, like this:
|
|
|
|
#
|
2017-11-20 18:41:47 +01:00
|
|
|
# GUIX_PROFILE=/path/to/profile ; \\
|
2015-05-06 17:08:00 +02:00
|
|
|
# source /path/to/profile/etc/profile
|
|
|
|
#
|
|
|
|
# When GUIX_PROFILE is undefined, the various environment variables refer
|
|
|
|
# to this specific profile generation.
|
|
|
|
\n" port)
|
2018-07-09 10:44:36 +02:00
|
|
|
(let ((variables (evaluate-search-paths search-paths
|
2015-05-06 17:08:00 +02:00
|
|
|
(list output))))
|
|
|
|
(for-each (write-environment-variable-definition port)
|
|
|
|
(map (abstract-profile output) variables))))))
|
2015-05-06 14:14:58 +02:00
|
|
|
|
2018-09-10 23:37:48 +02:00
|
|
|
(define* (ensure-writable-directory directory
|
|
|
|
#:key (symlink symlink))
|
2015-05-08 15:39:45 +02:00
|
|
|
"Ensure DIRECTORY exists and is writable. If DIRECTORY is currently a
|
|
|
|
symlink (to a read-only directory in the store), then delete the symlink and
|
|
|
|
instead make DIRECTORY a \"real\" directory containing symlinks."
|
2018-09-10 23:37:48 +02:00
|
|
|
(define (absolute? file)
|
|
|
|
(string-prefix? "/" file))
|
|
|
|
|
2015-05-08 15:39:45 +02:00
|
|
|
(define (unsymlink link)
|
2018-09-10 23:37:48 +02:00
|
|
|
(let* ((target (match (readlink link)
|
|
|
|
((? absolute? target)
|
|
|
|
target)
|
|
|
|
((? string? relative)
|
|
|
|
(string-append (dirname link) "/" relative))))
|
2015-05-18 09:47:29 +02:00
|
|
|
;; TARGET might itself be a symlink, so append "/" to make sure
|
|
|
|
;; 'scandir' enters it.
|
|
|
|
(files (scandir (string-append target "/")
|
2015-05-08 15:39:45 +02:00
|
|
|
(negate (cut member <> '("." ".."))))))
|
|
|
|
(delete-file link)
|
|
|
|
(mkdir link)
|
|
|
|
(for-each (lambda (file)
|
|
|
|
(symlink (string-append target "/" file)
|
|
|
|
(string-append link "/" file)))
|
|
|
|
files)))
|
|
|
|
|
|
|
|
(catch 'system-error
|
|
|
|
(lambda ()
|
|
|
|
(mkdir directory))
|
|
|
|
(lambda args
|
|
|
|
(let ((errno (system-error-errno args)))
|
|
|
|
(if (= errno EEXIST)
|
|
|
|
(let ((stat (lstat directory)))
|
|
|
|
(case (stat:type stat)
|
|
|
|
((symlink)
|
|
|
|
;; "Unsymlink" DIRECTORY so that it is writable.
|
|
|
|
(unsymlink directory))
|
|
|
|
((directory)
|
|
|
|
#t)
|
|
|
|
(else
|
|
|
|
(error "cannot mkdir because a same-named file exists"
|
|
|
|
directory))))
|
|
|
|
(apply throw args))))))
|
|
|
|
|
|
|
|
(define* (build-profile output inputs
|
2018-04-28 22:20:36 +02:00
|
|
|
#:key manifest search-paths
|
|
|
|
(symlink symlink))
|
|
|
|
"Build a user profile from INPUTS in directory OUTPUT, using SYMLINK to
|
|
|
|
create symlinks. Write MANIFEST, an sexp, to OUTPUT/manifest. Create
|
|
|
|
OUTPUT/etc/profile with Bash definitions for -all the variables listed in
|
|
|
|
SEARCH-PATHS."
|
2019-01-20 01:33:25 +01:00
|
|
|
(define manifest-file
|
|
|
|
(string-append output "/manifest"))
|
|
|
|
|
2015-05-08 15:39:45 +02:00
|
|
|
;; Make the symlinks.
|
|
|
|
(union-build output inputs
|
2018-04-28 22:20:36 +02:00
|
|
|
#:symlink symlink
|
2015-05-08 15:39:45 +02:00
|
|
|
#:log-port (%make-void-port "w"))
|
|
|
|
|
2019-01-20 01:33:25 +01:00
|
|
|
;; If one of the INPUTS provides a '/manifest' file, delete it. That can
|
|
|
|
;; happen if MANIFEST contains something such as a Guix instance, which is
|
|
|
|
;; ultimately built as a profile.
|
|
|
|
(when (file-exists? manifest-file)
|
|
|
|
(delete-file manifest-file))
|
|
|
|
|
2015-05-08 15:39:45 +02:00
|
|
|
;; Store meta-data.
|
2019-01-20 01:33:25 +01:00
|
|
|
(call-with-output-file manifest-file
|
2015-05-08 15:39:45 +02:00
|
|
|
(lambda (p)
|
|
|
|
(pretty-print manifest p)))
|
|
|
|
|
|
|
|
;; Make sure we can write to 'OUTPUT/etc'. 'union-build' above could have
|
|
|
|
;; made 'etc' a symlink to a read-only sub-directory in the store so we need
|
|
|
|
;; to work around that.
|
2018-09-10 23:37:48 +02:00
|
|
|
(ensure-writable-directory (string-append output "/etc")
|
|
|
|
#:symlink symlink)
|
2015-05-08 15:39:45 +02:00
|
|
|
|
|
|
|
;; Write 'OUTPUT/etc/profile'.
|
|
|
|
(build-etc/profile output search-paths))
|
|
|
|
|
2015-05-06 14:14:58 +02:00
|
|
|
;;; profile.scm ends here
|