environ.scm 4.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
;;; Guix-kernel -- Guix kernel for Jupyter
;;; Copyright (C) 2018  Pierre-Antoine Rouby <pierre-antoine.rouby@inria.fr>
;;;
;;; This program 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.
;;;
;;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

17
18
19
20
21
22
23
24
(define-module (guix-kernel environ)
  #:use-module (gnu packages)
  #:use-module (guix licenses)
  #:use-module (guix packages)
  #:use-module (guix records)
  #:use-module (guix store)
  #:use-module (guix utils)
  #:use-module (guix monads)
25
  #:use-module (guix profiles)
26
27
  #:use-module (guix derivations)
  #:use-module (guix build utils)
28
  #:use-module (guix search-paths)
29
  #:use-module (srfi srfi-1)
30
  #:use-module (srfi srfi-11)
31
  #:use-module (srfi srfi-26)
32
  #:use-module (ice-9 match)
33
  #:export (guile->bin-path
34
            guile-current-version->path
35
36
            guile-current-load-path->args-list
            guile-current-load-compiled-path->args-list
37

38
            package-in-list->path
39
40
41
            make-new-environment

            env->str))                  ;Export for tests.
42

43
44
(define (store) (open-connection))

45
46
47
;;
;; Guix package.
;;
48

49
50
51
52
53
54
(define (m-package-by-name->package-path name)
  (mlet %store-monad ((drv (package->derivation
                            (specification->package name))))
    (mbegin %store-monad
      (built-derivations (list drv))
      (return (derivation->output-path drv)))))
55

56
57
58
59
(define (package-name->path name)
  "Return store path for package coresponding to NAME."
  (run-with-store (store) (m-package-by-name->package-path name)))

60
61
62
63
64
65
66
67
68
69
70
71
72
73
;;
;; Profile.
;;

(define (m-build-profile->path lst)
  (mlet* %store-monad ((man -> (specifications->manifest lst))
                       (drv    (profile-derivation man)))
    (mbegin %store-monad
      (built-derivations (list drv))
      (return (derivation->output-path drv)))))

(define (new-profile->path packages)
  (run-with-store (store) (m-build-profile->path packages)))

74
75
76
77
78
;;
;; Guile.
;;

(define (guile->bin-path guile)
79
  "Return path to guile executable file. GUILE is package specification."
80
  (string-append guile "/bin/guile"))
81

82
83
(define (guile-current-version->path)
  "Return path to current guile executable file."
84
  (guile->bin-path (package-name->path "guile")))
85

86
87
88
89
90
91
92
93
94
95
(define (guile-current-load-path->args-list)
  "Return list of load path with '-L' prefix for each path."
  (append-map (cut list "-L" <>)
              %load-path))

(define (guile-current-load-compiled-path->args-list)
  "Return list of load compiled path with '-C' prefix for each path."
  (append-map (cut list "-C" <>)
              %load-compiled-path))

96
97
98
99
100
101
102
103
104
105
106
107
;;
;; Create environment.
;;

(define (package-in-list->path packages name)
  "Return path for package NAME in PACKAGES list."
  (cond
   ((null? packages) #f)
   ((equal? (car (string-split (car packages) #\@)) name)
    (package-name->path (car packages)))
   (else (package-in-list->path (cdr packages) name))))

108
109
(define (make-profile packages)
  "Create profile with all PACKAGES, and return profile path."
110
111
112
113
114
  (catch #t
    (λ ()
      (new-profile->path packages))
    (λ error
      (throw 'guix-kernel "Package not found !" error))))
115
116
117
118
119
120

(define (env->str name value)
  (string-append (string-upcase name) "="
                 (if (not value) "" value)))

(define (make-new-environment environ name packages)
121
122
123
124
125
126
127
128
129
130
  (let* ((home    (env->str "HOME"    (getenv "HOME")))
         (user    (env->str "USER"    (getenv "USER")))
         (logname (env->str "LOGNAME" (getenv "LOGNAME")))
         (term    (env->str "TERM"    (getenv "TERM")))
         (pwd     (env->str "PWD"     (getenv "PWD")))
         (pager   (env->str "PAGER"   (getenv "PAGER")))
         (shell   (env->str "SHELL"   (getenv "SHELL")))
         ;; Modified variables.
         (ps1  (env->str "PS1" (string-append "'Jupyter Guix Kernel ["
                                              name "] -> '")))
131
         (jenv (env->str "JUPYTER_ENV" name))     ;Environment name.
132
133
         ;; Paths.
         (profile  (make-profile packages))
134
         (paths    (profile-search-paths profile)))
135
    ;; List of environment variables.
136
    (cons* home user logname term pwd pager shell ps1 jenv
137
           (string-append "GUIX_PROFILE=" profile)
138
139
140
141
142
           (map (match-lambda
                  ((spec . value)
                   (env->str (search-path-specification-variable spec)
                             value)))
                paths))))