1 { pkgs, lib, hostName, ... }:
 
   4   # syncoid --create-bookmark --no-privilege-elevation --no-sync-snap --recvoptions '' --sendoptions raw --recursive oignon/home off2/julm/backup/oignon/home
 
   5   # zfs list -t snapshot -o name | grep ^oignon/home | while read -r snap; do zfs bookmark "$snap" "${snap//@/#}"; done
 
   6   # Take regular snapshots, and prune old ones
 
   9     extraArgs = [ "--verbose" ];
 
  11       "${hostName}/home" = {
 
  29       "off2/julm/backup/oignon" = {
 
  40   # Trigger backups when disks are plugged
 
  41   services.udev.extraRules = ''
 
  42     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"
 
  43     # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
 
  44     ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
 
  46   # Show what's happening to the user
 
  47   systemd.services."zfs-term@" = {
 
  48     description = "ZFS terminal for: %I";
 
  49     unitConfig.StopWhenUnneeded = false;
 
  50     environment.DISPLAY = ":0";
 
  51     environment.XAUTHORITY = "/home/julm/.Xauthority";
 
  52     after = [ "graphical.target" ];
 
  53     bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  57       ExecStart = pkgs.writeShellScript "zfs-force-import" ''
 
  60         ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
 
  61           -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
 
  62           -u zfs-force-import@$DESTPOOL \
 
  63           -u zfs-local-backup-home@$DESTPOOL"
 
  67   # Force zpool import, even if the disk has not been exported, or has been imported on another computer
 
  68   systemd.services."zfs-force-import@" = {
 
  69     description = "ZFS force import: %I";
 
  72       StartLimitInterval = 200;
 
  73       StopWhenUnneeded = true;
 
  75     wants = [ "zfs-term@%i.service" ];
 
  76     bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  77     path = lib.mkBefore [ "/run/booted-system/sw" ];
 
  80       RemainAfterExit = true;
 
  82       SyslogIdentifier = "zfs-force-import@%i";
 
  83       Restart = "on-failure";
 
  84       ExecStart = pkgs.writeShellScript "zfs-force-import" ''
 
  87         # Import the zpool, using stable paths
 
  88         zpool import -d /dev/disk/by-id/ || true
 
  89         zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
 
  90         zpool reopen "$DESTPOOL" ||
 
  91         zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
 
  92         zpool clear -nFX "$DESTPOOL"
 
  96   # Prune old snapshots on the backup and send new ones
 
  97   systemd.services."zfs-local-backup-home@" = {
 
  98     description = "ZFS backup home, on: %I";
 
  99     wants = [ "zfs-term@%i.service" ];
 
 100     after = [ "zfs-force-import@%i.service" ];
 
 101     requires = [ "zfs-force-import@%i.service" ];
 
 102     bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
 103     path = lib.mkBefore [ "/run/booted-system/sw" ];
 
 104     serviceConfig = rec {
 
 107       CacheDirectory = [ "zfs-usb-backup/%I" ];
 
 108       RuntimeDirectory = [ "zfs-usb-backup/%I" ];
 
 111       SyslogIdentifier = "zfs-local-backup-home@%i";
 
 112       ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
 
 115         if zpool status "$DESTPOOL"; then
 
 116           zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
 
 117           zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
 
 118           zpool scrub -p "$DESTPOOL" || true
 
 121       ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
 
 124         # sanoid is quite conservative:
 
 125         # by setting hourly=24, a snapshot must be >24 hours old
 
 126         # and there must been >24 total hourly snapshots,
 
 127         # or nothing is pruned.
 
 128         install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
 
 132           process_children_only=false
 
 134           [$DESTPOOL/${User}/backup/${hostName}/home]
 
 142         ${pkgs.sanoid}/bin/sanoid \
 
 143           --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
 
 144           --configdir /tmp/sanoid \
 
 146           --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
 
 149         for dataset in ${hostName}/home; do
 
 150           ${pkgs.sanoid}/bin/syncoid \
 
 152             --exclude "home/room" \
 
 154             --no-privilege-elevation \
 
 161             "$DESTPOOL"/${User}/backup/"$dataset"
 
 164       ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
 
 167         # Only if the zpool still exists to avoid uninterruptible hanging
 
 168         if zpool status -v "$DESTPOOL"; then
 
 169           # Scrub the zpool 1 minute (in the background)
 
 170           zpool scrub "$DESTPOOL"
 
 173         while zpool status -v "$DESTPOOL"; do
 
 174           zpool scrub -p "$DESTPOOL" || true
 
 176           # Export the zpool (to avoid a forced import later on)
 
 177           zpool export "$DESTPOOL" || true
 
 179         systemctl --no-block stop zfs-term@"$DESTPOOL"
 
 183   programs.bash.interactiveShellInit = ''
 
 184     mount-zfs-backup () {
 
 188       zpool status "$zpool" 2>/dev/null ||
 
 189       sudo zpool import -d /dev/disk/by-id/ "$zpool"
 
 190       trap "sudo zpool export $zpool" EXIT
 
 191       zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
 
 192       grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
 
 193       ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
 
 197   programs.bash.shellAliases = {
 
 198     mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";