{
  config,
  lib,
  namespace,
  ...
}:
with lib;
let
  cfg = config.${namespace}.services.home-assistant;
in
{
  config = mkIf cfg.enable {
    services = {
      music-assistant = {
        enable = true;
        providers = [
          # "airplay" # music-assistant: airplay support is missing libraop, a library we will not package because it depends on OpenSSL 1.1.
          "alexa"
          "apple_music"
          "audible"
          "audiobookshelf"
          "bluesound"
          "builtin"
          "builtin_player"
          "chromecast"
          "deezer"
          "dlna"
          "fanarttv"
          "filesystem_local"
          "filesystem_smb"
          "fully_kiosk"
          "gpodder"
          "hass"
          "hass_players"
          "ibroadcast"
          "itunes_podcasts"
          "jellyfin"
          "lastfm_scrobble"
          "listenbrainz_scrobble"
          "lrclib"
          "musicbrainz"
          "musiccast"
          "nugs"
          "opensubsonic"
          "player_group"
          "plex"
          "podcastfeed"
          "qobuz"
          "radiobrowser"
          "siriusxm"
          "snapcast"
          "sonos"
          "sonos_s1"
          "soundcloud"
          "spotify"
          "spotify_connect"
          "squeezelite"
          "subsonic_scrobble"
          "template_player_provider"
          "test"
          "theaudiodb"
          "tidal"
          "tunein"
          "ytmusic"
        ];
      };

      # Enable AirPlay
      pipewire = {
        raopOpenFirewall = true;

        extraConfig.pipewire = {
          "10-airplay" = {
            "context.modules" = [
              {
                name = "libpipewire-module-raop-discover";
              }
            ];
          };
        };
      };
    };

    users.users.music-assistant = {
      isSystemUser = true;
      group = "music-assistant";
      home = "/var/lib/music-assistant";
    };
    users.groups.music-assistant = { };

    systemd.services.music-assistant.serviceConfig = {
      DynamicUser = lib.mkForce false;
      StateDirectory = "music-assistant";
      User = "music-assistant";
      Group = "music-assistant";
    };
  };
}
