Need some advice to create a trac-service-type

Hello, with some help of ChatGPT I got some code to implement a service for

https://trac.edgewall.org/

I need some advice for a workflow to test it.

(define-module (gnu services trac)
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu packages guile-xyz)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (srfi srfi-1)
  #:use-module (guix records))

(define-record-type* <trac-configuration>
  trac-configuration make-trac-configuration
  trac-configuration?
  (project-name trac-configuration-project-name "MyProject")
  (database trac-configuration-database "sqlite:db/trac.db")
  (repository-dir trac-configuration-repository-dir "/srv/git/")
  (extra-config trac-configuration-extra-config '()))

(define (generate-trac-ini config)
  (string-append
   "[project]\n"
   "name = " (trac-configuration-project-name config) "\n"
   "[database]\n"
   "connection_string = " (trac-configuration-database config) "\n"
   "[repositories]\n"
   "default = " (trac-configuration-repository-dir config) "\n"
   ;; Append extra configuration
   (string-join
    (map (lambda (entry)
           (string-append (car entry) " = " (cdr entry)))
         (trac-configuration-extra-config config))
    "\n")))

(define trac-service
  (shepherd-service
   (provision '(trac))
   (start #~(lambda _
              (let* ((config (load-configuration "path-to-config-file"))
                     (trac-ini (generate-trac-ini config)))
                (call-with-output-file "/etc/trac.ini"
                  (lambda (port)
                    (display trac-ini port)))
                (invoke "tracd" "--config=/etc/trac.ini"))))
   (stop #~(lambda _
             (invoke "pkill" "tracd")))))

(define (trac-service-start config)
  #~(lambda ()
      (let ((user "trac") ;; The non-root user to run the service
            (config-file "/etc/trac.ini") ;; Path to the trac.ini file
            (data-dir "/var/lib/trac"))  ;; Path to the Trac project directory
        ;; Ensure the config file and directories exist
        (unless (file-exists? config-file)
          (error "Trac config file not found: " config-file))
        (unless (file-exists? data-dir)
          (error "Trac data directory not found: " data-dir))
        ;; Run as the 'trac' user
        (invoke "su" "-s" "/bin/sh" "-c"
                (string-append
                 "tracd --port=8000 --config=" config-file
                 " " data-dir)
                user))))

(define (trac-service-stop)
  #~(lambda ()
      (invoke "pkill" "-f" "tracd")))

(define trac-shepherd-service
  (shepherd-service
   (provision '(trac))
   (requirement '(networking)) ;; Ensure networking is available
   (start (trac-service-start config))
   (stop (trac-service-stop))))

Can you please format your code in a code block, e.g.

```lisp
your code
```

Otherwise all the indentation is lost. Also, you might want to flag this as Guix instead of General.

1 Like