Package: guix-patches;
Reported by: Antero Mejr <antero <at> mailbox.org>
Date: Sat, 15 Apr 2023 01:45:01 UTC
Severity: normal
Tags: moreinfo, patch
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
From: Antero Mejr <antero <at> mailbox.org> To: guix-patches <at> gnu.org Subject: [PATCH] environment: Add --remote option and emacsclient-eshell backend. Date: Sat, 15 Apr 2023 01:44:30 +0000
* 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 ((($ <search-path-specification> 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? -- 2.39.2
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.