unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 63048@debbugs.gnu.org
Cc: "Ludovic Courtès" <ludovic.courtes@inria.fr>,
	"Ekaitz Zarraga" <ekaitz@elenq.tech>
Subject: bug#63048: [PATCH] home: services: bash: Properly quote shell aliases.
Date: Thu,  6 Jul 2023 17:01:54 +0200	[thread overview]
Message-ID: <ccd45825fa1739631584cb6a357bc7782217c8aa.1688655471.git.ludo@gnu.org> (raw)
In-Reply-To: <rG1_O28tWZDkb02eKEC4gH9wvuQg9BWh8rKKUmlNt6wNibGsE_WeBtjyoJ5oaLxpSJSnONPWnFPjn8pVZgWOKfy614I8hzZO6Z-7oMSL0-I=@elenq.tech>

From: Ludovic Courtès <ludovic.courtes@inria.fr>

Fixes <https://issues.guix.gnu.org/63048>.
Reported by Ekaitz Zarraga <ekaitz@elenq.tech>.

* gnu/home/services.scm (with-shell-quotation-bindings): New procedure.
(environment-variable-shell-definitions): Use it instead of inline copy.
* gnu/home/services/shells.scm (bash-serialize-aliases): Use it.  Add
clause for 'literal-string?'.
* tests/guix-home.sh: Add 'aliases' to 'home-bash-extension' and test it.
---
 gnu/home/services.scm        | 53 ++++++++++++++++++++----------------
 gnu/home/services/shells.scm | 28 ++++++++++++-------
 tests/guix-home.sh           |  7 ++++-
 3 files changed, 54 insertions(+), 34 deletions(-)

Hi!

Here's a patch that fixes it.  AFAICS 'guix home import' does the right
thing, so this is really just fixing the way aliases are serialized.

Let me know what you think!

Ludo'.

diff --git a/gnu/home/services.scm b/gnu/home/services.scm
index b17a34d19d..042eba4780 100644
--- a/gnu/home/services.scm
+++ b/gnu/home/services.scm
@@ -53,6 +53,7 @@ (define-module (gnu home services)
             literal-string?
             literal-string-value
 
+            with-shell-quotation-bindings
             environment-variable-shell-definitions
             home-files-directory
             xdg-configuration-files-directory
@@ -183,11 +184,10 @@ (define-record-type <literal-string>
   literal-string?
   (str literal-string-value))
 
-(define (environment-variable-shell-definitions variables)
-  "Return a gexp that evaluates to a list of POSIX shell statements defining
-VARIABLES, a list of environment variable name/value pairs.  The returned code
-ensures variable values are properly quoted."
-  #~(let* ((quote-string
+(define (with-shell-quotation-bindings exp)
+  "Insert EXP, a gexp, in a lexical environment providing the
+'shell-single-quote' and 'shell-double-quote' bindings."
+#~(let* ((quote-string
             (lambda (value quoted-chars)
               (list->string (string-fold-right
                              (lambda (chr lst)
@@ -206,24 +206,31 @@ (define (environment-variable-shell-definitions variables)
               ;; Single-quote VALUE to enter a literal string.
               (string-append "'" (quote-string value '(#\'))
                              "'"))))
-      (string-append
-       #$@(map (match-lambda
-                 ((key . #f)
-                  "")
-                 ((key . #t)
-                  #~(string-append "export " #$key "\n"))
-                 ((key . (or (? string? value)
-                             (? file-like? value)
-                             (? gexp? value)))
-                  #~(string-append "export " #$key "="
-                                   (shell-double-quote #$value)
-                                   "\n"))
-                 ((key . (? literal-string? value))
-                  #~(string-append "export " #$key "="
-                                   (shell-single-quote
-                                    #$(literal-string-value value))
-                                   "\n")))
-               variables))))
+      #$exp))
+
+(define (environment-variable-shell-definitions variables)
+  "Return a gexp that evaluates to a list of POSIX shell statements defining
+VARIABLES, a list of environment variable name/value pairs.  The returned code
+ensures variable values are properly quoted."
+  (with-shell-quotation-bindings
+   #~(string-append
+      #$@(map (match-lambda
+                ((key . #f)
+                 "")
+                ((key . #t)
+                 #~(string-append "export " #$key "\n"))
+                ((key . (or (? string? value)
+                            (? file-like? value)
+                            (? gexp? value)))
+                 #~(string-append "export " #$key "="
+                                  (shell-double-quote #$value)
+                                  "\n"))
+                ((key . (? literal-string? value))
+                 #~(string-append "export " #$key "="
+                                  (shell-single-quote
+                                   #$(literal-string-value value))
+                                  "\n")))
+              variables))))
 
 (define (environment-variables->setup-environment-script vars)
   "Return a file that can be sourced by a POSIX compliant shell which
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index 415b5470c5..7960590e7c 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -313,16 +313,24 @@ (define home-zsh-service-type
 ;;;
 
 (define (bash-serialize-aliases field-name val)
-  #~(string-append
-     #$@(map
-         (match-lambda
-           ((key . #f)
-            "")
-           ((key . #t)
-            #~(string-append "alias " #$key "\n"))
-           ((key . value)
-            #~(string-append "alias " #$key "=\"" #$value "\"\n")))
-         val)))
+  (with-shell-quotation-bindings
+   #~(string-append
+      #$@(map
+          (match-lambda
+            ((key . #f)
+             "")
+            ((key . #t)
+             #~(string-append "alias " #$key "\n"))
+            ((key . (? literal-string? value))
+             #~(string-append "alias " #$key "="
+                              (shell-single-quote
+                               #$(literal-string-value value))
+                              "\n"))
+            ((key . value)
+             #~(string-append "alias " #$key "="
+                              (shell-double-quote #$value)
+                              "\n")))
+          val))))
 
 (define-configuration home-bash-configuration
   (package
diff --git a/tests/guix-home.sh b/tests/guix-home.sh
index e9ef76c862..e6d16d7fab 100644
--- a/tests/guix-home.sh
+++ b/tests/guix-home.sh
@@ -1,7 +1,7 @@
 # GNU Guix --- Functional package management for GNU
 # Copyright © 2021-2023 Andrew Tropin <andrew@trop.in>
 # Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
-# Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
+# Copyright © 2022, 2023 Ludovic Courtès <ludo@gnu.org>
 #
 # This file is part of GNU Guix.
 #
@@ -94,6 +94,9 @@ trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
                    (home-bash-extension
                     (environment-variables
                       '(("PS1" . "$GUIX_ENVIRONMENT λ ")))
+                    (aliases
+                      `(("run" . "guix shell")
+                        ("path" . ,(literal-string "echo $PATH"))))
                     (bashrc
                      (list
                       (plain-file
@@ -149,6 +152,8 @@ EOF
     test -d "${HOME}/.guix-home"
     test -h "${HOME}/.bash_profile"
     test -h "${HOME}/.bashrc"
+    grep 'alias run="guix shell"' "$HOME/.bashrc"
+    grep "alias path='echo \$PATH'" "$HOME/.bashrc"
     test "$(tail -n 2 "${HOME}/.bashrc")" == "\
 # dot-bashrc test file for guix home
 # the content of bashrc-test-config.sh"

base-commit: 2426e51688d479042ea115a634c6be2d8b9f3b99
-- 
2.40.1





  reply	other threads:[~2023-07-06 15:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-24  7:57 bug#63048: [Home] Shell alias values are not properly quoted Ludovic Courtès
2023-04-24 10:10 ` Ekaitz Zarraga
2023-04-24 17:41   ` Ekaitz Zarraga
2023-07-06 15:01     ` Ludovic Courtès [this message]
2023-07-11 15:02       ` Ludovic Courtès

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=ccd45825fa1739631584cb6a357bc7782217c8aa.1688655471.git.ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=63048@debbugs.gnu.org \
    --cc=ekaitz@elenq.tech \
    --cc=ludovic.courtes@inria.fr \
    /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).