From patchwork Mon May 19 00:29:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 42741 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id E72A127BC4A; Mon, 19 May 2025 01:31:25 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL, RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 55A7827BC49 for ; Mon, 19 May 2025 01:31:24 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uGoPY-0003xX-Jl; Sun, 18 May 2025 20:31:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uGoPS-0003v5-Gl for guix-patches@gnu.org; Sun, 18 May 2025 20:31:16 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uGoPQ-0001FY-L7; Sun, 18 May 2025 20:31:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=XwCkbEj8vfgAwu7Ge8WVzlgQQKDphxSijvbBFRVwgEA=; b=r37CLFK2vQI4SizZRmibjqdP0WDR89dToQQevQRBxjLs7fVbYJGInqxhC1rAR0kTJq3zw9hhYR9sYZvoRpD4FhDHkYZ7Fd0fZgsNcuoq6mtp6uC/FokCCLDmis/SQVsECLQXWHsHzCRMrP6eWHYdUrn/dqIiKzTxB4Cg6QZWYT7MXJqaMdHBs8aXFV8+oEywAEa+CAe+HjbJy1Pu8vo9bogmmoR/SnyL25YUlz7BaaVqF2Rft5Rp4+9jZh52wqLKUiUhckMVmq4hRIpftEI5k0XZjdrQK38d86n6+rDwOQeRxn0JrEZpuXCE0VJ1IZHpWcj1l/cnj5FcW2sEInOBPA==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1uGoPK-0000C6-Ob; Sun, 18 May 2025 20:31:06 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#78308] [PATCH v3 07/10] system: Factorize bashrc default configuration. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: maxim.cournoyer@gmail.com, rutherther@ditigal.xyz, lgcoelho@disroot.org, andrew@trop.in, gabriel@erlikon.ch, hako@ultrarare.space, janneke@gnu.org, ludo@gnu.org, tanguy@bioneland.org, guix-patches@gnu.org Resent-Date: Mon, 19 May 2025 00:31:06 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 78308 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 78308@debbugs.gnu.org Cc: Maxim Cournoyer , Maxim Cournoyer , Rutherther , Luis Guilherme Coelho , Andrew Tropin , Gabriel Wicki , Hilton Chain , Janneke Nieuwenhuizen , Ludovic =?utf-8?q?Court=C3=A8s?= , Tanguy Le Carrour X-Debbugs-Original-Xcc: Maxim Cournoyer , Rutherther , Luis Guilherme Coelho , Andrew Tropin , Gabriel Wicki , Hilton Chain , Janneke Nieuwenhuizen , Ludovic =?utf-8?q?Court=C3=A8s?= , Tanguy Le Carrour Received: via spool by 78308-submit@debbugs.gnu.org id=B78308.174761461327093 (code B ref 78308); Mon, 19 May 2025 00:31:06 +0000 Received: (at 78308) by debbugs.gnu.org; 19 May 2025 00:30:13 +0000 Received: from localhost ([127.0.0.1]:34063 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uGoOS-00071b-Kg for submit@debbugs.gnu.org; Sun, 18 May 2025 20:30:13 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]:55774) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1uGoOB-0006hY-1x for 78308@debbugs.gnu.org; Sun, 18 May 2025 20:29:58 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-742b614581dso2209142b3a.3 for <78308@debbugs.gnu.org>; Sun, 18 May 2025 17:29:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747614588; x=1748219388; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XwCkbEj8vfgAwu7Ge8WVzlgQQKDphxSijvbBFRVwgEA=; b=D25q1eFzxNIoICkqB1cUvOFYGwQEL8CBamdAFXVUhuSo4mtg1oMVZDreEA+LRWAwxK gIXVmdm9s4zlCasNkPUPz5PKGRg31evLcQTWQEtQBuCVn0ElmhmDSOTQNbaWhdGGprht +JD+l81i9yHWMBAz0QyN5otNOrDqklEWxAQYpRQDI+YvYxDSQBttxtwztLeLWE72JkFs xxuhlkWN7Fj2/rcm1FPKP1Wjm2nowvgdFravMX63bhDJ9xCS/JWK6xRYreC+7ZX6Yv7A B9eUwz25huryoeYq9iDd5C2vj3MozzWZVriFGDfI7GAMMe53gxW1bK7ayXVQI4d9AOFH MYPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747614588; x=1748219388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XwCkbEj8vfgAwu7Ge8WVzlgQQKDphxSijvbBFRVwgEA=; b=jZBsV8/YahgSJ/DFphbbTAdDFC78tafBpHM1XFIegm//jOWtRDcEVrTxbP154ejcoR 154RZ772zX3w9zkEjpLvH7m6Wmy+IcbtX+ki7VI6UaX9Zjx6PXTTMZO9TpHiodIqOEXE 1TXTtsD1VpakH+pb8hx/2FCS2/ixrmNLyxlbWX5TXynuKDxKhpawnf91/iNlWJJEPIsl Y9ruv8NnfuG34WLL+gcz+Wyq9hqQXcMEArWjYR96ulOnUTXnMBCtyHr1Al4TMYNJ33jr u9drRRkoUAawi9ofmeNmg3dI2V5ISmazzDGO9nJKdK4A1daJTmg9jydYXsl0/DudIybW e9pg== X-Gm-Message-State: AOJu0YzXWh4RbgXJGawJrPmLiDY+2GBp0WSeB8q0E4oBL0lXGkCBo+UF aO89/gF83UfBSZMdIKPvYdqR5dEYqOqeLk+uwGAVj+avyPx508mlSjEdmUJIGg== X-Gm-Gg: ASbGncuUj8ax2r0Jn7djMI/5qkzF61pfSjZEUfXHnwE4EIIjM164Z94M/XJcKm7WKti qwmthPFvVpsQ2FnlWg1GPVV5r7SzI6mC6KPNjz8TBpeG9OeKjZU/rJ5PRB82Z+GQzIIPnLgblR4 GePF/+VN5mG55P4aFglk/HpbL9256wgzjatOimyv2QHm7E+Jc7OjnXGf2cHruKxYGMpMD9EoD20 6oWVZqUQlS7ncoGPPXsZ1i3+1X9G+oapYxSs0wCNHh0sM/T9tJYMTrWORBMn9uWDe1vMsUIg+F/ bH9Mk0aIzso50TkLGKJElJbS+dPpUFQ/z5uv3rcimFn5yEObNip+hmeefzJbGjHTHBN7eRc= X-Google-Smtp-Source: AGHT+IH3cvyi/yUTS11aZsYrBbOqGcJ3FhAZG1lBPWvR6eJAbuhAUnRR1ehDHvgKGfTznzyMV6BF1Q== X-Received: by 2002:a05:6a00:1994:b0:736:46b4:bef2 with SMTP id d2e1a72fcca58-742accc6d5fmr13334007b3a.6.1747614588263; Sun, 18 May 2025 17:29:48 -0700 (PDT) Received: from localhost.localdomain ([2405:6586:be0:0:83c8:d31d:2cec:f542]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742a970c882sm5254027b3a.55.2025.05.18.17.29.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 May 2025 17:29:47 -0700 (PDT) From: Maxim Cournoyer Date: Mon, 19 May 2025 09:29:05 +0900 Message-ID: <703cabd6f79c691b33c639ae7a1952ef1a7ebb0e.1747614548.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches 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): : Guard against souring /etc/bashrc in non-interactive, SSH case. : 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 --- doc/guix.texi | 19 +++++++++++-------- gnu/home/services/shells.scm | 14 +++++++------- gnu/services.scm | 23 +++++++++++++++++++++-- gnu/services/base.scm | 5 ++++- gnu/system.scm | 24 +++++++----------------- gnu/system/shadow.scm | 18 ++++++++++-------- gnu/tests/base.scm | 17 ++++++++++------- 7 files changed, 70 insertions(+), 50 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 746722caa61..80a109e2bb3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -47654,15 +47654,15 @@ Service Reference and are silently dropped. The default value is made available via the @var{%default-etc-bashrc-d-files} variable for users to extended. Package objects can also be provided directly to have their -@file{etc/bashrc.d/*.sh} prefixed files added. An example usage may -look like: +@file{etc/bashrc.d/*.sh} prefixed files added. Since the service is +part @code{%base-services}, it can be extended via @code{simple-service} +like so: @example (use-package-modules gnome) ;for the `vte' package -(service etc-bashrc-d-service-type - (append (list (file-append vte "/etc/profile.d/vte.sh")) - %default-etc-bashrc-d-files)) +(simple-service 'vte-integration etc-bashrc-d-service-type + (list (file-append vte "/etc/profile.d/vte.sh"))) @end example @end defvar @@ -49288,8 +49288,11 @@ Shells Home Services The Bash package to use. @item @code{guix-defaults?} (default: @code{#t}) (type: boolean) -Add sane defaults like reading @file{/etc/bashrc} and coloring the output of -@command{ls} to the top of the @file{.bashrc} file. +Add sane defaults like setting @env{PS1}, @env{SHELL}, and ensuring +@file{/etc/profile} is sourced for non-interactive SSH shells. If you +use Guix System, is it safe to set this to @code{#f}, as in this case +this is already taken care of by the globally installed +@file{/etc/bashrc}. @item @code{environment-variables} (default: @code{'()}) (type: alist) Association list of environment variables to set for the Bash session. The @@ -49297,7 +49300,7 @@ Shells Home Services 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) +@item @code{aliases} (default: @code{%default-bash-aliases}) (type: alist) 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, 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 @@ (define-module (gnu home services shells) #:use-module (gnu services configuration) + #:use-module ((gnu services) #:select (%default-bash-aliases)) #:autoload (gnu system shadow) (%default-bashrc %default-zprofile) #:use-module (gnu home services utils) #:use-module (gnu home services) @@ -333,9 +334,12 @@ (define-configuration home-bash-configuration (package bash) "The Bash package to use.") (guix-defaults? + ;; TODO: Set to #f when the target system is determined to be Guix System. (boolean #t) - "Add sane defaults like reading @file{/etc/bashrc} and coloring the output of -@command{ls} to the top of the @file{.bashrc} file.") + "Add sane defaults like setting @env{PS1}, @env{SHELL}, and ensuring +@file{/etc/profile} is sourced for non-interactive SSH shells. If you use +Guix System, is it safe to set this to @code{#f}, as in this case this is +already taken care of by the globally installed @file{/etc/bashrc}.") (environment-variables (alist '()) "Association list of environment variables to set for the Bash session. The @@ -344,7 +348,7 @@ (define-configuration home-bash-configuration added after the contents of the @code{bash-profile} field." (serializer serialize-posix-env-vars)) (aliases - (alist '()) + (alist %default-bash-aliases) "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 @@ -423,10 +427,6 @@ (define (add-bash-configuration config) 'bashrc (if (home-bash-configuration-guix-defaults? config) (list (plain-file-content %default-bashrc) "\n" - ;; The host distro might provide a bad 'PS1' - ;; default--e.g., not taking $GUIX_ENVIRONMENT into - ;; account. Provide a good default here when asked. - "PS1='\\u@\\h \\w${GUIX_ENVIRONMENT:+ [env]}\\$ '\n" (serialize-field 'aliases)) (list (serialize-field 'aliases)))) (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 @@ (define-module (gnu services) etc-profile-d-service-type etc-bashrc-d-service-type %default-etc-bashrc-d-files + %default-bash-aliases etc-directory privileged-program-service-type setuid-program-service-type ; deprecated @@ -993,9 +994,27 @@ (define etc-profile-d-service-type (define files->bashrc-d-directory (make-files->etc-directory "bashrc.d")) +;;; Use an alist to be compatible with . +(define %default-bash-aliases + '(("ls" . "ls -p --color=auto") + ("ll" . "ls -l") + ("grep" . "grep --color=auto") + ("ip" . "ip -color=auto"))) + +;;; ... but avoid the full blown bash-serialize-aliases, which depends on +;;; other 'guix home' definitions such as `shell-double-quote'. +(define %default-bashrc-d-aliases + (plain-file "aliases.sh" + (string-join + (map (match-lambda + ((alias . value) + (format #f "~a=~s~%" alias value))) + %default-bash-aliases) + ""))) + (define %default-etc-bashrc-d-files - (list (file-append bash-completion - "/etc/profile.d/bash_completion.sh"))) + (list (file-append bash-completion "/etc/profile.d/bash_completion.sh") + %default-bashrc-d-aliases)) (define etc-bashrc-d-service-type (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 @@ (define %base-services (service mingetty-service-type (mingetty-configuration (tty "tty5"))) (service mingetty-service-type (mingetty-configuration - (tty "tty6"))) + (tty "tty6"))) + + ;; Extra Bash configuration including Bash completion and aliases. + (service etc-bashrc-d-service-type) (service static-networking-service-type (list %loopback-static-networking)) diff --git a/gnu/system.scm b/gnu/system.scm index 50f739c17c7..0b251bd3a08 100644 --- a/gnu/system.scm +++ b/gnu/system.scm @@ -1132,23 +1132,13 @@ (define* (operating-system-etc-service os) done unset i -if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ] -then - # Load Bash-specific initialization code. - . /etc/bashrc +if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]; then + # Load Bash-specific initialization code, taking care to not source + # /etc/bashrc when invoked from a non-interactive SSH shell, + # to avoid recursion (/etc/bashrc also sources /etc/profile + # in the non-login, non-interactive SSH case). + [[ $- != *i* && -n $SSH_CLIENT ]] || source /etc/bashrc fi -")) - - (bashrc (plain-file "bashrc" "\ -# Bash-specific initialization. - -# Provide a default prompt. The user's ~/.bashrc can override it. -PS1='\\u@\\h \\w${GUIX_ENVIRONMENT:+ [env]}\\$ ' - -for i in /etc/bashrc.d/*.sh; do - [[ -r $i ]] && source \"$i\" -done -unset i "))) (service etc-service-type `(("os-release" ,os-release) @@ -1159,7 +1149,7 @@ (define* (operating-system-etc-service os) ("issue" ,issue) ,@(if nsswitch `(("nsswitch.conf" ,nsswitch)) '()) ("profile" ,profile) - ("bashrc" ,bashrc) + ("bashrc" ,%default-bashrc) ;; Write the operating-system-host-name to /etc/hostname to prevent ;; NetworkManager from changing the system's hostname when connecting ;; 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 @@ (define %base-user-accounts (define %default-bashrc (plain-file "bashrc" "\ -# Bash initialization for interactive non-login shells and -# for remote shells (info \"(bash) Bash Startup Files\"). +# Bash-specific initialization, including for non-login and remote +# shells (info \"(bash) Bash Startup Files\"). + +# Provide a default prompt. +PS1='\\u@\\h \\w${GUIX_ENVIRONMENT:+ [env]}\\$ ' # Export 'SHELL' to child processes. Programs such as 'screen' # honor it and otherwise use /bin/sh. @@ -165,10 +168,11 @@ (define %default-bashrc return fi -alias ls='ls -p --color=auto' -alias ll='ls -l' -alias grep='grep --color=auto' -alias ip='ip -color=auto'\n")) +for i in /etc/bashrc.d/*.sh; do + [[ -r $i ]] && source \"$i\" +done +unset i +")) (define %default-bash-profile (plain-file "bash_profile" "\ @@ -289,12 +293,10 @@ (define (default-skeletons) 'useradd' in the home directory of newly created user accounts." (let ((profile %default-bash-profile) - (bashrc %default-bashrc) (zprofile %default-zprofile) (xdefaults %default-xdefaults) (gdbinit %default-gdbinit)) `((".bash_profile" ,profile) - (".bashrc" ,bashrc) ;; Zsh sources ~/.zprofile before ~/.zshrc, and it sources ~/.zlogin ;; after ~/.zshrc. To avoid interfering with any customizations a user ;; 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 @@ (define* (run-basic-test os command #:optional (name "basic") "\ . /etc/bashrc set -e -x +test -f /etc/bashrc.d/bash_completion.sh +test -f /etc/bashrc.d/aliases.sh test -f /etc/bashrc.d/test_bashrc_d.sh test \"$BASHRC_D_OK\" = yes")) marionette))) @@ -606,14 +608,15 @@ (define* (test-basic-os #:optional (kernel linux-libre)) (plain-file "invalid-name" "not a POSIX script -- ignore me"))) - (service + (simple-service + 'extra-bashrc-d-files etc-bashrc-d-service-type - (list (plain-file - "test_bashrc_d.sh" - "export BASHRC_D_OK=yes\n") - (plain-file - "invalid-name" - "not a Bash script -- ignore me"))) + (list (plain-file + "test_bashrc_d.sh" + "export BASHRC_D_OK=yes\n") + (plain-file + "invalid-name" + "not a Bash script -- ignore me"))) %base-services))) #:imported-modules '((gnu services herd) (guix combinators))))