From patchwork Fri Apr 19 14:04:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 8361 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DB12016ECD; Fri, 19 Apr 2019 15:11:19 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.0 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id 469CB16EC9 for ; Fri, 19 Apr 2019 15:11:19 +0100 (BST) Received: from localhost ([127.0.0.1]:57107 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hHUEY-0001Ql-Rd for patchwork@mira.cbaines.net; Fri, 19 Apr 2019 10:11:18 -0400 Received: from eggs.gnu.org ([209.51.188.92]:56090) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hHU8d-0004eR-Kc for guix-patches@gnu.org; Fri, 19 Apr 2019 10:05:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hHU8b-0005yr-Hq for guix-patches@gnu.org; Fri, 19 Apr 2019 10:05:10 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:59113) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hHU8X-0005q6-VG for guix-patches@gnu.org; Fri, 19 Apr 2019 10:05:06 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hHU8X-0002SW-NR for guix-patches@gnu.org; Fri, 19 Apr 2019 10:05:05 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#34638] [PATCH v2 4/4] inferior: Add 'open-inferior/container'. Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 19 Apr 2019 14:05:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 34638 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 34638@debbugs.gnu.org Received: via spool by 34638-submit@debbugs.gnu.org id=B34638.15556826729387 (code B ref 34638); Fri, 19 Apr 2019 14:05:05 +0000 Received: (at 34638) by debbugs.gnu.org; 19 Apr 2019 14:04:32 +0000 Received: from localhost ([127.0.0.1]:44420 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hHU80-0002RF-3N for submit@debbugs.gnu.org; Fri, 19 Apr 2019 10:04:32 -0400 Received: from mira.cbaines.net ([212.71.252.8]:48054) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hHU7x-0002QV-2Y for 34638@debbugs.gnu.org; Fri, 19 Apr 2019 10:04:30 -0400 Received: from localhost (71.38.6.51.dyn.plus.net [51.6.38.71]) by mira.cbaines.net (Postfix) with ESMTPSA id DC9EF16ED3 for <34638@debbugs.gnu.org>; Fri, 19 Apr 2019 15:04:27 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 12551412 for <34638@debbugs.gnu.org>; Fri, 19 Apr 2019 14:04:27 +0000 (UTC) From: Christopher Baines Date: Fri, 19 Apr 2019 15:04:27 +0100 Message-Id: <20190419140427.15183-4-mail@cbaines.net> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190419140427.15183-1-mail@cbaines.net> References: <20190419140427.15183-1-mail@cbaines.net> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches --- guix/inferior.scm | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/guix/inferior.scm b/guix/inferior.scm index 6d18ab90e9..8238c7fb38 100644 --- a/guix/inferior.scm +++ b/guix/inferior.scm @@ -19,6 +19,7 @@ (define-module (guix inferior) #:use-module (srfi srfi-9) #:use-module (srfi srfi-9 gnu) + #:use-module (srfi srfi-11) #:use-module ((guix utils) #:select (%current-system source-properties->location @@ -40,6 +41,9 @@ #:use-module (guix store) #:use-module (guix derivations) #:use-module (guix base32) + #:use-module (gnu system file-systems) + #:use-module (gnu build linux-container) + #:use-module (guix build syscalls) #:use-module (gcrypt hash) #:autoload (guix cache) (maybe-remove-expired-cache-entries) #:autoload (guix ui) (show-what-to-build*) @@ -54,6 +58,7 @@ #:use-module ((rnrs bytevectors) #:select (string->utf8)) #:export (inferior? open-inferior + open-inferior/container port->inferior close-inferior inferior-eval @@ -137,6 +142,77 @@ it's an old Guix." ((@ (guix scripts repl) machine-repl)))))) pipe))) +(define* (open-inferior/container store guix-store-item + #:key + (command "bin/guix") + (share-host-network? #f) + (extra-shared-directories '()) + (extra-environment-variables '())) + (define requisite-store-items + (requisites store (list guix-store-item))) + + (define shared-directory + (mkdtemp! (string-append (or (getenv "TMPDIR") "/tmp") + "/guix-inferior.XXXXXX"))) + + (define mappings + (append + (map (lambda (dir) + (file-system-mapping + (source dir) + (target dir) + (writable? #f))) + `(;; Share a directory, used in inferior-eval-with-store + ,shared-directory + ,@requisite-store-items + ,@extra-shared-directories)) + (if share-host-network? + %network-file-mappings + '()))) + + (define mounts + (append %container-file-systems + (map file-system-mapping->bind-mount + mappings))) + + (define (inferior-pipe/container store + guix-store-item + shared-directory + command) + (start-child-in-container + (list (string-append guix-store-item "/bin/guix") + ;; TODO I'm not sure why "repl" is duplicated in the following + ;; command + "repl" "repl" "-t" "machine") + #:read? #t + #:write? #t + #:mounts mounts + #:namespaces (if share-host-network? + (delq 'net %namespaces) + %namespaces) + #:extra-environment-variables + `(;; Set HOME, so that the (guix profiles) module can be loaded, without it + ;; trying to read from /etc/passwd + "HOME=/tmp" + ,@extra-environment-variables))) + + (let*-values + (((pipe pid) + (inferior-pipe/container store + guix-store-item + shared-directory + command)) + ((close-inferior-pipe) + (lambda (pipe*) + (unless (eq? pipe pipe*) + (error "wrong pipe being closed")) + (close-port pipe) + (cdr (waitpid pid))))) + + (port->inferior pipe + shared-directory + close-inferior-pipe))) + (define* (port->inferior pipe shared-directory #:optional (close close-port)) "Given PIPE, an input/output port, return an inferior that talks over PIPE. PIPE is closed with CLOSE when 'close-inferior' is called on the returned