Subject: [PATCH] home: Add home-xorg-server-service-type.

From: Sergey Trofimov <[email-masked]>
Date: Sun, 19 Mar 2023 22:54:09 +0100

* gnu/home/services/desktop.scm
(home-xorg-server-service-type): New variable.
* gnu/services/xorg.scm (xorg-server-wrapper-package):
Rename from xorg-server-profile-server and export.
* doc/guix.texi: Document home-xorg-server-service-type.
---
 doc/guix.texi                 | 27 +++++++++++++++++++
 gnu/home/services/desktop.scm | 17 +++++++++++-
 gnu/services/xorg.scm         | 51 ++++++++++++++++++-----------------
 3 files changed, 69 insertions(+), 26 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index fa9ea5a6ec..9c1df5d1b5 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -42661,6 +42661,33 @@ The list of expressions to be read by @code{xmodmap} on service startup.
 @end table
 @end deftp
 
+@defvar home-xorg-server-service-type
+This is the service type to run rootless @code{xorg-server}.  It takes
+@code{xorg-configuration} which is used to generate the Xorg server
+start script.
+
+@lisp
+(service home-xorg-server-service-type
+         (xorg-configuration
+          (modules (list xf86-video-intel xf86-input-libinput))
+          (drivers (list "intel"))))
+@end lisp
+
+Xorg can be started after logging in the console using @command{xinit}
+or @command{sx}.  This could also be automated by adding auto-start
+launcher to the shell's rc file.
+
+@lisp
+(simple-service
+ 'sx-autostart home-bash-service-type
+ (home-bash-extension
+  (bash-profile
+   (list (plain-file
+          "bash-sx-autostart"
+          "[[ ! $DISPLAY && $(tty) == /dev/tty1 ]] && exec sx")))))
+@end lisp
+@end defvar
+
 @node Guix Home Services
 @subsection Guix Home Services
 
diff --git a/gnu/home/services/desktop.scm b/gnu/home/services/desktop.scm
index fb1cd44060..cfb2dd52fa 100644
--- a/gnu/home/services/desktop.scm
+++ b/gnu/home/services/desktop.scm
@@ -25,6 +25,7 @@ (define-module (gnu home services desktop)
   #:autoload   (gnu packages glib)    (dbus)
   #:autoload   (gnu packages xdisorg) (redshift unclutter)
   #:autoload   (gnu packages xorg) (setxkbmap xmodmap)
+  #:use-module (gnu services xorg)
   #:use-module (guix records)
   #:use-module (guix gexp)
   #:use-module (srfi srfi-1)
@@ -37,7 +38,11 @@ (define-module (gnu home services desktop)
             home-dbus-service-type
 
             home-unclutter-configuration
-            home-unclutter-service-type))
+            home-unclutter-service-type
+
+            home-xorg-server-service-type)
+
+  #:re-export (xorg-configuration))
 
 
 ;;;
@@ -332,3 +337,13 @@ (define home-xmodmap-service-type
    (default-value (home-xmodmap-configuration))
    (description "Run the @code{xmodmap} utility to modify keymaps and pointer
 buttons under the Xorg display server via user-defined expressions.")))
+
+(define home-xorg-server-service-type
+  (service-type
+   (name 'xorg-server)
+   (extensions
+    (list (service-extension home-profile-service-type
+                             (compose list xorg-server-wrapper-package))))
+   (default-value (xorg-configuration))
+   (description "Add @command{X} to the home profile, to be used with
+@command{sx} or @command{xinit}.")))
diff --git a/gnu/services/xorg.scm b/gnu/services/xorg.scm
index c4745cecf5..0e8f352926 100644
--- a/gnu/services/xorg.scm
+++ b/gnu/services/xorg.scm
@@ -84,6 +84,7 @@ (define-module (gnu services xorg)
             xorg-wrapper
             xorg-start-command
             xinitrc
+            xorg-server-wrapper-package
             xorg-server-service-type
 
             %default-slim-theme
@@ -506,37 +507,37 @@ (define-syntax handle-xorg-configuration
                       (xorg-configuration xorg-configuration))
                      config)))))))
 
-(define (xorg-server-profile-service config)
-  ;; XXX: profile-service-type only accepts <package> objects.
-  (list
-   (package
-     (name "xorg-wrapper")
-     (version (package-version xorg-server))
-     (source (xorg-wrapper config))
-     (build-system trivial-build-system)
-     (arguments
-      '(#:modules ((guix build utils))
-        #:builder
-        (begin
-          (use-modules (guix build utils))
-          (let* ((source (assoc-ref %build-inputs "source"))
-                 (out (assoc-ref %outputs "out"))
-                 (bin (string-append out "/bin")))
-            (mkdir-p bin)
-            (symlink source (string-append bin "/X"))
-            (symlink source (string-append bin "/Xorg"))
-            #t))))
-     (home-page (package-home-page xorg-server))
-     (synopsis (package-synopsis xorg-server))
-     (description (package-description xorg-server))
-     (license (package-license xorg-server)))))
+(define (xorg-server-wrapper-package config)
+  "Return a package with @command{X} and @command{Xorg} commands launching
+xorg-server with provided @var{config}."
+  (package
+    (name "xorg-wrapper")
+    (version (package-version xorg-server))
+    (source (xorg-wrapper config))
+    (build-system trivial-build-system)
+    (arguments
+     '(#:modules ((guix build utils))
+       #:builder
+       (begin
+         (use-modules (guix build utils))
+         (let* ((source (assoc-ref %build-inputs "source"))
+                (out (assoc-ref %outputs "out"))
+                (bin (string-append out "/bin")))
+           (mkdir-p bin)
+           (symlink source (string-append bin "/X"))
+           (symlink source (string-append bin "/Xorg"))
+           #t))))
+    (home-page (package-home-page xorg-server))
+    (synopsis (package-synopsis xorg-server))
+    (description (package-description xorg-server))
+    (license (package-license xorg-server))))
 
 (define xorg-server-service-type
   (service-type
    (name 'xorg-server)
    (extensions
     (list (service-extension profile-service-type
-                             xorg-server-profile-service)))
+                             (compose list xorg-server-wrapper-package))))
    (default-value (xorg-configuration))
    (description "Add @command{X} to the system profile, to be used with
 @command{sx} or @command{xinit}.")))
-- 
2.39.2