avahi.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. (define-module (gnu services avahi)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services base)
  21. #:use-module (gnu services shepherd)
  22. #:use-module (gnu services dbus)
  23. #:use-module (gnu system shadow)
  24. #:use-module (gnu packages avahi)
  25. #:use-module (gnu packages admin)
  26. #:use-module (guix deprecation)
  27. #:use-module (guix records)
  28. #:use-module (guix gexp)
  29. #:export (avahi-configuration
  30. avahi-configuration?
  31. avahi-configuration-avahi
  32. avahi-configuration-debug?
  33. avahi-configuration-host-name
  34. avahi-configuration-publish?
  35. avahi-configuration-publish-workstation?
  36. avahi-configuration-ipv4?
  37. avahi-configuration-ipv6?
  38. avahi-configuration-wide-area?
  39. avahi-configuration-domains-to-browse
  40. avahi-service
  41. avahi-service-type))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; This module provides service definitions for the Avahi
  45. ;;; "zero-configuration" tool set.
  46. ;;;
  47. ;;; Code:
  48. (define-record-type* <avahi-configuration>
  49. avahi-configuration make-avahi-configuration
  50. avahi-configuration?
  51. (avahi avahi-configuration-avahi ;<package>
  52. (default avahi))
  53. (debug? avahi-configuration-debug? ;Boolean
  54. (default #f))
  55. (host-name avahi-configuration-host-name ;string | #f
  56. (default #f))
  57. (publish? avahi-configuration-publish? ;boolean
  58. (default #t))
  59. ;; The default for this was #t in Avahi 0.6.31 and became #f in 0.7. For
  60. ;; now we stick to the old default.
  61. (publish-workstation? avahi-configuration-publish-workstation? ;Boolean
  62. (default #t))
  63. (ipv4? avahi-configuration-ipv4? ;Boolean
  64. (default #t))
  65. (ipv6? avahi-configuration-ipv6? ;Boolean
  66. (default #t))
  67. (wide-area? avahi-configuration-wide-area? ;Boolean
  68. (default #f))
  69. (domains-to-browse avahi-configuration-domains-to-browse ;list of strings
  70. (default '())))
  71. (define* (configuration-file config)
  72. "Return an avahi-daemon configuration file based on CONFIG, an
  73. <avahi-configuration>."
  74. (define (bool value)
  75. (if value "yes\n" "no\n"))
  76. (define host-name (avahi-configuration-host-name config))
  77. (plain-file "avahi-daemon.conf"
  78. (string-append
  79. "[server]\n"
  80. (if host-name
  81. (string-append "host-name=" host-name "\n")
  82. "")
  83. "browse-domains=" (string-join
  84. (avahi-configuration-domains-to-browse
  85. config))
  86. "\n"
  87. "use-ipv4=" (bool (avahi-configuration-ipv4? config))
  88. "use-ipv6=" (bool (avahi-configuration-ipv6? config))
  89. "[wide-area]\n"
  90. "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
  91. "[publish]\n"
  92. "disable-publishing="
  93. (bool (not (avahi-configuration-publish? config)))
  94. "publish-workstation="
  95. (bool (avahi-configuration-publish-workstation? config)))))
  96. (define %avahi-accounts
  97. ;; Account and group for the Avahi daemon.
  98. (list (user-group (name "avahi") (system? #t))
  99. (user-account
  100. (name "avahi")
  101. (group "avahi")
  102. (system? #t)
  103. (comment "Avahi daemon user")
  104. (home-directory "/var/empty")
  105. (shell (file-append shadow "/sbin/nologin")))))
  106. (define %avahi-activation
  107. ;; Activation gexp.
  108. #~(begin
  109. (use-modules (guix build utils))
  110. (mkdir-p "/run/avahi-daemon")))
  111. (define (avahi-shepherd-service config)
  112. "Return a list of <shepherd-service> for CONFIG."
  113. (let ((config (configuration-file config))
  114. (debug? (avahi-configuration-debug? config))
  115. (avahi (avahi-configuration-avahi config)))
  116. (list (shepherd-service
  117. (documentation "Run the Avahi mDNS/DNS-SD responder.")
  118. (provision '(avahi-daemon))
  119. (requirement '(user-processes dbus-system networking))
  120. (start #~(make-forkexec-constructor
  121. (list #$(file-append avahi "/sbin/avahi-daemon")
  122. "--daemonize"
  123. #$@(if debug? #~("--debug") #~())
  124. "-f" #$config)
  125. #:pid-file "/run/avahi-daemon/pid"))
  126. (stop #~(make-kill-destructor))))))
  127. (define avahi-service-type
  128. (let ((avahi-package (compose list avahi-configuration-avahi)))
  129. (service-type (name 'avahi)
  130. (description
  131. "Run @command{avahi-daemon}, a host and service discovery
  132. daemon that implements the multicast DNS (mDNS) and DNS service
  133. discovery (DNS-SD) protocols. Additionally, extend the C library's name
  134. service switch (NSS) with support for @code{.local} host name resolution.")
  135. (extensions
  136. (list (service-extension shepherd-root-service-type
  137. avahi-shepherd-service)
  138. (service-extension dbus-root-service-type
  139. avahi-package)
  140. (service-extension account-service-type
  141. (const %avahi-accounts))
  142. (service-extension activation-service-type
  143. (const %avahi-activation))
  144. (service-extension nscd-service-type
  145. (const (list nss-mdns)))
  146. ;; Provide 'avahi-browse', 'avahi-resolve', etc. in
  147. ;; the system profile.
  148. (service-extension profile-service-type
  149. avahi-package)))
  150. (default-value (avahi-configuration)))))
  151. (define-deprecated (avahi-service #:key (avahi avahi) debug?
  152. host-name
  153. (publish? #t)
  154. (ipv4? #t) (ipv6? #t)
  155. wide-area?
  156. (domains-to-browse '()))
  157. avahi-service-type
  158. "Return a service that runs @command{avahi-daemon}, a system-wide
  159. mDNS/DNS-SD responder that allows for service discovery and
  160. \"zero-configuration\" host name lookups (see @uref{https://avahi.org/}), and
  161. extends the name service cache daemon (nscd) so that it can resolve
  162. @code{.local} host names using
  163. @uref{http://0pointer.de/lennart/projects/nss-mdns/, nss-mdns}. Additionally,
  164. add the @var{avahi} package to the system profile so that commands such as
  165. @command{avahi-browse} are directly usable.
  166. If @var{host-name} is different from @code{#f}, use that as the host name to
  167. 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.
  171. When @var{wide-area?} is true, DNS-SD over unicast DNS is enabled.
  172. Boolean values @var{ipv4?} and @var{ipv6?} determine whether to use IPv4/IPv6
  173. sockets."
  174. (service avahi-service-type
  175. (avahi-configuration
  176. (avahi avahi) (debug? debug?) (host-name host-name)
  177. (publish? publish?) (ipv4? ipv4?) (ipv6? ipv6?)
  178. (wide-area? wide-area?)
  179. (domains-to-browse domains-to-browse))))
  180. ;;; avahi.scm ends here