1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu services avahi)
20 #:use-module (gnu services)
21 #:use-module (gnu services base)
22 #:use-module (gnu services shepherd)
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-configuration
35 ;;; This module provides service definitions for the Avahi
36 ;;; "zero-configuration" tool set.
41 (define-record-type* <avahi-configuration>
42 avahi-configuration make-avahi-configuration
44 (avahi avahi-configuration-avahi ;<package>
46 (debug? avahi-configuration-debug? ;Boolean
48 (host-name avahi-configuration-host-name) ;string
49 (publish? avahi-configuration-publish?) ;Boolean
51 ;; The default for this was #t in Avahi 0.6.31 and became #f in 0.7. For
52 ;; now we stick to the old default.
53 (publish-workstation? avahi-configuration-publish-workstation? ;Boolean
56 (ipv4? avahi-configuration-ipv4?) ;Boolean
57 (ipv6? avahi-configuration-ipv6?) ;Boolean
58 (wide-area? avahi-configuration-wide-area?) ;Boolean
59 (domains-to-browse avahi-configuration-domains-to-browse)) ;list of strings
61 (define* (configuration-file config)
62 "Return an avahi-daemon configuration file based on CONFIG, an
63 <avahi-configuration>."
65 (if value "yes\n" "no\n"))
67 (define host-name (avahi-configuration-host-name config))
69 (plain-file "avahi-daemon.conf"
73 (string-append "host-name=" host-name "\n")
76 "browse-domains=" (string-join
77 (avahi-configuration-domains-to-browse
80 "use-ipv4=" (bool (avahi-configuration-ipv4? config))
81 "use-ipv6=" (bool (avahi-configuration-ipv6? config))
83 "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
86 (bool (not (avahi-configuration-publish? config)))
87 "publish-workstation="
88 (bool (avahi-configuration-publish-workstation? config)))))
90 (define %avahi-accounts
91 ;; Account and group for the Avahi daemon.
92 (list (user-group (name "avahi") (system? #t))
97 (comment "Avahi daemon user")
98 (home-directory "/var/empty")
99 (shell (file-append shadow "/sbin/nologin")))))
101 (define %avahi-activation
104 (use-modules (guix build utils))
105 (mkdir-p "/run/avahi-daemon")))
107 (define (avahi-shepherd-service config)
108 "Return a list of <shepherd-service> for CONFIG."
109 (let ((config (configuration-file config))
110 (debug? (avahi-configuration-debug? config))
111 (avahi (avahi-configuration-avahi config)))
112 (list (shepherd-service
113 (documentation "Run the Avahi mDNS/DNS-SD responder.")
114 (provision '(avahi-daemon))
115 (requirement '(dbus-system networking))
117 (start #~(make-forkexec-constructor
118 (list #$(file-append avahi "/sbin/avahi-daemon")
120 #$@(if debug? #~("--debug") #~())
122 #:pid-file "/run/avahi-daemon/pid"))
123 (stop #~(make-kill-destructor))))))
125 (define avahi-service-type
126 (let ((avahi-package (compose list avahi-configuration-avahi)))
127 (service-type (name 'avahi)
129 "Run @command{avahi-daemon}, a host and service discovery
130 daemon that implements the multicast DNS (mDNS) and DNS service
131 discovery (DNS-SD) protocols. Additionally, extend the C library's name
132 service switch (NSS) with support for @code{.local} host name resolution.")
134 (list (service-extension shepherd-root-service-type
135 avahi-shepherd-service)
136 (service-extension dbus-root-service-type
138 (service-extension account-service-type
139 (const %avahi-accounts))
140 (service-extension activation-service-type
141 (const %avahi-activation))
142 (service-extension nscd-service-type
143 (const (list nss-mdns)))
145 ;; Provide 'avahi-browse', 'avahi-resolve', etc. in
146 ;; the system profile.
147 (service-extension profile-service-type
150 (define* (avahi-service #:key (avahi avahi) debug?
153 (ipv4? #t) (ipv6? #t)
155 (domains-to-browse '()))
156 "Return a service that runs @command{avahi-daemon}, a system-wide
157 mDNS/DNS-SD responder that allows for service discovery and
158 \"zero-configuration\" host name lookups (see @uref{http://avahi.org/}), and
159 extends the name service cache daemon (nscd) so that it can resolve
160 @code{.local} host names using
161 @uref{http://0pointer.de/lennart/projects/nss-mdns/, nss-mdns}. Additionally,
162 add the @var{avahi} package to the system profile so that commands such as
163 @command{avahi-browse} are directly usable.
165 If @var{host-name} is different from @code{#f}, use that as the host name to
166 publish for this machine; otherwise, use the machine's actual host name.
168 When @var{publish?} is true, publishing of host names and services is allowed;
169 in particular, avahi-daemon will publish the machine's host name and IP
170 address via mDNS on the local network.
172 When @var{wide-area?} is true, DNS-SD over unicast DNS is enabled.
174 Boolean values @var{ipv4?} and @var{ipv6?} determine whether to use IPv4/IPv6
176 (service avahi-service-type
178 (avahi avahi) (debug? debug?) (host-name host-name)
179 (publish? publish?) (ipv4? ipv4?) (ipv6? ipv6?)
180 (wide-area? wide-area?)
181 (domains-to-browse domains-to-browse))))
183 ;;; avahi.scm ends here