2014-09-21 13:40:05 -04:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2015-05-28 08:41:04 -04:00
|
|
|
;;; Copyright © 2014, 2015 David Thompson <davet@gnu.org>
|
2015-05-05 08:10:57 -04:00
|
|
|
;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
|
2014-09-21 13:40:05 -04: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 scripts environment)
|
|
|
|
#:use-module (guix ui)
|
|
|
|
#:use-module (guix store)
|
|
|
|
#:use-module (guix derivations)
|
|
|
|
#:use-module (guix packages)
|
|
|
|
#:use-module (guix profiles)
|
2015-05-05 04:59:26 -04:00
|
|
|
#:use-module (guix search-paths)
|
2014-09-21 13:40:05 -04:00
|
|
|
#:use-module (guix utils)
|
|
|
|
#:use-module (guix monads)
|
2015-06-30 17:23:06 -04:00
|
|
|
#:use-module ((guix gexp) #:select (lower-inputs))
|
2014-09-21 13:40:05 -04:00
|
|
|
#:use-module (guix scripts build)
|
|
|
|
#:use-module (gnu packages)
|
|
|
|
#:use-module (ice-9 format)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (srfi srfi-1)
|
2015-06-30 17:16:42 -04:00
|
|
|
#:use-module (srfi srfi-11)
|
2014-09-21 13:40:05 -04:00
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (srfi srfi-37)
|
|
|
|
#:use-module (srfi srfi-98)
|
|
|
|
#:export (guix-environment))
|
|
|
|
|
2015-06-30 17:23:06 -04:00
|
|
|
(define (evaluate-input-search-paths inputs search-paths)
|
|
|
|
"Evaluate SEARCH-PATHS, a list of search-path specifications, for the
|
|
|
|
directories corresponding to INPUTS, a list of (DERIVATION) or (DERIVATION
|
|
|
|
OUTPUT) tuples."
|
|
|
|
(let ((directories (map (match-lambda
|
|
|
|
(((? derivation? drv))
|
|
|
|
(derivation->output-path drv))
|
|
|
|
(((? derivation? drv) output)
|
|
|
|
(derivation->output-path drv output))
|
|
|
|
(((? string? item))
|
|
|
|
item))
|
|
|
|
inputs)))
|
|
|
|
(evaluate-search-paths search-paths directories)))
|
2014-09-21 13:40:05 -04:00
|
|
|
|
|
|
|
;; Protect some env vars from purification. Borrowed from nix-shell.
|
|
|
|
(define %precious-variables
|
|
|
|
'("HOME" "USER" "LOGNAME" "DISPLAY" "TERM" "TZ" "PAGER"))
|
|
|
|
|
|
|
|
(define (purify-environment)
|
|
|
|
"Unset almost all environment variables. A small number of variables such
|
|
|
|
as 'HOME' and 'USER' are left untouched."
|
|
|
|
(for-each unsetenv
|
|
|
|
(remove (cut member <> %precious-variables)
|
|
|
|
(match (get-environment-variables)
|
|
|
|
(((names . _) ...)
|
|
|
|
names)))))
|
|
|
|
|
2015-06-30 17:23:06 -04:00
|
|
|
(define (create-environment inputs paths pure?)
|
|
|
|
"Set the environment variables specified by PATHS for all the packages
|
|
|
|
within INPUTS. When PURE? is #t, unset the variables in the current
|
|
|
|
environment. Otherwise, augment existing enviroment variables with additional
|
|
|
|
search paths."
|
2014-09-21 13:40:05 -04:00
|
|
|
(when pure? (purify-environment))
|
2015-05-05 09:02:35 -04:00
|
|
|
(for-each (match-lambda
|
|
|
|
((($ <search-path-specification> variable _ separator) . value)
|
|
|
|
(let ((current (getenv variable)))
|
|
|
|
(setenv variable
|
|
|
|
(if (and current (not pure?))
|
|
|
|
(string-append value separator current)
|
|
|
|
value)))))
|
2015-07-01 08:39:14 -04:00
|
|
|
(evaluate-input-search-paths inputs paths))
|
|
|
|
|
|
|
|
;; Give users a way to know that they're in 'guix environment', so they can
|
|
|
|
;; adjust 'PS1' accordingly, for instance.
|
|
|
|
(setenv "GUIX_ENVIRONMENT" "t"))
|
2014-09-21 13:40:05 -04:00
|
|
|
|
2015-06-30 17:23:06 -04:00
|
|
|
(define (show-search-paths inputs search-paths pure?)
|
|
|
|
"Display SEARCH-PATHS applied to the packages specified by INPUTS, a list of
|
|
|
|
(DERIVATION) or (DERIVATION OUTPUT) tuples. When PURE? is #t, do not augment
|
|
|
|
existing environment variables with additional search paths."
|
2015-05-05 09:02:35 -04:00
|
|
|
(for-each (match-lambda
|
|
|
|
((search-path . value)
|
|
|
|
(display
|
|
|
|
(search-path-definition search-path value
|
|
|
|
#:kind (if pure? 'exact 'prefix)))
|
|
|
|
(newline)))
|
2015-06-30 17:23:06 -04:00
|
|
|
(evaluate-input-search-paths inputs search-paths)))
|
|
|
|
|
2015-06-30 17:16:42 -04:00
|
|
|
(define (package+propagated-inputs package output)
|
|
|
|
"Return the union of PACKAGE's OUTPUT and its transitive propagated inputs."
|
|
|
|
`((,(package-name package) ,package ,output)
|
2015-06-30 17:23:06 -04:00
|
|
|
,@(package-transitive-propagated-inputs package)))
|
2014-09-21 13:40:05 -04:00
|
|
|
|
|
|
|
(define (show-help)
|
|
|
|
(display (_ "Usage: guix environment [OPTION]... PACKAGE...
|
|
|
|
Build an environment that includes the dependencies of PACKAGE and execute a
|
|
|
|
shell command in that environment.\n"))
|
|
|
|
(display (_ "
|
|
|
|
-e, --expression=EXPR create environment for the package that EXPR
|
|
|
|
evaluates to"))
|
|
|
|
(display (_ "
|
|
|
|
-l, --load=FILE create environment for the package that the code within
|
|
|
|
FILE evaluates to"))
|
|
|
|
(display (_ "
|
|
|
|
-E, --exec=COMMAND execute COMMAND in new environment"))
|
2015-05-28 08:41:04 -04:00
|
|
|
(display (_ "
|
|
|
|
--ad-hoc include all specified packages in the environment instead
|
|
|
|
of only their inputs"))
|
2014-09-21 13:40:05 -04:00
|
|
|
(display (_ "
|
2014-10-29 18:40:17 -04:00
|
|
|
--pure unset existing environment variables"))
|
2014-09-21 13:40:05 -04:00
|
|
|
(display (_ "
|
2014-10-29 18:40:17 -04:00
|
|
|
--search-paths display needed environment variable definitions"))
|
2015-06-30 11:42:35 -04:00
|
|
|
(display (_ "
|
|
|
|
-s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
|
2014-09-21 13:40:05 -04:00
|
|
|
(newline)
|
|
|
|
(show-build-options-help)
|
|
|
|
(newline)
|
|
|
|
(display (_ "
|
|
|
|
-h, --help display this help and exit"))
|
|
|
|
(display (_ "
|
|
|
|
-V, --version display version information and exit"))
|
2014-10-29 18:40:17 -04:00
|
|
|
(newline)
|
2014-09-21 13:40:05 -04:00
|
|
|
(show-bug-report-information))
|
|
|
|
|
|
|
|
(define %default-options
|
|
|
|
;; Default to opening a new shell.
|
|
|
|
`((exec . ,(or (getenv "SHELL") "/bin/sh"))
|
2015-06-30 11:42:35 -04:00
|
|
|
(system . ,(%current-system))
|
2014-09-21 13:40:05 -04:00
|
|
|
(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)))
|
|
|
|
(option '(#\e "expression") #t #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'expression arg result)))
|
2015-05-28 08:41:04 -04:00
|
|
|
(option '("ad-hoc") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'ad-hoc? #t result)))
|
2014-09-21 13:40:05 -04:00
|
|
|
(option '(#\n "dry-run") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'dry-run? #t result)))
|
2015-06-30 11:42:35 -04:00
|
|
|
(option '(#\s "system") #t #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'system arg
|
|
|
|
(alist-delete 'system result eq?))))
|
2014-09-21 13:40:05 -04:00
|
|
|
%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 (options/resolve-packages opts)
|
|
|
|
"Return OPTS with package specification strings replaced by actual
|
|
|
|
packages."
|
2015-06-30 17:16:42 -04:00
|
|
|
(append-map (match-lambda
|
|
|
|
(('package . (? string? spec))
|
|
|
|
(let-values (((package output)
|
|
|
|
(specification->package+output spec)))
|
|
|
|
`((package ,package ,output))))
|
|
|
|
(('expression . str)
|
|
|
|
;; Add all the outputs of the package STR evaluates to.
|
|
|
|
(match (read/eval str)
|
|
|
|
((? package? package)
|
|
|
|
(map (lambda (output)
|
|
|
|
`(package ,package ,output))
|
|
|
|
(package-outputs package)))))
|
|
|
|
(('load . file)
|
|
|
|
;; Add all the outputs of the package defined in FILE.
|
2015-06-30 17:31:24 -04:00
|
|
|
(let ((package (load* file (make-user-module '()))))
|
2015-06-30 17:16:42 -04:00
|
|
|
(map (lambda (output)
|
|
|
|
`(package ,package ,output))
|
|
|
|
(package-outputs package))))
|
|
|
|
(opt (list opt)))
|
|
|
|
opts))
|
2014-09-21 13:40:05 -04:00
|
|
|
|
|
|
|
(define (build-inputs inputs opts)
|
2015-06-30 17:23:06 -04:00
|
|
|
"Build the derivations in INPUTS, a list of (DERIVATION) or (DERIVATION
|
|
|
|
OUTPUT) tuples, using the build options in OPTS."
|
2014-09-21 13:40:05 -04:00
|
|
|
(let ((substitutes? (assoc-ref opts 'substitutes?))
|
2015-06-30 17:23:06 -04:00
|
|
|
(dry-run? (assoc-ref opts 'dry-run?)))
|
|
|
|
(match inputs
|
|
|
|
(((derivations _ ...) ...)
|
|
|
|
(mbegin %store-monad
|
|
|
|
(show-what-to-build* derivations
|
|
|
|
#:use-substitutes? substitutes?
|
|
|
|
#:dry-run? dry-run?)
|
|
|
|
(if dry-run?
|
|
|
|
(return #f)
|
|
|
|
(mbegin %store-monad
|
|
|
|
(set-build-options-from-command-line* opts)
|
|
|
|
(built-derivations derivations)
|
|
|
|
(return derivations))))))))
|
2014-09-21 13:40:05 -04:00
|
|
|
|
|
|
|
;; Entry point.
|
|
|
|
(define (guix-environment . args)
|
2015-02-25 17:31:51 -05:00
|
|
|
(define (handle-argument arg result)
|
|
|
|
(alist-cons 'package arg result))
|
2014-09-21 13:40:05 -04:00
|
|
|
|
2015-03-16 08:59:59 -04:00
|
|
|
(with-error-handling
|
2015-06-11 05:09:12 -04:00
|
|
|
(let* ((opts (parse-command-line args %options (list %default-options)
|
|
|
|
#:argument-handler handle-argument))
|
|
|
|
(pure? (assoc-ref opts 'pure))
|
|
|
|
(ad-hoc? (assoc-ref opts 'ad-hoc?))
|
|
|
|
(command (assoc-ref opts 'exec))
|
|
|
|
(packages (pick-all (options/resolve-packages opts) 'package))
|
|
|
|
(inputs (if ad-hoc?
|
2015-06-30 17:16:42 -04:00
|
|
|
(append-map (match-lambda
|
|
|
|
((package output)
|
|
|
|
(package+propagated-inputs package
|
|
|
|
output)))
|
|
|
|
packages)
|
2015-06-30 17:23:06 -04:00
|
|
|
(append-map (compose bag-transitive-inputs
|
2015-06-30 17:16:42 -04:00
|
|
|
package->bag
|
|
|
|
first)
|
2015-06-30 17:23:06 -04:00
|
|
|
packages)))
|
|
|
|
(paths (delete-duplicates
|
|
|
|
(cons $PATH
|
|
|
|
(append-map (match-lambda
|
|
|
|
((label (? package? p) _ ...)
|
|
|
|
(package-native-search-paths p))
|
|
|
|
(_
|
|
|
|
'()))
|
|
|
|
inputs))
|
|
|
|
eq?)))
|
2015-06-11 05:09:12 -04:00
|
|
|
(with-store store
|
2015-06-30 17:23:06 -04:00
|
|
|
(run-with-store store
|
|
|
|
(mlet %store-monad ((inputs (lower-inputs
|
|
|
|
(map (match-lambda
|
|
|
|
((label item)
|
|
|
|
(list item))
|
|
|
|
((label item output)
|
|
|
|
(list item output)))
|
|
|
|
inputs)
|
2015-06-30 11:42:35 -04:00
|
|
|
#:system (assoc-ref opts 'system))))
|
2015-06-11 05:09:12 -04:00
|
|
|
(mbegin %store-monad
|
2015-06-30 17:23:06 -04:00
|
|
|
;; First build INPUTS. This is necessary even for
|
|
|
|
;; --search-paths.
|
|
|
|
(build-inputs inputs opts)
|
|
|
|
(cond ((assoc-ref opts 'dry-run?)
|
|
|
|
(return #t))
|
|
|
|
((assoc-ref opts 'search-paths)
|
|
|
|
(show-search-paths inputs paths pure?)
|
|
|
|
(return #t))
|
|
|
|
(else
|
|
|
|
(create-environment inputs paths pure?)
|
2015-07-01 16:55:47 -04:00
|
|
|
(return (exit (status:exit-val (system command)))))))))))))
|