GNU bug report logs - #51543
[PATCH 0/2] Some improvements to the Bash home service

Previous Next

Package: guix-patches;

Reported by: Xinglu Chen <public <at> yoctocell.xyz>

Date: Mon, 1 Nov 2021 09:44:01 UTC

Severity: normal

Tags: patch

Done: Ludovic Courtès <ludo <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 51543 in the body.
You can then email your comments to 51543 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Mon, 01 Nov 2021 09:44:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Xinglu Chen <public <at> yoctocell.xyz>:
New bug report received and forwarded. Copy sent to guix-patches <at> gnu.org. (Mon, 01 Nov 2021 09:44:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: guix-patches <at> gnu.org
Subject: [PATCH 0/2] Some improvements to the Bash home service
Date: Mon, 01 Nov 2021 10:43:18 +0100
[Message part 1 (text/plain, inline)]
This series adds an ‘aliases’ field to the Bash home service, and
documents the ‘home-bash-extension’ configuration record in the manual.

Xinglu Chen (2):
  home: services: bash: Add ‘aliases’ field.
  doc: Document ‘home-bash-extension’ configuration record.

 doc/guix.texi                | 38 ++++++++++++++
 gnu/home/services/shells.scm | 99 ++++++++++++++++++++++++++----------
 guix/scripts/home/import.scm | 24 +++++++++
 3 files changed, 134 insertions(+), 27 deletions(-)


base-commit: 1a80b8909a521b91d30649a011b0257d0fadc18c
-- 
2.33.0



[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Mon, 01 Nov 2021 09:46:01 GMT) Full text and rfc822 format available.

Message #8 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Subject: [PATCH 1/2] home: services: bash: Add ‘aliases’ field.
Date: Mon, 01 Nov 2021 10:45:32 +0100
* doc/guix.texi (Shells Home Services): Document it.
* gnu/home/services/shells.scm (bash-serialize-aliases): New procedure.
(home-bash-configuration, home-bash-extension): Add ‘aliases’ field.
(home-bash-extensions): Adjust accordingly.
* guix/scripts/home/import.scm (generate-bash-configuration+modules): Populate
the ‘alias’ field.
---
 doc/guix.texi                | 14 ++++++
 gnu/home/services/shells.scm | 85 ++++++++++++++++++++++++++----------
 guix/scripts/home/import.scm | 24 ++++++++++
 3 files changed, 100 insertions(+), 23 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index ea1973f02c..f7312a5b30 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -36173,6 +36173,20 @@
 @item @code{environment-variables} (default: @code{()}) (type: alist)
 Association list of environment variables to set for the Bash session.
 
+@item @code{aliases} (default: @code{()}) (type: alist)
+Association list of aliases to set for the Bash session.  The alias will
+automatically be quoted, so something line this:
+
+@lisp
+'((\"ls\" . \"ls -alF\"))
+@end lisp
+
+turns into
+
+@example
+alias ls=\"ls -alF\"
+@end example
+
 @item @code{bash-profile} (default: @code{()}) (type: text-config)
 List of file-like objects, which will be added to @file{.bash_profile}.
 Used for executing user's commands at start of login shell (In most
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index e2730967b2..bd1595a041 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -305,6 +305,18 @@ (define home-zsh-service-type
 ;;; Bash.
 ;;;
 
+(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)))
+
 (define-configuration home-bash-configuration
   (package
    (package bash)
@@ -317,6 +329,21 @@ (define-configuration home-bash-configuration
    (alist '())
    "Association list of environment variables to set for the Bash session."
    serialize-posix-env-vars)
+  (aliases
+   (alist '())
+   "Association list of aliases to set for the Bash session.  The alias will
+automatically be quoted, so something line this:
+
+@lisp
+'((\"ls\" . \"ls -alF\"))
+@end lisp
+
+turns into
+
+@example
+alias ls=\"ls -alF\"
+@end example"
+   bash-serialize-aliases)
   (bash-profile
    (text-config '())
    "List of file-like objects, which will be added to @file{.bash_profile}.
@@ -387,10 +414,11 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
       (if (or extra-content
               (not (null? ((configuration-field-getter field-obj) config))))
           `(,(object->snake-case-string file-name)
-            ,(mixed-text-file
+            ,(apply mixed-text-file
               (object->snake-case-string file-name)
-              (if extra-content extra-content "")
-              (serialize-field field)))
+              (append
+               (if extra-content extra-content '())
+               (list (serialize-field field)))))
           '())))
 
   (filter
@@ -413,8 +441,9 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
      ,@(list (file-if-not-empty
               'bashrc
               (if (home-bash-configuration-guix-defaults? config)
-                  guix-bashrc
-                  #f))
+                  (list guix-bashrc
+                        (serialize-field 'aliases))
+                  (list (serialize-field 'alises))))
              (file-if-not-empty 'bash-logout)))))
 
 (define (add-bash-packages config)
@@ -424,6 +453,9 @@ (define-configuration/no-serialization home-bash-extension
   (environment-variables
    (alist '())
    "Association list of environment variables to set.")
+  (aliases
+   (alist '())
+   "Association list of aliases to set.")
   (bash-profile
    (text-config '())
    "List of file-like objects.")
@@ -435,24 +467,31 @@ (define-configuration/no-serialization home-bash-extension
    "List of file-like objects."))
 
 (define (home-bash-extensions original-config extension-configs)
-  (home-bash-configuration
-   (inherit original-config)
-   (environment-variables
-    (append (home-bash-configuration-environment-variables original-config)
-            (append-map
-             home-bash-extension-environment-variables extension-configs)))
-   (bash-profile
-    (append (home-bash-configuration-bash-profile original-config)
-            (append-map
-             home-bash-extension-bash-profile extension-configs)))
-   (bashrc
-    (append (home-bash-configuration-bashrc original-config)
-            (append-map
-             home-bash-extension-bashrc extension-configs)))
-   (bash-logout
-    (append (home-bash-configuration-bash-logout original-config)
-            (append-map
-             home-bash-extension-bash-logout extension-configs)))))
+  (match original-config
+    (($ <home-bash-configuration> _ _ _ environment-variables aliases
+                                  bash-profile bashrc bash-logout)
+     (home-bash-configuration
+      (inherit original-config)
+      (environment-variables
+       (append environment-variables
+               (append-map
+                home-bash-extension-environment-variables extension-configs)))
+      (aliases
+       (append aliases
+               (append-map
+                home-bash-extension-aliases extension-configs)))
+      (bash-profile
+       (append bash-profile
+               (append-map
+                home-bash-extension-bash-profile extension-configs)))
+      (bashrc
+       (append bashrc
+               (append-map
+                home-bash-extension-bashrc extension-configs)))
+      (bash-logout
+       (append bash-logout
+               (append-map
+                home-bash-extension-bash-logout extension-configs)))))))
 
 (define home-bash-service-type
   (service-type (name 'home-bash)
diff --git a/guix/scripts/home/import.scm b/guix/scripts/home/import.scm
index 7a7712dd96..68420ff7f9 100644
--- a/guix/scripts/home/import.scm
+++ b/guix/scripts/home/import.scm
@@ -27,6 +27,9 @@ (define-module (guix scripts home import)
   #:use-module (gnu packages)
   #:use-module (ice-9 match)
   #:use-module (ice-9 pretty-print)
+  #:use-module (ice-9 rdelim)
+  #:use-module (ice-9 regex)
+  #:use-module (ice-9 popen)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:export (import-manifest
@@ -56,11 +59,32 @@ (define (generate-bash-configuration+modules destination-directory)
   (define (destination-append path)
     (string-append destination-directory "/" path))
 
+  (define (bash-alias->pair line)
+    (if (string-prefix? "alias" (pk line))
+        (let ((matched (string-match "alias (.+)=\"?'?([^\"']+)\"?'?" line)))
+          `(,(match:substring matched 1) . ,(match:substring matched 2)))
+        '()))
+  
+  (define (parse-aliases input)
+    (let loop ((line (read-line input))
+               (result '()))
+      (if (eof-object? line)
+          (reverse result)
+          (loop (read-line input)
+                (cons (bash-alias->pair line) result)))))
+
   (let ((rc (destination-append ".bashrc"))
         (profile (destination-append ".bash_profile"))
         (logout (destination-append ".bash_logout")))
     `((service home-bash-service-type
                (home-bash-configuration
+                ,@(if (file-exists? rc)
+                      `((aliases
+                         ',(let* ((port (open-pipe* OPEN_READ "bash" "-i" "-c" "alias"))
+                               (alist (parse-aliases port)))
+                           (close-port port)
+                           (filter (negate null?) alist))))
+                      '())
                 ,@(if (file-exists? rc)
                       `((bashrc
                          (list (local-file ,rc
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Mon, 01 Nov 2021 09:47:02 GMT) Full text and rfc822 format available.

Message #11 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Subject: [PATCH 2/2] doc: Document ‘home-bash-extension’ configuration record.
Date: Mon, 01 Nov 2021 10:45:53 +0100
* doc/guix.texi (Shells Home Services): Document ‘home-bash-extension’
  configuration record.
* gnu/home/services/shells.scm (generate-home-bash-documentation): Extract
  docstrings from ‘home-bash-extension’.

Fixes: <https://issues.guix.gnu.org/50991>
---
 doc/guix.texi                | 24 ++++++++++++++++++++++++
 gnu/home/services/shells.scm | 14 ++++++++++----
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index f7312a5b30..a3b440f5c9 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -36206,7 +36206,31 @@
 process for example).
 
 @end table
+@end deftp
+
+To extend the Bash service, one has to use a @code{home-bash-extension},
+which contains mostly the same fields as @code{home-bash-configuration}.
+
+@deftp {Data Type} home-bash-extension
+Available @code{home-bash-extension} fields are:
+
+@table @asis
+@item @code{environment-variables} (default: @code{()}) (type: alist)
+Association list of environment variables to set.
+
+@item @code{aliases} (default: @code{()}) (type: alist)
+Association list of aliases to set.
 
+@item @code{bash-profile} (default: @code{()}) (type: text-config)
+List of file-like objects.
+
+@item @code{bashrc} (default: @code{()}) (type: text-config)
+List of file-like objects.
+
+@item @code{bash-logout} (default: @code{()}) (type: text-config)
+List of file-like objects.
+
+@end table
 @end deftp
 
 @subsubheading Zsh Home Service
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index bd1595a041..9eeb7153e3 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -648,10 +648,16 @@ (define (generate-home-shell-profile-documentation)
    'home-shell-profile-configuration))
 
 (define (generate-home-bash-documentation)
-  (generate-documentation
-   `((home-bash-configuration
-      ,home-bash-configuration-fields))
-   'home-bash-configuration))
+  (string-append
+   (generate-documentation
+    `((home-bash-configuration
+       ,home-bash-configuration-fields))
+    'home-bash-configuration)
+   "\n\n"
+   (generate-documentation
+    `((home-bash-extension
+       ,home-bash-extension-fields))
+    'home-bash-extension)))
 
 (define (generate-home-zsh-documentation)
   (generate-documentation
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Mon, 01 Nov 2021 10:46:02 GMT) Full text and rfc822 format available.

Message #14 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>, 51543 <at> debbugs.gnu.org
Subject: Re: [PATCH 2/2] doc: Document
 ‘home-bash-extension’ configuration record.
Date: Mon, 01 Nov 2021 11:45:00 +0100
Hi,

Am Montag, den 01.11.2021, 10:45 +0100 schrieb Xinglu Chen:
> * doc/guix.texi (Shells Home Services): Document ‘home-bash-
> extension’
>   configuration record.
> * gnu/home/services/shells.scm (generate-home-bash-documentation):
> Extract
>   docstrings from ‘home-bash-extension’.
> 
> Fixes: <https://issues.guix.gnu.org/50991>
> ---
>  doc/guix.texi                | 24 ++++++++++++++++++++++++
>  gnu/home/services/shells.scm | 14 ++++++++++----
>  2 files changed, 34 insertions(+), 4 deletions(-)
> 
> diff --git a/doc/guix.texi b/doc/guix.texi
> index f7312a5b30..a3b440f5c9 100644
> --- a/doc/guix.texi
> +++ b/doc/guix.texi
> @@ -36206,7 +36206,31 @@
>  process for example).
>  
>  @end table
> +@end deftp
> +
> +To extend the Bash service, one has to use a @code{home-bash-
> extension},
> +which contains mostly the same fields as @code{home-bash-
> configuration}.
This sounds like you're forcing people to extend their services.  Write
it "You can extend the bash service by using home-bash-extension, whose
fields mostly mirror that of home-bash-service".

> +@deftp {Data Type} home-bash-extension
> +Available @code{home-bash-extension} fields are:
> +
> +@table @asis
> +@item @code{environment-variables} (default: @code{()}) (type:
> alist)
> +Association list of environment variables to set.
> +
> +@item @code{aliases} (default: @code{()}) (type: alist)
> +Association list of aliases to set.
>  
> +@item @code{bash-profile} (default: @code{()}) (type: text-config)
> +List of file-like objects.
> +
> +@item @code{bashrc} (default: @code{()}) (type: text-config)
> +List of file-like objects.
> +
> +@item @code{bash-logout} (default: @code{()}) (type: text-config)
> +List of file-like objects.
> +
> +@end table
>  @end deftp
This documentation is a little sparse, don't you agree?  Are the keys
to environment-variables strings or symbols?  At which point are these
fields inserted into which files (e.g. do the aliases come before
profile or after it)?

If some field is already described as part of home-bash-service, you
might also want to link back to it, but you should still state where
the extension occurs.  Is new code added to the front or to the back
for instance.  (On that note, is the text-config type well-documented?)

Regards,
Liliana





Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Mon, 01 Nov 2021 13:23:02 GMT) Full text and rfc822 format available.

Message #17 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: Liliana Marie Prikler <liliana.prikler <at> gmail.com>, 51543 <at> debbugs.gnu.org
Subject: Re: [bug#51543] [PATCH 2/2] doc: Document ‘home-bash-extension’ configuration record.
Date: Mon, 01 Nov 2021 14:22:01 +0100
[Message part 1 (text/plain, inline)]
Hi,

On Mon, Nov 01 2021, Liliana Marie Prikler wrote:

> Hi,
>
> Am Montag, den 01.11.2021, 10:45 +0100 schrieb Xinglu Chen:
>> * doc/guix.texi (Shells Home Services): Document ‘home-bash-
>> extension’
>>   configuration record.
>> * gnu/home/services/shells.scm (generate-home-bash-documentation):
>> Extract
>>   docstrings from ‘home-bash-extension’.
>> 
>> Fixes: <https://issues.guix.gnu.org/50991>
>> ---
>>  doc/guix.texi                | 24 ++++++++++++++++++++++++
>>  gnu/home/services/shells.scm | 14 ++++++++++----
>>  2 files changed, 34 insertions(+), 4 deletions(-)
>> 
>> diff --git a/doc/guix.texi b/doc/guix.texi
>> index f7312a5b30..a3b440f5c9 100644
>> --- a/doc/guix.texi
>> +++ b/doc/guix.texi
>> @@ -36206,7 +36206,31 @@
>>  process for example).
>>  
>>  @end table
>> +@end deftp
>> +
>> +To extend the Bash service, one has to use a @code{home-bash-
>> extension},
>> +which contains mostly the same fields as @code{home-bash-
>> configuration}.
> This sounds like you're forcing people to extend their services.  Write
> it "You can extend the bash service by using home-bash-extension, whose
> fields mostly mirror that of home-bash-service".

Indeed, that sounds better.

>> +@deftp {Data Type} home-bash-extension
>> +Available @code{home-bash-extension} fields are:
>> +
>> +@table @asis
>> +@item @code{environment-variables} (default: @code{()}) (type:
>> alist)
>> +Association list of environment variables to set.
>> +
>> +@item @code{aliases} (default: @code{()}) (type: alist)
>> +Association list of aliases to set.
>>  
>> +@item @code{bash-profile} (default: @code{()}) (type: text-config)
>> +List of file-like objects.
>> +
>> +@item @code{bashrc} (default: @code{()}) (type: text-config)
>> +List of file-like objects.
>> +
>> +@item @code{bash-logout} (default: @code{()}) (type: text-config)
>> +List of file-like objects.
>> +
>> +@end table
>>  @end deftp
> This documentation is a little sparse, don't you agree?  Are the keys
> to environment-variables strings or symbols?

The keys should be strings; the rules for
‘home-environment-variable-service-type’ apply here (see “11.3.1
Essential Home Services”).

> At which point are these fields inserted into which files (e.g. do the
> aliases come before profile or after it)?

Good question!  The contents of ‘aliases’ and ‘bashrc’ are put into
~/.bashrc, in that order.  The contents of ‘bash-profile’ and
‘environment-variables’ are put into ~/.bash_profile, in that order.
This doesn’t seem that consistent, is there any preference to what order
should be used?

> If some field is already described as part of home-bash-service, you
> might also want to link back to it, but you should still state where
> the extension occurs.  Is new code added to the front or to the back
> for instance.  (On that note, is the text-config type
> well-documented?)

I don’t think there is a way to link to ‘home-bash-configuration’ using
Texinfo; one can only link to “Shells Home Services”.
[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Mon, 01 Nov 2021 16:40:01 GMT) Full text and rfc822 format available.

Message #20 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>, 51543 <at> debbugs.gnu.org
Subject: Re: [bug#51543] [PATCH 2/2] doc: Document
 ‘home-bash-extension’ configuration record.
Date: Mon, 01 Nov 2021 17:38:52 +0100
Hi,

Am Montag, den 01.11.2021, 14:22 +0100 schrieb Xinglu Chen:
> [...]
> 
> The keys should be strings; the rules for
> ‘home-environment-variable-service-type’ apply here (see “11.3.1
> Essential Home Services”).
You might want to explicitly state that.

> > At which point are these fields inserted into which files (e.g. do
> > the aliases come before profile or after it)?
> 
> Good question!  The contents of ‘aliases’ and ‘bashrc’ are put into
> ~/.bashrc, in that order.  The contents of ‘bash-profile’ and
> ‘environment-variables’ are put into ~/.bash_profile, in that order.
> This doesn’t seem that consistent, is there any preference to what
> order should be used?
You can use whichever makes sense to you, I'm just pointing out that it
ought to be documented.

> > If some field is already described as part of home-bash-service,
> > you might also want to link back to it, but you should still state
> > where the extension occurs.  Is new code added to the front or to
> > the back for instance.  (On that note, is the text-config type
> > well-documented?)
> 
> I don’t think there is a way to link to ‘home-bash-configuration’
> using Texinfo; one can only link to “Shells Home Services”.
Texinfo should support anchors, which can point to arbitrary text.  Of
course one could overdo it by linking each and every field, but imho
having one link for context is better than none.

Thoughts?





Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Fri, 05 Nov 2021 11:58:01 GMT) Full text and rfc822 format available.

Message #23 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: Liliana Marie Prikler <liliana.prikler <at> gmail.com>, 51543 <at> debbugs.gnu.org
Subject: Re: [bug#51543] [PATCH 2/2] doc: Document ‘home-bash-extension’ configuration record.
Date: Fri, 05 Nov 2021 12:56:59 +0100
[Message part 1 (text/plain, inline)]
Hi,

On Mon, Nov 01 2021, Liliana Marie Prikler wrote:

> Hi,
>
> Am Montag, den 01.11.2021, 14:22 +0100 schrieb Xinglu Chen:
>> [...]
>> 
>> The keys should be strings; the rules for
>> ‘home-environment-variable-service-type’ apply here (see “11.3.1
>> Essential Home Services”).
> You might want to explicitly state that.

Yes, good idea.

>> > At which point are these fields inserted into which files (e.g. do
>> > the aliases come before profile or after it)?
>> 
>> Good question!  The contents of ‘aliases’ and ‘bashrc’ are put into
>> ~/.bashrc, in that order.  The contents of ‘bash-profile’ and
>> ‘environment-variables’ are put into ~/.bash_profile, in that order.
>> This doesn’t seem that consistent, is there any preference to what
>> order should be used?
> You can use whichever makes sense to you, I'm just pointing out that it
> ought to be documented.
>
>> > If some field is already described as part of home-bash-service,
>> > you might also want to link back to it, but you should still state
>> > where the extension occurs.  Is new code added to the front or to
>> > the back for instance.  (On that note, is the text-config type
>> > well-documented?)
>> 
>> I don’t think there is a way to link to ‘home-bash-configuration’
>> using Texinfo; one can only link to “Shells Home Services”.
> Texinfo should support anchors, which can point to arbitrary text.  Of
> course one could overdo it by linking each and every field, but imho
> having one link for context is better than none.

Ah, that would do it.  Thanks for the pointer.

I will send an updated series.
[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Fri, 05 Nov 2021 14:04:01 GMT) Full text and rfc822 format available.

Message #26 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH 0/2] Some improvements to the Bash home service
Date: Fri, 05 Nov 2021 15:03:03 +0100
[Message part 1 (text/plain, inline)]
Changes since v1:

* Improvements to the documentation of ‘home-bash-configuration’ and
  ‘home-bash-extension’.

* Reorder the serialization of the ‘guix-defaults?’, ‘alias’, and
  ‘bashrc’ fields.

Xinglu Chen (2):
  home: services: bash: Add ‘aliases’ field.
  doc: Improve documentation of the Bash home service

 doc/guix.texi                |  54 +++++++++++++++++-
 gnu/home/services/shells.scm | 108 +++++++++++++++++++++++++----------
 guix/scripts/home/import.scm |  24 ++++++++
 3 files changed, 153 insertions(+), 33 deletions(-)


base-commit: 0e19713c1fbfd3a01347e0d490434a53a596ed3c
-- 
2.33.0



[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Fri, 05 Nov 2021 14:04:02 GMT) Full text and rfc822 format available.

Message #29 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH 1/2] home: services: bash: Add ‘aliases’ field.
Date: Fri, 05 Nov 2021 15:03:11 +0100
* doc/guix.texi (Shells Home Services): Document it.
* gnu/home/services/shells.scm (bash-serialize-aliases): New procedure.
(home-bash-configuration, home-bash-extension): Add ‘aliases’ field.
(home-bash-extensions): Adjust accordingly.
* guix/scripts/home/import.scm (generate-bash-configuration+modules): Populate
the ‘alias’ field.
---
 doc/guix.texi                | 14 ++++++
 gnu/home/services/shells.scm | 83 ++++++++++++++++++++++++++----------
 guix/scripts/home/import.scm | 24 +++++++++++
 3 files changed, 98 insertions(+), 23 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index ea1973f02c..f7312a5b30 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -36173,6 +36173,20 @@
 @item @code{environment-variables} (default: @code{()}) (type: alist)
 Association list of environment variables to set for the Bash session.
 
+@item @code{aliases} (default: @code{()}) (type: alist)
+Association list of aliases to set for the Bash session.  The alias will
+automatically be quoted, so something line this:
+
+@lisp
+'((\"ls\" . \"ls -alF\"))
+@end lisp
+
+turns into
+
+@example
+alias ls=\"ls -alF\"
+@end example
+
 @item @code{bash-profile} (default: @code{()}) (type: text-config)
 List of file-like objects, which will be added to @file{.bash_profile}.
 Used for executing user's commands at start of login shell (In most
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index e2730967b2..f24e47f762 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -305,6 +305,18 @@ (define home-zsh-service-type
 ;;; Bash.
 ;;;
 
+(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)))
+
 (define-configuration home-bash-configuration
   (package
    (package bash)
@@ -317,6 +329,21 @@ (define-configuration home-bash-configuration
    (alist '())
    "Association list of environment variables to set for the Bash session."
    serialize-posix-env-vars)
+  (aliases
+   (alist '())
+   "Association list of aliases to set for the Bash session.  The alias will
+automatically be quoted, so something line this:
+
+@lisp
+'((\"ls\" . \"ls -alF\"))
+@end lisp
+
+turns into
+
+@example
+alias ls=\"ls -alF\"
+@end example"
+   bash-serialize-aliases)
   (bash-profile
    (text-config '())
    "List of file-like objects, which will be added to @file{.bash_profile}.
@@ -387,10 +414,10 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
       (if (or extra-content
               (not (null? ((configuration-field-getter field-obj) config))))
           `(,(object->snake-case-string file-name)
-            ,(mixed-text-file
+            ,(apply mixed-text-file
               (object->snake-case-string file-name)
-              (if extra-content extra-content "")
-              (serialize-field field)))
+              (cons (serialize-field field)
+                    (if extra-content extra-content '()))))
           '())))
 
   (filter
@@ -413,8 +440,8 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
      ,@(list (file-if-not-empty
               'bashrc
               (if (home-bash-configuration-guix-defaults? config)
-                  guix-bashrc
-                  #f))
+                  (list (serialize-field 'aliases) guix-bashrc)
+                  (list (serialize-field 'alises))))
              (file-if-not-empty 'bash-logout)))))
 
 (define (add-bash-packages config)
@@ -424,6 +451,9 @@ (define-configuration/no-serialization home-bash-extension
   (environment-variables
    (alist '())
    "Association list of environment variables to set.")
+  (aliases
+   (alist '())
+   "Association list of aliases to set.")
   (bash-profile
    (text-config '())
    "List of file-like objects.")
@@ -435,24 +465,31 @@ (define-configuration/no-serialization home-bash-extension
    "List of file-like objects."))
 
 (define (home-bash-extensions original-config extension-configs)
-  (home-bash-configuration
-   (inherit original-config)
-   (environment-variables
-    (append (home-bash-configuration-environment-variables original-config)
-            (append-map
-             home-bash-extension-environment-variables extension-configs)))
-   (bash-profile
-    (append (home-bash-configuration-bash-profile original-config)
-            (append-map
-             home-bash-extension-bash-profile extension-configs)))
-   (bashrc
-    (append (home-bash-configuration-bashrc original-config)
-            (append-map
-             home-bash-extension-bashrc extension-configs)))
-   (bash-logout
-    (append (home-bash-configuration-bash-logout original-config)
-            (append-map
-             home-bash-extension-bash-logout extension-configs)))))
+  (match original-config
+    (($ <home-bash-configuration> _ _ _ environment-variables aliases
+                                  bash-profile bashrc bash-logout)
+     (home-bash-configuration
+      (inherit original-config)
+      (environment-variables
+       (append environment-variables
+               (append-map
+                home-bash-extension-environment-variables extension-configs)))
+      (aliases
+       (append aliases
+               (append-map
+                home-bash-extension-aliases extension-configs)))
+      (bash-profile
+       (append bash-profile
+               (append-map
+                home-bash-extension-bash-profile extension-configs)))
+      (bashrc
+       (append bashrc
+               (append-map
+                home-bash-extension-bashrc extension-configs)))
+      (bash-logout
+       (append bash-logout
+               (append-map
+                home-bash-extension-bash-logout extension-configs)))))))
 
 (define home-bash-service-type
   (service-type (name 'home-bash)
diff --git a/guix/scripts/home/import.scm b/guix/scripts/home/import.scm
index 7a7712dd96..fbf89069a7 100644
--- a/guix/scripts/home/import.scm
+++ b/guix/scripts/home/import.scm
@@ -27,6 +27,9 @@ (define-module (guix scripts home import)
   #:use-module (gnu packages)
   #:use-module (ice-9 match)
   #:use-module (ice-9 pretty-print)
+  #:use-module (ice-9 rdelim)
+  #:use-module (ice-9 regex)
+  #:use-module (ice-9 popen)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:export (import-manifest
@@ -56,11 +59,32 @@ (define (generate-bash-configuration+modules destination-directory)
   (define (destination-append path)
     (string-append destination-directory "/" path))
 
+  (define (bash-alias->pair line)
+    (if (string-prefix? "alias" line)
+        (let ((matched (string-match "alias (.+)=\"?'?([^\"']+)\"?'?" line)))
+          `(,(match:substring matched 1) . ,(match:substring matched 2)))
+        '()))
+  
+  (define (parse-aliases input)
+    (let loop ((line (read-line input))
+               (result '()))
+      (if (eof-object? line)
+          (reverse result)
+          (loop (read-line input)
+                (cons (bash-alias->pair line) result)))))
+
   (let ((rc (destination-append ".bashrc"))
         (profile (destination-append ".bash_profile"))
         (logout (destination-append ".bash_logout")))
     `((service home-bash-service-type
                (home-bash-configuration
+                ,@(if (file-exists? rc)
+                      `((aliases
+                         ',(let* ((port (open-pipe* OPEN_READ "bash" "-i" "-c" "alias"))
+                               (alist (parse-aliases port)))
+                           (close-port port)
+                           (filter (negate null?) alist))))
+                      '())
                 ,@(if (file-exists? rc)
                       `((bashrc
                          (list (local-file ,rc
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Fri, 05 Nov 2021 14:04:02 GMT) Full text and rfc822 format available.

Message #32 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH 2/2] doc: Improve documentation of the Bash home service
Date: Fri, 05 Nov 2021 15:03:18 +0100
* doc/guix.texi (Shells Home Services): Document ‘home-bash-extension’
  configuration record.
* gnu/home/services/shells.scm (generate-home-bash-documentation): Extract
  docstrings from ‘home-bash-extension’.
  (home-bash-configuration): Expound on docstrings.

Fixes: <https://issues.guix.gnu.org/50991>
---
 doc/guix.texi                | 44 ++++++++++++++++++++++++++++++++----
 gnu/home/services/shells.scm | 29 ++++++++++++++++--------
 2 files changed, 59 insertions(+), 14 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index f7312a5b30..002193e994 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -36159,6 +36159,7 @@
 
 @subsubheading Bash Home Service
 
+@anchor{home-bash-configuration}
 @deftp {Data Type} home-bash-configuration
 Available @code{home-bash-configuration} fields are:
 
@@ -36167,15 +36168,20 @@
 The Bash package to use.
 
 @item @code{guix-defaults?} (default: @code{#t}) (type: boolean)
-Add sane defaults like reading @file{/etc/bashrc}, coloring output for
-@code{ls} provided by guix to @file{.bashrc}.
+Add sane defaults like reading @file{/etc/bashrc} and coloring the output of
+@command{ls} to the end of the @file{.bashrc} file.
 
 @item @code{environment-variables} (default: @code{()}) (type: alist)
-Association list of environment variables to set for the Bash session.
+Association list of environment variables to set for the Bash session.  The
+rules for the @code{home-environment-variables-service-type} apply
+here (@pxref{Essential Home Services}).  The contents of this field will be
+added after the contents of the @code{bash-profile} field.
 
 @item @code{aliases} (default: @code{()}) (type: alist)
-Association list of aliases to set for the Bash session.  The alias will
-automatically be quoted, so something line this:
+Association list of aliases to set for the Bash session.  The aliases
+will be defined after the contents of the @code{bashrc} field has been
+put in the @file{.bashrc} file.  The alias will automatically be quoted,
+so something line this:
 
 @lisp
 '((\"ls\" . \"ls -alF\"))
@@ -36206,7 +36212,35 @@
 process for example).
 
 @end table
+@end deftp
+
+You can extend the Bash service by using the @code{home-bash-extension}
+configuration record, whose fields most mirror that of
+@code{home-bash-configuration} (@pxref{home-bash-configuration}).  The
+contents of the extensions will be added to the end of the corresponding
+Bash configuration files (@pxref{Bash Startup Files,,, bash, The GNU
+Bash Reference Manual}.
+
+@deftp {Data Type} home-bash-extension
+Available @code{home-bash-extension} fields are:
+
+@table @asis
+@item @code{environment-variables} (default: @code{()}) (type: alist)
+Association list of environment variables to set.
+
+@item @code{aliases} (default: @code{()}) (type: alist)
+Association list of aliases to set.
 
+@item @code{bash-profile} (default: @code{()}) (type: text-config)
+List of file-like objects.
+
+@item @code{bashrc} (default: @code{()}) (type: text-config)
+List of file-like objects.
+
+@item @code{bash-logout} (default: @code{()}) (type: text-config)
+List of file-like objects.
+
+@end table
 @end deftp
 
 @subsubheading Zsh Home Service
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index f24e47f762..9b8427da7b 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -323,16 +323,21 @@ (define-configuration home-bash-configuration
    "The Bash package to use.")
   (guix-defaults?
    (boolean #t)
-   "Add sane defaults like reading @file{/etc/bashrc}, coloring output
-for @code{ls} provided by guix to @file{.bashrc}.")
+   "Add sane defaults like reading @file{/etc/bashrc} and coloring the output of
+@command{ls} to the end of the @file{.bashrc} file.")
   (environment-variables
    (alist '())
-   "Association list of environment variables to set for the Bash session."
+   "Association list of environment variables to set for the Bash session.  The
+rules for the @code{home-environment-variables-service-type} apply
+here (@pxref{Essential Home Services}).  The contents of this field will be
+added after the contents of the @code{bash-profile} field."
    serialize-posix-env-vars)
   (aliases
    (alist '())
-   "Association list of aliases to set for the Bash session.  The alias will
-automatically be quoted, so something line this:
+   "Association list of aliases to set for the Bash session.  The aliases will be
+defined after the contents of the @code{bashrc} field has been put in the
+@file{.bashrc} file.  The alias will automatically be quoted, so something line
+this:
 
 @lisp
 '((\"ls\" . \"ls -alF\"))
@@ -646,10 +651,16 @@ (define (generate-home-shell-profile-documentation)
    'home-shell-profile-configuration))
 
 (define (generate-home-bash-documentation)
-  (generate-documentation
-   `((home-bash-configuration
-      ,home-bash-configuration-fields))
-   'home-bash-configuration))
+  (string-append
+   (generate-documentation
+    `((home-bash-configuration
+       ,home-bash-configuration-fields))
+    'home-bash-configuration)
+   "\n\n"
+   (generate-documentation
+    `((home-bash-extension
+       ,home-bash-extension-fields))
+    'home-bash-extension)))
 
 (define (generate-home-zsh-documentation)
   (generate-documentation
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Fri, 05 Nov 2021 19:37:02 GMT) Full text and rfc822 format available.

Message #35 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>, 51543 <at> debbugs.gnu.org
Subject: Re: [PATCH 2/2] doc: Improve documentation of the Bash home service
Date: Fri, 05 Nov 2021 20:36:21 +0100
Hi,

Am Freitag, den 05.11.2021, 15:03 +0100 schrieb Xinglu Chen:
> @item @code{guix-defaults?} (default: @code{#t}) (type: boolean)
> -Add sane defaults like reading @file{/etc/bashrc}, coloring output
> for
> -@code{ls} provided by guix to @file{.bashrc}.
> +Add sane defaults like reading @file{/etc/bashrc} and coloring the
> output of
> +@command{ls} to the end of the @file{.bashrc} file.
Regarding this option, you might want to instead provide a sane-
defaults-bash-service or something along those lines instead of using
an extra field.  However, as this field already existed before, this is
not a blocker for this series.



> [...]
>  @end table
> +@end deftp
> +
> +You can extend the Bash service by using the @code{home-bash-
> extension}
> +configuration record, whose fields most mirror that of
> +@code{home-bash-configuration} (@pxref{home-bash-
> configuration}).  The
> +contents of the extensions will be added to the end of the
> corresponding
> +Bash configuration files (@pxref{Bash Startup Files,,, bash, The GNU
> +Bash Reference Manual}.
> +
> +@deftp {Data Type} home-bash-extension
> +Available @code{home-bash-extension} fields are:
> +
> +@table @asis
> +@item @code{environment-variables} (default: @code{()}) (type:
> alist)
> +Association list of environment variables to set.
> +
> +@item @code{aliases} (default: @code{()}) (type: alist)
> +Association list of aliases to set.
>  
> +@item @code{bash-profile} (default: @code{()}) (type: text-config)
> +List of file-like objects.
> +
> +@item @code{bashrc} (default: @code{()}) (type: text-config)
> +List of file-like objects.
> +
> +@item @code{bash-logout} (default: @code{()}) (type: text-config)
> +List of file-like objects.
> +
> +@end table
>  @end deftp
Is there a reason why this documentation stayed more or less the same? 
I see the fields have an updated documentation, but it appears not to
be reflected here.  Or are strings taken from the [1/2] patch?

Either way, since the data types ought to be already known, you should
write something along the lines of "Additional environment variables to
set.  These will be concatenated with the environment variables from
other extensions and the base service to form one coherent block of
environment variables." and so on, focusing on what it does rather than
what it is.

Cheers





Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Sun, 07 Nov 2021 11:21:02 GMT) Full text and rfc822 format available.

Message #38 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: Liliana Marie Prikler <liliana.prikler <at> gmail.com>, 51543 <at> debbugs.gnu.org
Subject: Re: [PATCH 2/2] doc: Improve documentation of the Bash home service
Date: Sun, 07 Nov 2021 12:20:19 +0100
[Message part 1 (text/plain, inline)]
Hi,

On Fri, Nov 05 2021, Liliana Marie Prikler wrote:

> Hi,
>
> Am Freitag, den 05.11.2021, 15:03 +0100 schrieb Xinglu Chen:
>> @item @code{guix-defaults?} (default: @code{#t}) (type: boolean)
>> -Add sane defaults like reading @file{/etc/bashrc}, coloring output
>> for
>> -@code{ls} provided by guix to @file{.bashrc}.
>> +Add sane defaults like reading @file{/etc/bashrc} and coloring the
>> output of
>> +@command{ls} to the end of the @file{.bashrc} file.
> Regarding this option, you might want to instead provide a sane-
> defaults-bash-service or something along those lines instead of using
> an extra field.  However, as this field already existed before, this is
> not a blocker for this series.

That sounds like a good idea!

>> [...]
>>  @end table
>> +@end deftp
>> +
>> +You can extend the Bash service by using the @code{home-bash-
>> extension}
>> +configuration record, whose fields most mirror that of
>> +@code{home-bash-configuration} (@pxref{home-bash-
>> configuration}).  The
>> +contents of the extensions will be added to the end of the
>> corresponding
>> +Bash configuration files (@pxref{Bash Startup Files,,, bash, The GNU
>> +Bash Reference Manual}.
>> +
>> +@deftp {Data Type} home-bash-extension
>> +Available @code{home-bash-extension} fields are:
>> +
>> +@table @asis
>> +@item @code{environment-variables} (default: @code{()}) (type:
>> alist)
>> +Association list of environment variables to set.
>> +
>> +@item @code{aliases} (default: @code{()}) (type: alist)
>> +Association list of aliases to set.
>>  
>> +@item @code{bash-profile} (default: @code{()}) (type: text-config)
>> +List of file-like objects.
>> +
>> +@item @code{bashrc} (default: @code{()}) (type: text-config)
>> +List of file-like objects.
>> +
>> +@item @code{bash-logout} (default: @code{()}) (type: text-config)
>> +List of file-like objects.
>> +
>> +@end table
>>  @end deftp
> Is there a reason why this documentation stayed more or less the same? 
> I see the fields have an updated documentation, but it appears not to
> be reflected here.  Or are strings taken from the [1/2] patch?

I didn’t change the docstrings for ‘home-bash-extension’, only for
‘home-bash-configuration’.

> Either way, since the data types ought to be already known, you should
> write something along the lines of "Additional environment variables to
> set.  These will be concatenated with the environment variables from
> other extensions and the base service to form one coherent block of
> environment variables." and so on, focusing on what it does rather than
> what it is.

Yeah, that sounds a lot more informative than the current docstring.

[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Sun, 07 Nov 2021 11:37:02 GMT) Full text and rfc822 format available.

Message #41 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH v3 0/2] Some improvements to the Bash home service
Date: Sun, 07 Nov 2021 12:36:12 +0100
[Message part 1 (text/plain, inline)]
Changes since v2:

* Improve docstrings of ‘home-bash-extension’ fields by describing what
  they do instead of what they ought to be.

Xinglu Chen (2):
  home: services: bash: Add ‘aliases’ field.
  doc: Improve documentation of the Bash home service

 doc/guix.texi                |  60 ++++++++++++++++-
 gnu/home/services/shells.scm | 122 +++++++++++++++++++++++++----------
 guix/scripts/home/import.scm |  24 +++++++
 3 files changed, 169 insertions(+), 37 deletions(-)


base-commit: 1ffc0a6be3c1613b2d99ceea098174d1f11f6f3f
-- 
2.33.0



[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Sun, 07 Nov 2021 11:37:02 GMT) Full text and rfc822 format available.

Message #44 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH v3 1/2] home: services: bash: Add ‘aliases’ field.
Date: Sun, 07 Nov 2021 12:36:19 +0100
* doc/guix.texi (Shells Home Services): Document it.
* gnu/home/services/shells.scm (bash-serialize-aliases): New procedure.
(home-bash-configuration, home-bash-extension): Add ‘aliases’ field.
(home-bash-extensions): Adjust accordingly.
* guix/scripts/home/import.scm (generate-bash-configuration+modules): Populate
the ‘alias’ field.
---
 doc/guix.texi                | 14 ++++++
 gnu/home/services/shells.scm | 83 ++++++++++++++++++++++++++----------
 guix/scripts/home/import.scm | 24 +++++++++++
 3 files changed, 98 insertions(+), 23 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index ea1973f02c..f7312a5b30 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -36173,6 +36173,20 @@
 @item @code{environment-variables} (default: @code{()}) (type: alist)
 Association list of environment variables to set for the Bash session.
 
+@item @code{aliases} (default: @code{()}) (type: alist)
+Association list of aliases to set for the Bash session.  The alias will
+automatically be quoted, so something line this:
+
+@lisp
+'((\"ls\" . \"ls -alF\"))
+@end lisp
+
+turns into
+
+@example
+alias ls=\"ls -alF\"
+@end example
+
 @item @code{bash-profile} (default: @code{()}) (type: text-config)
 List of file-like objects, which will be added to @file{.bash_profile}.
 Used for executing user's commands at start of login shell (In most
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index e2730967b2..f24e47f762 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -305,6 +305,18 @@ (define home-zsh-service-type
 ;;; Bash.
 ;;;
 
+(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)))
+
 (define-configuration home-bash-configuration
   (package
    (package bash)
@@ -317,6 +329,21 @@ (define-configuration home-bash-configuration
    (alist '())
    "Association list of environment variables to set for the Bash session."
    serialize-posix-env-vars)
+  (aliases
+   (alist '())
+   "Association list of aliases to set for the Bash session.  The alias will
+automatically be quoted, so something line this:
+
+@lisp
+'((\"ls\" . \"ls -alF\"))
+@end lisp
+
+turns into
+
+@example
+alias ls=\"ls -alF\"
+@end example"
+   bash-serialize-aliases)
   (bash-profile
    (text-config '())
    "List of file-like objects, which will be added to @file{.bash_profile}.
@@ -387,10 +414,10 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
       (if (or extra-content
               (not (null? ((configuration-field-getter field-obj) config))))
           `(,(object->snake-case-string file-name)
-            ,(mixed-text-file
+            ,(apply mixed-text-file
               (object->snake-case-string file-name)
-              (if extra-content extra-content "")
-              (serialize-field field)))
+              (cons (serialize-field field)
+                    (if extra-content extra-content '()))))
           '())))
 
   (filter
@@ -413,8 +440,8 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
      ,@(list (file-if-not-empty
               'bashrc
               (if (home-bash-configuration-guix-defaults? config)
-                  guix-bashrc
-                  #f))
+                  (list (serialize-field 'aliases) guix-bashrc)
+                  (list (serialize-field 'alises))))
              (file-if-not-empty 'bash-logout)))))
 
 (define (add-bash-packages config)
@@ -424,6 +451,9 @@ (define-configuration/no-serialization home-bash-extension
   (environment-variables
    (alist '())
    "Association list of environment variables to set.")
+  (aliases
+   (alist '())
+   "Association list of aliases to set.")
   (bash-profile
    (text-config '())
    "List of file-like objects.")
@@ -435,24 +465,31 @@ (define-configuration/no-serialization home-bash-extension
    "List of file-like objects."))
 
 (define (home-bash-extensions original-config extension-configs)
-  (home-bash-configuration
-   (inherit original-config)
-   (environment-variables
-    (append (home-bash-configuration-environment-variables original-config)
-            (append-map
-             home-bash-extension-environment-variables extension-configs)))
-   (bash-profile
-    (append (home-bash-configuration-bash-profile original-config)
-            (append-map
-             home-bash-extension-bash-profile extension-configs)))
-   (bashrc
-    (append (home-bash-configuration-bashrc original-config)
-            (append-map
-             home-bash-extension-bashrc extension-configs)))
-   (bash-logout
-    (append (home-bash-configuration-bash-logout original-config)
-            (append-map
-             home-bash-extension-bash-logout extension-configs)))))
+  (match original-config
+    (($ <home-bash-configuration> _ _ _ environment-variables aliases
+                                  bash-profile bashrc bash-logout)
+     (home-bash-configuration
+      (inherit original-config)
+      (environment-variables
+       (append environment-variables
+               (append-map
+                home-bash-extension-environment-variables extension-configs)))
+      (aliases
+       (append aliases
+               (append-map
+                home-bash-extension-aliases extension-configs)))
+      (bash-profile
+       (append bash-profile
+               (append-map
+                home-bash-extension-bash-profile extension-configs)))
+      (bashrc
+       (append bashrc
+               (append-map
+                home-bash-extension-bashrc extension-configs)))
+      (bash-logout
+       (append bash-logout
+               (append-map
+                home-bash-extension-bash-logout extension-configs)))))))
 
 (define home-bash-service-type
   (service-type (name 'home-bash)
diff --git a/guix/scripts/home/import.scm b/guix/scripts/home/import.scm
index 7a7712dd96..fbf89069a7 100644
--- a/guix/scripts/home/import.scm
+++ b/guix/scripts/home/import.scm
@@ -27,6 +27,9 @@ (define-module (guix scripts home import)
   #:use-module (gnu packages)
   #:use-module (ice-9 match)
   #:use-module (ice-9 pretty-print)
+  #:use-module (ice-9 rdelim)
+  #:use-module (ice-9 regex)
+  #:use-module (ice-9 popen)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:export (import-manifest
@@ -56,11 +59,32 @@ (define (generate-bash-configuration+modules destination-directory)
   (define (destination-append path)
     (string-append destination-directory "/" path))
 
+  (define (bash-alias->pair line)
+    (if (string-prefix? "alias" line)
+        (let ((matched (string-match "alias (.+)=\"?'?([^\"']+)\"?'?" line)))
+          `(,(match:substring matched 1) . ,(match:substring matched 2)))
+        '()))
+  
+  (define (parse-aliases input)
+    (let loop ((line (read-line input))
+               (result '()))
+      (if (eof-object? line)
+          (reverse result)
+          (loop (read-line input)
+                (cons (bash-alias->pair line) result)))))
+
   (let ((rc (destination-append ".bashrc"))
         (profile (destination-append ".bash_profile"))
         (logout (destination-append ".bash_logout")))
     `((service home-bash-service-type
                (home-bash-configuration
+                ,@(if (file-exists? rc)
+                      `((aliases
+                         ',(let* ((port (open-pipe* OPEN_READ "bash" "-i" "-c" "alias"))
+                               (alist (parse-aliases port)))
+                           (close-port port)
+                           (filter (negate null?) alist))))
+                      '())
                 ,@(if (file-exists? rc)
                       `((bashrc
                          (list (local-file ,rc
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Sun, 07 Nov 2021 11:37:02 GMT) Full text and rfc822 format available.

Message #47 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51543 <at> debbugs.gnu.org
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH v3 2/2] doc: Improve documentation of the Bash home service
Date: Sun, 07 Nov 2021 12:36:29 +0100
* doc/guix.texi (Shells Home Services): Document ‘home-bash-extension’
  configuration record.
* gnu/home/services/shells.scm (generate-home-bash-documentation): Extract
  docstrings from ‘home-bash-extension’.
  (home-bash-configuration): Expound on docstrings.
  (home-bash-extension): Likewise.

Fixes: <https://issues.guix.gnu.org/50991>
---
 doc/guix.texi                | 50 ++++++++++++++++++++++++++++++++----
 gnu/home/services/shells.scm | 45 ++++++++++++++++++++++----------
 2 files changed, 76 insertions(+), 19 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index f7312a5b30..db1bf6efa7 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -36159,6 +36159,7 @@
 
 @subsubheading Bash Home Service
 
+@anchor{home-bash-configuration}
 @deftp {Data Type} home-bash-configuration
 Available @code{home-bash-configuration} fields are:
 
@@ -36167,15 +36168,20 @@
 The Bash package to use.
 
 @item @code{guix-defaults?} (default: @code{#t}) (type: boolean)
-Add sane defaults like reading @file{/etc/bashrc}, coloring output for
-@code{ls} provided by guix to @file{.bashrc}.
+Add sane defaults like reading @file{/etc/bashrc} and coloring the output of
+@command{ls} to the end of the @file{.bashrc} file.
 
 @item @code{environment-variables} (default: @code{()}) (type: alist)
-Association list of environment variables to set for the Bash session.
+Association list of environment variables to set for the Bash session.  The
+rules for the @code{home-environment-variables-service-type} apply
+here (@pxref{Essential Home Services}).  The contents of this field will be
+added after the contents of the @code{bash-profile} field.
 
 @item @code{aliases} (default: @code{()}) (type: alist)
-Association list of aliases to set for the Bash session.  The alias will
-automatically be quoted, so something line this:
+Association list of aliases to set for the Bash session.  The aliases
+will be defined after the contents of the @code{bashrc} field has been
+put in the @file{.bashrc} file.  The alias will automatically be quoted,
+so something line this:
 
 @lisp
 '((\"ls\" . \"ls -alF\"))
@@ -36206,7 +36212,41 @@
 process for example).
 
 @end table
+@end deftp
+
+You can extend the Bash service by using the @code{home-bash-extension}
+configuration record, whose fields most mirror that of
+@code{home-bash-configuration} (@pxref{home-bash-configuration}).  The
+contents of the extensions will be added to the end of the corresponding
+Bash configuration files (@pxref{Bash Startup Files,,, bash, The GNU
+Bash Reference Manual}.
+
+@deftp {Data Type} home-bash-extension
+Available @code{home-bash-extension} fields are:
+
+@table @asis
+@item @code{environment-variables} (default: @code{()}) (type: alist)
+Additional environment variables to set.  These will be combined with the
+environment variables from other extensions and the base service to form one
+coherent block of environment variables.
+
+@item @code{aliases} (default: @code{()}) (type: alist)
+Additional aliases to set.  These will be combined with the aliases from
+other extensions and the base service.
 
+@item @code{bash-profile} (default: @code{()}) (type: text-config)
+Additional text blocks to add to @file{.bash_profile}, which will be combined
+with text blocks from other extensions and the base service.
+
+@item @code{bashrc} (default: @code{()}) (type: text-config)
+Additional text blocks to add to @file{.bashrc}, which will be combined
+with text blocks from other extensions and the base service.
+
+@item @code{bash-logout} (default: @code{()}) (type: text-config)
+Additional text blocks to add to @file{.bash_logout}, which will be combined
+with text blocks from other extensions and the base service.
+
+@end table
 @end deftp
 
 @subsubheading Zsh Home Service
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index f24e47f762..81d07da86c 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -323,16 +323,21 @@ (define-configuration home-bash-configuration
    "The Bash package to use.")
   (guix-defaults?
    (boolean #t)
-   "Add sane defaults like reading @file{/etc/bashrc}, coloring output
-for @code{ls} provided by guix to @file{.bashrc}.")
+   "Add sane defaults like reading @file{/etc/bashrc} and coloring the output of
+@command{ls} to the end of the @file{.bashrc} file.")
   (environment-variables
    (alist '())
-   "Association list of environment variables to set for the Bash session."
+   "Association list of environment variables to set for the Bash session.  The
+rules for the @code{home-environment-variables-service-type} apply
+here (@pxref{Essential Home Services}).  The contents of this field will be
+added after the contents of the @code{bash-profile} field."
    serialize-posix-env-vars)
   (aliases
    (alist '())
-   "Association list of aliases to set for the Bash session.  The alias will
-automatically be quoted, so something line this:
+   "Association list of aliases to set for the Bash session.  The aliases will be
+defined after the contents of the @code{bashrc} field has been put in the
+@file{.bashrc} file.  The alias will automatically be quoted, so something line
+this:
 
 @lisp
 '((\"ls\" . \"ls -alF\"))
@@ -450,19 +455,25 @@ (define (add-bash-packages config)
 (define-configuration/no-serialization home-bash-extension
   (environment-variables
    (alist '())
-   "Association list of environment variables to set.")
+   "Additional environment variables to set.  These will be combined with the
+environment variables from other extensions and the base service to form one
+coherent block of environment variables.")
   (aliases
    (alist '())
-   "Association list of aliases to set.")
+   "Additional aliases to set.  These will be combined with the aliases from
+other extensions and the base service.")
   (bash-profile
    (text-config '())
-   "List of file-like objects.")
+   "Additional text blocks to add to @file{.bash_profile}, which will be combined
+with text blocks from other extensions and the base service.")
   (bashrc
    (text-config '())
-   "List of file-like objects.")
+   "Additional text blocks to add to @file{.bashrc}, which will be combined
+with text blocks from other extensions and the base service.")
   (bash-logout
    (text-config '())
-   "List of file-like objects."))
+   "Additional text blocks to add to @file{.bash_logout}, which will be combined
+with text blocks from other extensions and the base service."))
 
 (define (home-bash-extensions original-config extension-configs)
   (match original-config
@@ -646,10 +657,16 @@ (define (generate-home-shell-profile-documentation)
    'home-shell-profile-configuration))
 
 (define (generate-home-bash-documentation)
-  (generate-documentation
-   `((home-bash-configuration
-      ,home-bash-configuration-fields))
-   'home-bash-configuration))
+  (string-append
+   (generate-documentation
+    `((home-bash-configuration
+       ,home-bash-configuration-fields))
+    'home-bash-configuration)
+   "\n\n"
+   (generate-documentation
+    `((home-bash-extension
+       ,home-bash-extension-fields))
+    'home-bash-extension)))
 
 (define (generate-home-zsh-documentation)
   (generate-documentation
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Sun, 07 Nov 2021 20:19:01 GMT) Full text and rfc822 format available.

Message #50 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>, 51543 <at> debbugs.gnu.org
Subject: Re: [PATCH v3 2/2] doc: Improve documentation of the Bash home service
Date: Sun, 07 Nov 2021 21:18:12 +0100
Am Sonntag, den 07.11.2021, 12:36 +0100 schrieb Xinglu Chen:
> * doc/guix.texi (Shells Home Services): Document ‘home-bash-
> extension’
>   configuration record.
> * gnu/home/services/shells.scm (generate-home-bash-documentation):
> Extract
>   docstrings from ‘home-bash-extension’.
>   (home-bash-configuration): Expound on docstrings.
>   (home-bash-extension): Likewise.
> 
> Fixes: <https://issues.guix.gnu.org/50991>
The docstrings LGTM now.  This series is probably safe to go to master,
though you might want to wait for people to complain about the addition
of a field possibly breaking ABI and what not :P

Cheers





Reply sent to Ludovic Courtès <ludo <at> gnu.org>:
You have taken responsibility. (Sun, 07 Nov 2021 20:59:02 GMT) Full text and rfc822 format available.

Notification sent to Xinglu Chen <public <at> yoctocell.xyz>:
bug acknowledged by developer. (Sun, 07 Nov 2021 20:59:02 GMT) Full text and rfc822 format available.

Message #55 received at 51543-done <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: Xinglu Chen <public <at> yoctocell.xyz>
Cc: 51543-done <at> debbugs.gnu.org,
 Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: Re: bug#51543: [PATCH 0/2] Some improvements to the Bash home service
Date: Sun, 07 Nov 2021 21:58:00 +0100
Hello,

Xinglu Chen <public <at> yoctocell.xyz> skribis:

>   home: services: bash: Add ‘aliases’ field.
>   doc: Improve documentation of the Bash home service

Applied, thanks!

Liliana Marie Prikler <liliana.prikler <at> gmail.com> skribis:

> The docstrings LGTM now.  This series is probably safe to go to master,
> though you might want to wait for people to complain about the addition
> of a field possibly breaking ABI and what not :P

Users won’t see any ABI breakage so I think we’re fine.  Applied!

Thanks Xinglu & Liliana!

Ludo’.




Information forwarded to guix-patches <at> gnu.org:
bug#51543; Package guix-patches. (Sat, 13 Nov 2021 20:37:02 GMT) Full text and rfc822 format available.

Message #58 received at 51543 <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: Xinglu Chen <public <at> yoctocell.xyz>
Cc: Liliana Marie Prikler <liliana.prikler <at> gmail.com>, 51543 <at> debbugs.gnu.org
Subject: Re: bug#51543: [PATCH 0/2] Some improvements to the Bash home service
Date: Sat, 13 Nov 2021 21:35:52 +0100
Hi!

Xinglu Chen <public <at> yoctocell.xyz> skribis:

>   home: services: bash: Add ‘aliases’ field.
>   doc: Improve documentation of the Bash home service

I had overlooked it but it breaks tests/guix-home.sh and
tests/home-import.scm.  Could you take a look and send a patch in a new
issue?

Thanks,
Ludo’.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 12 Dec 2021 12:24:08 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 97 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.