services: Rename 'dmd' services to 'shepherd'.
[jackhill/guix/guix.git] / gnu / services / avahi.scm
CommitLineData
f4561be2 1;;; GNU Guix --- Functional package management for GNU
893106cb 2;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
f4561be2
LC
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)
0adfe95a 21 #:use-module (gnu services base)
0190c1c0 22 #:use-module (gnu services shepherd)
0adfe95a 23 #:use-module (gnu services dbus)
f4561be2
LC
24 #:use-module (gnu system shadow)
25 #:use-module (gnu packages avahi)
5e25ebe2 26 #:use-module (gnu packages admin)
0adfe95a 27 #:use-module (guix records)
f4561be2 28 #:use-module (guix gexp)
893106cb
LC
29 #:export (avahi-service
30 avahi-service-type))
f4561be2
LC
31
32;;; Commentary:
33;;;
34;;; This module provides service definitions for the Avahi
35;;; "zero-configuration" tool set.
36;;;
37;;; Code:
38
0adfe95a
LC
39 ;; TODO: Export.
40(define-record-type* <avahi-configuration>
41 avahi-configuration make-avahi-configuration
42 avahi-configuration?
43 (avahi avahi-configuration-avahi ;<package>
44 (default avahi))
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
51
52(define* (configuration-file config)
53 "Return an avahi-daemon configuration file based on CONFIG, an
54<avahi-configuration>."
f4561be2
LC
55 (define (bool value)
56 (if value "yes\n" "no\n"))
57
0adfe95a
LC
58 (define host-name (avahi-configuration-host-name config))
59
be1c2c54
LC
60 (plain-file "avahi-daemon.conf"
61 (string-append
62 "[server]\n"
63 (if host-name
64 (string-append "host-name=" host-name "\n")
65 "")
f4561be2 66
0adfe95a
LC
67 "browse-domains=" (string-join
68 (avahi-configuration-domains-to-browse
69 config))
be1c2c54 70 "\n"
0adfe95a
LC
71 "use-ipv4=" (bool (avahi-configuration-ipv4? config))
72 "use-ipv6=" (bool (avahi-configuration-ipv6? config))
be1c2c54 73 "[wide-area]\n"
0adfe95a 74 "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
be1c2c54 75 "[publish]\n"
0adfe95a
LC
76 "disable-publishing="
77 (bool (not (avahi-configuration-publish? config))))))
78
79(define %avahi-accounts
80 ;; Account and group for the Avahi daemon.
81 (list (user-group (name "avahi") (system? #t))
82 (user-account
83 (name "avahi")
84 (group "avahi")
85 (system? #t)
86 (comment "Avahi daemon user")
87 (home-directory "/var/empty")
88 (shell #~(string-append #$shadow "/sbin/nologin")))))
89
90(define %avahi-activation
91 ;; Activation gexp.
92 #~(begin
93 (use-modules (guix build utils))
94 (mkdir-p "/var/run/avahi-daemon")))
95
d4053c71
AK
96(define (avahi-shepherd-service config)
97 "Return a list of <shepherd-service> for CONFIG."
0adfe95a
LC
98 (let ((config (configuration-file config))
99 (avahi (avahi-configuration-avahi config)))
d4053c71 100 (list (shepherd-service
0adfe95a
LC
101 (documentation "Run the Avahi mDNS/DNS-SD responder.")
102 (provision '(avahi-daemon))
103 (requirement '(dbus-system networking))
104
105 (start #~(make-forkexec-constructor
106 (list (string-append #$avahi "/sbin/avahi-daemon")
107 "--syslog" "-f" #$config)))
108 (stop #~(make-kill-destructor))))))
109
110(define avahi-service-type
1065bed9
LC
111 (let ((avahi-package (compose list avahi-configuration-avahi)))
112 (service-type (name 'avahi)
113 (extensions
d4053c71
AK
114 (list (service-extension shepherd-root-service-type
115 avahi-shepherd-service)
1065bed9
LC
116 (service-extension dbus-root-service-type
117 avahi-package)
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)))
124
125 ;; Provide 'avahi-browse', 'avahi-resolve', etc. in
126 ;; the system profile.
127 (service-extension profile-service-type
128 avahi-package))))))
f4561be2
LC
129
130(define* (avahi-service #:key (avahi avahi)
131 host-name
132 (publish? #t)
133 (ipv4? #t) (ipv6? #t)
134 wide-area?
135 (domains-to-browse '()))
136 "Return a service that runs @command{avahi-daemon}, a system-wide
137mDNS/DNS-SD responder that allows for service discovery and
cc9c1f39
LC
138\"zero-configuration\" host name lookups (see @uref{http://avahi.org/}), and
139extends the name service cache daemon (nscd) so that it can resolve
140@code{.local} host names using
1065bed9
LC
141@uref{http://0pointer.de/lennart/projects/nss-mdns/, nss-mdns}. Additionally,
142add the @var{avahi} package to the system profile so that commands such as
143@command{avahi-browse} are directly usable.
f4561be2
LC
144
145If @var{host-name} is different from @code{#f}, use that as the host name to
146publish for this machine; otherwise, use the machine's actual host name.
147
148When @var{publish?} is true, publishing of host names and services is allowed;
149in particular, avahi-daemon will publish the machine's host name and IP
150address via mDNS on the local network.
151
152When @var{wide-area?} is true, DNS-SD over unicast DNS is enabled.
153
154Boolean values @var{ipv4?} and @var{ipv6?} determine whether to use IPv4/IPv6
155sockets."
0adfe95a
LC
156 (service avahi-service-type
157 (avahi-configuration
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))))
f4561be2
LC
162
163;;; avahi.scm ends here