fix(avahi): workaround weird hostname conflicts

This commit is contained in:
arcnmx 2024-02-20 16:04:14 -08:00
parent 7dd0e14e1f
commit 4505d8b340

View file

@ -1,9 +1,12 @@
{
pkgs,
config,
lib,
...
}: let
inherit (lib.modules) mkDefault;
inherit (lib.modules) mkDefault mkIf mkOptionDefault;
inherit (lib.strings) makeBinPath;
cfg = config.services.avahi;
in {
services.avahi = {
enable = mkDefault true;
@ -16,4 +19,41 @@ in {
};
wideArea = mkDefault false;
};
systemd.services = let
# work around a weird bug or interaction in avahi-daemon
RestartSec = 2;
daemon = "avahi-daemon.service";
avahi-daemon-watchdog = pkgs.writeShellScript "avahi-daemon-watchdog" ''
set -eu
export PATH="$PATH:${makeBinPath [ config.systemd.package pkgs.coreutils pkgs.gnugrep ]}"
while read -r line; do
if [[ $line = *"Host name conflict"* ]]; then
if systemctl is-active ${daemon} > /dev/null; then
echo restarting avahi-daemon due to host name conflict... >&2
systemctl stop ${daemon}
sleep ${toString RestartSec}
systemctl start ${daemon}
fi
fi
done < <(journalctl -o cat -feu ${daemon} | grep -F 'Host name conflict, retrying with ')
'';
in mkIf (cfg.enable && cfg.publish.enable) {
avahi-daemon = {
serviceConfig = {
inherit RestartSec;
};
};
avahi-daemon-watchdog = {
wantedBy = [ daemon ];
serviceConfig = {
Type = mkOptionDefault "exec";
ExecStart = [
"${avahi-daemon-watchdog}"
];
Restart = mkOptionDefault "on-failure";
RestartSec = mkOptionDefault RestartSec;
};
};
};
}