environ.scm 3.95 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
25
26
(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)
  #:use-module (guix derivations)
  #:use-module (guix build utils)
27
  #:use-module (srfi srfi-1)
28
  #:use-module (srfi srfi-11)
29
  #:use-module (srfi srfi-26)
30
  #:export (guile->bin-path
31
            guile-current-version->path
32
33
            guile-current-load-path->args-list
            guile-current-load-compiled-path->args-list
34

35
36
37
38
39
40
41
42
            package-name->path
            package-in-list->path
            make-path
            make-new-environment))

;;
;; Guix package.
;;
43
44

(define (store) (open-connection))
45

46
47
48
49
50
51
(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)))))
52

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

;;
;; Guile.
;;

(define (guile->bin-path guile)
62
  "Return path to guile executable file. GUILE is package specification."
63
  (string-append guile "/bin/guile"))
64

65
66
(define (guile-current-version->path)
  "Return path to current guile executable file."
67
  (guile->bin-path (package-name->path (string-append "guile" "@" (version)))))
68

69
70
71
72
73
74
75
76
77
78
(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))

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;;
;; 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))))

(define (make-path packages)
  "Return PATH environment variable from PACKAGES list."
  (string-append
   "PATH=" (string-join
            (map (cut package-name->path <>) packages)
            "/bin:")
   "/bin"))

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

(define (make-new-environment environ name packages)
  (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")))
        (ps1     (env->str "PS1"     (string-append "'Jupyter Guix Kernel ["
                                                    name "] -> '")))
        (jenv    (env->str "JUPYTER_ENV" name))
        (path    (make-path packages)))
    (list home user logname term pwd pager shell ps1 jenv path)))