summaryrefslogtreecommitdiff
path: root/gnu
diff options
context:
space:
mode:
authorMaxim Cournoyer <maxim.cournoyer@gmail.com>2025-05-08 11:03:35 +0900
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>2025-05-26 13:43:03 +0900
commitc743d646ee9104aa14d066cdf6657a0fac82ebea (patch)
tree0cbd78ae16fbbbfec8344b031f7f10dce407a571 /gnu
parent45b883a9d33ff3bfd0bdb9c9eeea4d64eb73e10c (diff)
system: Factorize bashrc default configuration.
This factorizes out the remaining bashrc bits from /etc/skel/.bashrc to a the template used for both /etc/bashrc on Guix System and ~/.bashrc for home-bash-service-type. Rationale: The use of /etc/skel introduce state: the file is only copied originally when the user account is created, and never (automatically) refreshed again. * gnu/system.scm (operating-system-etc-service): <profile>: Guard against souring /etc/bashrc in non-interactive, SSH case. <bashrc>: Use %default-bashrc, having migrated the remaining definitions to... * gnu/system/shadow.scm (%default-bashrc): ... here. Factorize aliases to... * gnu/services.scm (%default-bash-aliases): ... here. (%default-bashrc-d-aliases): New variable. (%default-etc-bashrc-d-files): Include it in the default configuration. * gnu/services/base.scm (%base-services): Register etc-bashrc-d-service-type. * gnu/home/services/shells.scm (add-bash-configuration): Do not set PS1, now part of %default-bashrc. (home-bash-configuration) [guix-defaults?]: Update doc. [aliases]: Set %default-bash-aliases as the default value. Update doc. * doc/guix.texi (Shells Home Services): Update documentation. (Service Reference): Update example. Change-Id: I340c614983a78fd20a9c4a9705e7fc542ae9b513
Diffstat (limited to 'gnu')
-rw-r--r--gnu/home/services/shells.scm14
-rw-r--r--gnu/services.scm23
-rw-r--r--gnu/services/base.scm5
-rw-r--r--gnu/system.scm24
-rw-r--r--gnu/system/shadow.scm18
-rw-r--r--gnu/tests/base.scm17
6 files changed, 59 insertions, 42 deletions
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index bab5730c3d6..969a3eb8dcf 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -20,6 +20,7 @@
20 20
21(define-module (gnu home services shells) 21(define-module (gnu home services shells)
22 #:use-module (gnu services configuration) 22 #:use-module (gnu services configuration)
23 #:use-module ((gnu services) #:select (%default-bash-aliases))
23 #:autoload (gnu system shadow) (%default-bashrc %default-zprofile) 24 #:autoload (gnu system shadow) (%default-bashrc %default-zprofile)
24 #:use-module (gnu home services utils) 25 #:use-module (gnu home services utils)
25 #:use-module (gnu home services) 26 #:use-module (gnu home services)
@@ -333,9 +334,12 @@ another process for example)."))
333 (package bash) 334 (package bash)
334 "The Bash package to use.") 335 "The Bash package to use.")
335 (guix-defaults? 336 (guix-defaults?
337 ;; TODO: Set to #f when the target system is determined to be Guix System.
336 (boolean #t) 338 (boolean #t)
337 "Add sane defaults like reading @file{/etc/bashrc} and coloring the output of 339 "Add sane defaults like setting @env{PS1}, @env{SHELL}, and ensuring
338@command{ls} to the top of the @file{.bashrc} file.") 340@file{/etc/profile} is sourced for non-interactive SSH shells. If you use
341Guix System, is it safe to set this to @code{#f}, as in this case this is
342already taken care of by the globally installed @file{/etc/bashrc}.")
339 (environment-variables 343 (environment-variables
340 (alist '()) 344 (alist '())
341 "Association list of environment variables to set for the Bash session. The 345 "Association list of environment variables to set for the Bash session. The
@@ -344,7 +348,7 @@ here (@pxref{Essential Home Services}). The contents of this field will be
344added after the contents of the @code{bash-profile} field." 348added after the contents of the @code{bash-profile} field."
345 (serializer serialize-posix-env-vars)) 349 (serializer serialize-posix-env-vars))
346 (aliases 350 (aliases
347 (alist '()) 351 (alist %default-bash-aliases)
348 "Association list of aliases to set for the Bash session. The aliases will be 352 "Association list of aliases to set for the Bash session. The aliases will be
349defined after the contents of the @code{bashrc} field has been put in the 353defined after the contents of the @code{bashrc} field has been put in the
350@file{.bashrc} file. The alias will automatically be quoted, so something line 354@file{.bashrc} file. The alias will automatically be quoted, so something line
@@ -423,10 +427,6 @@ if [ -f ~/.bashrc ]; then source ~/.bashrc; fi
423 'bashrc 427 'bashrc
424 (if (home-bash-configuration-guix-defaults? config) 428 (if (home-bash-configuration-guix-defaults? config)
425 (list (plain-file-content %default-bashrc) "\n" 429 (list (plain-file-content %default-bashrc) "\n"
426 ;; The host distro might provide a bad 'PS1'
427 ;; default--e.g., not taking $GUIX_ENVIRONMENT into
428 ;; account. Provide a good default here when asked.
429 "PS1='\\u@\\h \\w${GUIX_ENVIRONMENT:+ [env]}\\$ '\n"
430 (serialize-field 'aliases)) 430 (serialize-field 'aliases))
431 (list (serialize-field 'aliases)))) 431 (list (serialize-field 'aliases))))
432 (file-if-not-empty 'bash-logout))))) 432 (file-if-not-empty 'bash-logout)))))
diff --git a/gnu/services.scm b/gnu/services.scm
index 5dbda176b80..159d357c3e7 100644
--- a/gnu/services.scm
+++ b/gnu/services.scm
@@ -125,6 +125,7 @@
125 etc-profile-d-service-type 125 etc-profile-d-service-type
126 etc-bashrc-d-service-type 126 etc-bashrc-d-service-type
127 %default-etc-bashrc-d-files 127 %default-etc-bashrc-d-files
128 %default-bash-aliases
128 etc-directory 129 etc-directory
129 privileged-program-service-type 130 privileged-program-service-type
130 setuid-program-service-type ; deprecated 131 setuid-program-service-type ; deprecated
@@ -993,9 +994,27 @@ log in.")))
993(define files->bashrc-d-directory 994(define files->bashrc-d-directory
994 (make-files->etc-directory "bashrc.d")) 995 (make-files->etc-directory "bashrc.d"))
995 996
997;;; Use an alist to be compatible with <home-bash-configuration>.
998(define %default-bash-aliases
999 '(("ls" . "ls -p --color=auto")
1000 ("ll" . "ls -l")
1001 ("grep" . "grep --color=auto")
1002 ("ip" . "ip -color=auto")))
1003
1004;;; ... but avoid the full blown bash-serialize-aliases, which depends on
1005;;; other 'guix home' definitions such as `shell-double-quote'.
1006(define %default-bashrc-d-aliases
1007 (plain-file "aliases.sh"
1008 (string-join
1009 (map (match-lambda
1010 ((alias . value)
1011 (format #f "~a=~s~%" alias value)))
1012 %default-bash-aliases)
1013 "")))
1014
996(define %default-etc-bashrc-d-files 1015(define %default-etc-bashrc-d-files
997 (list (file-append bash-completion 1016 (list (file-append bash-completion "/etc/profile.d/bash_completion.sh")
998 "/etc/profile.d/bash_completion.sh"))) 1017 %default-bashrc-d-aliases))
999 1018
1000(define etc-bashrc-d-service-type 1019(define etc-bashrc-d-service-type
1001 (service-type 1020 (service-type
diff --git a/gnu/services/base.scm b/gnu/services/base.scm
index 5a3dd2f555d..1f5da97f0a7 100644
--- a/gnu/services/base.scm
+++ b/gnu/services/base.scm
@@ -4138,7 +4138,10 @@ login manager daemon.")
4138 (service mingetty-service-type (mingetty-configuration 4138 (service mingetty-service-type (mingetty-configuration
4139 (tty "tty5"))) 4139 (tty "tty5")))
4140 (service mingetty-service-type (mingetty-configuration 4140 (service mingetty-service-type (mingetty-configuration
4141 (tty "tty6"))) 4141 (tty "tty6")))
4142
4143 ;; Extra Bash configuration including Bash completion and aliases.
4144 (service etc-bashrc-d-service-type)
4142 4145
4143 (service static-networking-service-type 4146 (service static-networking-service-type
4144 (list %loopback-static-networking)) 4147 (list %loopback-static-networking))
diff --git a/gnu/system.scm b/gnu/system.scm
index 30f55967c43..06181606fa4 100644
--- a/gnu/system.scm
+++ b/gnu/system.scm
@@ -1141,23 +1141,13 @@ for i in /etc/profile.d/*.sh; do
1141done 1141done
1142unset i 1142unset i
1143 1143
1144if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ] 1144if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]; then
1145then 1145 # Load Bash-specific initialization code, taking care to not source
1146 # Load Bash-specific initialization code. 1146 # /etc/bashrc when invoked from a non-interactive SSH shell,
1147 . /etc/bashrc 1147 # to avoid recursion (/etc/bashrc also sources /etc/profile
1148 # in the non-login, non-interactive SSH case).
1149 [[ $- != *i* && -n $SSH_CLIENT ]] || source /etc/bashrc
1148fi 1150fi
1149"))
1150
1151 (bashrc (plain-file "bashrc" "\
1152# Bash-specific initialization.
1153
1154# Provide a default prompt. The user's ~/.bashrc can override it.
1155PS1='\\u@\\h \\w${GUIX_ENVIRONMENT:+ [env]}\\$ '
1156
1157for i in /etc/bashrc.d/*.sh; do
1158 [[ -r $i ]] && source \"$i\"
1159done
1160unset i
1161"))) 1151")))
1162 (service etc-service-type 1152 (service etc-service-type
1163 `(("os-release" ,os-release) 1153 `(("os-release" ,os-release)
@@ -1168,7 +1158,7 @@ unset i
1168 ("issue" ,issue) 1158 ("issue" ,issue)
1169 ,@(if nsswitch `(("nsswitch.conf" ,nsswitch)) '()) 1159 ,@(if nsswitch `(("nsswitch.conf" ,nsswitch)) '())
1170 ("profile" ,profile) 1160 ("profile" ,profile)
1171 ("bashrc" ,bashrc) 1161 ("bashrc" ,%default-bashrc)
1172 ;; Write the operating-system-host-name to /etc/hostname to prevent 1162 ;; Write the operating-system-host-name to /etc/hostname to prevent
1173 ;; NetworkManager from changing the system's hostname when connecting 1163 ;; NetworkManager from changing the system's hostname when connecting
1174 ;; to certain networks. Some discussion at 1164 ;; to certain networks. Some discussion at
diff --git a/gnu/system/shadow.scm b/gnu/system/shadow.scm
index b9a5a6134bc..6443fd9922d 100644
--- a/gnu/system/shadow.scm
+++ b/gnu/system/shadow.scm
@@ -147,8 +147,11 @@
147 147
148(define %default-bashrc 148(define %default-bashrc
149 (plain-file "bashrc" "\ 149 (plain-file "bashrc" "\
150# Bash initialization for interactive non-login shells and 150# Bash-specific initialization, including for non-login and remote
151# for remote shells (info \"(bash) Bash Startup Files\"). 151# shells (info \"(bash) Bash Startup Files\").
152
153# Provide a default prompt.
154PS1='\\u@\\h \\w${GUIX_ENVIRONMENT:+ [env]}\\$ '
152 155
153# Export 'SHELL' to child processes. Programs such as 'screen' 156# Export 'SHELL' to child processes. Programs such as 'screen'
154# honor it and otherwise use /bin/sh. 157# honor it and otherwise use /bin/sh.
@@ -165,10 +168,11 @@ then
165 return 168 return
166fi 169fi
167 170
168alias ls='ls -p --color=auto' 171for i in /etc/bashrc.d/*.sh; do
169alias ll='ls -l' 172 [[ -r $i ]] && source \"$i\"
170alias grep='grep --color=auto' 173done
171alias ip='ip -color=auto'\n")) 174unset i
175"))
172 176
173(define %default-bash-profile 177(define %default-bash-profile
174 (plain-file "bash_profile" "\ 178 (plain-file "bash_profile" "\
@@ -289,12 +293,10 @@ home-config"))
289'useradd' in the home directory of newly created user accounts." 293'useradd' in the home directory of newly created user accounts."
290 294
291 (let ((profile %default-bash-profile) 295 (let ((profile %default-bash-profile)
292 (bashrc %default-bashrc)
293 (zprofile %default-zprofile) 296 (zprofile %default-zprofile)
294 (xdefaults %default-xdefaults) 297 (xdefaults %default-xdefaults)
295 (gdbinit %default-gdbinit)) 298 (gdbinit %default-gdbinit))
296 `((".bash_profile" ,profile) 299 `((".bash_profile" ,profile)
297 (".bashrc" ,bashrc)
298 ;; Zsh sources ~/.zprofile before ~/.zshrc, and it sources ~/.zlogin 300 ;; Zsh sources ~/.zprofile before ~/.zshrc, and it sources ~/.zlogin
299 ;; after ~/.zshrc. To avoid interfering with any customizations a user 301 ;; after ~/.zshrc. To avoid interfering with any customizations a user
300 ;; may have made in their ~/.zshrc, put this in .zprofile, not .zlogin. 302 ;; may have made in their ~/.zshrc, put this in .zprofile, not .zlogin.
diff --git a/gnu/tests/base.scm b/gnu/tests/base.scm
index 0f31c152e0f..db904d7f102 100644
--- a/gnu/tests/base.scm
+++ b/gnu/tests/base.scm
@@ -187,6 +187,8 @@ test \"$PROFILE_D_OK\" = yes")
187 "\ 187 "\
188. /etc/bashrc 188. /etc/bashrc
189set -e -x 189set -e -x
190test -f /etc/bashrc.d/bash_completion.sh
191test -f /etc/bashrc.d/aliases.sh
190test -f /etc/bashrc.d/test_bashrc_d.sh 192test -f /etc/bashrc.d/test_bashrc_d.sh
191test \"$BASHRC_D_OK\" = yes")) 193test \"$BASHRC_D_OK\" = yes"))
192 marionette))) 194 marionette)))
@@ -606,14 +608,15 @@ functionality tests, using the given KERNEL.")
606 (plain-file 608 (plain-file
607 "invalid-name" 609 "invalid-name"
608 "not a POSIX script -- ignore me"))) 610 "not a POSIX script -- ignore me")))
609 (service 611 (simple-service
612 'extra-bashrc-d-files
610 etc-bashrc-d-service-type 613 etc-bashrc-d-service-type
611 (list (plain-file 614 (list (plain-file
612 "test_bashrc_d.sh" 615 "test_bashrc_d.sh"
613 "export BASHRC_D_OK=yes\n") 616 "export BASHRC_D_OK=yes\n")
614 (plain-file 617 (plain-file
615 "invalid-name" 618 "invalid-name"
616 "not a Bash script -- ignore me"))) 619 "not a Bash script -- ignore me")))
617 %base-services))) 620 %base-services)))
618 #:imported-modules '((gnu services herd) 621 #:imported-modules '((gnu services herd)
619 (guix combinators)))) 622 (guix combinators))))