summary refs log tree commit diff
path: root/gnu/home/services/shells.scm
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/home/services/shells.scm')
-rw-r--r--gnu/home/services/shells.scm126
1 files changed, 68 insertions, 58 deletions
diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index ca7f4ac0ad..7b9769bcf3 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -171,56 +171,27 @@ Used for executing user's commands at the exit of login shell.  It
 won't be read in some cases (if the shell terminates by exec'ing
 another process for example)."))
 
-(define (add-zsh-configuration config)
-  (let* ((xdg-flavor? (home-zsh-configuration-xdg-flavor? config)))
-
-    (define prefix-file
-      (cut string-append
-        (if xdg-flavor?
-            "config/zsh/."
-            "") <>))
-
-    (define (filter-fields field)
-      (filter-configuration-fields home-zsh-configuration-fields
-                                   (list field)))
-
-    (define (serialize-field field)
-      (serialize-configuration
-       config
-       (filter-fields field)))
-
-    (define (file-if-not-empty field)
-      (let ((file-name (symbol->string field))
-            (field-obj (car (filter-fields field))))
-        (if (not (null? ((configuration-field-getter field-obj) config)))
-            `(,(prefix-file file-name)
-              ,(mixed-text-file
-                file-name
-                (serialize-field field)))
-            '())))
-
-    (filter
-     (compose not null?)
-     `(,(if xdg-flavor?
-            `("zshenv"
-              ,(mixed-text-file
-                "auxiliary-zshenv"
-                (if xdg-flavor?
-                    "source ${XDG_CONFIG_HOME:-$HOME/.config}/zsh/.zshenv\n"
-                    "")))
-            '())
-       (,(prefix-file "zshenv")
-        ,(mixed-text-file
-          "zshenv"
-          (if xdg-flavor?
-              "export ZDOTDIR=${XDG_CONFIG_HOME:-$HOME/.config}/zsh\n"
-              "")
-          (serialize-field 'zshenv)
-          (serialize-field 'environment-variables)))
-       (,(prefix-file "zprofile")
-        ,(mixed-text-file
-          "zprofile"
-          "\
+(define (zsh-filter-fields field)
+  (filter-configuration-fields home-zsh-configuration-fields (list field)))
+
+(define (zsh-serialize-field config field)
+  (serialize-configuration config (zsh-filter-fields field)))
+
+(define* (zsh-field-not-empty? config field)
+  (let ((file-name (symbol->string field))
+        (field-obj (car (zsh-filter-fields field))))
+    (not (null? ((configuration-field-getter field-obj) config)))))
+
+(define (zsh-file-zshenv config)
+  (mixed-text-file
+   "zshenv"
+   (zsh-serialize-field config 'zshenv)
+   (zsh-serialize-field config 'environment-variables)))
+
+(define (zsh-file-zprofile config)
+  (mixed-text-file
+   "zprofile"
+   "\
 # Setups system and user profiles and related variables
 source /etc/profile
 # Setups home environment profile
@@ -229,11 +200,47 @@ source ~/.profile
 # It's only necessary if zsh is a login shell, otherwise profiles will
 # be already sourced by bash
 "
-          (serialize-field 'zprofile)))
-
-       ,@(list (file-if-not-empty 'zshrc)
-               (file-if-not-empty 'zlogin)
-               (file-if-not-empty 'zlogout))))))
+   (zsh-serialize-field config 'zprofile)))
+
+(define (zsh-file-by-field config field)
+  (match field
+    ('zshenv (zsh-file-zshenv config))
+    ('zprofile (zsh-file-zprofile config))
+    (e (mixed-text-file
+        (symbol->string field)
+        (zsh-serialize-field config field)))))
+
+(define (zsh-get-configuration-files config)
+  `(("zprofile" ,(zsh-file-by-field config 'zprofile)) ;; Always non-empty
+    ,@(if (and (zsh-field-not-empty? config 'zshenv)
+               (zsh-field-not-empty? config 'environment-variables))
+          `(("zshenv" ,(zsh-file-by-field config 'zshenv))) '())
+    ,@(if (zsh-field-not-empty? config 'zshrc)
+          `(("zshrc" ,(zsh-file-by-field config 'zshrc))) '())
+    ,@(if (zsh-field-not-empty? config 'zlogin)
+          `(("zlogin" ,(zsh-file-by-field config 'zlogin))) '())
+    ,@(if (zsh-field-not-empty? config 'zlogout)
+          `(("zlogout" ,(zsh-file-by-field config 'zlogout))) '())))
+
+(define (zsh-home-files config)
+  (define zshenv-auxiliary-file
+    (mixed-text-file
+     "zshenv-auxiliary"
+     "export ZDOTDIR=${XDG_CONFIG_HOME:-$HOME/.config}/zsh\n"
+     "[[ -f $ZDOTDIR/.zshenv ]] && source $ZDOTDIR/.zshenv\n"))
+
+  (if (home-zsh-configuration-xdg-flavor? config)
+      `(("zshenv" ,zshenv-auxiliary-file))
+      (zsh-get-configuration-files config)))
+
+(define (zsh-xdg-configuration-files config)
+  (if (home-zsh-configuration-xdg-flavor? config)
+      (map
+       (lambda (lst)
+         (cons (string-append "zsh/." (car lst))
+               (cdr lst)))
+       (zsh-get-configuration-files config))
+      '()))
 
 (define (add-zsh-packages config)
   (list (home-zsh-configuration-package config)))
@@ -291,7 +298,10 @@ source ~/.profile
                 (extensions
                  (list (service-extension
                         home-files-service-type
-                        add-zsh-configuration)
+                        zsh-home-files)
+                       (service-extension
+                        home-xdg-configuration-files-service-type
+                        zsh-xdg-configuration-files)
                        (service-extension
                         home-profile-service-type
                         add-zsh-packages)))
@@ -324,7 +334,7 @@ source ~/.profile
   (guix-defaults?
    (boolean #t)
    "Add sane defaults like reading @file{/etc/bashrc} and coloring the output of
-@command{ls} to the end of the @file{.bashrc} file.")
+@command{ls} to the top of the @file{.bashrc} file.")
   (environment-variables
    (alist '())
    "Association list of environment variables to set for the Bash session.  The
@@ -448,7 +458,7 @@ if [ -f ~/.bashrc ]; then source ~/.bashrc; fi
               'bashrc
               (if (home-bash-configuration-guix-defaults? config)
                   (list (serialize-field 'aliases) guix-bashrc)
-                  (list (serialize-field 'alises))))
+                  (list (serialize-field 'aliases))))
              (file-if-not-empty 'bash-logout)))))
 
 (define (add-bash-packages config)