services: Add D-Bus and Avahi.
authorLudovic Courtès <ludo@gnu.org>
Mon, 26 May 2014 16:23:12 +0000 (18:23 +0200)
committerLudovic Courtès <ludo@gnu.org>
Mon, 26 May 2014 21:38:06 +0000 (23:38 +0200)
* gnu/services/dbus.scm: New file.
* gnu/services/avahi.scm: New file.
* gnu-system.am (GNU_SYSTEM_MODULES): Add them.

gnu-system.am
gnu/services/avahi.scm [new file with mode: 0644]
gnu/services/dbus.scm [new file with mode: 0644]

index ab8bf11..242dc2d 100644 (file)
@@ -245,7 +245,9 @@ GNU_SYSTEM_MODULES =                                \
   gnu/packages/zip.scm                         \
                                                \
   gnu/services.scm                             \
+  gnu/services/avahi.scm                       \
   gnu/services/base.scm                                \
+  gnu/services/dbus.scm                                \
   gnu/services/dmd.scm                         \
   gnu/services/networking.scm                  \
   gnu/services/xorg.scm                                \
diff --git a/gnu/services/avahi.scm b/gnu/services/avahi.scm
new file mode 100644 (file)
index 0000000..4b52fd7
--- /dev/null
@@ -0,0 +1,108 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2014 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 services avahi)
+  #:use-module (gnu services)
+  #:use-module (gnu system shadow)
+  #:use-module (gnu packages avahi)
+  #:use-module (guix monads)
+  #:use-module (guix gexp)
+  #:export (avahi-service))
+
+;;; Commentary:
+;;;
+;;; This module provides service definitions for the Avahi
+;;; "zero-configuration" tool set.
+;;;
+;;; Code:
+
+(define* (configuration-file #:key host-name publish?
+                             ipv4? ipv6? wide-area? domains-to-browse)
+  "Return an avahi-daemon configuration file."
+  (define (bool value)
+    (if value "yes\n" "no\n"))
+
+  (text-file "avahi-daemon.conf"
+             (string-append
+              "[server]\n"
+              (if host-name
+                  (string-append "host-name=" host-name "\n")
+                  "")
+
+              "browse-domains=" (string-join domains-to-browse)
+              "\n"
+              "use-ipv4=" (bool ipv4?)
+              "use-ipv6=" (bool ipv6?)
+              "[wide-area]\n"
+              "enable-wide-area=" (bool wide-area?)
+              "[publish]\n"
+              "disable-publishing=" (bool (not publish?)))))
+
+(define* (avahi-service #:key (avahi avahi)
+                        host-name
+                        (publish? #t)
+                        (ipv4? #t) (ipv6? #t)
+                        wide-area?
+                        (domains-to-browse '()))
+  "Return a service that runs @command{avahi-daemon}, a system-wide
+mDNS/DNS-SD responder that allows for service discovery and
+\"zero-configuration\" host name lookups.
+
+If @var{host-name} is different from @code{#f}, use that as the host name to
+publish for this machine; otherwise, use the machine's actual host name.
+
+When @var{publish?} is true, publishing of host names and services is allowed;
+in particular, avahi-daemon will publish the machine's host name and IP
+address via mDNS on the local network.
+
+When @var{wide-area?} is true, DNS-SD over unicast DNS is enabled.
+
+Boolean values @var{ipv4?} and @var{ipv6?} determine whether to use IPv4/IPv6
+sockets."
+  (mlet %store-monad ((config (configuration-file #:host-name host-name
+                                                  #:publish? publish?
+                                                  #:ipv4? ipv4?
+                                                  #:ipv6? ipv6?
+                                                  #:wide-area? wide-area?
+                                                  #:domains-to-browse
+                                                  domains-to-browse)))
+    (return
+     (service
+      (documentation "Run the Avahi mDNS/DNS-SD responder.")
+      (provision '(avahi-daemon))
+      (requirement '(dbus-system networking))
+
+      (start #~(make-forkexec-constructor
+                (string-append #$avahi "/sbin/avahi-daemon")
+                "--syslog" "-f" #$config))
+      (stop #~(make-kill-destructor))
+      (activate #~(begin
+                    (use-modules (guix build utils))
+                    (mkdir-p "/var/run/avahi-daemon")))
+
+      (user-groups (list (user-group
+                          (name "avahi"))))
+      (user-accounts (list (user-account
+                            (name "avahi")
+                            (group "avahi")
+                            (comment "Avahi daemon user")
+                            (home-directory "/var/empty")
+                            (shell
+                             "/run/current-system/profile/sbin/nologin"))))))))
+
+;;; avahi.scm ends here
diff --git a/gnu/services/dbus.scm b/gnu/services/dbus.scm
new file mode 100644 (file)
index 0000000..3fbbfde
--- /dev/null
@@ -0,0 +1,120 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2014 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 services dbus)
+  #:use-module (gnu services)
+  #:use-module (gnu system shadow)
+  #:use-module (gnu packages glib)
+  #:use-module (guix monads)
+  #:use-module (guix gexp)
+  #:export (dbus-service))
+
+;;; Commentary:
+;;;
+;;; This module supports the configuration of the D-Bus message bus
+;;; (http://dbus.freedesktop.org/).  D-Bus is an inter-process communication
+;;; facility.  Its "system bus" is used to allow system services to
+;;; communicate and be notified of system-wide events.
+;;;
+;;; Code:
+
+(define (dbus-configuration-directory dbus services)
+  "Return a configuration directory for @var{dbus} that includes the
+@code{etc/dbus-1/system.d} directories of each package listed in
+@var{services}."
+  (define build
+    #~(begin
+        (use-modules (sxml simple))
+
+        (define (services->sxml services)
+          ;; Return the SXML 'includedir' clauses for DIRS.
+          `(busconfig
+            ,@(map (lambda (dir)
+                     `(includedir ,(string-append dir
+                                                  "/etc/dbus-1/system.d")))
+                   services)))
+
+        (mkdir #$output)
+        (copy-file (string-append #$dbus "/etc/dbus-1/system.conf")
+                   (string-append #$output "/system.conf"))
+
+        ;; The default 'system.conf' has an <includedir> clause for
+        ;; 'system.d', so create it.
+        (mkdir (string-append #$output "/system.d"))
+
+        ;; 'system-local.conf' is automatically included by the default
+        ;; 'system.conf', so this is where we stuff our own things.
+        (call-with-output-file (string-append #$output "/system-local.conf")
+          (lambda (port)
+            (sxml->xml (services->sxml (list #$@services))
+                       port)))))
+
+  (gexp->derivation "dbus-configuration" build))
+
+(define* (dbus-service services #:key (dbus dbus))
+  "Return a service that runs the system bus, using @var{dbus}, with support
+for @var{services}.
+
+@var{services} must be a list of packages that provide an
+@file{etc/dbus-1/system.d} directory containing additional D-Bus configuration
+and policy files.  For example, to allow avahi-daemon to use the system bus,
+@var{services} must be equal to @code{(list avahi)}."
+  (mlet %store-monad ((conf (dbus-configuration-directory dbus services)))
+    (return
+     (service
+      (documentation "Run the D-Bus system daemon.")
+      (provision '(dbus-system))
+      (requirement '(user-processes))
+      (start #~(make-forkexec-constructor
+                (string-append #$dbus "/bin/dbus-daemon")
+                "--nofork"
+                (string-append "--config-file=" #$conf "/system.conf")))
+      (stop #~(make-kill-destructor))
+      (user-groups (list (user-group
+                          (name "messagebus"))))
+      (user-accounts (list (user-account
+                            (name "messagebus")
+                            (group "messagebus")
+                            (comment "D-Bus system bus user")
+                            (home-directory "/var/run/dbus")
+                            (shell
+                             "/run/current-system/profile/sbin/nologin"))))
+      (activate #~(begin
+                    (use-modules (guix build utils))
+
+                    (mkdir-p "/var/run/dbus")
+
+                    (let ((user (getpwnam "messagebus")))
+                      (chown "/var/run/dbus"
+                             (passwd:uid user) (passwd:gid user)))
+
+                    (unless (file-exists? "/etc/machine-id")
+                      (format #t "creating /etc/machine-id...~%")
+                      (let ((prog (string-append #$dbus "/bin/dbus-uuidgen")))
+                        ;; XXX: We can't use 'system' because the initrd's
+                        ;; guile system(3) only works when 'sh' is in $PATH.
+                        (let ((pid (primitive-fork)))
+                          (if (zero? pid)
+                              (call-with-output-file "/etc/machine-id"
+                                (lambda (port)
+                                  (close-fdes 1)
+                                  (dup2 (port->fdes port) 1)
+                                  (execl prog)))
+                              (waitpid pid)))))))))))
+
+;;; dbus.scm ends here