2014-09-21 19:40:05 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2015-05-28 14:41:04 +02:00
|
|
|
;;; Copyright © 2014, 2015 David Thompson <davet@gnu.org>
|
2016-03-02 13:43:13 +01:00
|
|
|
;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
|
2014-09-21 19:40:05 +02: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)
|
2016-03-02 13:43:13 +01:00
|
|
|
#:use-module (guix grafts)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (guix derivations)
|
|
|
|
#:use-module (guix packages)
|
|
|
|
#:use-module (guix profiles)
|
2015-05-05 10:59:26 +02:00
|
|
|
#:use-module (guix search-paths)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (guix utils)
|
2015-06-19 14:57:44 +02:00
|
|
|
#:use-module (guix build utils)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (guix monads)
|
2015-06-30 23:23:06 +02:00
|
|
|
#:use-module ((guix gexp) #:select (lower-inputs))
|
2015-09-10 11:37:36 +02:00
|
|
|
#:use-module (guix scripts)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (guix scripts build)
|
2015-06-19 14:57:44 +02:00
|
|
|
#:use-module (gnu build linux-container)
|
|
|
|
#:use-module (gnu system linux-container)
|
|
|
|
#:use-module (gnu system file-systems)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (gnu packages)
|
2015-06-19 14:57:44 +02:00
|
|
|
#:use-module (gnu packages bash)
|
2016-02-12 21:39:26 +01:00
|
|
|
#:use-module (gnu packages commencement)
|
|
|
|
#:use-module (gnu packages guile)
|
|
|
|
#:use-module ((gnu packages bootstrap) #:select (%bootstrap-guile))
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (ice-9 format)
|
|
|
|
#:use-module (ice-9 match)
|
2015-06-19 14:57:44 +02:00
|
|
|
#:use-module (ice-9 rdelim)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (srfi srfi-1)
|
2015-06-30 23:16:42 +02:00
|
|
|
#:use-module (srfi srfi-11)
|
2014-09-21 19:40:05 +02:00
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (srfi srfi-37)
|
|
|
|
#:use-module (srfi srfi-98)
|
|
|
|
#:export (guix-environment))
|
|
|
|
|
2016-02-12 21:39:26 +01:00
|
|
|
(define (evaluate-profile-search-paths profile search-paths)
|
2015-06-30 23:23:06 +02:00
|
|
|
"Evaluate SEARCH-PATHS, a list of search-path specifications, for the
|
2016-02-12 21:39:26 +01:00
|
|
|
directories in PROFILE, the store path of a profile."
|
|
|
|
(evaluate-search-paths search-paths (list profile)))
|
2014-09-21 19:40:05 +02:00
|
|
|
|
|
|
|
;; Protect some env vars from purification. Borrowed from nix-shell.
|
|
|
|
(define %precious-variables
|
|
|
|
'("HOME" "USER" "LOGNAME" "DISPLAY" "TERM" "TZ" "PAGER"))
|
|
|
|
|
2015-10-09 18:11:24 +02:00
|
|
|
(define %default-shell
|
|
|
|
(or (getenv "SHELL") "/bin/sh"))
|
|
|
|
|
2015-06-19 14:57:44 +02:00
|
|
|
(define %network-configuration-files
|
|
|
|
'("/etc/resolv.conf"
|
|
|
|
"/etc/nsswitch.conf"
|
|
|
|
"/etc/services"
|
|
|
|
"/etc/hosts"))
|
|
|
|
|
2014-09-21 19:40:05 +02:00
|
|
|
(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)))))
|
|
|
|
|
2016-02-12 21:39:26 +01:00
|
|
|
(define (create-environment profile paths pure?)
|
|
|
|
"Set the environment variables specified by PATHS for PROFILE. When PURE?
|
|
|
|
is #t, unset the variables in the current environment. Otherwise, augment
|
|
|
|
existing enviroment variables with additional search paths."
|
2014-09-21 19:40:05 +02:00
|
|
|
(when pure? (purify-environment))
|
2015-05-05 15:02:35 +02: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)))))
|
2016-02-12 21:39:26 +01:00
|
|
|
(evaluate-profile-search-paths profile paths))
|
2015-07-01 14:39:14 +02:00
|
|
|
|
|
|
|
;; 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 19:40:05 +02:00
|
|
|
|
2016-02-12 21:39:26 +01:00
|
|
|
(define (show-search-paths profile search-paths pure?)
|
|
|
|
"Display SEARCH-PATHS applied to PROFILE. When PURE? is #t, do not augment
|
2015-06-30 23:23:06 +02:00
|
|
|
existing environment variables with additional search paths."
|
2015-05-05 15:02:35 +02:00
|
|
|
(for-each (match-lambda
|
|
|
|
((search-path . value)
|
|
|
|
(display
|
|
|
|
(search-path-definition search-path value
|
|
|
|
#:kind (if pure? 'exact 'prefix)))
|
|
|
|
(newline)))
|
2016-02-12 21:39:26 +01:00
|
|
|
(evaluate-profile-search-paths profile search-paths)))
|
|
|
|
|
|
|
|
(define (strip-input-name input)
|
|
|
|
"Remove the name element from the tuple INPUT."
|
|
|
|
(match input
|
|
|
|
((_ package) package)
|
|
|
|
((_ package output)
|
|
|
|
(list package output))))
|
2015-06-30 23:23:06 +02:00
|
|
|
|
2015-06-30 23:16:42 +02:00
|
|
|
(define (package+propagated-inputs package output)
|
|
|
|
"Return the union of PACKAGE's OUTPUT and its transitive propagated inputs."
|
2016-02-12 21:39:26 +01:00
|
|
|
(cons (list package output)
|
|
|
|
(map strip-input-name
|
|
|
|
(package-transitive-propagated-inputs package))))
|
|
|
|
|
|
|
|
(define (package-or-package+output? expr)
|
|
|
|
"Return #t if EXPR is a package or a 2 element list consisting of a package
|
|
|
|
and an output string."
|
|
|
|
(match expr
|
|
|
|
((or (? package?) ; bare package object
|
|
|
|
((? package?) (? string?))) ; package+output tuple
|
|
|
|
#t)
|
|
|
|
(_ #f)))
|
|
|
|
|
|
|
|
(define (package-environment-inputs package)
|
|
|
|
"Return a list of the transitive input packages for PACKAGE."
|
|
|
|
;; Remove non-package inputs such as origin records.
|
|
|
|
(filter package-or-package+output?
|
|
|
|
(map strip-input-name
|
|
|
|
(bag-transitive-inputs
|
|
|
|
(package->bag package)))))
|
2014-09-21 19:40:05 +02:00
|
|
|
|
|
|
|
(define (show-help)
|
2015-10-09 18:11:24 +02:00
|
|
|
(display (_ "Usage: guix environment [OPTION]... PACKAGE... [-- COMMAND...]
|
|
|
|
Build an environment that includes the dependencies of PACKAGE and execute
|
|
|
|
COMMAND or an interactive shell in that environment.\n"))
|
2014-09-21 19:40:05 +02:00
|
|
|
(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"))
|
2015-05-28 14:41:04 +02:00
|
|
|
(display (_ "
|
|
|
|
--ad-hoc include all specified packages in the environment instead
|
|
|
|
of only their inputs"))
|
2014-09-21 19:40:05 +02:00
|
|
|
(display (_ "
|
2014-10-29 23:40:17 +01:00
|
|
|
--pure unset existing environment variables"))
|
2014-09-21 19:40:05 +02:00
|
|
|
(display (_ "
|
2014-10-29 23:40:17 +01:00
|
|
|
--search-paths display needed environment variable definitions"))
|
2015-06-30 17:42:35 +02:00
|
|
|
(display (_ "
|
|
|
|
-s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
|
2015-06-19 14:57:44 +02:00
|
|
|
(display (_ "
|
|
|
|
-C, --container run command within an isolated container"))
|
|
|
|
(display (_ "
|
|
|
|
-N, --network allow containers to access the network"))
|
|
|
|
(display (_ "
|
|
|
|
--share=SPEC for containers, share writable host file system
|
|
|
|
according to SPEC"))
|
|
|
|
(display (_ "
|
|
|
|
--expose=SPEC for containers, expose read-only host file system
|
|
|
|
according to SPEC"))
|
|
|
|
(display (_ "
|
|
|
|
--bootstrap use bootstrap binaries to build the environment"))
|
2014-09-21 19:40:05 +02: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 23:40:17 +01:00
|
|
|
(newline)
|
2014-09-21 19:40:05 +02:00
|
|
|
(show-bug-report-information))
|
|
|
|
|
|
|
|
(define %default-options
|
2015-11-02 00:34:53 +01:00
|
|
|
`((system . ,(%current-system))
|
2014-09-21 19:40:05 +02:00
|
|
|
(substitutes? . #t)
|
2016-03-02 13:43:13 +01:00
|
|
|
(graft? . #t)
|
2014-09-21 19:40:05 +02:00
|
|
|
(max-silent-time . 3600)
|
|
|
|
(verbosity . 0)))
|
|
|
|
|
2015-10-26 03:33:33 +01:00
|
|
|
(define (tag-package-arg opts arg)
|
|
|
|
"Return a two-element list with the form (TAG ARG) that tags ARG with either
|
|
|
|
'ad-hoc' in OPTS has the 'ad-hoc?' key set to #t, or 'inputs' otherwise."
|
|
|
|
;; Normally, the transitive inputs to a package are added to an environment,
|
|
|
|
;; but the ad-hoc? flag changes the meaning of a package argument such that
|
|
|
|
;; the package itself is added to the environment instead.
|
|
|
|
(if (assoc-ref opts 'ad-hoc?)
|
|
|
|
`(ad-hoc-package ,arg)
|
|
|
|
`(package ,arg)))
|
|
|
|
|
2014-09-21 19:40:05 +02:00
|
|
|
(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)))
|
2015-10-09 18:11:24 +02:00
|
|
|
(option '(#\E "exec") #t #f ; deprecated
|
2014-09-21 19:40:05 +02:00
|
|
|
(lambda (opt name arg result)
|
2015-10-09 18:11:24 +02:00
|
|
|
(alist-cons 'exec (list %default-shell "-c" arg) result)))
|
2014-09-21 19:40:05 +02:00
|
|
|
(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)
|
2015-10-26 03:33:33 +01:00
|
|
|
(alist-cons 'load
|
|
|
|
(tag-package-arg result arg)
|
|
|
|
result)))
|
2014-09-21 19:40:05 +02:00
|
|
|
(option '(#\e "expression") #t #f
|
|
|
|
(lambda (opt name arg result)
|
2015-10-26 03:33:33 +01:00
|
|
|
(alist-cons 'expression
|
|
|
|
(tag-package-arg result arg)
|
|
|
|
result)))
|
2015-05-28 14:41:04 +02:00
|
|
|
(option '("ad-hoc") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'ad-hoc? #t result)))
|
2014-09-21 19:40:05 +02:00
|
|
|
(option '(#\n "dry-run") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'dry-run? #t result)))
|
2015-06-30 17:42:35 +02:00
|
|
|
(option '(#\s "system") #t #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'system arg
|
|
|
|
(alist-delete 'system result eq?))))
|
2015-06-19 14:57:44 +02:00
|
|
|
(option '(#\C "container") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'container? #t result)))
|
|
|
|
(option '(#\N "network") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'network? #t result)))
|
|
|
|
(option '("share") #t #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'file-system-mapping
|
|
|
|
(specification->file-system-mapping arg #t)
|
|
|
|
result)))
|
|
|
|
(option '("expose") #t #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'file-system-mapping
|
|
|
|
(specification->file-system-mapping arg #f)
|
|
|
|
result)))
|
|
|
|
(option '("bootstrap") #f #f
|
|
|
|
(lambda (opt name arg result)
|
|
|
|
(alist-cons 'bootstrap? #t result)))
|
2014-09-21 19:40:05 +02: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))
|
|
|
|
|
2015-10-26 03:33:33 +01:00
|
|
|
(define (compact lst)
|
|
|
|
"Remove all #f elements from LST."
|
|
|
|
(filter identity lst))
|
|
|
|
|
2014-09-21 19:40:05 +02:00
|
|
|
(define (options/resolve-packages opts)
|
|
|
|
"Return OPTS with package specification strings replaced by actual
|
|
|
|
packages."
|
2016-02-12 21:39:26 +01:00
|
|
|
(define (package->output package mode)
|
|
|
|
(match package
|
|
|
|
((? package?)
|
|
|
|
(list mode package "out"))
|
|
|
|
(((? package? package) (? string? output))
|
|
|
|
(list mode package output))))
|
2015-10-31 02:02:51 +01:00
|
|
|
|
|
|
|
(define (packages->outputs packages mode)
|
|
|
|
(match packages
|
2016-02-12 21:39:26 +01:00
|
|
|
((? package-or-package+output? package) ; single package
|
|
|
|
(list (package->output package mode)))
|
|
|
|
(((? package-or-package+output?) ...) ; many packages
|
|
|
|
(map (cut package->output <> mode) packages))))
|
2015-10-31 02:02:51 +01:00
|
|
|
|
2015-10-26 03:33:33 +01:00
|
|
|
(compact
|
|
|
|
(append-map (match-lambda
|
|
|
|
(('package mode (? string? spec))
|
|
|
|
(let-values (((package output)
|
|
|
|
(specification->package+output spec)))
|
|
|
|
(list (list mode package output))))
|
|
|
|
(('expression mode str)
|
|
|
|
;; Add all the outputs of the package STR evaluates to.
|
2015-10-31 02:02:51 +01:00
|
|
|
(packages->outputs (read/eval str) mode))
|
2015-10-26 03:33:33 +01:00
|
|
|
(('load mode file)
|
|
|
|
;; Add all the outputs of the package defined in FILE.
|
2015-10-31 02:02:51 +01:00
|
|
|
(let ((module (make-user-module '())))
|
|
|
|
(packages->outputs (load* file module) mode)))
|
2015-10-26 03:33:33 +01:00
|
|
|
(_ '(#f)))
|
|
|
|
opts)))
|
2014-09-21 19:40:05 +02:00
|
|
|
|
2016-02-12 21:39:26 +01:00
|
|
|
(define* (build-environment derivations opts)
|
|
|
|
"Build the DERIVATIONS required by the environment using the build options
|
|
|
|
in OPTS."
|
2014-09-21 19:40:05 +02:00
|
|
|
(let ((substitutes? (assoc-ref opts 'substitutes?))
|
2015-06-30 23:23:06 +02:00
|
|
|
(dry-run? (assoc-ref opts 'dry-run?)))
|
2016-02-12 21:39:26 +01:00
|
|
|
(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))))))
|
|
|
|
|
|
|
|
(define (inputs->profile-derivation inputs system bootstrap?)
|
|
|
|
"Return the derivation for a profile consisting of INPUTS for SYSTEM.
|
|
|
|
BOOTSTRAP? specifies whether to use the bootstrap Guile to build the
|
|
|
|
profile."
|
|
|
|
(profile-derivation (packages->manifest inputs)
|
|
|
|
#:system system
|
|
|
|
#:hooks (if bootstrap?
|
|
|
|
'()
|
|
|
|
%default-profile-hooks)))
|
2014-09-21 19:40:05 +02:00
|
|
|
|
2015-06-19 14:57:44 +02:00
|
|
|
(define requisites* (store-lift requisites))
|
|
|
|
|
|
|
|
(define (inputs->requisites inputs)
|
|
|
|
"Convert INPUTS, a list of input tuples or store path strings, into a set of
|
|
|
|
requisite store items i.e. the union closure of all the inputs."
|
|
|
|
(define (input->requisites input)
|
|
|
|
(requisites*
|
|
|
|
(match input
|
|
|
|
((drv output)
|
|
|
|
(derivation->output-path drv output))
|
|
|
|
((drv)
|
|
|
|
(derivation->output-path drv))
|
|
|
|
((? direct-store-path? path)
|
|
|
|
path))))
|
|
|
|
|
|
|
|
(mlet %store-monad ((reqs (sequence %store-monad
|
|
|
|
(map input->requisites inputs))))
|
|
|
|
(return (delete-duplicates (concatenate reqs)))))
|
|
|
|
|
2015-11-21 22:24:45 +01:00
|
|
|
(define (status->exit-code status)
|
|
|
|
"Compute the exit code made from STATUS, a value as returned by 'waitpid',
|
|
|
|
and suitable for 'exit'."
|
|
|
|
;; See <bits/waitstatus.h>.
|
|
|
|
(or (status:exit-val status)
|
|
|
|
(logior #x80 (status:term-sig status))))
|
|
|
|
|
|
|
|
(define exit/status (compose exit status->exit-code))
|
|
|
|
(define primitive-exit/status (compose primitive-exit status->exit-code))
|
2015-06-19 14:57:44 +02:00
|
|
|
|
|
|
|
(define (launch-environment command inputs paths pure?)
|
|
|
|
"Run COMMAND in a new environment containing INPUTS, using the native search
|
|
|
|
paths defined by the list PATHS. When PURE?, pre-existing environment
|
|
|
|
variables are cleared before setting the new ones."
|
|
|
|
(create-environment inputs paths pure?)
|
|
|
|
(apply system* command))
|
|
|
|
|
|
|
|
(define* (launch-environment/container #:key command bash user-mappings
|
2016-02-12 21:39:26 +01:00
|
|
|
profile paths network?)
|
|
|
|
"Run COMMAND within a container that features the software in PROFILE.
|
|
|
|
Environment variables are set according to PATHS, a list of native search
|
|
|
|
paths. The global shell is BASH, a file name for a GNU Bash binary in the
|
|
|
|
store. When NETWORK?, access to the host system network is permitted.
|
|
|
|
USER-MAPPINGS, a list of file system mappings, contains the user-specified
|
|
|
|
host file systems to mount inside the container."
|
2015-06-19 14:57:44 +02:00
|
|
|
(mlet %store-monad ((reqs (inputs->requisites
|
2016-02-12 21:39:26 +01:00
|
|
|
(list (direct-store-path bash) profile))))
|
2015-06-19 14:57:44 +02:00
|
|
|
(return
|
|
|
|
(let* ((cwd (getcwd))
|
|
|
|
;; Bind-mount all requisite store items, user-specified mappings,
|
|
|
|
;; /bin/sh, the current working directory, and possibly networking
|
|
|
|
;; configuration files within the container.
|
|
|
|
(mappings
|
|
|
|
(append user-mappings
|
|
|
|
;; Current working directory.
|
|
|
|
(list (file-system-mapping
|
|
|
|
(source cwd)
|
|
|
|
(target cwd)
|
|
|
|
(writable? #t)))
|
|
|
|
;; When in Rome, do as Nix build.cc does: Automagically
|
|
|
|
;; map common network configuration files.
|
|
|
|
(if network?
|
|
|
|
(filter-map (lambda (file)
|
|
|
|
(and (file-exists? file)
|
|
|
|
(file-system-mapping
|
|
|
|
(source file)
|
|
|
|
(target file)
|
|
|
|
(writable? #f))))
|
|
|
|
%network-configuration-files)
|
|
|
|
'())
|
|
|
|
;; Mappings for the union closure of all inputs.
|
|
|
|
(map (lambda (dir)
|
|
|
|
(file-system-mapping
|
|
|
|
(source dir)
|
|
|
|
(target dir)
|
|
|
|
(writable? #f)))
|
|
|
|
reqs)))
|
|
|
|
(file-systems (append %container-file-systems
|
|
|
|
(map mapping->file-system mappings))))
|
|
|
|
(exit/status
|
|
|
|
(call-with-container (map file-system->spec file-systems)
|
|
|
|
(lambda ()
|
|
|
|
;; Setup global shell.
|
|
|
|
(mkdir-p "/bin")
|
|
|
|
(symlink bash "/bin/sh")
|
|
|
|
|
|
|
|
;; Setup directory for temporary files.
|
|
|
|
(mkdir-p "/tmp")
|
|
|
|
(for-each (lambda (var)
|
|
|
|
(setenv var "/tmp"))
|
|
|
|
;; The same variables as in Nix's 'build.cc'.
|
|
|
|
'("TMPDIR" "TEMPDIR" "TMP" "TEMP"))
|
|
|
|
|
|
|
|
;; From Nix build.cc:
|
|
|
|
;;
|
|
|
|
;; Set HOME to a non-existing path to prevent certain
|
|
|
|
;; programs from using /etc/passwd (or NIS, or whatever)
|
|
|
|
;; to locate the home directory (for example, wget looks
|
|
|
|
;; for ~/.wgetrc). I.e., these tools use /etc/passwd if
|
|
|
|
;; HOME is not set, but they will just assume that the
|
|
|
|
;; settings file they are looking for does not exist if
|
|
|
|
;; HOME is set but points to some non-existing path.
|
|
|
|
(setenv "HOME" "/homeless-shelter")
|
|
|
|
|
|
|
|
;; For convenience, start in the user's current working
|
|
|
|
;; directory rather than the root directory.
|
|
|
|
(chdir cwd)
|
|
|
|
|
|
|
|
(primitive-exit/status
|
|
|
|
;; A container's environment is already purified, so no need to
|
|
|
|
;; request it be purified again.
|
2016-02-12 21:39:26 +01:00
|
|
|
(launch-environment command profile paths #f)))
|
2015-06-19 14:57:44 +02:00
|
|
|
#:namespaces (if network?
|
|
|
|
(delq 'net %namespaces) ; share host network
|
|
|
|
%namespaces)))))))
|
|
|
|
|
|
|
|
(define (environment-bash container? bootstrap? system)
|
|
|
|
"Return a monadic value in the store monad for the version of GNU Bash
|
|
|
|
needed in the environment for SYSTEM, if any. If CONTAINER? is #f, return #f.
|
|
|
|
If CONTAINER? and BOOTSTRAP?, return the store path for the bootstrap Bash.
|
|
|
|
Otherwise, return the derivation for the Bash package."
|
|
|
|
(with-monad %store-monad
|
|
|
|
(cond
|
|
|
|
((and container? (not bootstrap?))
|
|
|
|
(package->derivation bash))
|
|
|
|
;; Use the bootstrap Bash instead.
|
|
|
|
((and container? bootstrap?)
|
|
|
|
(interned-file
|
|
|
|
(search-bootstrap-binary "bash" system)))
|
|
|
|
(else
|
|
|
|
(return #f)))))
|
|
|
|
|
2015-10-09 18:11:24 +02:00
|
|
|
(define (parse-args args)
|
|
|
|
"Parse the list of command line arguments ARGS."
|
2015-02-25 23:31:51 +01:00
|
|
|
(define (handle-argument arg result)
|
2015-10-26 03:33:33 +01:00
|
|
|
(alist-cons 'package (tag-package-arg result arg) result))
|
2014-09-21 19:40:05 +02:00
|
|
|
|
2015-10-09 18:11:24 +02:00
|
|
|
;; The '--' token is used to separate the command to run from the rest of
|
|
|
|
;; the operands.
|
|
|
|
(let-values (((args command) (split args "--")))
|
|
|
|
(let ((opts (parse-command-line args %options (list %default-options)
|
|
|
|
#:argument-handler handle-argument)))
|
|
|
|
(if (null? command)
|
|
|
|
opts
|
|
|
|
(alist-cons 'exec command opts)))))
|
|
|
|
|
2015-11-03 15:02:10 +01:00
|
|
|
(define (assert-container-features)
|
|
|
|
"Check if containers can be created and exit with an informative error
|
|
|
|
message if any test fails."
|
|
|
|
(unless (user-namespace-supported?)
|
|
|
|
(report-error (_ "cannot create container: user namespaces unavailable\n"))
|
|
|
|
(leave (_ "is your kernel version < 3.10?\n")))
|
|
|
|
|
|
|
|
(unless (unprivileged-user-namespace-supported?)
|
|
|
|
(report-error (_ "cannot create container: unprivileged user cannot create user namespaces\n"))
|
|
|
|
(leave (_ "please set /proc/sys/kernel/unprivileged_userns_clone to \"1\"\n")))
|
|
|
|
|
|
|
|
(unless (setgroups-supported?)
|
|
|
|
(report-error (_ "cannot create container: /proc/self/setgroups does not exist\n"))
|
|
|
|
(leave (_ "is your kernel version < 3.19?\n"))))
|
|
|
|
|
2015-10-09 18:11:24 +02:00
|
|
|
;; Entry point.
|
|
|
|
(define (guix-environment . args)
|
2015-03-16 13:59:59 +01:00
|
|
|
(with-error-handling
|
2015-06-19 14:57:44 +02:00
|
|
|
(let* ((opts (parse-args args))
|
|
|
|
(pure? (assoc-ref opts 'pure))
|
|
|
|
(container? (assoc-ref opts 'container?))
|
|
|
|
(network? (assoc-ref opts 'network?))
|
|
|
|
(bootstrap? (assoc-ref opts 'bootstrap?))
|
|
|
|
(system (assoc-ref opts 'system))
|
2015-11-02 00:34:53 +01:00
|
|
|
(command (or (assoc-ref opts 'exec)
|
|
|
|
;; Spawn a shell if the user didn't specify
|
|
|
|
;; anything in particular.
|
|
|
|
(if container?
|
|
|
|
;; The user's shell is likely not available
|
|
|
|
;; within the container.
|
|
|
|
'("/bin/sh")
|
|
|
|
(list %default-shell))))
|
2015-10-26 03:33:33 +01:00
|
|
|
(packages (options/resolve-packages opts))
|
2015-06-19 14:57:44 +02:00
|
|
|
(mappings (pick-all opts 'file-system-mapping))
|
2015-10-26 03:33:33 +01:00
|
|
|
(inputs (delete-duplicates
|
|
|
|
(append-map (match-lambda
|
|
|
|
(('ad-hoc-package package output)
|
|
|
|
(package+propagated-inputs package
|
|
|
|
output))
|
2016-02-12 21:39:26 +01:00
|
|
|
(('package package _)
|
|
|
|
(package-environment-inputs package)))
|
2015-10-26 03:33:33 +01:00
|
|
|
packages)))
|
2015-06-19 14:57:44 +02:00
|
|
|
(paths (delete-duplicates
|
|
|
|
(cons $PATH
|
|
|
|
(append-map (match-lambda
|
2016-02-12 21:39:26 +01:00
|
|
|
((or ((? package? p) _ ...)
|
|
|
|
(? package? p))
|
|
|
|
(package-native-search-paths p))
|
|
|
|
(_ '()))
|
2015-06-19 14:57:44 +02:00
|
|
|
inputs))
|
|
|
|
eq?)))
|
2015-11-03 15:02:10 +01:00
|
|
|
|
|
|
|
(when container? (assert-container-features))
|
|
|
|
|
2015-06-11 11:09:12 +02:00
|
|
|
(with-store store
|
2016-02-12 21:39:26 +01:00
|
|
|
;; Use the bootstrap Guile when requested.
|
2016-03-02 13:43:13 +01:00
|
|
|
(parameterize ((%graft? (assoc-ref opts 'graft?))
|
|
|
|
(%guile-for-build
|
2016-02-12 21:39:26 +01:00
|
|
|
(package-derivation
|
|
|
|
store
|
|
|
|
(if bootstrap?
|
|
|
|
%bootstrap-guile
|
|
|
|
(canonical-package guile-2.0)))))
|
|
|
|
(set-build-options-from-command-line store opts)
|
|
|
|
(run-with-store store
|
|
|
|
;; Containers need a Bourne shell at /bin/sh.
|
|
|
|
(mlet* %store-monad ((bash (environment-bash container?
|
|
|
|
bootstrap?
|
|
|
|
system))
|
|
|
|
(prof-drv (inputs->profile-derivation
|
|
|
|
inputs system bootstrap?))
|
|
|
|
(profile -> (derivation->output-path prof-drv)))
|
2015-06-19 14:57:44 +02:00
|
|
|
;; First build the inputs. This is necessary even for
|
2016-02-12 21:39:26 +01:00
|
|
|
;; --search-paths. Additionally, we might need to build bash for
|
|
|
|
;; a container.
|
|
|
|
(mbegin %store-monad
|
|
|
|
(build-environment (if (derivation? bash)
|
|
|
|
(list prof-drv bash)
|
|
|
|
(list prof-drv))
|
|
|
|
opts)
|
|
|
|
(cond
|
|
|
|
((assoc-ref opts 'dry-run?)
|
|
|
|
(return #t))
|
|
|
|
((assoc-ref opts 'search-paths)
|
|
|
|
(show-search-paths profile paths pure?)
|
|
|
|
(return #t))
|
|
|
|
(container?
|
|
|
|
(let ((bash-binary
|
|
|
|
(if bootstrap?
|
|
|
|
bash
|
|
|
|
(string-append (derivation->output-path bash)
|
|
|
|
"/bin/sh"))))
|
|
|
|
(launch-environment/container #:command command
|
|
|
|
#:bash bash-binary
|
|
|
|
#:user-mappings mappings
|
|
|
|
#:profile profile
|
|
|
|
#:paths paths
|
|
|
|
#:network? network?)))
|
|
|
|
(else
|
|
|
|
(return
|
|
|
|
(exit/status
|
|
|
|
(launch-environment command profile paths pure?)))))))))))))
|