services: 'dmd-service-type' takes a service name.
[jackhill/guix/guix.git] / gnu / services / avahi.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu services avahi)
20 #:use-module (gnu services)
21 #:use-module (gnu services base)
22 #:use-module (gnu services dmd)
23 #:use-module (gnu services dbus)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu packages avahi)
26 #:use-module (gnu packages admin)
27 #:use-module (guix records)
28 #:use-module (guix gexp)
29 #:export (avahi-service))
30
31 ;;; Commentary:
32 ;;;
33 ;;; This module provides service definitions for the Avahi
34 ;;; "zero-configuration" tool set.
35 ;;;
36 ;;; Code:
37
38 ;; TODO: Export.
39 (define-record-type* <avahi-configuration>
40 avahi-configuration make-avahi-configuration
41 avahi-configuration?
42 (avahi avahi-configuration-avahi ;<package>
43 (default avahi))
44 (host-name avahi-configuration-host-name) ;string
45 (publish? avahi-configuration-publish?) ;Boolean
46 (ipv4? avahi-configuration-ipv4?) ;Boolean
47 (ipv6? avahi-configuration-ipv6?) ;Boolean
48 (wide-area? avahi-configuration-wide-area?) ;Boolean
49 (domains-to-browse avahi-configuration-domains-to-browse)) ;list of strings
50
51 (define* (configuration-file config)
52 "Return an avahi-daemon configuration file based on CONFIG, an
53 <avahi-configuration>."
54 (define (bool value)
55 (if value "yes\n" "no\n"))
56
57 (define host-name (avahi-configuration-host-name config))
58
59 (plain-file "avahi-daemon.conf"
60 (string-append
61 "[server]\n"
62 (if host-name
63 (string-append "host-name=" host-name "\n")
64 "")
65
66 "browse-domains=" (string-join
67 (avahi-configuration-domains-to-browse
68 config))
69 "\n"
70 "use-ipv4=" (bool (avahi-configuration-ipv4? config))
71 "use-ipv6=" (bool (avahi-configuration-ipv6? config))
72 "[wide-area]\n"
73 "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
74 "[publish]\n"
75 "disable-publishing="
76 (bool (not (avahi-configuration-publish? config))))))
77
78 (define %avahi-accounts
79 ;; Account and group for the Avahi daemon.
80 (list (user-group (name "avahi") (system? #t))
81 (user-account
82 (name "avahi")
83 (group "avahi")
84 (system? #t)
85 (comment "Avahi daemon user")
86 (home-directory "/var/empty")
87 (shell #~(string-append #$shadow "/sbin/nologin")))))
88
89 (define %avahi-activation
90 ;; Activation gexp.
91 #~(begin
92 (use-modules (guix build utils))
93 (mkdir-p "/var/run/avahi-daemon")))
94
95 (define (avahi-dmd-service config)
96 "Return a list of <dmd-service> for CONFIG."
97 (let ((config (configuration-file config))
98 (avahi (avahi-configuration-avahi config)))
99 (list (dmd-service
100 (documentation "Run the Avahi mDNS/DNS-SD responder.")
101 (provision '(avahi-daemon))
102 (requirement '(dbus-system networking))
103
104 (start #~(make-forkexec-constructor
105 (list (string-append #$avahi "/sbin/avahi-daemon")
106 "--syslog" "-f" #$config)))
107 (stop #~(make-kill-destructor))))))
108
109 (define avahi-service-type
110 (service-type (name 'avahi)
111 (extensions
112 (list (service-extension dmd-root-service-type
113 avahi-dmd-service)
114 (service-extension dbus-root-service-type
115 (compose list
116 avahi-configuration-avahi))
117 (service-extension account-service-type
118 (const %avahi-accounts))
119 (service-extension activation-service-type
120 (const %avahi-activation))
121 (service-extension nscd-service-type
122 (const (list nss-mdns)))))))
123
124 (define* (avahi-service #:key (avahi avahi)
125 host-name
126 (publish? #t)
127 (ipv4? #t) (ipv6? #t)
128 wide-area?
129 (domains-to-browse '()))
130 "Return a service that runs @command{avahi-daemon}, a system-wide
131 mDNS/DNS-SD responder that allows for service discovery and
132 \"zero-configuration\" host name lookups (see @uref{http://avahi.org/}), and
133 extends the name service cache daemon (nscd) so that it can resolve
134 @code{.local} host names using
135 @uref{http://0pointer.de/lennart/projects/nss-mdns/, nss-mdns}.
136
137 If @var{host-name} is different from @code{#f}, use that as the host name to
138 publish for this machine; otherwise, use the machine's actual host name.
139
140 When @var{publish?} is true, publishing of host names and services is allowed;
141 in particular, avahi-daemon will publish the machine's host name and IP
142 address via mDNS on the local network.
143
144 When @var{wide-area?} is true, DNS-SD over unicast DNS is enabled.
145
146 Boolean values @var{ipv4?} and @var{ipv6?} determine whether to use IPv4/IPv6
147 sockets."
148 (service avahi-service-type
149 (avahi-configuration
150 (avahi avahi) (host-name host-name)
151 (publish? publish?) (ipv4? ipv4?) (ipv6? ipv6?)
152 (wide-area? wide-area?)
153 (domains-to-browse domains-to-browse))))
154
155 ;;; avahi.scm ends here