1# Avahi daemon. 2{ config, lib, utils, pkgs, ... }: 3 4with lib; 5 6let 7 8 cfg = config.services.avahi; 9 10 # We must escape interfaces due to the systemd interpretation 11 subsystemDevice = interface: 12 "sys-subsystem-net-devices-${utils.escapeSystemdPath interface}.device"; 13 14 avahiDaemonConf = with cfg; pkgs.writeText "avahi-daemon.conf" '' 15 [server] 16 ${# Users can set `networking.hostName' to the empty string, when getting 17 # a host name from DHCP. In that case, let Avahi take whatever the 18 # current host name is; setting `host-name' to the empty string in 19 # `avahi-daemon.conf' would be invalid. 20 if hostName != "" 21 then "host-name=${hostName}" 22 else ""} 23 browse-domains=${concatStringsSep ", " browseDomains} 24 use-ipv4=${if ipv4 then "yes" else "no"} 25 use-ipv6=${if ipv6 then "yes" else "no"} 26 ${optionalString (interfaces!=null) "allow-interfaces=${concatStringsSep "," interfaces}"} 27 28 [wide-area] 29 enable-wide-area=${if wideArea then "yes" else "no"} 30 31 [publish] 32 disable-publishing=${if publish.enable then "no" else "yes"} 33 disable-user-service-publishing=${if publish.userServices then "no" else "yes"} 34 publish-addresses=${if publish.userServices || publish.addresses then "yes" else "no"} 35 publish-hinfo=${if publish.hinfo then "yes" else "no"} 36 publish-workstation=${if publish.workstation then "yes" else "no"} 37 publish-domain=${if publish.domain then "yes" else "no"} 38 ''; 39 40in 41 42{ 43 44 ###### interface 45 46 options = { 47 48 services.avahi = { 49 50 enable = mkOption { 51 default = false; 52 description = '' 53 Whether to run the Avahi daemon, which allows Avahi clients 54 to use Avahi's service discovery facilities and also allows 55 the local machine to advertise its presence and services 56 (through the mDNS responder implemented by `avahi-daemon'). 57 ''; 58 }; 59 60 hostName = mkOption { 61 type = types.str; 62 description = '' 63 Host name advertised on the LAN. If not set, avahi will use the value 64 of config.networking.hostName. 65 ''; 66 }; 67 68 browseDomains = mkOption { 69 default = [ "0pointer.de" "zeroconf.org" ]; 70 description = '' 71 List of non-local DNS domains to be browsed. 72 ''; 73 }; 74 75 ipv4 = mkOption { 76 default = true; 77 description = ''Whether to use IPv4''; 78 }; 79 80 ipv6 = mkOption { 81 default = false; 82 description = ''Whether to use IPv6''; 83 }; 84 85 interfaces = mkOption { 86 type = types.nullOr (types.listOf types.str); 87 default = null; 88 description = '' 89 List of network interfaces that should be used by the <command>avahi-daemon</command>. 90 Other interfaces will be ignored. If <literal>null</literal> all local interfaces 91 except loopback and point-to-point will be used. 92 ''; 93 }; 94 95 wideArea = mkOption { 96 default = true; 97 description = ''Whether to enable wide-area service discovery.''; 98 }; 99 100 publish = { 101 enable = mkOption { 102 default = false; 103 description = ''Whether to allow publishing in general.''; 104 }; 105 106 userServices = mkOption { 107 default = false; 108 description = ''Whether to publish user services. Will set <literal>addresses=true</literal>.''; 109 }; 110 111 addresses = mkOption { 112 default = false; 113 description = ''Whether to register mDNS address records for all local IP addresses.''; 114 }; 115 116 hinfo = mkOption { 117 default = false; 118 description = '' 119 Whether to register an mDNS HINFO record which contains information about the 120 local operating system and CPU. 121 ''; 122 }; 123 124 workstation = mkOption { 125 default = false; 126 description = ''Whether to register a service of type "_workstation._tcp" on the local LAN.''; 127 }; 128 129 domain = mkOption { 130 default = false; 131 description = ''Whether to announce the locally used domain name for browsing by other hosts.''; 132 }; 133 134 }; 135 136 nssmdns = mkOption { 137 default = false; 138 description = '' 139 Whether to enable the mDNS NSS (Name Service Switch) plug-in. 140 Enabling it allows applications to resolve names in the `.local' 141 domain by transparently querying the Avahi daemon. 142 ''; 143 }; 144 145 }; 146 147 }; 148 149 150 ###### implementation 151 152 config = mkIf cfg.enable { 153 154 services.avahi.hostName = mkDefault config.networking.hostName; 155 156 users.extraUsers = singleton 157 { name = "avahi"; 158 uid = config.ids.uids.avahi; 159 description = "`avahi-daemon' privilege separation user"; 160 home = "/var/empty"; 161 }; 162 163 users.extraGroups = singleton 164 { name = "avahi"; 165 gid = config.ids.gids.avahi; 166 }; 167 168 system.nssModules = optional cfg.nssmdns pkgs.nssmdns; 169 170 environment.systemPackages = [ pkgs.avahi ]; 171 172 systemd.services.avahi-daemon = 173 let 174 deps = optionals (cfg.interfaces!=null) (map subsystemDevice cfg.interfaces); 175 in 176 { description = "Avahi daemon"; 177 wantedBy = [ "ip-up.target" ]; 178 bindsTo = deps; 179 after = deps; 180 before = [ "ip-up.target" ]; 181 # Receive restart event after resume 182 partOf = [ "post-resume.target" ]; 183 184 path = [ pkgs.coreutils pkgs.avahi ]; 185 186 preStart = "mkdir -p /var/run/avahi-daemon"; 187 188 script = 189 '' 190 # Make NSS modules visible so that `avahi_nss_support ()' can 191 # return a sensible value. 192 export LD_LIBRARY_PATH="${config.system.nssModules.path}" 193 194 exec ${pkgs.avahi}/sbin/avahi-daemon --syslog -f "${avahiDaemonConf}" 195 ''; 196 }; 197 198 services.dbus.enable = true; 199 services.dbus.packages = [ pkgs.avahi ]; 200 201 # Enabling Avahi without exposing it in the firewall doesn't make 202 # sense. 203 networking.firewall.allowedUDPPorts = [ 5353 ]; 204 205 }; 206 207}