From patchwork Sat Apr 15 01:44:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antero Mejr X-Patchwork-Id: 49172 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 4514617598; Sat, 15 Apr 2023 02:45:31 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 443E817591 for ; Sat, 15 Apr 2023 02:45:28 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pnUyP-0007kq-QF; Fri, 14 Apr 2023 21:45:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pnUyN-0007kY-U1 for guix-patches@gnu.org; Fri, 14 Apr 2023 21:45:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pnUyM-0002RG-Fd; Fri, 14 Apr 2023 21:45:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pnUyL-0002nD-VS; Fri, 14 Apr 2023 21:45:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#62848] [PATCH] environment: Add --remote option and emacsclient-eshell backend. Resent-From: Antero Mejr Original-Sender: "Debbugs-submit" Resent-CC: ludo@gnu.org, guix-patches@gnu.org Resent-Date: Sat, 15 Apr 2023 01:45:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 62848 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 62848@debbugs.gnu.org Cc: ludo@gnu.org X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: ludo@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.168152308910697 (code B ref -1); Sat, 15 Apr 2023 01:45:01 +0000 Received: (at submit) by debbugs.gnu.org; 15 Apr 2023 01:44:49 +0000 Received: from localhost ([127.0.0.1]:47975 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pnUy8-0002mS-Ss for submit@debbugs.gnu.org; Fri, 14 Apr 2023 21:44:49 -0400 Received: from lists.gnu.org ([209.51.188.17]:43362) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pnUy6-0002mK-La for submit@debbugs.gnu.org; Fri, 14 Apr 2023 21:44:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pnUy6-0007jQ-8E for guix-patches@gnu.org; Fri, 14 Apr 2023 21:44:46 -0400 Received: from mout-p-103.mailbox.org ([2001:67c:2050:0:465::103]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1pnUy3-0002Nn-SQ for guix-patches@gnu.org; Fri, 14 Apr 2023 21:44:45 -0400 Received: from smtp2.mailbox.org (unknown [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4Pyx2D2K26z9sTW for ; Sat, 15 Apr 2023 03:44:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1681523076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Fud33XhSKWquFwTA+snCKPbCHOnxcbM8c4LOw0T+0SU=; b=Zf1U5knKOqI6c4Mj5cRHqXkUESgh9JF9oTigLLoOdwgZXR2utP9MrHdbpWIZfiYd2S3QVc 4/uENfs5yFJIwiNzZ3DOM6jS0JMhJabEDgTipr4Fyb1myP7J0ACVr1bdGAIvoH40vXMeb/ k+bFnD74zYdV98Rr4FS9QLyB/hBbFhQ+hUuVG6jBU13d0GmQT2CnusNJJQQmztC3I3RQzv A1zIuF8eptMfhnrg8XeVOCi8IDz9IhfbFhOHcVDLWyHyV8Ma4m7+Uz5lQv36QhYGEzVqgB A+J8L67cCRhqpqp5SVkE6fMhwL1asdJROGX4j2T74FRzReXYEs6I2NKxDFxyYQ== Date: Sat, 15 Apr 2023 01:44:30 +0000 Message-Id: <20230415014430.15466-1-antero@mailbox.org> MIME-Version: 1.0 X-MBO-RS-ID: 0565ed057fd7a0bc191 X-MBO-RS-META: mc1tk1zd54a5s58i1r1j3c6awn5cpibg Received-SPF: pass client-ip=2001:67c:2050:0:465::103; envelope-from=antero@mailbox.org; helo=mout-p-103.mailbox.org X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Antero Mejr X-ACL-Warn: , Antero Mejr via Guix-patches X-Patchwork-Original-From: Antero Mejr via Guix-patches via From: Antero Mejr Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/scripts/environment.scm (launch-environment/eshell): New procedure. (guix-environment*): Add logic for remote backend switching. (%options): Add --remote and --list-remote-backends options. (show-environment-options-help): Add help text for new options. * guix/profiles.scm (load-profile)[getenv-proc, setenv-proc, unsetenv-proc]: New optional keyword arguments. (purify-environment)[unsetenv-proc]: New argument. * doc/guix.texi(Invoking guix shell): Document --remote and --list-remote-backends options. --- "guix shell" doesn't support eshell due to eshell's quirks. This change makes environments/profiles more network transparent to support eshell, and maybe other remote environments in the future. doc/guix.texi | 17 ++++++++ guix/profiles.scm | 30 ++++++++------ guix/scripts/environment.scm | 80 ++++++++++++++++++++++++++++++++++-- 3 files changed, 112 insertions(+), 15 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index adb1975935..f609e0c9b6 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -6245,6 +6245,23 @@ environment. @itemx -s @var{system} Attempt to build for @var{system}---e.g., @code{i686-linux}. +@item --remote=@var{backend}[=@var{args}] +Create an environment over a remote connection using @var{backend}, +optionally passing @var{args} to the backend. + +This option causes the @option{--container} option to be ignored. + +When @var{backend} is @code{emacsclient-eshell}, a new eshell buffer +with the Guix environment will be opened. An Emacs server must already +be running, and the @code{emacsclient} program must be available. Due +to the way @code{eshell} handles commands, the @var{command} argument, +if specified, will run in the initial @code{eshell} environment instead +of the Guix @code{eshell} environment. + +@item --list-remote-backends +Display the @var{backend} options for @code{guix shell --remote=BACKEND} +and exit. + @item --container @itemx -C @cindex container diff --git a/guix/profiles.scm b/guix/profiles.scm index 03333785f9..1bf6783eea 100644 --- a/guix/profiles.scm +++ b/guix/profiles.scm @@ -2062,10 +2062,10 @@ (define %precious-variables ;; Environment variables in the default 'load-profile' white list. '("HOME" "USER" "LOGNAME" "DISPLAY" "XAUTHORITY" "TERM" "TZ" "PAGER")) -(define (purify-environment white-list white-list-regexps) +(define (purify-environment white-list white-list-regexps unsetenv-proc) "Unset all environment variables except those that match the regexps in WHITE-LIST-REGEXPS and those listed in WHITE-LIST." - (for-each unsetenv + (for-each unsetenv-proc (remove (lambda (variable) (or (member variable white-list) (find (cut regexp-exec <> variable) @@ -2077,23 +2077,29 @@ (define (purify-environment white-list white-list-regexps) (define* (load-profile profile #:optional (manifest (profile-manifest profile)) #:key pure? (white-list-regexps '()) - (white-list %precious-variables)) + (white-list %precious-variables) + (getenv-proc getenv) (setenv-proc setenv) + (unsetenv-proc unsetenv)) "Set the environment variables specified by MANIFEST for PROFILE. When PURE? is #t, unset the variables in the current environment except those that match the regexps in WHITE-LIST-REGEXPS and those listed in WHITE-LIST. Otherwise, augment existing environment variables with additional search -paths." +paths. +GETENV-PROC is a one-argument procedure that returns an env var value. +SETENV-PROC is a two-argument procedure the sets environment variables. +UNSETENV-PROC is a one-argument procedure that unsets environment variables. +Change those procedures to load a profile over a remote connection." (when pure? - (purify-environment white-list white-list-regexps)) + (purify-environment white-list white-list-regexps unsetenv-proc)) (for-each (match-lambda ((($ variable _ separator) . value) - (let ((current (getenv variable))) - (setenv variable - (if (and current (not pure?)) - (if separator - (string-append value separator current) - value) - value))))) + (let ((current (getenv-proc variable))) + (setenv-proc variable + (if (and current (not pure?)) + (if separator + (string-append value separator current) + value) + value))))) (profile-search-paths profile manifest))) (define (profile-regexp profile) diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm index ebfc05731c..7e67cf1d1d 100644 --- a/guix/scripts/environment.scm +++ b/guix/scripts/environment.scm @@ -53,6 +53,7 @@ (define-module (guix scripts environment) #:autoload (gnu packages bootstrap) (bootstrap-executable %bootstrap-guile) #:autoload (gnu packages package-management) (guix) #:use-module (ice-9 match) + #:use-module (ice-9 popen) #:autoload (ice-9 rdelim) (read-line) #:use-module (ice-9 vlist) #:autoload (web uri) (string->uri uri-scheme) @@ -104,6 +105,13 @@ (define (show-environment-options-help) (display (G_ " -r, --root=FILE make FILE a symlink to the result, and register it as a garbage collector root")) + (display (G_ " + --remote=BACKEND[=ARGS] + create environment over a remote connection by + passing ARGS to BACKEND")) + (display (G_ " + --list-remote-backends + list available remote backends and exit")) (display (G_ " -C, --container run command within an isolated container")) (display (G_ " @@ -283,6 +291,13 @@ (define %options (option '("bootstrap") #f #f (lambda (opt name arg result) (alist-cons 'bootstrap? #t result))) + (option '("remote") #t #f + (lambda (opt name arg result) + (alist-cons 'remote arg result))) + (option '("list-remote-backends") #f #f + (lambda args + (display "emacsclient-eshell\n") + (exit 0))) (append %transformation-options %standard-build-options @@ -715,6 +730,50 @@ (define* (launch-environment/fork command profile manifest ((_ . status) status))))) +(define* (launch-environment/eshell args command profile manifest + #:key pure? (white-list '())) + "Create an new eshell buffer with an environment containing PROFILE, +with the search paths specified by MANIFEST. When PURE?, pre-existing +environment variables are cleared before setting the new ones, except those +matching the regexps in WHITE-LIST." + + (define (escape in) + (string-append "\"" (string-replace-substring in "\"" "\\\"") "\"")) + + (define* (ec cmd #:optional (check? #f)) + (let* ((cmd (string-append "emacsclient " args " -e " (escape cmd))) + (port (open-input-pipe cmd)) + (str (read-line port)) + (code (status:exit-val (close-pipe port)))) + (if (and check? (or (not (eqv? code 0)) (eof-object? str))) + (leave + (G_ "Emacs server connection failed. Is the server running?~%"))) + str)) + + (let ((buf (ec "(buffer-name (eshell t))" #t))) + (define (ec-buf cmd) + (ec (string-append "(with-current-buffer " buf " " cmd ")"))) + + (load-profile + profile manifest #:pure? pure? #:white-list-regexps white-list + #:setenv-proc (lambda (var val) + (ec-buf + (if (string=? var "PATH") + ;; TODO: TRAMP support? + (string-append "(eshell-set-path " (escape val) ")") + (string-append "(setenv " (escape var) " " + (escape val) ")")))) + #:unsetenv-proc (lambda (var) + (ec-buf + (string-append "(setenv " (escape var) ")")))) + + (match command + ((program . args) + (ec-buf + (string-append + "(eshell-command " + (escape (string-append program " " (string-join args)))")")))))) + (define* (launch-environment/container #:key command bash user user-mappings profile manifest link-profile? network? map-cwd? emulate-fhs? nesting? @@ -1081,7 +1140,10 @@ (define (guix-environment* opts) '("/bin/sh") (list %default-shell)))) (mappings (pick-all opts 'file-system-mapping)) - (white-list (pick-all opts 'inherit-regexp))) + (white-list (pick-all opts 'inherit-regexp)) + (remote (match (string-split (assoc-ref opts 'remote) #\=) + ((x) (cons x "")) + ((x . y) (cons x (string-join y)))))) (define store-needed? ;; Whether connecting to the daemon is needed. @@ -1119,6 +1181,10 @@ (define-syntax-rule (with-store/maybe store exp ...) (when (pair? symlinks) (leave (G_ "'--symlink' cannot be used without '--container~%'")))) + (when (and remote (not (member (car remote) '("emacsclient-eshell")))) + (leave + (G_ "Invalid remote backend, see --list-remote-backends for options.~%'"))) + (with-store/maybe store (with-status-verbosity (assoc-ref opts 'verbosity) (define manifest-from-opts @@ -1172,15 +1238,23 @@ (define manifest (mwhen (assoc-ref opts 'check?) (return - (if container? + (if (or container? remote) (warning (G_ "'--check' is unnecessary \ -when using '--container'; doing nothing~%")) +when using '--container' or '--remote'; doing nothing~%")) (validate-child-shell-environment profile manifest)))) (cond ((assoc-ref opts 'search-paths) (show-search-paths profile manifest #:pure? pure?) (return #t)) + (remote + (match (car remote) + ("emacsclient-eshell" + (return + (launch-environment/eshell (cdr remote) + command profile manifest + #:white-list white-list + #:pure? pure?))))) (container? (let ((bash-binary (if bootstrap?