9   # Show what's happening to the user
 
  10   systemd.services."zfs-term@" = {
 
  11     description = "ZFS terminal for: %I";
 
  12     unitConfig.StopWhenUnneeded = false;
 
  13     environment.DISPLAY = ":0";
 
  14     environment.XAUTHORITY = "/home/sevy/.Xauthority";
 
  15     after = [ "graphical.target" ];
 
  20         pkgs.writeShellScript "zfs-term" ''
 
  23           ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
 
  24             -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
 
  25             -u zfs-import@$DESTPOOL \
 
  26             -u zfs-local-backup-home@$DESTPOOL"
 
  31   # Prune old snapshots on the backup and send new ones
 
  32   systemd.services."zfs-local-backup-home@" = {
 
  33     description = "ZFS backup home, on: %I";
 
  34     wants = [ "zfs-term@%i.service" ];
 
  35     after = [ "zfs-import@%i.service" ];
 
  36     requires = [ "zfs-import@%i.service" ];
 
  37     path = lib.mkBefore [ "/run/booted-system/sw" ];
 
  41       CacheDirectory = [ "zfs-usb-backup/%I" ];
 
  42       RuntimeDirectory = [ "zfs-usb-backup/%I" ];
 
  45       SyslogIdentifier = "zfs-local-backup-home@%i";
 
  48         + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
 
  51           if zpool status "$DESTPOOL"; then
 
  52             zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
 
  53             zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
 
  54             zpool scrub -p "$DESTPOOL" || true
 
  59         pkgs.writeShellScript "zfs-local-backup-home" ''
 
  62           # sanoid is quite conservative:
 
  63           # by setting hourly=24, a snapshot must be >24 hours old
 
  64           # and there must been >24 total hourly snapshots,
 
  65           # or nothing is pruned.
 
  66           install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
 
  70             process_children_only=false
 
  72             [$DESTPOOL/${User}/backup/${hostName}/home]
 
  80           ${pkgs.sanoid}/bin/sanoid \
 
  81             --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
 
  82             --configdir /tmp/sanoid \
 
  84             --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
 
  87           for dataset in ${hostName}/home; do
 
  88             ${pkgs.sanoid}/bin/syncoid \
 
  90               --exclude "home/Downloads" \
 
  91               --exclude "home/Videos" \
 
  93               --no-privilege-elevation \
 
 100               "$DESTPOOL"/${User}/backup/"$dataset"
 
 106         + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
 
 109           # Only if the zpool still exists to avoid uninterruptible hanging
 
 110           if zpool status -v "$DESTPOOL"; then
 
 111             # Scrub the zpool 1 minute (in the background)
 
 112             zpool scrub "$DESTPOOL"
 
 115           while zpool status -v "$DESTPOOL"; do
 
 116             zpool scrub -p "$DESTPOOL" || true
 
 118             # Export the zpool (to avoid a forced import later on)
 
 119             zpool export "$DESTPOOL" || true
 
 121           systemctl --no-block stop zfs-term@"$DESTPOOL"
 
 126   programs.bash.interactiveShellInit = ''
 
 127     mount-zfs-backup () {
 
 131       zpool status "$zpool" 2>/dev/null ||
 
 132       sudo zpool import -d /dev/disk/by-id/ "$zpool"
 
 133       trap "sudo zpool export $zpool" EXIT
 
 134       zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
 
 135       grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
 
 136       ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup