{
  pkgs,
  lib,
  osConfig,
  config,
  ...
}:
let
  inherit (lib) concatStringsSep listToAttrs;
  inherit (lib.my) getExe;
  plugins = [
    "convert"
    "chroma"
    "inline"
    "fish"
    "fetchart"
    "lastgenre"
    "scrub"
    "duplicates"
    "info"
    "missing"
    "unimported"
    "badfiles"
    "embedart"
    "play"
  ];

  mkConf =
    a:
    let
      mkGroup = name: values: ''
        ${name} {
            ${lib.concatStringsSep "\n\t" values}
        }'';
      stringify = lib.mapAttrsToList (
        name: value:
        if lib.isAttrs value then
          mkGroup name (stringify value)
        else if (lib.isInt value || lib.isFloat value) then
          ''${name} "${toString value}"''
        else
          ''${name} "${value}"''
      );
    in
    concatStringsSep "\n" (stringify a);

  mpdSocket = "/run/user/${toString osConfig.users.users.${config.home.username}.uid}/mpd/socket";
in
{
  config = {
    services.mpd = {
      enable = true;
      extraConfig = mkConf {
        auto_update = "yes";
        log_file = "syslog";
        bind_to_address = mpdSocket;
        audio_output = {
          type = "pipewire";
          name = "PipeWire Sound Server";
        };
      };
    };

    programs.beets = {
      package = pkgs.beets.override {
        pluginOverrides = listToAttrs (
          map (name: {
            value.enable = true;
            inherit name;
          }) plugins
        );
      };
      settings = {
        plugins = concatStringsSep " " plugins;
        per_disc_numbering = "yes";
        asciify_paths = true;
        convert = {
          auto = "yes";
          format = "flac";
          never_convert_lossy_files = "yes";
        };
        play = {
          command =
            let
              mpc = "${getExe pkgs.mpc} --host=\"${mpdSocket}\"";
              ncmpcpp = getExe pkgs.ncmpcpp;

              command = pkgs.writers.writeFishBin "play-command" ''
                set playlist "$argv[1]";
                ${mpc} clear;
                ${mpc} load "$playlist";
                rm "$playlist";
                ${mpc} play;
                ${ncmpcpp} $args;
              '';
            in
            "${command}/bin/play-command";
        };
        embedart = {
          maxwidth = 500;
        };
        fetchart = {
          auto = true;
          cover_format = "jpg";
          minheight = 500;
          minwidth = 500;
          enforce_ratio = true;
          high_resolution = true;
        };
        duplicates = {
          count = true;
        };
        lastgenre = {
          auto = "yes";
          count = 5;
          separator = ";";
        };
        replace = {
          "[\\\\/]" = "_";
          "^\\." = "_";
          "[\\x00-\\x1f]" = "_";
          "\\.$" = "_";
          "\\s+$" = "";
          "^\\s+" = "";
          "^-" = "_";
        };
        import = {
          incremental = "yes";
          bell = "yes";
          languages = "en";
        };
        item_fields = {
          disc_and_track = # python
            "u'%02i.%02i' % (disc, track) if disctotal > 1 else u'%02i' % (track)";
          primary_albumartist = # python
            ''
              if albumartist.lower().strip().startswith(albumartists[0].lower().strip()):
                  return albumartists[0]
              else:
                 return None
            '';
        };
        paths = {
          default = "%ifdef{primary_albumartist, $primary_albumartist, $albumartist}/$album%aunique{}/$disc_and_track - $artist - $title";
        };
      };
    };
  };
}