{ pkgs, lib, config, 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 = ''
  ACTION=="add",    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", ENV{SYSTEMD_WANTS}+="zfs-local-backup-home@WD10JPVT.service", ENV{SYSTEMD_ALIAS}="/sys/subsystem/usb/WD10JPVT"
  # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
  ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"

  ACTION=="add",    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", ENV{SYSTEMD_USER_WANTS}+="rsync-backup@GE_SP.service", ENV{SYSTEMD_ALIAS}="/sys/subsystem/usb/GE_SP"
  ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", TAG+="systemd"
'';
programs.bash.shellAliases = {
  mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
};
}