services: guix: Generalize extensions.
[jackhill/guix/guix.git] / gnu / services / avahi.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 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 deprecation)
28 #:use-module (guix records)
29 #:use-module (guix gexp)
30 #:export (avahi-configuration
31 avahi-configuration?
32
33 avahi-configuration-avahi
34 avahi-configuration-debug?
35 avahi-configuration-host-name
36 avahi-configuration-publish?
37 avahi-configuration-publish-workstation?
38 avahi-configuration-ipv4?
39 avahi-configuration-ipv6?
40 avahi-configuration-wide-area?
41 avahi-configuration-domains-to-browse
42
43 avahi-service-type))
44
45 ;;; Commentary:
46 ;;;
47 ;;; This module provides service definitions for the Avahi
48 ;;; "zero-configuration" tool set.
49 ;;;
50 ;;; Code:
51
52 (define-record-type* <avahi-configuration>
53 avahi-configuration make-avahi-configuration
54 avahi-configuration?
55 (avahi avahi-configuration-avahi ;file-like
56 (default avahi))
57 (debug? avahi-configuration-debug? ;Boolean
58 (default #f))
59 (host-name avahi-configuration-host-name ;string | #f
60 (default #f))
61 (publish? avahi-configuration-publish? ;boolean
62 (default #t))
63
64 ;; The default for this was #t in Avahi 0.6.31 and became #f in 0.7. For
65 ;; now we stick to the old default.
66 (publish-workstation? avahi-configuration-publish-workstation? ;Boolean
67 (default #t))
68
69 (ipv4? avahi-configuration-ipv4? ;Boolean
70 (default #t))
71 (ipv6? avahi-configuration-ipv6? ;Boolean
72 (default #t))
73 (wide-area? avahi-configuration-wide-area? ;Boolean
74 (default #f))
75 (domains-to-browse avahi-configuration-domains-to-browse ;list of strings
76 (default '())))
77
78 (define* (configuration-file config)
79 "Return an avahi-daemon configuration file based on CONFIG, an
80 <avahi-configuration>."
81 (define (bool value)
82 (if value "yes\n" "no\n"))
83
84 (define host-name (avahi-configuration-host-name config))
85
86 (plain-file "avahi-daemon.conf"
87 (string-append
88 "[server]\n"
89 (if host-name
90 (string-append "host-name=" host-name "\n")
91 "")
92
93 "browse-domains=" (string-join
94 (avahi-configuration-domains-to-browse
95 config))
96 "\n"
97 "use-ipv4=" (bool (avahi-configuration-ipv4? config))
98 "use-ipv6=" (bool (avahi-configuration-ipv6? config))
99 "[wide-area]\n"
100 "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
101 "[publish]\n"
102 "disable-publishing="
103 (bool (not (avahi-configuration-publish? config)))
104 "publish-workstation="
105 (bool (avahi-configuration-publish-workstation? config)))))
106
107 (define %avahi-accounts
108 ;; Account and group for the Avahi daemon.
109 (list (user-group (name "avahi") (system? #t))
110 (user-account
111 (name "avahi")
112 (group "avahi")
113 (system? #t)
114 (comment "Avahi daemon user")
115 (home-directory "/var/empty")
116 (shell (file-append shadow "/sbin/nologin")))))
117
118 (define %avahi-activation
119 ;; Activation gexp.
120 #~(begin
121 (use-modules (guix build utils))
122 (mkdir-p "/run/avahi-daemon")))
123
124 (define (avahi-shepherd-service config)
125 "Return a list of <shepherd-service> for CONFIG."
126 (let ((config (configuration-file config))
127 (debug? (avahi-configuration-debug? config))
128 (avahi (avahi-configuration-avahi config)))
129 (list (shepherd-service
130 (documentation "Run the Avahi mDNS/DNS-SD responder.")
131 (provision '(avahi-daemon))
132 (requirement '(user-processes dbus-system networking))
133
134 (start #~(make-forkexec-constructor
135 (list #$(file-append avahi "/sbin/avahi-daemon")
136 "--daemonize"
137 #$@(if debug? #~("--debug") #~())
138 "-f" #$config)
139 #:pid-file "/run/avahi-daemon/pid"))
140 (stop #~(make-kill-destructor))))))
141
142 (define avahi-service-type
143 (let ((avahi-package (compose list avahi-configuration-avahi)))
144 (service-type (name 'avahi)
145 (description
146 "Run @command{avahi-daemon}, a host and service discovery
147 daemon that implements the multicast DNS (mDNS) and DNS service
148 discovery (DNS-SD) protocols. Additionally, extend the C library's name
149 service switch (NSS) with support for @code{.local} host name resolution.")
150 (extensions
151 (list (service-extension shepherd-root-service-type
152 avahi-shepherd-service)
153 (service-extension dbus-root-service-type
154 avahi-package)
155 (service-extension account-service-type
156 (const %avahi-accounts))
157 (service-extension activation-service-type
158 (const %avahi-activation))
159 (service-extension nscd-service-type
160 (const (list nss-mdns)))
161
162 ;; Provide 'avahi-browse', 'avahi-resolve', etc. in
163 ;; the system profile.
164 (service-extension profile-service-type
165 avahi-package)))
166 (default-value (avahi-configuration)))))
167
168 ;;; avahi.scm ends here