{ pkgs, hostName, ... }:
with builtins;
{
  imports = [
    backup/zfs-backup.nix
    backup/rsync-backup.nix
  ];
  # Take regular snapshots, and prune old ones
  services.sanoid = {
    enable = true;
    extraArgs = [ "--verbose" ];
    datasets = {
      "${hostName}/home" = {
        autosnap = true;
        autoprune = true;
        hourly = 24;
        daily = 3;
        monthly = 0;
        yearly = 0;
        recursive = true;
      };
      "${hostName}/home/Documents" = {
        autosnap = true;
        autoprune = true;
        hourly = 24;
        daily = 31;
        monthly = 0;
        yearly = 0;
        recursive = true;
      };
      "${hostName}/home/Downloads" = {
        autosnap = false;
        autoprune = false;
      };
      "${hostName}/home/Videos" = {
        autosnap = false;
        autoprune = false;
      };
    };
  };
  # Trigger backups when disks are plugged
  services.udev.extraRules = ''
    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", \
      ACTION=="add", ENV{SYSTEMD_WANTS}+="zfs-local-backup-home@WD10JPVT.service"
    # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", \
      ACTION=="remove", RUN+="${pkgs.systemd}/bin/systemctl stop --no-block zfs-local-backup-home@WD10JPVT.service"

    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", \
      ACTION=="add", ENV{SYSTEMD_USER_WANTS}+="rsync-backup@GE_SP.service"
    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", \
      ACTION=="remove", RUN+="${pkgs.systemd}/bin/systemctl stop --no-block zfs-import@off3.service"
  '';
  programs.bash.shellAliases = {
    mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
  };
}