{
  lib,
  pkgs,
  host_secrets,
  ...
}: let
  ports = import ../data/ports.nix {};
  secrets = host_secrets;
in {
  environment.systemPackages = with pkgs; [mpc_cli];

  services.mpd = {
    enable = true;
    network.listenAddress = "0.0.0.0";
    musicDirectory = "/Music";
    credentials = [
      {
        passwordFile = "${secrets.mpd_control_password.path}";
        permissions = ["read" "add" "control" "admin"];
      }
    ];
    extraConfig =
      ''
        host_permissions "127.0.0.1 read,add,control,admin"
        samplerate_converter "0"
        metadata_to_use "title,artist"
        auto_update "yes"
        audio_buffer_size "4096"
        replaygain "track"
        audio_output_format "44100:16:2"
      ''
      + lib.concatStringsSep "\n" (lib.forEach ["low" "medium" "high"] (quality: let
        bitrates = {
          "low" = "64";
          "medium" = "96";
          "high" = "128";
        };
        bitrate = bitrates.${quality};
      in ''
        audio_output {
          type		"httpd"
          name		"HTTP Opus ${bitrate}k"
          encoder		"opus"
          port		"${toString ports."mpd-opus-${quality}"}"
          bitrate		"${bitrate}000"
          format		"44100:16:2"
          always_on       "yes"
          tags            "yes"
          signal "music"
        }
      ''))
      + ''
        audio_output {
          type		"httpd"
          name		"HTTP FLAC"
          encoder		"flac"
          port		"${toString ports.mpd-flac}"
          format		"44100:16:2"
          always_on       "yes"
          tags            "yes"
        }
      '';
  };
}