1 { pkgs, lib, hostName, ... }:
 
   4   # Show what's happening to the user
 
   5   systemd.services."zfs-term@" = {
 
   6     description = "ZFS terminal for: %I";
 
   7     unitConfig.StopWhenUnneeded = false;
 
   8     environment.DISPLAY = ":0";
 
   9     environment.XAUTHORITY = "/home/sevy/.Xauthority";
 
  10     after = [ "graphical.target" ];
 
  14       ExecStart = pkgs.writeShellScript "zfs-term" ''
 
  17         ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
 
  18           -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
 
  19           -u zfs-force-import@$DESTPOOL \
 
  20           -u zfs-local-backup-home@$DESTPOOL"
 
  24   # Force zpool import, even if the disk has not been exported, or has been imported on another computer
 
  25   systemd.services."zfs-force-import@" = {
 
  26     description = "ZFS force import: %I";
 
  29       StartLimitInterval = 200;
 
  30       StopWhenUnneeded = true;
 
  32     wants = [ "zfs-term@%i.service" ];
 
  33     bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  34     path = lib.mkBefore [ "/run/booted-system/sw" ];
 
  37       RemainAfterExit = true;
 
  39       SyslogIdentifier = "zfs-force-import@%i";
 
  40       Restart = "on-failure";
 
  41       ExecStart = pkgs.writeShellScript "zfs-force-import" ''
 
  44         # Import the zpool, using stable paths
 
  45         zpool import -d /dev/disk/by-id/ || true
 
  46         zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
 
  47         zpool reopen "$DESTPOOL" ||
 
  48         zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
 
  49         zpool clear -nFX "$DESTPOOL"
 
  53   # Prune old snapshots on the backup and send new ones
 
  54   systemd.services."zfs-local-backup-home@" = {
 
  55     description = "ZFS backup home, on: %I";
 
  56     wants = [ "zfs-term@%i.service" ];
 
  57     after = [ "zfs-force-import@%i.service" ];
 
  58     requires = [ "zfs-force-import@%i.service" ];
 
  59     bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  60     path = lib.mkBefore [ "/run/booted-system/sw" ];
 
  64       CacheDirectory = [ "zfs-usb-backup/%I" ];
 
  65       RuntimeDirectory = [ "zfs-usb-backup/%I" ];
 
  68       SyslogIdentifier = "zfs-local-backup-home@%i";
 
  69       ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
 
  72         if zpool status "$DESTPOOL"; then
 
  73           zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
 
  74           zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
 
  75           zpool scrub -p "$DESTPOOL" || true
 
  78       ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
 
  81         # sanoid is quite conservative:
 
  82         # by setting hourly=24, a snapshot must be >24 hours old
 
  83         # and there must been >24 total hourly snapshots,
 
  84         # or nothing is pruned.
 
  85         install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
 
  89           process_children_only=false
 
  91           [$DESTPOOL/${User}/backup/${hostName}/home]
 
  99         ${pkgs.sanoid}/bin/sanoid \
 
 100           --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
 
 101           --configdir /tmp/sanoid \
 
 103           --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
 
 106         for dataset in ${hostName}/home; do
 
 107           ${pkgs.sanoid}/bin/syncoid \
 
 109             --exclude "home/Downloads" \
 
 110             --exclude "home/Videos" \
 
 112             --no-privilege-elevation \
 
 119             "$DESTPOOL"/${User}/backup/"$dataset"
 
 122       ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
 
 125         # Only if the zpool still exists to avoid uninterruptible hanging
 
 126         if zpool status -v "$DESTPOOL"; then
 
 127           # Scrub the zpool 1 minute (in the background)
 
 128           zpool scrub "$DESTPOOL"
 
 131         while zpool status -v "$DESTPOOL"; do
 
 132           zpool scrub -p "$DESTPOOL" || true
 
 134           # Export the zpool (to avoid a forced import later on)
 
 135           zpool export "$DESTPOOL" || true
 
 137         systemctl --no-block stop zfs-term@"$DESTPOOL"
 
 141   programs.bash.interactiveShellInit = ''
 
 142     mount-zfs-backup () {
 
 146       zpool status "$zpool" 2>/dev/null ||
 
 147       sudo zpool import -d /dev/disk/by-id/ "$zpool"
 
 148       trap "sudo zpool export $zpool" EXIT
 
 149       zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
 
 150       grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
 
 151       ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup