avahi.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014-2020, 2022 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-type))
  41. ;;; Commentary:
  42. ;;;
  43. ;;; This module provides service definitions for the Avahi
  44. ;;; "zero-configuration" tool set.
  45. ;;;
  46. ;;; Code:
  47. (define-record-type* <avahi-configuration>
  48. avahi-configuration make-avahi-configuration
  49. avahi-configuration?
  50. (avahi avahi-configuration-avahi ;file-like
  51. (default avahi))
  52. (debug? avahi-configuration-debug? ;Boolean
  53. (default #f))
  54. (host-name avahi-configuration-host-name ;string | #f
  55. (default #f))
  56. (publish? avahi-configuration-publish? ;boolean
  57. (default #t))
  58. ;; The default for this was #t in Avahi 0.6.31 and became #f in 0.7. For
  59. ;; now we stick to the old default.
  60. (publish-workstation? avahi-configuration-publish-workstation? ;Boolean
  61. (default #t))
  62. (ipv4? avahi-configuration-ipv4? ;Boolean
  63. (default #t))
  64. (ipv6? avahi-configuration-ipv6? ;Boolean
  65. (default #t))
  66. (wide-area? avahi-configuration-wide-area? ;Boolean
  67. (default #f))
  68. (domains-to-browse avahi-configuration-domains-to-browse ;list of strings
  69. (default '())))
  70. (define* (configuration-file config)
  71. "Return an avahi-daemon configuration file based on CONFIG, an
  72. <avahi-configuration>."
  73. (define (bool value)
  74. (if value "yes\n" "no\n"))
  75. (define host-name (avahi-configuration-host-name config))
  76. (plain-file "avahi-daemon.conf"
  77. (string-append
  78. "[server]\n"
  79. (if host-name
  80. (string-append "host-name=" host-name "\n")
  81. "")
  82. "browse-domains=" (string-join
  83. (avahi-configuration-domains-to-browse
  84. config))
  85. "\n"
  86. "use-ipv4=" (bool (avahi-configuration-ipv4? config))
  87. "use-ipv6=" (bool (avahi-configuration-ipv6? config))
  88. "[wide-area]\n"
  89. "enable-wide-area=" (bool (avahi-configuration-wide-area? config))
  90. "[publish]\n"
  91. "disable-publishing="
  92. (bool (not (avahi-configuration-publish? config)))
  93. "publish-workstation="
  94. (bool (avahi-configuration-publish-workstation? config)))))
  95. (define %avahi-accounts
  96. ;; Account and group for the Avahi daemon.
  97. (list (user-group (name "avahi") (system? #t))
  98. (user-account
  99. (name "avahi")
  100. (group "avahi")
  101. (system? #t)
  102. (comment "Avahi daemon user")
  103. (home-directory "/var/empty")
  104. (shell (file-append shadow "/sbin/nologin")))))
  105. (define %avahi-activation
  106. ;; Activation gexp.
  107. #~(begin
  108. (use-modules (guix build utils))
  109. (mkdir-p "/run/avahi-daemon")))
  110. (define (avahi-shepherd-service config)
  111. "Return a list of <shepherd-service> for CONFIG."
  112. (let ((config (configuration-file config))
  113. (debug? (avahi-configuration-debug? config))
  114. (avahi (avahi-configuration-avahi config)))
  115. (list (shepherd-service
  116. (documentation "Run the Avahi mDNS/DNS-SD responder.")
  117. (provision '(avahi-daemon))
  118. (requirement '(user-processes dbus-system networking))
  119. (start #~(make-forkexec-constructor
  120. (list #$(file-append avahi "/sbin/avahi-daemon")
  121. "--daemonize"
  122. #$@(if debug? #~("--debug") #~())
  123. "-f" #$config)
  124. #:pid-file "/run/avahi-daemon/pid"))
  125. (stop #~(make-kill-destructor))
  126. (actions (list (shepherd-configuration-action config)))))))
  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. ;;; avahi.scm ends here