Subject: [PATCH v2 1/5] DRAFT gnu: home: services: Add home-git-daemon-service-type.

From: "Jan (janneke) Nieuwenhuizen" <[email-masked]>
Date: Thu, 16 Feb 2023 16:53:51 +0100

* gnu/home/services/shepherd-xyz.scm: New file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add it.
---
 gnu/home/services/shepherd-xyz.scm | 122 +++++++++++++++++++++++++++++
 gnu/local.mk                       |   3 +-
 2 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 gnu/home/services/shepherd-xyz.scm

diff --git a/gnu/home/services/shepherd-xyz.scm b/gnu/home/services/shepherd-xyz.scm
new file mode 100644
index 0000000000..4d84008dae
--- /dev/null
+++ b/gnu/home/services/shepherd-xyz.scm
@@ -0,0 +1,122 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2023 Janneke Nieuwenhuizen <[email-masked]>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu home services shepherd-xyz)
+  #:use-module (srfi srfi-26)
+
+  #:use-module (ice-9 match)
+
+  #:use-module (shepherd support)
+
+  #:use-module (gnu home services)
+  #:use-module (gnu home services shepherd)
+  #:use-module (gnu packages version-control)
+  #:use-module (gnu services configuration)
+  #:use-module (gnu services shepherd)
+  #:use-module (guix records)
+  #:use-module (guix gexp)
+
+  #:export (home-git-daemon-configuration
+            home-git-daemon-service-type))
+
+;;; Commentary:
+;;
+;; Home Services for the Shepherd.
+;;
+;; Example configurations.
+;;
+;; Git daemon: use $HOME/src as the base directory.
+;;
+;; (service home-git-service-type
+;;          (home-git-configuration
+;;           (root (string-append (getenv "HOME") "/src"))))
+;;
+;;; Code:
+
+;;;
+;;; Directories.
+;;;
+(define %user-data-home
+  ;; Default cache directory if shepherd is run as a normal user.
+  (string-append (or (getenv "XDG_DATA_HOME")
+                     (string-append user-homedir "/.local/share"))))
+
+(define %user-log-dir
+  ;; Default log directory if shepherd is run as a normal user.
+  (string-append (or (getenv "XDG_DATA_HOME")
+                     (string-append user-homedir "/.local/var/log"))))
+
+
+;;;
+;;; Git daemon.
+;;;
+(define-record-type* <home-git-daemon-configuration>
+  home-git-daemon-configuration make-home-git-daemon-configuration
+  home-git-daemon-configuration?
+  (git           home-git-daemon-git                     ;file-like
+                 (default git))
+  (root          home-git-daemon-root                    ;string
+                 (default (string-append %user-data-home
+                                         "/git-daemon")))
+  (export-all?   home-git-daemon-export-all?             ;boolean
+                 (default #f))
+  (listen        home-git-daemon-configuration-listen    ;list of string
+                 (default '()))
+  (port          home-git-daemon-configuration-port      ;number | #f
+                 (default #f))
+  (whitelist     home-git-daemon-configuration-whitelist ;list of string
+                 (default '()))
+  (extra-options home-git-daemon-extra-options           ;list of string
+                 (default '())))
+
+(define (home-git-daemon-services config)
+  "Return a <shepherd-service> for a git daemon with CONFIG."
+  (match config
+    (($ <home-git-daemon-configuration>
+        git root export-all? listen port whitelist extra-options)
+     (let* ((git (file-append git "/bin/git"))
+            (command `(,git
+                       "daemon"
+                       ,(string-append "--base-path=" root)
+                       ,@(if export-all?
+                             '("--export-all")
+                             '())
+                       ,@(map (cute string-append "--listen=" <>) listen)
+                       ,@(if port
+                             `(,(string-append
+                                 "--port=" (number->string port)))
+                             '())
+                       ,@extra-options
+                       ,@whitelist))
+            (log-file (string-append %user-log-dir "/git-daemon.log")))
+       (list (shepherd-service
+              (documentation "Run the git daemon.")
+              (provision '(git-daemon))
+              (start #~(make-forkexec-constructor '#$command
+                                                  #:log-file #$log-file))
+              (stop #~(make-kill-destructor))))))))
+
+(define home-git-daemon-service-type
+  (service-type
+   (name 'home-git-daemon)
+   (default-value (home-git-daemon-configuration))
+   (extensions
+    (list (service-extension home-shepherd-service-type
+                             home-git-daemon-services)))
+   (description
+    "Install and configure the git-daemon as a shepherd service.")))
diff --git a/gnu/local.mk b/gnu/local.mk
index 7278c50e4f..134b81f24f 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -11,7 +11,7 @@
 # Copyright © 2016 Ben Woodcroft <[email-masked]>
 # Copyright © 2016, 2017, 2018, 2019 Alex Vong <[email-masked]>
 # Copyright © 2016, 2017, 2018, 2019, 2020, 2021 Efraim Flashner <[email-masked]>
-# Copyright © 2016, 2017, 2018, 2019, 2020, 2021, 2022 Jan (janneke) Nieuwenhuizen <[email-masked]>
+# Copyright © 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Jan (janneke) Nieuwenhuizen <[email-masked]>
 # Copyright © 2017, 2018, 2019, 2020 Tobias Geerinckx-Rice <[email-masked]>
 # Copyright © 2017, 2018 Clément Lassieur <[email-masked]>
 # Copyright © 2017, 2020 Mathieu Othacehe <[email-masked]>
@@ -94,6 +94,7 @@ GNU_SYSTEM_MODULES =				\
   %D%/home/services/pm.scm			\
   %D%/home/services/shells.scm			\
   %D%/home/services/shepherd.scm		\
+  %D%/home/services/shepherd-xyz.scm		\
   %D%/home/services/ssh.scm			\
   %D%/home/services/mcron.scm			\
   %D%/home/services/utils.scm			\
-- 
2.38.1