aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021, 2023 Andrew Tropin <andrew@trop.in>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2024 Ludovic Courtès <ludo@gnu.org>
;;;
;;; 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)
  #:use-module (gnu home services)
  #:use-module (gnu packages admin)
  #:use-module (gnu services shepherd)
  #:use-module (guix sets)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (srfi srfi-1)
  #:export (home-shepherd-service-type

            home-shepherd-configuration
            home-shepherd-configuration?
            home-shepherd-configuration-shepherd
            home-shepherd-configuration-auto-start?
            home-shepherd-configuration-services)
  #:re-export (shepherd-service
               shepherd-service?
               shepherd-service-documentation
               shepherd-service-provision
               shepherd-service-canonical-name
               shepherd-service-requirement
               shepherd-service-one-shot?
               shepherd-service-respawn?
               shepherd-service-start
               shepherd-service-stop
               shepherd-service-auto-start?
               shepherd-service-modules

               shepherd-action
               shepherd-configuration-action))

(define-record-type* <home-shepherd-configuration>
  home-shepherd-configuration make-home-shepherd-configuration
  home-shepherd-configuration?
  (shepherd home-shepherd-configuration-shepherd
            (default shepherd-0.10)) ; package
  (auto-start? home-shepherd-configuration-auto-start?
               (default #t))
  (daemonize? home-shepherd-configuration-daemonize?
              (default #t))
  (services home-shepherd-configuration-services
            (default '())))

(define (home-shepherd-configuration-file config)
  "Return the shepherd configuration file for SERVICES.  SHEPHERD is used
as shepherd package."
  (let* ((daemonize? (home-shepherd-configuration-daemonize? config))
         (services (home-shepherd-configuration-services config))
         (_ (assert-valid-graph services))
         (files (map shepherd-service-file services))
         ;; TODO: Add compilation of services, it can improve start
         ;; time.
         ;; (scm->go (cute scm->go <> shepherd))
         )
    (define config
      #~(begin
          (use-modules (srfi srfi-34)
                       (system repl error-handling))

          (define (make-user-module)
            ;; Copied from (shepherd support), where it's private.
            (let ((m (make-fresh-user-module)))
              (module-use! m (resolve-interface '(shepherd service)))
              m))

          (register-services
           (map (lambda (file)
                  (save-module-excursion
                   (lambda ()
                     (set-current-module (make-user-module))
                     (load file))))
                '#$files))

          #$@(if daemonize?
                 `((action 'root 'daemonize))
                 '())

          (format #t "Starting services...~%")
          (let ((services-to-start
                 '#$(append-map shepherd-service-provision
                                (filter shepherd-service-auto-start?
                                        services))))
            (start-in-the-background services-to-start)

            (redirect-port (open-input-file "/dev/null")
                           (current-input-port)))))

    (scheme-file "shepherd.conf" config)))

(define (launch-shepherd-gexp config)
  (let* ((shepherd (home-shepherd-configuration-shepherd config)))
    (if (home-shepherd-configuration-auto-start? config)
        (with-imported-modules '((guix build utils))
          #~(unless (file-exists?
                     (string-append
                      (or (getenv "XDG_RUNTIME_DIR")
                          (format #f "/run/user/~a" (getuid)))
                      "/shepherd/socket"))
              (let* ((state-dir (or (getenv "XDG_STATE_HOME")
                                    (format #f "~a/.local/state"
                                            (getenv "HOME"))))
                     (log-dir (string-append state-dir "/log")))
                ;; TODO: Remove it, 0.9.2 creates it automatically?
                ((@ (guix build utils) mkdir-p) log-dir)
                (system*
                 #$(file-append shepherd "/bin/shepherd")
                 "--logfile"
                 (string-append log-dir "/shepherd.log")
                 "--config"
                 #$(home-shepherd-configuration-file config)))))
        #~"")))

(define (reload-configuration-gexp config)
  (let* ((shepherd (home-shepherd-configuration-shepherd config)))
    #~(system*
       #$(file-append shepherd "/bin/herd")
       "load" "root"
       #$(home-shepherd-configuration-file config))))

(define (ensure-shepherd-gexp config)
  #~(if (file-exists?
         (string-append
          (or (getenv "XDG_RUNTIME_DIR")
              (format #f "/run/user/~a" (getuid)))
          "/shepherd/socket"))
        #$(reload-configuration-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))))

(define home-shepherd-service-type
  (service-type (name 'home-shepherd)
                (extensions
                 (list (service-extension
                        home-run-on-first-login-service-type
                        launch-shepherd-gexp)
                       (service-extension
                        home-xdg-configuration-files-service-type
                        shepherd-xdg-configuration-files)
                       (service-extension
                        home-activation-service-type
                        ensure-shepherd-gexp)
                       (service-extension
                        home-profile-service-type
                        (lambda (config)
                          `(,(home-shepherd-configuration-shepherd config))))))
                (compose concatenate)
                (extend
                 (lambda (config extra-services)
                   (home-shepherd-configuration
                    (inherit config)
                    (services
                     (append (home-shepherd-configuration-services config)
                             extra-services)))))
                (default-value (home-shepherd-configuration))
                (description "Configure and install userland Shepherd.")))

(define-service-type-mapping
  shepherd-root-service-type => home-shepherd-service-type)

rkrave, geary)[native-inputs]: Likewise. * gnu/packages/guile-xyz.scm (guile-gi, emacsy)[native-inputs]: Likewise. * gnu/packages/image.scm (gpick)[native-inputs]: Likewise. * gnu/packages/messaging.scm (telegram-purple)[native-inputs]: Likewise. * gnu/packages/music.scm (fmit)[native-inputs]: Likewise. * gnu/packages/package-management.scm (flatpak)[native-inputs]: Likewise. * gnu/packages/patchutils.scm (quilt)[native-inputs]: Likewise. * gnu/packages/photo.scm (hugin)[native-inputs]: Likewise. * gnu/packages/security-token.scm (eid-mw)[native-inputs]: Likewise. * gnu/packages/telephony.scm (jami)[native-inputs]: Likewise. * gnu/packages/texinfo.scm (pinfo)[native-inputs]: Likewise. * gnu/packages/vulkan.scm (vkd3d)[native-inputs]: Likewise. * gnu/packages/web.scm (cadaver)[native-inputs]: Likewise. Marius Bakke 2020-04-02gnu: darktable: Make some inputs native....* gnu/packages/photo.scm (darktable)[inputs]: Move intltool, perl & pkg-config from here... [native-inputs]: ...to here. Signed-off-by: Mathieu Othacehe <m.othacehe@gmail.com> Vincent Legoll 2020-03-18Merge branch 'master' into core-updatesMarius Bakke 2020-03-15gnu: Add rapid-photo-downloader...* gnu/packages/photo.scm (rapid-photo-downloader): New variable. Signed-off-by: Christopher Baines <mail@cbaines.net> Sebastian Schott 2020-03-04Merge branch 'master' into core-updatesMarius Bakke 2020-02-25gnu: libgphoto2: Update to 2.5.24....* gnu/packages/photo.scm (libgphoto2): Update to 2.5.24. Marius Bakke 2020-02-20gnu: perl-image-exiftool: Update to 11.85....* gnu/packages/photo.scm (perl-image-exiftool): Update to 11.85. Leo Famulari 2020-02-20gnu: RawTherapee: Update to 5.8....* gnu/packages/photo.scm (rawtherapee): Update to 5.8. Leo Famulari 2020-01-22gnu: Replace uses of 'libjpeg' with 'libjpeg-turbo'....* gnu/packages/abiword.scm (abiword)[inputs]: Change from LIBJPEG to LIBJPEG-TURBO. * gnu/packages/admin.scm (testdisk)[inputs]: Likewise. * gnu/packages/algebra.scm (giac)[inputs]: Likewise. * gnu/packages/animation.scm (synfig)[inputs]: Likewise. * gnu/packages/astronomy.scm (gnuastro, celestia)[inputs]: Likewise. * gnu/packages/cdrom.scm (dvdstyler)[inputs]: Likewise. * gnu/packages/cran.scm (r-jpeg, r-tiff, r-readbitmap)[inputs]: Likewise. * gnu/packages/cups.scm (cups-filters, hplip)[inputs]: Likewise. * gnu/packages/display-managers.scm (slim)[inputs]: Likewise. * gnu/packages/ebook.scm (fbreader)[inputs]: Likewise. * gnu/packages/emacs.scm (emacs)[inputs]: Likewise. * gnu/packages/enlightenment.scm (efl)[propagated-inputs]: Likewise. * gnu/packages/fltk.scm (fltk, ntk)[inputs]: Likewise. * gnu/packages/fontutils.scm (fontforge)[inputs]: Likewise. * gnu/packages/freedesktop.scm (weston)[inputs]: Likewise. * gnu/packages/game-development.scm (sfml, allegro, aseprite, python-pygame, eureka, ioquake3)[inputs]: Likewise. * gnu/packages/games.scm (adanaxisgpl, freedroidrpg, irrlicht, minetest, fizmo, supertuxkart, gzdoom, xmoto, xonotic)[inputs]: Likewise. * gnu/packages/gd.scm (gd, perl-gd)[inputs]: Likewise. * gnu/packages/ghostscript.scm (lcms)[inputs]: Likewise. (ghostscript)[inputs, native-inputs]: Likewise. * gnu/packages/gimp.scm (gegl, gimp)[inputs]: Likewise. * gnu/packages/gnome.scm (libgnomeui, eog, tracker-miners, gthumb)[inputs]: Likewise. * gnu/packages/gnunet.scm (libextractor)[inputs]: Likewise. * gnu/packages/gnustep.scm (windowmaker)[inputs]: Likewise. * gnu/packages/graphics.scm (blender, blender-2.79, openimageio, openscenegraph, openscenegraph-3.4, povray, fgallery)[inputs]: Likewise. * gnu/packages/graphviz.scm (graphviz)[inputs]: Likewise. * gnu/packages/gstreamer.scm (gst-plugins-good)[inputs]: Likewise. * gnu/packages/gtk.scm (gdk-pixbuf)[inputs]: Likewise. * gnu/packages/image-processing.scm (dcmtk, mia, vtk, opencv, vips, nip2, vxl, insight-toolkit)[inputs]: Likewise. * gnu/packages/image-viewers.scm (gpicview, luminance-hdr)[inputs]: Likewise. * gnu/packages/image.scm (jpegoptim, libtiff, leptonica, imlib2, freeimage, vigra, libwebp, libmng, jasper, steghide, jp2a)[inputs]: Likewise. * gnu/packages/imagemagick.scm (imagemagick, graphicsmagick)[inputs]: Likewise. * gnu/packages/java.scm (icedtea-6, icedtea-7, openjdk9, openjdk11, openjdk12)[inputs]: Likewise. * gnu/packages/kde-frameworks.scm (khtml)[inputs]: Likewise. * gnu/packages/kodi.scm (kodi)[inputs]: Likewise. * gnu/packages/machine-learning.scm (dlib, tensorflow)[inputs]: Likewise. * gnu/packages/mate.scm (atril, eom)[inputs]: Likewise. * gnu/packages/maths.scm (hdf4, hdf-java, hdf-eos2, netcdf)[inputs]: Likewise. * gnu/packages/netpbm.scm (netpbm)[inputs]: Likewise. * gnu/packages/pdf.scm (zathura-pdf-mupdf, podofo, mupdf, fbida)[inputs]: Likewise. * gnu/packages/photo.scm (libraw, libpano13, enblend-enfuse, darktable, hugin, rawtherapee)[inputs]: Likewise. * gnu/packages/prolog.scm (swi-prolog)[native-inputs]: Likewise. * gnu/packages/python-xyz.scm (python-hdf4, python-pillow)[inputs]: Likewise. * gnu/packages/qt.scm (qtbase, qtwebkit)[inputs]: Likewise. * gnu/packages/rdesktop.scm (freerdp)[inputs]: Likewise. * gnu/packages/scanner.scm (sane-backends, xsane)[inputs]: Likewise. * gnu/packages/scheme.scm (racket)[inputs]: Likewise. * gnu/packages/scribus.scm (scribus)[inputs]: Likewise. * gnu/packages/sdl.scm (sdl-image)[propagated-inputs]: Likewise. (guile-sdl)[native-inputs]: Likewise. * gnu/packages/spice.scm (spice-gtk)[inputs]: Likewise. * gnu/packages/statistics.scm (r-with-tests)[inputs]: Likewise. * gnu/packages/tcl.scm (perl-tk)[inputs]: Likewise. * gnu/packages/upnp.scm (readymedia)[inputs]: Likewise. * gnu/packages/video.scm (mplayer, mpv, v4l-utils, motion)[inputs]: Likewise. * gnu/packages/web-browsers.scm (dillo, links)[inputs]: Likewise. * gnu/packages/web.scm (netsurf)[inputs]: Likewise. * gnu/packages/webkit.scm (webkitgtk)[inputs]: Likewise. * gnu/packages/wine.scm (wine)[inputs]: Likewise. * gnu/packages/wv.scm (wv)[inputs]: Likewise. * gnu/packages/wxwidgets.scm (wxwidgets, wxwidgets-2)[inputs]: Likewise. * gnu/packages/xdisorg.scm (xscreensaver)[inputs]: Likewise. * gnu/packages/xfce.scm (tumbler)[inputs]: Likewise. * gnu/packages/xfig.scm (xfig, transfig)[inputs]: Likewise. * gnu/packages/xorg.scm (xpra)[inputs]: Likewise. Marius Bakke