1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 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-service
34 ;;; This module provides service definitions for the Avahi
35 ;;; "zero-configuration" tool set.
40 (define-record-type* <avahi-configuration>
41 avahi-configuration make-avahi-configuration
43 (avahi avahi-configuration-avahi ;<package>
45 (host-name avahi-configuration-host-name) ;string
46 (publish? avahi-configuration-publish?) ;Boolean
47 (ipv4? avahi-configuration-ipv4?) ;Boolean
48 (ipv6? avahi-configuration-ipv6?) ;Boolean
49 (wide-area? avahi-configuration-wide-area?) ;Boolean
50 (domains-to-browse avahi-configuration-domains-to-browse)) ;list of strings
52 (define* (configuration-file config)
53 "Return an avahi-daemon configuration file based on CONFIG, an
54 <avahi-configuration>."
56 (if value "yes\n" "no\n"))
58 (define host-name (avahi-configuration-host-name config))
60 (plain-file "avahi-daemon.conf"
64 (string-append "host-name=" host-name "\n")
67 "browse-domains=" (string-join
68 (avahi-configuration-domains-to-browse
71 "use-ipv4=" (bool (avahi-configuration-ipv4? config))
72 "use-ipv6=" (bool (avahi-configuration-ipv6? config))
74 "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
77 (bool (not (avahi-configuration-publish? config))))))
79 (define %avahi-accounts
80 ;; Account and group for the Avahi daemon.
81 (list (user-group (name "avahi") (system? #t))
86 (comment "Avahi daemon user")
87 (home-directory "/var/empty")
88 (shell #~(string-append #$shadow "/sbin/nologin")))))
90 (define %avahi-activation
93 (use-modules (guix build utils))
94 (mkdir-p "/var/run/avahi-daemon")))
96 (define (avahi-dmd-service config)
97 "Return a list of <dmd-service> for CONFIG."
98 (let ((config (configuration-file config))
99 (avahi (avahi-configuration-avahi config)))
101 (documentation "Run the Avahi mDNS/DNS-SD responder.")
102 (provision '(avahi-daemon))
103 (requirement '(dbus-system networking))
105 (start #~(make-forkexec-constructor
106 (list (string-append #$avahi "/sbin/avahi-daemon")
107 "--syslog" "-f" #$config)))
108 (stop #~(make-kill-destructor))))))
110 (define avahi-service-type
111 (let ((avahi-package (compose list avahi-configuration-avahi)))
112 (service-type (name 'avahi)
114 (list (service-extension dmd-root-service-type
116 (service-extension dbus-root-service-type
118 (service-extension account-service-type
119 (const %avahi-accounts))
120 (service-extension activation-service-type
121 (const %avahi-activation))
122 (service-extension nscd-service-type
123 (const (list nss-mdns)))
125 ;; Provide 'avahi-browse', 'avahi-resolve', etc. in
126 ;; the system profile.
127 (service-extension profile-service-type
130 (define* (avahi-service #:key (avahi avahi)
133 (ipv4? #t) (ipv6? #t)
135 (domains-to-browse '()))
136 "Return a service that runs @command{avahi-daemon}, a system-wide
137 mDNS/DNS-SD responder that allows for service discovery and
138 \"zero-configuration\" host name lookups (see @uref{http://avahi.org/}), and
139 extends the name service cache daemon (nscd) so that it can resolve
140 @code{.local} host names using
141 @uref{http://0pointer.de/lennart/projects/nss-mdns/, nss-mdns}. Additionally,
142 add the @var{avahi} package to the system profile so that commands such as
143 @command{avahi-browse} are directly usable.
145 If @var{host-name} is different from @code{#f}, use that as the host name to
146 publish for this machine; otherwise, use the machine's actual host name.
148 When @var{publish?} is true, publishing of host names and services is allowed;
149 in particular, avahi-daemon will publish the machine's host name and IP
150 address via mDNS on the local network.
152 When @var{wide-area?} is true, DNS-SD over unicast DNS is enabled.
154 Boolean values @var{ipv4?} and @var{ipv6?} determine whether to use IPv4/IPv6
156 (service avahi-service-type
158 (avahi avahi) (host-name host-name)
159 (publish? publish?) (ipv4? ipv4?) (ipv6? ipv6?)
160 (wide-area? wide-area?)
161 (domains-to-browse domains-to-browse))))
163 ;;; avahi.scm ends here