avahi.scm 7.2 KB

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