guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

12/13: services: Add the Guix Home service.


From: guix-commits
Subject: 12/13: services: Add the Guix Home service.
Date: Wed, 17 Apr 2024 06:16:50 -0400 (EDT)

civodul pushed a commit to branch master
in repository guix.

commit 59bb53823e6babb2d50e246d312879980c7988c9
Author: Richard Sent <richard@freakingpenguin.com>
AuthorDate: Thu Mar 21 14:36:43 2024 -0400

    services: Add the Guix Home service.
    
    This patch adds a Guix Home service, which allows for configuring/deploying 
an
    operating-system declaration with an associated home-environment.
    
    * gnu/services/guix.scm: Add guix-home-service and 
guix-home-shepherd-service
    * gnu/home/services/shepherd.scm: Don't attempt to launch user shepherd when
    the system shepherd runs guix-home-<user>
    * doc/guix.texi: Add documentation for guix-home-service
    * gnu/tests/guix.scm: Add a test to verify guix-home-service-type is able to
    activate a home environment
    
    Change-Id: Ifbcc0878d934aa4abe34bb2123b5081fb432aa8e
    Signed-off-by: Ludovic Courtès <ludo@gnu.org>
---
 doc/guix.texi                  | 32 ++++++++++++++++++
 gnu/home/services/shepherd.scm |  7 +++-
 gnu/services/guix.scm          | 38 ++++++++++++++++++++++
 gnu/tests/guix.scm             | 73 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 149 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 964a262a7a..ec0edbbbf0 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -126,6 +126,7 @@ Copyright @copyright{} 2023 Graham James Addis@*
 Copyright @copyright{} 2023 Tomas Volf@*
 Copyright @copyright{} 2024 Herman Rimm@*
 Copyright @copyright{} 2024 Matthew Trzcinski@*
+Copyright @copyright{} 2024 Richard Sent@*
 
 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -39350,6 +39351,37 @@ Extra command line options for 
@code{guix-data-service-process-jobs}.
 @end table
 @end deftp
 
+@subsubheading Guix Home Service
+The Guix Home Service allows for associating Guix @ref{Declaring the
+Home Environment, home-environment} declarations with a Guix
+@ref{operating-system Reference, operating-system}.
+
+@defvar guix-home-service-type
+Service type for the Guix Home Service.  Its value must be a list of
+lists containing user and home environment pairs.  The key of each pair
+is a string representing the user to deploy the configuration under and
+the value is a home-environment configuration.
+
+@lisp
+(define my-home
+  (home-environment
+   ...))
+
+(operating-system
+  (services (list
+             (service guix-home-service-type
+                      `(("alice" ,my-home))))))
+@end lisp
+
+This service can be extended by other services to add additional home
+environments, as in this example:
+
+@lisp
+(simple-service 'my-extra-home home-service-type
+                `(("bob" ,my-extra-home))))
+@end lisp
+@end defvar
+
 @subsubheading Nar Herder
 The @uref{https://git.cbaines.net/guix/nar-herder/about/,Nar Herder} is
 a utility for managing a collection of nars.
diff --git a/gnu/home/services/shepherd.scm b/gnu/home/services/shepherd.scm
index 962e633618..a3ca21b319 100644
--- a/gnu/home/services/shepherd.scm
+++ b/gnu/home/services/shepherd.scm
@@ -144,7 +144,12 @@ as shepherd package."
               (format #f "/run/user/~a" (getuid)))
           "/shepherd/socket"))
         #$(reload-configuration-gexp config)
-        #$(launch-shepherd-gexp config)))
+        ;; Don't attempt to start user shepherd if the system is running the
+        ;; activation script. /run/user/<uid> may not have been created
+        ;; yet. But do otherwise so if the runtime dir does not exist an error
+        ;; is logged.
+        (unless (getenv "GUIX_SYSTEM_IS_RUNNING_HOME_ACTIVATE")
+          #$(launch-shepherd-gexp config))))
 
 (define (shepherd-xdg-configuration-files config)
   `(("shepherd/init.scm" ,(home-shepherd-configuration-file config))))
diff --git a/gnu/services/guix.scm b/gnu/services/guix.scm
index 8b326d9124..96f5ecaac0 100644
--- a/gnu/services/guix.scm
+++ b/gnu/services/guix.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2019, 2020, 2021, 2022 Christopher Baines <mail@cbaines.net>
+;;; Copyright © 2024 Andrew Tropin <andrew@trop.in>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -101,6 +102,8 @@
 
             guix-data-service-type
 
+            guix-home-service-type
+
             nar-herder-service-type
             nar-herder-configuration
             nar-herder-configuration?
@@ -687,6 +690,41 @@ ca-certificates.crt file in the system profile."
     "Run an instance of the Guix Data Service.")))
 
 
+;;;
+;;; Guix Home Service
+;;;
+
+(define (guix-home-shepherd-service config)
+  (map (match-lambda
+         ((user he)
+          (shepherd-service
+           (documentation "Activate Guix Home.")
+           (requirement '(user-processes))
+           (provision (list (symbol-append 'guix-home- (string->symbol user))))
+           (one-shot? #t)
+           (auto-start? #t)
+           (start #~(make-forkexec-constructor
+                     '(#$(file-append he "/activate"))
+                     #:user #$user
+                     #:environment-variables
+                     (list (string-append "HOME=" (passwd:dir (getpw #$user)))
+                           "GUIX_SYSTEM_IS_RUNNING_HOME_ACTIVATE=t")
+                     #:group (group:name (getgrgid (passwd:gid (getpw 
#$user))))))
+           (stop #~(make-kill-destructor)))))
+       config))
+
+(define guix-home-service-type
+  (service-type
+   (name 'guix-home)
+   (description "Sets up Guix Home for the specified user accounts.")
+   (extensions (list (service-extension
+                      shepherd-root-service-type
+                      guix-home-shepherd-service)))
+   (compose concatenate)
+   (extend append)
+   (default-value '())))
+
+
 ;;;
 ;;; Nar Herder
 ;;;
diff --git a/gnu/tests/guix.scm b/gnu/tests/guix.scm
index 240ded4825..12ad1bf255 100644
--- a/gnu/tests/guix.scm
+++ b/gnu/tests/guix.scm
@@ -17,6 +17,8 @@
 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
 
 (define-module (gnu tests guix)
+  #:use-module (gnu home)
+  #:use-module (gnu home services)
   #:use-module (gnu tests)
   #:use-module (gnu system)
   #:use-module (gnu system file-systems)
@@ -37,6 +39,7 @@
   #:use-module (ice-9 match)
   #:export (%test-guix-build-coordinator
             %test-guix-data-service
+            %test-guix-home-service
             %test-nar-herder
             %test-bffe))
 
@@ -252,6 +255,76 @@ host       all     all     ::1/128         trust"))))))
    (value (run-guix-data-service-test))))
 
 
+;;;
+;;; Guix Home
+;;;
+
+(define %guix-home-service-he
+  (home-environment
+   (services
+    (list (simple-service 'guix-home-service-test
+                          home-files-service-type
+                          `(("guix-home-service-activated"
+                             ,(plain-file "guix-home-service-activated"
+                                          "Guix Home service activated"))))))))
+
+(define %guix-home-service-os
+  (simple-operating-system
+   (service guix-home-service-type
+            `(("alice" ,%guix-home-service-he)))))
+
+(define (run-guix-home-service-test)
+  (define os
+    (marionette-operating-system
+     %guix-home-service-os
+     #:imported-modules '((gnu services herd))))
+
+  (define vm
+    (virtual-machine
+     (operating-system os)
+     (memory-size 1024)))
+
+  (define test
+    (with-imported-modules '((gnu build marionette))
+      #~(begin
+          (use-modules (srfi srfi-64)
+                       (gnu build marionette))
+
+          (define marionette
+            (make-marionette (list #$vm)))
+
+          (test-runner-current (system-test-runner #$output))
+          (test-begin "guix-home-service")
+
+          (test-assert "service started"
+            (marionette-eval
+             '(begin
+                (use-modules (gnu services herd))
+                (match (start-service 'guix-home-alice)
+                  (#f #f)
+                  ;; herd returns (running #f), likely because of one shot,
+                  ;; so consider any non-error a success.
+                  (('service response-parts ...) #t)))
+             marionette))
+
+          (test-assert "file-exists"
+            (marionette-eval
+             '(begin
+                (sleep 3) ;make sure service has time to symlink files
+                (file-exists? "/home/alice/guix-home-service-activated"))
+             marionette))
+
+          (test-end))))
+
+  (gexp->derivation "guix-home-service-test" test))
+
+(define %test-guix-home-service
+  (system-test
+   (name "guix-home-service")
+   (description "Activate a Guix home environment.")
+   (value (run-guix-home-service-test))))
+
+
 ;;;
 ;;; Nar Herder
 ;;;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]