{
  self,
  pkgs,
  ...
}: let
  inherit (pkgs) writeShellScriptBin;
  inherit (builtins) toFile;

  backupSchedules = import "${self}/data/backupSchedules.nix";

  rcloneConfig = toFile "rclone.conf" ''
    [Music]
    type = webdav
    url = https://storage-webdav.owo.monster/MusicRO/
    vendor = other
  '';
in {
  environment.systemPackages = with pkgs; [
    rclone
    (writeShellScriptBin "rclone-music" ''
      rclone --config ${rcloneConfig} "$@"
    '')
  ];

  systemd.tmpfiles.rules = [
    "d /Music - mpd mpd"
  ];

  systemd.services.music-sync = {
    wantedBy = ["multi-user.target"];
    after = ["network.target"];
    partOf = ["mpd.service"];

    path = with pkgs; [bash rclone];

    script = ''
      set -e
      rclone --config ${rcloneConfig} sync Music: /Music
      chown -R mpd:mpd /Music
    '';
  };

  systemd.timers.music-sync = {
    wantedBy = ["timers.target"];
    partOf = ["music-sync.service"];
    timerConfig = backupSchedules.music;
  };

  systemd.services.mpd = {
    after = ["music-copy.service"];
    serviceConfig = {
      ReadOnlyPaths = "/Music";
    };
  };
}