unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: David Thompson <dthompson2@worcester.edu>
To: guix-devel@gnu.org
Cc: David Thompson <davet@gnu.org>
Subject: [PATCH 14/15] scripts: environment: Add --container option.
Date: Mon,  6 Jul 2015 09:16:43 -0400	[thread overview]
Message-ID: <1436188604-2813-14-git-send-email-dthompson2@worcester.edu> (raw)
In-Reply-To: <1436188604-2813-1-git-send-email-dthompson2@worcester.edu>

From: David Thompson <davet@gnu.org>

* guix/scripts/enviroment.scm (show-help): Show help for new option.
  (%options): Add --container option.
  (launch-environment, launch-environment/container): New procedures.
  (guix-environment): Spawn new process in a container when requested.
* doc/guix.texi (Invoking guix environment): Document it.
---
 doc/guix.texi                | 16 +++++++++++++++
 guix/scripts/environment.scm | 48 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index d24f97e..57fc446 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -4191,6 +4191,15 @@ NumPy:
 guix environment --ad-hoc python2-numpy python-2.7 -E python
 @end example
 
+Sometimes it is desirable to isolate the environment as much as
+possible, for maximal purity and reproducibility.  For example, the
+following command spawns a Guile REPL in a ``container'' where only the
+store and the current working directory are mounted:
+
+@example
+guix environment --ad-hoc --container guile --exec=guile
+@end example
+
 The available options are summarized below.
 
 @table @code
@@ -4256,6 +4265,13 @@ environment.
 @item --system=@var{system}
 @itemx -s @var{system}
 Attempt to build for @var{system}---e.g., @code{i686-linux}.
+
+@item --container
+@itemx -C
+Run command within an isolated container.  The current working directory
+outside the container is mapped to @file{/env} inside the container.
+Additionally, the spawned process runs as the current user outside the
+container, but has root privileges in the context of the container.
 @end table
 
 It also supports all of the common build options that @command{guix
diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
index ecdbc7a..41d1554 100644
--- a/guix/scripts/environment.scm
+++ b/guix/scripts/environment.scm
@@ -28,6 +28,9 @@
   #:use-module (guix monads)
   #:use-module ((guix gexp) #:select (lower-inputs))
   #:use-module (guix scripts build)
+  #:use-module (gnu build linux-container)
+  #:use-module (gnu system linux-container)
+  #:use-module (gnu system file-systems)
   #:use-module (gnu packages)
   #:use-module (ice-9 format)
   #:use-module (ice-9 match)
@@ -122,6 +125,8 @@ shell command in that environment.\n"))
       --search-paths     display needed environment variable definitions"))
   (display (_ "
   -s, --system=SYSTEM    attempt to build for SYSTEM--e.g., \"i686-linux\""))
+  (display (_ "
+  -C, --container        run command within an isolated container"))
   (newline)
   (show-build-options-help)
   (newline)
@@ -174,6 +179,9 @@ shell command in that environment.\n"))
                  (lambda (opt name arg result)
                    (alist-cons 'system arg
                                (alist-delete 'system result eq?))))
+         (option '(#\C "container") #f #f
+                 (lambda (opt name arg result)
+                   (alist-cons 'container? #t result)))
          %standard-build-options))
 
 (define (pick-all alist key)
@@ -229,6 +237,38 @@ OUTPUT) tuples, using the build options in OPTS."
                (built-derivations derivations)
                (return derivations))))))))
 
+(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?)
+  (system command))
+
+(define (launch-environment/container command inputs paths)
+  "Run COMMAND within a Linux container that includes INPUTS and the
+environment variables defined by PATHS, a list of native search paths."
+  ;; Bind-mount the store and the current working directory within the
+  ;; container.
+  (let* ((mappings
+          (list (file-system-mapping
+                 (source (%store-prefix))
+                 (target (%store-prefix))
+                 (writable? #f))
+                (file-system-mapping
+                 (source (getcwd))
+                 (target "/env")
+                 (writable? #t))))
+         (file-systems
+          (append %container-file-systems
+                  (map mapping->file-system mappings)))
+         (status
+          (call-with-container (map file-system->spec file-systems)
+            (lambda ()
+              (chdir "/env")
+              ;; A container's environment is already purified.
+              (launch-environment command inputs paths #f)))))
+    (status:exit-val status)))
+
 ;; Entry point.
 (define (guix-environment . args)
   (define (handle-argument arg result)
@@ -238,6 +278,7 @@ OUTPUT) tuples, using the build options in OPTS."
     (let* ((opts     (parse-command-line args %options (list %default-options)
                                          #:argument-handler handle-argument))
            (pure?    (assoc-ref opts 'pure))
+           (container? (assoc-ref opts 'container?))
            (ad-hoc?  (assoc-ref opts 'ad-hoc?))
            (command  (assoc-ref opts 'exec))
            (packages (pick-all (options/resolve-packages opts) 'package))
@@ -279,6 +320,9 @@ OUTPUT) tuples, using the build options in OPTS."
                     ((assoc-ref opts 'search-paths)
                      (show-search-paths inputs paths pure?)
                      (return #t))
+                    (container?
+                     (return
+                      (launch-environment/container command inputs paths)))
                     (else
-                     (create-environment inputs paths pure?)
-                     (return (exit (status:exit-val (system command)))))))))))))
+                     (return
+                      (launch-environment command inputs paths pure?)))))))))))
-- 
2.4.3

  parent reply	other threads:[~2015-07-06 13:17 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-06 13:16 [PATCH 01/15] build: syscalls: Add additional mount flags David Thompson
2015-07-06 13:16 ` [PATCH 02/15] build: syscalls: Add unmount flags David Thompson
2015-07-07 14:50   ` Ludovic Courtès
2015-07-07 22:44     ` Thompson, David
2015-07-06 13:16 ` [PATCH 03/15] build: syscalls: Add mkdtemp! David Thompson
2015-07-07 13:15   ` Ludovic Courtès
2015-07-07 22:52     ` Thompson, David
2015-07-06 13:16 ` [PATCH 04/15] utils: Add call-with-temporary-directory David Thompson
2015-07-07 13:15   ` Ludovic Courtès
2015-07-07 22:54     ` Thompson, David
2015-07-06 13:16 ` [PATCH 05/15] build: syscalls: Add clone syscall wrapper David Thompson
2015-07-07 13:23   ` Ludovic Courtès
2015-07-08  0:28     ` Thompson, David
2015-07-11 10:18       ` Ludovic Courtès
2015-07-06 13:16 ` [PATCH 06/15] build: syscalls: Add setns " David Thompson
2015-07-07 13:28   ` Ludovic Courtès
2015-07-08  0:57     ` Thompson, David
2015-07-06 13:16 ` [PATCH 07/15] build: syscalls: Add pivot-root David Thompson
2015-07-07 13:35   ` Ludovic Courtès
2015-07-08  1:18     ` Thompson, David
2015-07-08 12:47       ` Ludovic Courtès
2015-07-06 13:16 ` [PATCH 08/15] gnu: build: Add Linux container module David Thompson
2015-07-07 13:51   ` Ludovic Courtès
2015-07-08 12:38     ` Thompson, David
2015-07-08 21:57       ` Ludovic Courtès
2015-07-09 12:56         ` Thompson, David
2015-07-06 13:16 ` [PATCH 09/15] gnu: system: Move <file-system-mapping> into (gnu system file-systems) David Thompson
2015-07-07 13:51   ` Ludovic Courtès
2015-07-08  1:21     ` Thompson, David
2015-07-06 13:16 ` [PATCH 10/15] gnu: system: Move file-system->spec to " David Thompson
2015-07-07 13:51   ` Ludovic Courtès
2015-07-08  1:22     ` Thompson, David
2015-07-06 13:16 ` [PATCH 11/15] gnu: system: Add Linux container module David Thompson
2015-07-07 13:55   ` Ludovic Courtès
2015-07-09 13:00     ` Thompson, David
2015-07-10 17:57       ` Ludovic Courtès
2015-07-06 13:16 ` [PATCH 12/15] gnu: system: Add Linux container file systems David Thompson
2015-07-07 13:56   ` Ludovic Courtès
2015-07-09 12:56     ` Thompson, David
2015-07-06 13:16 ` [PATCH 13/15] scripts: system: Add 'container' action David Thompson
2015-07-07 14:05   ` Ludovic Courtès
2015-10-27  0:24     ` Thompson, David
2015-10-27 17:41       ` Ludovic Courtès
2015-10-30 17:28         ` Thompson, David
2015-07-06 13:16 ` David Thompson [this message]
2015-07-07 14:35   ` [PATCH 14/15] scripts: environment: Add --container option Ludovic Courtès
2015-07-09 13:16     ` Thompson, David
2015-07-10 18:03       ` Ludovic Courtès
2015-09-05 23:45     ` Thompson, David
2015-09-11 12:39       ` Ludovic Courtès
2015-10-10 21:11         ` Thompson, David
2015-10-11 19:34           ` Ludovic Courtès
2015-10-17 10:05             ` Ludovic Courtès
2015-10-22  1:23               ` Thompson, David
2015-10-25 21:38                 ` Ludovic Courtès
2015-10-26  0:35                   ` Thompson, David
2015-10-27 10:13                     ` Ludovic Courtès
2015-10-31  1:25                       ` Thompson, David
2015-10-31 10:28                         ` Ludovic Courtès
2015-07-06 13:16 ` [PATCH 15/15] scripts: Add 'container' subcommand David Thompson
2015-07-07 14:50   ` Ludovic Courtès
2015-10-27  0:31     ` Thompson, David
2015-10-27 17:46       ` Ludovic Courtès
2015-07-07 13:14 ` [PATCH 01/15] build: syscalls: Add additional mount flags Ludovic Courtès
2015-07-07 22:42   ` Thompson, David

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1436188604-2813-14-git-send-email-dthompson2@worcester.edu \
    --to=dthompson2@worcester.edu \
    --cc=davet@gnu.org \
    --cc=guix-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).