nixos/services.avahi: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-28 21:19:01 +02:00 committed by Jörg Thalheim
parent d861803952
commit eb8d4ed264

View File

@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.avahi; cfg = config.services.avahi;
@ -13,15 +10,15 @@ let
# a host name from DHCP. In that case, let Avahi take whatever the # a host name from DHCP. In that case, let Avahi take whatever the
# current host name is; setting `host-name' to the empty string in # current host name is; setting `host-name' to the empty string in
# `avahi-daemon.conf' would be invalid. # `avahi-daemon.conf' would be invalid.
optionalString (hostName != "") "host-name=${hostName}"} lib.optionalString (hostName != "") "host-name=${hostName}"}
browse-domains=${concatStringsSep ", " browseDomains} browse-domains=${lib.concatStringsSep ", " browseDomains}
use-ipv4=${yesNo ipv4} use-ipv4=${yesNo ipv4}
use-ipv6=${yesNo ipv6} use-ipv6=${yesNo ipv6}
${optionalString (allowInterfaces!=null) "allow-interfaces=${concatStringsSep "," allowInterfaces}"} ${lib.optionalString (allowInterfaces!=null) "allow-interfaces=${lib.concatStringsSep "," allowInterfaces}"}
${optionalString (denyInterfaces!=null) "deny-interfaces=${concatStringsSep "," denyInterfaces}"} ${lib.optionalString (denyInterfaces!=null) "deny-interfaces=${lib.concatStringsSep "," denyInterfaces}"}
${optionalString (domainName!=null) "domain-name=${domainName}"} ${lib.optionalString (domainName!=null) "domain-name=${domainName}"}
allow-point-to-point=${yesNo allowPointToPoint} allow-point-to-point=${yesNo allowPointToPoint}
${optionalString (cacheEntriesMax!=null) "cache-entries-max=${toString cacheEntriesMax}"} ${lib.optionalString (cacheEntriesMax!=null) "cache-entries-max=${toString cacheEntriesMax}"}
[wide-area] [wide-area]
enable-wide-area=${yesNo wideArea} enable-wide-area=${yesNo wideArea}
@ -46,8 +43,8 @@ in
]; ];
options.services.avahi = { options.services.avahi = {
enable = mkOption { enable = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to run the Avahi daemon, which allows Avahi clients Whether to run the Avahi daemon, which allows Avahi clients
@ -57,28 +54,28 @@ in
''; '';
}; };
package = mkPackageOption pkgs "avahi" { }; package = lib.mkPackageOption pkgs "avahi" { };
hostName = mkOption { hostName = lib.mkOption {
type = types.str; type = lib.types.str;
default = config.networking.hostName; default = config.networking.hostName;
defaultText = literalExpression "config.networking.hostName"; defaultText = lib.literalExpression "config.networking.hostName";
description = '' description = ''
Host name advertised on the LAN. If not set, avahi will use the value Host name advertised on the LAN. If not set, avahi will use the value
of {option}`config.networking.hostName`. of {option}`config.networking.hostName`.
''; '';
}; };
domainName = mkOption { domainName = lib.mkOption {
type = types.str; type = lib.types.str;
default = "local"; default = "local";
description = '' description = ''
Domain name for all advertisements. Domain name for all advertisements.
''; '';
}; };
browseDomains = mkOption { browseDomains = lib.mkOption {
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
default = [ ]; default = [ ];
example = [ "0pointer.de" "zeroconf.org" ]; example = [ "0pointer.de" "zeroconf.org" ];
description = '' description = ''
@ -86,20 +83,20 @@ in
''; '';
}; };
ipv4 = mkOption { ipv4 = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = true; default = true;
description = "Whether to use IPv4."; description = "Whether to use IPv4.";
}; };
ipv6 = mkOption { ipv6 = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Whether to use IPv6."; description = "Whether to use IPv6.";
}; };
allowInterfaces = mkOption { allowInterfaces = lib.mkOption {
type = types.nullOr (types.listOf types.str); type = lib.types.nullOr (lib.types.listOf lib.types.str);
default = null; default = null;
description = '' description = ''
List of network interfaces that should be used by the {command}`avahi-daemon`. List of network interfaces that should be used by the {command}`avahi-daemon`.
@ -108,8 +105,8 @@ in
''; '';
}; };
denyInterfaces = mkOption { denyInterfaces = lib.mkOption {
type = types.nullOr (types.listOf types.str); type = lib.types.nullOr (lib.types.listOf lib.types.str);
default = null; default = null;
description = '' description = ''
List of network interfaces that should be ignored by the List of network interfaces that should be ignored by the
@ -119,8 +116,8 @@ in
''; '';
}; };
openFirewall = mkOption { openFirewall = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = true; default = true;
description = '' description = ''
Whether to open the firewall for UDP port 5353. Whether to open the firewall for UDP port 5353.
@ -128,8 +125,8 @@ in
''; '';
}; };
allowPointToPoint = mkOption { allowPointToPoint = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to use POINTTOPOINT interfaces. Might make mDNS unreliable due to usually large Whether to use POINTTOPOINT interfaces. Might make mDNS unreliable due to usually large
@ -138,22 +135,22 @@ in
''; '';
}; };
wideArea = mkOption { wideArea = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = true; default = true;
description = "Whether to enable wide-area service discovery."; description = "Whether to enable wide-area service discovery.";
}; };
reflector = mkOption { reflector = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Reflect incoming mDNS requests to all allowed network interfaces."; description = "Reflect incoming mDNS requests to all allowed network interfaces.";
}; };
extraServiceFiles = mkOption { extraServiceFiles = lib.mkOption {
type = with types; attrsOf (either str path); type = with lib.types; attrsOf (either str path);
default = { }; default = { };
example = literalExpression '' example = lib.literalExpression ''
{ {
ssh = "''${pkgs.avahi}/etc/avahi/services/ssh.service"; ssh = "''${pkgs.avahi}/etc/avahi/services/ssh.service";
smb = ''' smb = '''
@ -176,26 +173,26 @@ in
}; };
publish = { publish = {
enable = mkOption { enable = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Whether to allow publishing in general."; description = "Whether to allow publishing in general.";
}; };
userServices = mkOption { userServices = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Whether to publish user services. Will set `addresses=true`."; description = "Whether to publish user services. Will set `addresses=true`.";
}; };
addresses = mkOption { addresses = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Whether to register mDNS address records for all local IP addresses."; description = "Whether to register mDNS address records for all local IP addresses.";
}; };
hinfo = mkOption { hinfo = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to register a mDNS HINFO record which contains information about the Whether to register a mDNS HINFO record which contains information about the
@ -203,23 +200,23 @@ in
''; '';
}; };
workstation = mkOption { workstation = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to register a service of type "_workstation._tcp" on the local LAN. Whether to register a service of type "_workstation._tcp" on the local LAN.
''; '';
}; };
domain = mkOption { domain = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Whether to announce the locally used domain name for browsing by other hosts."; description = "Whether to announce the locally used domain name for browsing by other hosts.";
}; };
}; };
nssmdns4 = mkOption { nssmdns4 = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to enable the mDNS NSS (Name Service Switch) plug-in for IPv4. Whether to enable the mDNS NSS (Name Service Switch) plug-in for IPv4.
@ -228,8 +225,8 @@ in
''; '';
}; };
nssmdns6 = mkOption { nssmdns6 = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to enable the mDNS NSS (Name Service Switch) plug-in for IPv6. Whether to enable the mDNS NSS (Name Service Switch) plug-in for IPv6.
@ -243,8 +240,8 @@ in
''; '';
}; };
cacheEntriesMax = mkOption { cacheEntriesMax = lib.mkOption {
type = types.nullOr types.int; type = lib.types.nullOr lib.types.int;
default = null; default = null;
description = '' description = ''
Number of resource records to be cached per interface. Use 0 to Number of resource records to be cached per interface. Use 0 to
@ -252,8 +249,8 @@ in
''; '';
}; };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = types.lines; type = lib.types.lines;
default = ""; default = "";
description = '' description = ''
Extra config to append to avahi-daemon.conf. Extra config to append to avahi-daemon.conf.
@ -261,7 +258,7 @@ in
}; };
}; };
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
users.users.avahi = { users.users.avahi = {
description = "avahi-daemon privilege separation user"; description = "avahi-daemon privilege separation user";
home = "/var/empty"; home = "/var/empty";
@ -271,7 +268,7 @@ in
users.groups.avahi = { }; users.groups.avahi = { };
system.nssModules = optional (cfg.nssmdns4 || cfg.nssmdns6) pkgs.nssmdns; system.nssModules = lib.optional (cfg.nssmdns4 || cfg.nssmdns6) pkgs.nssmdns;
system.nssDatabases.hosts = let system.nssDatabases.hosts = let
mdns = if (cfg.nssmdns4 && cfg.nssmdns6) then mdns = if (cfg.nssmdns4 && cfg.nssmdns6) then
"mdns" "mdns"
@ -281,17 +278,17 @@ in
"mdns4" "mdns4"
else else
""; "";
in optionals (cfg.nssmdns4 || cfg.nssmdns6) (mkMerge [ in lib.optionals (cfg.nssmdns4 || cfg.nssmdns6) (lib.mkMerge [
(mkBefore [ "${mdns}_minimal [NOTFOUND=return]" ]) # before resolve (lib.mkBefore [ "${mdns}_minimal [NOTFOUND=return]" ]) # before resolve
(mkAfter [ "${mdns}" ]) # after dns (lib.mkAfter [ "${mdns}" ]) # after dns
]); ]);
environment.systemPackages = [ cfg.package ]; environment.systemPackages = [ cfg.package ];
environment.etc = (mapAttrs' environment.etc = (lib.mapAttrs'
(n: v: nameValuePair (n: v: lib.nameValuePair
"avahi/services/${n}.service" "avahi/services/${n}.service"
{ ${if types.path.check v then "source" else "text"} = v; } { ${if lib.types.path.check v then "source" else "text"} = v; }
) )
cfg.extraServiceFiles); cfg.extraServiceFiles);
@ -326,6 +323,6 @@ in
services.dbus.enable = true; services.dbus.enable = true;
services.dbus.packages = [ cfg.package ]; services.dbus.packages = [ cfg.package ];
networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ 5353 ]; networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [ 5353 ];
}; };
} }