mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2025-01-11 13:49:23 -05:00
home: services: bash: Add ‘aliases’ field.
* 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. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
parent
5eb5c0789f
commit
4b96998292
3 changed files with 98 additions and 23 deletions
|
@ -36173,6 +36173,20 @@ Add sane defaults like reading @file{/etc/bashrc}, coloring output for
|
||||||
@item @code{environment-variables} (default: @code{()}) (type: alist)
|
@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.
|
||||||
|
|
||||||
|
@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)
|
@item @code{bash-profile} (default: @code{()}) (type: text-config)
|
||||||
List of file-like objects, which will be added to @file{.bash_profile}.
|
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
|
Used for executing user's commands at start of login shell (In most
|
||||||
|
|
|
@ -305,6 +305,18 @@ (define home-zsh-service-type
|
||||||
;;; Bash.
|
;;; 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
|
(define-configuration home-bash-configuration
|
||||||
(package
|
(package
|
||||||
(package bash)
|
(package bash)
|
||||||
|
@ -317,6 +329,21 @@ (define-configuration home-bash-configuration
|
||||||
(alist '())
|
(alist '())
|
||||||
"Association list of environment variables to set for the Bash session."
|
"Association list of environment variables to set for the Bash session."
|
||||||
serialize-posix-env-vars)
|
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
|
(bash-profile
|
||||||
(text-config '())
|
(text-config '())
|
||||||
"List of file-like objects, which will be added to @file{.bash_profile}.
|
"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
|
(if (or extra-content
|
||||||
(not (null? ((configuration-field-getter field-obj) config))))
|
(not (null? ((configuration-field-getter field-obj) config))))
|
||||||
`(,(object->snake-case-string file-name)
|
`(,(object->snake-case-string file-name)
|
||||||
,(mixed-text-file
|
,(apply mixed-text-file
|
||||||
(object->snake-case-string file-name)
|
(object->snake-case-string file-name)
|
||||||
(if extra-content extra-content "")
|
(cons (serialize-field field)
|
||||||
(serialize-field field)))
|
(if extra-content extra-content '()))))
|
||||||
'())))
|
'())))
|
||||||
|
|
||||||
(filter
|
(filter
|
||||||
|
@ -413,8 +440,8 @@ (define* (file-if-not-empty field #:optional (extra-content #f))
|
||||||
,@(list (file-if-not-empty
|
,@(list (file-if-not-empty
|
||||||
'bashrc
|
'bashrc
|
||||||
(if (home-bash-configuration-guix-defaults? config)
|
(if (home-bash-configuration-guix-defaults? config)
|
||||||
guix-bashrc
|
(list (serialize-field 'aliases) guix-bashrc)
|
||||||
#f))
|
(list (serialize-field 'alises))))
|
||||||
(file-if-not-empty 'bash-logout)))))
|
(file-if-not-empty 'bash-logout)))))
|
||||||
|
|
||||||
(define (add-bash-packages config)
|
(define (add-bash-packages config)
|
||||||
|
@ -424,6 +451,9 @@ (define-configuration/no-serialization home-bash-extension
|
||||||
(environment-variables
|
(environment-variables
|
||||||
(alist '())
|
(alist '())
|
||||||
"Association list of environment variables to set.")
|
"Association list of environment variables to set.")
|
||||||
|
(aliases
|
||||||
|
(alist '())
|
||||||
|
"Association list of aliases to set.")
|
||||||
(bash-profile
|
(bash-profile
|
||||||
(text-config '())
|
(text-config '())
|
||||||
"List of file-like objects.")
|
"List of file-like objects.")
|
||||||
|
@ -435,24 +465,31 @@ (define-configuration/no-serialization home-bash-extension
|
||||||
"List of file-like objects."))
|
"List of file-like objects."))
|
||||||
|
|
||||||
(define (home-bash-extensions original-config extension-configs)
|
(define (home-bash-extensions original-config extension-configs)
|
||||||
(home-bash-configuration
|
(match original-config
|
||||||
(inherit original-config)
|
(($ <home-bash-configuration> _ _ _ environment-variables aliases
|
||||||
(environment-variables
|
bash-profile bashrc bash-logout)
|
||||||
(append (home-bash-configuration-environment-variables original-config)
|
(home-bash-configuration
|
||||||
(append-map
|
(inherit original-config)
|
||||||
home-bash-extension-environment-variables extension-configs)))
|
(environment-variables
|
||||||
(bash-profile
|
(append environment-variables
|
||||||
(append (home-bash-configuration-bash-profile original-config)
|
(append-map
|
||||||
(append-map
|
home-bash-extension-environment-variables extension-configs)))
|
||||||
home-bash-extension-bash-profile extension-configs)))
|
(aliases
|
||||||
(bashrc
|
(append aliases
|
||||||
(append (home-bash-configuration-bashrc original-config)
|
(append-map
|
||||||
(append-map
|
home-bash-extension-aliases extension-configs)))
|
||||||
home-bash-extension-bashrc extension-configs)))
|
(bash-profile
|
||||||
(bash-logout
|
(append bash-profile
|
||||||
(append (home-bash-configuration-bash-logout original-config)
|
(append-map
|
||||||
(append-map
|
home-bash-extension-bash-profile extension-configs)))
|
||||||
home-bash-extension-bash-logout 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
|
(define home-bash-service-type
|
||||||
(service-type (name 'home-bash)
|
(service-type (name 'home-bash)
|
||||||
|
|
|
@ -27,6 +27,9 @@ (define-module (guix scripts home import)
|
||||||
#:use-module (gnu packages)
|
#:use-module (gnu packages)
|
||||||
#:use-module (ice-9 match)
|
#:use-module (ice-9 match)
|
||||||
#:use-module (ice-9 pretty-print)
|
#: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-1)
|
||||||
#:use-module (srfi srfi-26)
|
#:use-module (srfi srfi-26)
|
||||||
#:export (import-manifest
|
#:export (import-manifest
|
||||||
|
@ -56,11 +59,32 @@ (define (generate-bash-configuration+modules destination-directory)
|
||||||
(define (destination-append path)
|
(define (destination-append path)
|
||||||
(string-append destination-directory "/" 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"))
|
(let ((rc (destination-append ".bashrc"))
|
||||||
(profile (destination-append ".bash_profile"))
|
(profile (destination-append ".bash_profile"))
|
||||||
(logout (destination-append ".bash_logout")))
|
(logout (destination-append ".bash_logout")))
|
||||||
`((service home-bash-service-type
|
`((service home-bash-service-type
|
||||||
(home-bash-configuration
|
(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)
|
,@(if (file-exists? rc)
|
||||||
`((bashrc
|
`((bashrc
|
||||||
(list (local-file ,rc
|
(list (local-file ,rc
|
||||||
|
|
Loading…
Reference in a new issue