unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob a4aaa86c20345e88cba5372296eebb0912597a0b 8421 bytes (raw)
name: guix/scripts/environment.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2014 David Thompson <davet@gnu.org>
;;;
;;; 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 scripts environment)
  #:use-module (guix ui)
  #:use-module (guix store)
  #:use-module (guix derivations)
  #:use-module (guix packages)
  #:use-module (guix profiles)
  #:use-module (guix utils)
  #:use-module (guix monads)
  #:use-module (guix gexp)
  #:use-module (guix build utils)
  #:use-module (guix build-system gnu)
  #:use-module (guix scripts build)
  #:use-module (gnu packages)
  #:use-module (gnu packages commencement)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (ice-9 pretty-print)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-37)
  #:use-module (srfi srfi-98)
  #:export (guix-environment))

(define (for-each-search-path proc inputs derivations pure?)
  "Apply PROC for each native search path in INPUTS in addition to 'PATH'.
Use the output paths of DERIVATIONS to build each search path.  When PURE? is
#t, the existing search path value is ignored.  Otherwise, the existing search
path value is appended."
  (let ((paths (map derivation->output-path derivations)))
    (for-each (match-lambda
               (($ <search-path-specification>
                   variable directories separator)
                (let* ((current (getenv variable))
                       (path ((@@ (guix build utils) search-path-as-list)
                              directories paths))
                       (value (list->search-path-as-string path separator)))
                  (proc variable
                        (if (and current (not pure?))
                            (string-append value separator current)
                            value)))))
              (cons* (search-path-specification
                      (variable "PATH")
                      (directories '("bin" "sbin"))
                      (separator ":"))
                     (delete-duplicates
                      (append-map package-native-search-paths inputs))))))

(define (purify-environment)
  "Unset almost all environment variables.  A small number of variables such
as 'HOME' and 'USER' are left untouched."
  (for-each unsetenv
            (filter (lambda (variable)
                      ;; Protect some env vars from purification.  Borrowed
                      ;; from nix-shell.
                      (not (member variable
                                   '("HOME" "USER" "LOGNAME" "DISPLAY"
                                     "TERM" "TZ" "PAGER"))))
                    (map car (get-environment-variables)))))

(define (create-environment inputs derivations pure?)
  "Set the needed environment variables for all packages within INPUTS.  When
PURE? is #t, unset the variables in the current environment.  Otherwise,
augment existing enviroment variables with additional search paths."
  (when pure? (purify-environment))
  (for-each-search-path setenv inputs derivations pure?))

(define (show-search-paths inputs derivations pure?)
  "Display the needed search paths to build an environment that contains the
packages within INPUTS.  When PURE? is #t, do not augment existing environment
variables with additional search paths."
  (for-each-search-path (lambda (variable value)
                          (format #t "export ~a=\"~a\"~%" variable value))
                        inputs derivations pure?))

(define (show-help)
  (display (_ "Usage: guix environment --package=PACKAGE [OPTION]...
Build an environment that includes the dependencies of PACKAGE.\n"))
  (display (_ "
  -p, --package=[PACKAGE]
                         build environment for PACKAGE"))
  (display (_ "
  -e, --exec             shell command to execute"))
  (display (_ "
  --pure                 unset existing environment variables"))
  (display (_ "
  --search-paths         display needed environment variable definitions"))
  (newline)
  (show-build-options-help)
  (newline)
  (display (_ "
  -h, --help             display this help and exit"))
  (display (_ "
  -V, --version          display version information and exit"))
  (show-bug-report-information))

(define %default-options
  ;; Default to opening a new shell.
  `((exec . ,(getenv "SHELL"))
    (substitutes? . #t)
    (max-silent-time . 3600)
    (verbosity . 0)))

(define %options
  ;; Specification of the command-line options.
  (cons* (option '(#\h "help") #f #f
                 (lambda args
                   (show-help)
                   (exit 0)))
         (option '(#\V "version") #f #f
                 (lambda args
                   (show-version-and-exit "guix environment")))
         (option '("pure") #f #f
                 (lambda (opt name arg result)
                   (alist-cons 'pure #t result)))
         (option '(#\e "exec") #t #f
                 (lambda (opt name arg result)
                   (alist-cons 'exec arg result)))
         (option '("search-paths") #f #f
                 (lambda (opt name arg result)
                   (alist-cons 'search-paths #t result)))
         (option '(#\l "load") #t #f
                 (lambda (opt name arg result)
                   (alist-cons 'load arg result)))
         %standard-build-options))

(define (pick-all alist key)
  "Return a list of values in ALIST associated with KEY."
  (define same-key? (cut eq? key <>))

  (fold (lambda (pair memo)
          (match pair
            (((? same-key? k) . v)
             (cons v memo))
            (_ memo)))
        '() alist))

(define (packages->transitive-inputs packages)
  "Return a list of the transitive inputs for all PACKAGES."
  (define (transitive-inputs package)
    (filter-map (match-lambda
                 ((_ (? package? package)) package)
                 (_ #f))
                (bag-transitive-inputs
                 (package->bag package))))
  (if (list? packages)
      (delete-duplicates
       (append-map transitive-inputs packages))
      (transitive-inputs packages)))

;; TODO: Deduplicate these.
(define show-what-to-build*
  (store-lift show-what-to-build))

(define set-build-options-from-command-line*
  (store-lift set-build-options-from-command-line))

(define (build-inputs inputs opts)
  "Build the packages in INPUTS using the build options in OPTS."
  (with-store store
    (run-with-store store
      (mlet* %store-monad ((drvs (sequence %store-monad
                                           (map package->derivation inputs))))
        (mbegin %store-monad
          (show-what-to-build* drvs
                               #:use-substitutes? (assoc-ref opts 'substitutes?)
                               #:dry-run? #f)
          (set-build-options-from-command-line* opts)
          (built-derivations drvs)
          (return drvs))))))

;; Entry point.
(define (guix-environment . args)
  (define (parse-options)
    (args-fold* args %options
                (lambda (opt name arg result)
                  (leave (_ "~A: unrecognized option~%") name))
                (lambda (arg result)
                  (alist-cons 'package arg result))
                %default-options))

  (let* ((opts (parse-options))
         (pure? (assoc-ref opts 'pure))
         (command (assoc-ref opts 'exec))
         ;; Load from file if given, otherwise search for packages.
         (inputs (packages->transitive-inputs
                  (or (and=> (assoc-ref opts 'load) load)
                      (map specification->package
                           (pick-all opts 'package)))))
         (drvs (build-inputs inputs opts)))
    (cond ((assoc-ref opts 'search-paths)
           (show-search-paths inputs drvs pure?))
          (else
           (create-environment inputs drvs pure?)
           (system command)))))

debug log:

solving a4aaa86 ...
found a4aaa86 in https://yhetil.org/guix-devel/87oatmch5i.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me/

applying [1/1] https://yhetil.org/guix-devel/87oatmch5i.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me/
diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
new file mode 100644
index 0000000..a4aaa86

Checking patch guix/scripts/environment.scm...
Applied patch guix/scripts/environment.scm cleanly.

index at:
100644 a4aaa86c20345e88cba5372296eebb0912597a0b	guix/scripts/environment.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).