profiles: Generate an 'etc/profile' file.
[jackhill/guix/guix.git] / guix / build / profiles.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix build profiles)
20 #:use-module (guix build union)
21 #:use-module (guix build utils)
22 #:use-module (guix search-paths)
23 #:use-module (srfi srfi-26)
24 #:use-module (ice-9 match)
25 #:use-module (ice-9 pretty-print)
26 #:export (build-profile))
27
28 ;;; Commentary:
29 ;;;
30 ;;; Build a user profile (essentially the union of all the installed packages)
31 ;;; with its associated meta-data.
32 ;;;
33 ;;; Code:
34
35 (define (abstract-profile profile)
36 "Return a procedure that replaces PROFILE in VALUE with a reference to the
37 'GUIX_PROFILE' environment variable. This allows users to specify what the
38 user-friendly name of the profile is, for instance ~/.guix-profile rather than
39 /gnu/store/...-profile."
40 (let ((replacement (string-append "${GUIX_PROFILE:-" profile "}")))
41 (match-lambda
42 ((search-path . value)
43 (let* ((separator (search-path-specification-separator search-path))
44 (items (string-tokenize* value separator))
45 (crop (cute string-drop <> (string-length profile))))
46 (cons search-path
47 (string-join (map (lambda (str)
48 (string-append replacement (crop str)))
49 items)
50 separator)))))))
51
52 (define (write-environment-variable-definition port)
53 "Write the given environment variable definition to PORT."
54 (match-lambda
55 ((search-path . value)
56 (display (search-path-definition search-path value #:kind 'prefix)
57 port)
58 (newline port))))
59
60 (define* (build-profile output inputs
61 #:key manifest search-paths)
62 "Build a user profile from INPUTS in directory OUTPUT. Write MANIFEST, an
63 sexp, to OUTPUT/manifest. Create OUTPUT/etc/profile with Bash definitions for
64 all the variables listed in SEARCH-PATHS."
65 ;; Make the symlinks.
66 (union-build output inputs
67 #:log-port (%make-void-port "w"))
68
69 ;; Store meta-data.
70 (call-with-output-file (string-append output "/manifest")
71 (lambda (p)
72 (pretty-print manifest p)))
73
74 ;; Add a ready-to-use Bash profile.
75 (mkdir-p (string-append output "/etc"))
76 (call-with-output-file (string-append output "/etc/profile")
77 (lambda (port)
78 ;; The use of $GUIX_PROFILE described below is not great. Another
79 ;; option would have been to use "$1" and have users run:
80 ;;
81 ;; source ~/.guix-profile/etc/profile ~/.guix-profile
82 ;;
83 ;; However, when 'source' is used with no arguments, $1 refers to the
84 ;; first positional parameter of the calling scripts, so we can rely on
85 ;; it.
86 (display "\
87 # Source this file to define all the relevant environment variables in Bash
88 # for this profile. You may want to define the 'GUIX_PROFILE' environment
89 # variable to point to the \"visible\" name of the profile, like this:
90 #
91 # GUIX_PROFILE=/path/to/profile
92 # source /path/to/profile/etc/profile
93 #
94 # When GUIX_PROFILE is undefined, the various environment variables refer
95 # to this specific profile generation.
96 \n" port)
97 (let ((variables (evaluate-search-paths (cons $PATH search-paths)
98 (list output))))
99 (for-each (write-environment-variable-definition port)
100 (map (abstract-profile output) variables))))))
101
102 ;;; profile.scm ends here