;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2014, 2015 David Thompson ;;; Copyright © 2014 Andreas Enge ;;; Copyright © 2014 Cyrill Schenkel ;;; Copyright © 2014 Ian Denhardt ;;; Copyright © 2015 Paul van der Walt ;;; Copyright © 2016, 2018, 2019, 2020 Leo Famulari ;;; Copyright © 2018–2021 Tobias Geerinckx-Rice ;;; Copyright © 2019 Evan Straw ;;; Copyright © 2020 Ricardo Wurmus ;;; Copyright © 2020 Lars-Dominik Braun ;;; Copyright © 2020 Simon Streit ;;; Copyright © 2021 Noah Evans ;;; ;;; 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 . (define-module (gnu packages mpd) #:use-module (gnu packages) #:use-module ((guix licenses) #:prefix license:) #:use-module (guix packages) #:use-module (guix download) #:use-module (guix git-download) #:use-module (guix utils) #:use-module (guix build-system cmake) #:use-module (guix build-system gnu) #:use-module (guix build-system meson) #:use-module (guix build-system python) #:use-module (gnu packages audio) #:use-module (gnu packages autotools) #:use-module (gnu packag
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017, 2018, 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
;;; Copyright © 2022 Leo Nikkilä <hello@lnikki.la>
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; 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 build shepherd)
  #:use-module (gnu system file-systems)
  #:use-module (gnu build linux-container)
  #:use-module (guix build utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 match)
  ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
  #:autoload (shepherd service) (fork+exec-command
                                 read-pid-file
                                 exec-command
                                 %precious-signals)
  #:autoload (shepherd system) (unblock-signals)
  #:export (default-mounts
            fork+exec-command/container))

;;; Commentary:
;;;
;;; This module provides extensions to the GNU Shepherd.  In particular, it
;;; provides a helper to start services in a container.
;;;
;;; Code:

(define (clean-up file)
  (when file
    (catch 'system-error
      (lambda ()
        (delete-file file))
      (lambda args
        (unless (= ENOENT (system-error-errno args))
          (apply throw args))))))

(define-syntax-rule (catch-system-error exp)
  (catch 'system-error
    (lambda ()
      exp)
    (const #f)))

(define (default-namespaces args)
  ;; Most daemons are here to talk to the network, and most of them expect to
  ;; run under a non-zero UID.
  (fold delq %namespaces '(net user)))

(define* (default-mounts #:key (namespaces (default-namespaces '())))
  (define (tmpfs directory)
    (file-system
      (device "none")
      (mount-point directory)
      (type "tmpfs")
      (check? #f)))

  (define accounts
    ;; This is for processes in the default user namespace but living in a
    ;; different mount namespace, so that they can lookup users.
    (list (file-system-mapping
           (source "/etc/passwd") (target source))
          (file-system-mapping
           (source "/etc/group") (target source))))

  (append (cons (tmpfs "/tmp") %container-file-systems)
          (let ((mappings `(,@(if (memq 'net namespaces)
                                  '()
                                  %network-file-mappings)
                            ,@(if (and (memq 'mnt namespaces)
                                       (not (memq 'user namespaces)))
                                  accounts
                                  '())

                            ;; Tell the process what timezone we're in.  This
                            ;; makes sure that, for instance, its syslog
                            ;; messages have the correct timestamp.
                            ,(file-system-mapping
                              (source "/etc/localtime")
                              (target source))

                            ,%store-mapping)))    ;XXX: coarse-grain
            (map file-system-mapping->bind-mount
                 (filter (lambda (mapping)
                           (file-exists? (file-system-mapping-source mapping)))
                         mappings)))))

(define* (exec-command* command #:key user group log-file pid-file
                        (supplementary-groups '())
                        (directory "/") (environment-variables (environ)))
  "Like 'exec-command', but first restore signal handles modified by
shepherd (PID 1)."
  ;; First restore the default handlers.
  (for-each (cut sigaction <> SIG_DFL) %precious-signals)

  ;; Unblock any signals that have been blocked by the parent process.
  (unblock-signals %precious-signals)

  (mkdir-p "/var/run")
  (clean-up pid-file)

  (exec-command command
                #:user user
                #:group group
                #:supplementary-groups supplementary-groups
                #:log-file log-file
                #:directory directory
                #:environment-variables environment-variables))

(define* (fork+exec-command/container command
                                      #:key pid
                                      #:allow-other-keys
                                      #:rest args)
  "This is a variant of 'fork+exec-command' procedure, that joins the
namespaces of process PID beforehand.  If there is no support for containers,
on Hurd systems for instance, fallback to direct forking."
  (define (strip-pid args)
    ;; TODO: Replace with 'strip-keyword-arguments' when that no longer pulls
    ;; in (guix config).
    (let loop ((args args)
               (result '()))
      (match args
        (()
         (reverse result))
        ((#:pid _ . rest)
         (loop rest result))
        ((head . rest)
         (loop rest (cons head result))))))

  (let ((container-support? (file-exists? "/proc/self/ns")))
    (if (and container-support?
             (not (and pid (= pid (getpid)))))
        (container-excursion* pid
          (lambda ()
            ;; Note: In the Shepherd 0.9, 'fork+exec-command' expects to be
            ;; called from the shepherd process (because it creates a pipe to
            ;; capture stdout/stderr and spawns a logging fiber) so we cannot
            ;; use it here.
            (match (primitive-fork)
              (0 (dynamic-wind
                   (const #t)
                   (lambda ()
                     (apply exec-command* command (strip-pid args)))
                   (lambda ()
                     (primitive-_exit 127))))
              (pid pid))))               ;XXX: assuming the same PID namespace
        (apply fork+exec-command command (strip-pid args)))))

;; Local Variables:
;; eval: (put 'container-excursion* 'scheme-indent-function 1)
;; End:

;;; shepherd.scm ends here
))) (native-inputs `(("pkg-config" ,pkg-config))) (arguments '(#:configure-flags '("BOOST_LIB_SUFFIX=" "--with-taglib" "--enable-clock"))) (synopsis "Featureful ncurses based MPD client inspired by ncmpc") (description "Ncmpcpp is an mpd client with a UI very similar to ncmpc, but it provides new useful features such as support for regular expressions for library searches, extended song format, items filtering, the ability to sort playlists, and a local file system browser.") (home-page "https://ncmpcpp.rybczak.net/") (license license:gpl2+))) (define-public mpdscribble (package (name "mpdscribble") (version "0.23") (source (origin (method url-fetch) (uri (string-append "http://www.musicpd.org/download/mpdscribble/" version "/mpdscribble-" version ".tar.xz")) (sha256 (base32 "0s66zqscb44p88cl3kcv5jkjcqsskcnrv7xgrjhzrchf2kcpwf53")))) (build-system meson-build-system) (inputs `(("boost" ,boost) ("curl" ,curl) ("libgcrypt" ,libgcrypt) ("libmpdclient" ,libmpdclient))) (native-inputs `(("pkg-config" ,pkg-config))) (synopsis "MPD client for track scrobbling") (description "mpdscribble is a Music Player Daemon client which submits information about tracks being played to a scrobbler, such as Libre.FM.") (home-page "https://www.musicpd.org/clients/mpdscribble/") (license license:gpl2+))) (define-public python-mpd2 (package (name "python-mpd2") (version "3.0.1") (source (origin (method url-fetch) (uri (pypi-uri "python-mpd2" version)) (sha256 (base32 "0fxssbmnv44m03shjyvbqslc69b0160702j2s0flgvdxjggrnbjj")))) (build-system python-build-system) (arguments '(#:phases (modify-phases %standard-phases (replace 'check (lambda _ (invoke "python" "-m" "pytest" "mpd/tests.py")))))) (native-inputs `(("python-mock" ,python-mock) ("python-pytest" ,python-pytest))) (home-page "https://github.com/Mic92/python-mpd2") (synopsis "Python MPD client library") (description "Python-mpd2 is a Python library which provides a client interface for the Music Player Daemon.") (license license:lgpl3+))) (define-public sonata (package (name "sonata") (version "1.7.0") (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/multani/sonata") (commit (string-append "v" version)))) (file-name (git-file-name name version)) (sha256 (base32 "0rl8w7s2asff626clzfvyz987l2k4ml5dg417mqp9v8a962q0v2x")))) (build-system python-build-system) (arguments `(#:modules ((guix build gnu-build-system) (guix build python-build-system) ((guix build glib-or-gtk-build-system) #:prefix glib-or-gtk:) (guix build utils)) #:imported-modules (,@%gnu-build-system-modules (guix build python-build-system) (guix build glib-or-gtk-build-system)) #:phases (modify-phases %standard-phases (add-after 'install 'glib-or-gtk-wrap (assoc-ref glib-or-gtk:%standard-phases 'glib-or-gtk-wrap)) (add-after 'install 'wrap-sonata (lambda* (#:key outputs #:allow-other-keys) (let ((out (assoc-ref outputs "out")) (gi-typelib-path (getenv "GI_TYPELIB_PATH"))) (wrap-program (string-append out "/bin/sonata") `("GI_TYPELIB_PATH" ":" prefix (,gi-typelib-path)))) #t))))) (native-inputs `(("gettext" ,gettext-minimal))) (inputs `(("python-mpd2" ,python-mpd2) ("gtk+" ,gtk+) ("gsettings-desktop-schemas" ,gsettings-desktop-schemas) ("gobject-introspection" ,gobject-introspection) ("adwaita-icon-theme" ,adwaita-icon-theme) ("python-pygobject" ,python-pygobject))) (synopsis "Elegant client for the Music Player Daemon") (description "Sonata is an elegant graphical client for the Music Player Daemon (MPD). It supports playlists, multiple profiles (connecting to different MPD servers, search and multimedia key support.") (home-page "https://www.nongnu.org/sonata/") (license license:gpl3+))) (define-public ashuffle (package (name "ashuffle") (version "2.0.2") (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/joshkunz/ashuffle") (commit (string-append "v" version)))) (file-name (git-file-name name version)) (sha256 (base32 "11aa95cg0yca2m2d00sar6wr14g3lc7cfm9bin1h7lk7asdm8azp")))) (native-inputs `(("pkg-config" ,pkg-config))) (inputs `(("libmpdclient" ,libmpdclient))) (build-system meson-build-system) (home-page "https://github.com/joshkunz/ashuffle") (synopsis "Automatic library-wide shuffle for mpd") (description "ashuffle is an application for automatically shuffling your MPD library in a similar way to many other music players' 'shuffle library' feature. ashuffle works like any other MPD client, and can be used alongside other MPD frontends.") (license license:expat))) (define-public mpdris2 (package (name "mpdris2") (version "0.8") (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/eonpatapon/mpDris2") (commit version))) (file-name (git-file-name name version)) (sha256 (base32 "048b8acsd1b8kcxzd9fsh5p9g2an9c4rznicfcpyrsjz5syv894h")))) (build-system gnu-build-system) ;; Manually wrap the binary, because we’re not using python-build-system. (arguments '(#:phases (modify-phases %standard-phases (add-after 'install 'wrap-program (lambda* (#:key inputs outputs #:allow-other-keys) (let ((out (assoc-ref outputs "out")) (python-path (getenv "PYTHONPATH"))) (wrap-program (string-append out "/bin/mpDris2") `("PYTHONPATH" ":" prefix (,python-path))) #t)))))) (inputs `(("python-mpd2" ,python-mpd2) ("python-dbus" ,python-dbus) ("python-pygobject" ,python-pygobject) ("python" ,python))) ; Sets PYTHONPATH. ;; For bootstrapping. (native-inputs `(("autoconf" ,autoconf) ("automake" ,automake) ("gettext" ,gettext-minimal) ("which" ,which) ("intltool" ,intltool))) (synopsis "MPRIS V2.1 support for MPD") (description "Client for the Music Player Daemon providing MPRIS 2 support") (home-page "https://github.com/eonpatapon/mpDris2") (license license:gpl3+))) (define-public cantata (package (name "cantata") (version "2.4.2") (source (origin (method url-fetch) (uri (string-append "https://github.com/CDrummond/" "cantata/releases/download/v" version "/" "cantata-" version ".tar.bz2")) (sha256 (base32 "10pcrpmb4n1mkgr21xd580nrbmh57q7s72cbs1zay847hc65vliy")))) (build-system cmake-build-system) (arguments `(#:tests? #f)) ; No test suite (native-inputs `(("pkg-config" ,pkg-config))) (inputs `(("eudev" ,eudev) ("ffmpeg" ,ffmpeg) ("libcdio-paranoia" ,libcdio-paranoia) ("libebur128" ,libebur128) ("libmtp" ,libmtp) ("mpg123" ,mpg123) ("qtbase" ,qtbase) ("qtmultimedia" ,qtmultimedia) ("qtsvg" ,qtsvg) ("taglib" ,taglib) ("zlib" ,zlib))) (synopsis "Graphical MPD Client") (description "Cantata is a graphical client for the Music Player Daemon (MPD), using the Qt5 toolkit. Its user interface is highly customizable, supporting multiple collections, ratings, and dynamic playlists. A local cache of the music library will be created to provide a hierarchy of albums and artists along with albumart.") (home-page "https://github.com/cdrummond/cantata") (license license:gpl3+))) (define-public mcg (package (name "mcg") (version "2.1.2") (source (origin (method git-fetch) (uri (git-reference (url "https://gitlab.com/coderkun/mcg") (commit (string-append "v" version)))) (file-name (git-file-name name version)) (sha256 (base32 "01iqxydssxyi4s644dwl64vm7xhn0szd99hdpywbipvb7kwp5196")))) (build-system python-build-system) (native-inputs `(("glib:bin" ,glib "bin") ("gobject-introspection" ,gobject-introspection) ("pkg-config" ,pkg-config))) (inputs `(("avahi" ,avahi) ("dconf" ,dconf) ("gsettings-desktop-schemas" ,gsettings-desktop-schemas) ("gtk+" ,gtk+) ("python-pygobject" ,python-pygobject))) (arguments `(#:imported-modules ((guix build glib-or-gtk-build-system) ,@%python-build-system-modules) #:modules ((guix build python-build-system) ((guix build glib-or-gtk-build-system) #:prefix glib-or-gtk:) (guix build utils)) #:phases (modify-phases %standard-phases (add-after 'install 'wrap-program (lambda* (#:key outputs #:allow-other-keys) (let ((prog (string-append (assoc-ref outputs "out") "/bin/mcg"))) (wrap-program prog `("PYTHONPATH" = (,(getenv "PYTHONPATH"))) `("GI_TYPELIB_PATH" = (,(getenv "GI_TYPELIB_PATH")))) #t))) (add-after 'wrap-program 'glib-or-gtk-wrap (assoc-ref glib-or-gtk:%standard-phases 'glib-or-gtk-wrap))))) (synopsis "Covergrid for the MPD") (description "mcg (CoverGrid) is a client for the Music Player Daemon (MPD), focusing on albums instead of single tracks. It is not intended to be a replacement for your favorite MPD client but an addition to get a better album-experience.") (home-page "https://gitlab.com/coderkun/mcg") (license license:gpl3+)))