1 { pkgs, lib, config, 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" = {
 
  20     "${hostName}/home/room" = {
 
  35 # Trigger backups when disks are plugged
 
  36 services.udev.extraRules = ''
 
  37   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"
 
  38   # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
 
  39   ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
 
  41 # Show what's happening to the user
 
  42 systemd.services."zfs-term@" = {
 
  43   description = "ZFS terminal for: %I";
 
  44   unitConfig.StopWhenUnneeded = false;
 
  45   environment.DISPLAY = ":0";
 
  46   environment.XAUTHORITY = "/home/julm/.Xauthority";
 
  47   after = [ "graphical.target" ];
 
  48   bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  52     ExecStart = pkgs.writeShellScript "zfs-force-import" ''
 
  55       ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
 
  56         -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
 
  57         -u zfs-force-import@$DESTPOOL \
 
  58         -u zfs-local-backup-home@$DESTPOOL"
 
  62 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
 
  63 systemd.services."zfs-force-import@" = {
 
  64   description = "ZFS force import: %I";
 
  67     StartLimitInterval = 200;
 
  68     StopWhenUnneeded = true;
 
  70   wants = [ "zfs-term@%i.service" ];
 
  71   bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  72   path = lib.mkBefore [ "/run/booted-system/sw" ];
 
  75     RemainAfterExit = true;
 
  77     SyslogIdentifier = "zfs-force-import@%i";
 
  78     Restart = "on-failure";
 
  79     ExecStart = pkgs.writeShellScript "zfs-force-import" ''
 
  82       # Import the zpool, using stable paths
 
  83       zpool import -d /dev/disk/by-id/ || true
 
  84       zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
 
  85       zpool reopen "$DESTPOOL" ||
 
  86       zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
 
  87       zpool clear -nFX "$DESTPOOL"
 
  91 # Prune old snapshots on the backup and send new ones
 
  92 systemd.services."zfs-local-backup-home@" = {
 
  93   description = "ZFS backup home, on: %I";
 
  94   wants = [ "zfs-term@%i.service" ];
 
  95   after = [ "zfs-force-import@%i.service" ];
 
  96   requires = [ "zfs-force-import@%i.service" ];
 
  97   bindsTo = [ "sys-subsystem-usb-%i.device" ];
 
  98   path = lib.mkBefore [ "/run/booted-system/sw" ];
 
 102     CacheDirectory = [ "zfs-usb-backup/%I" ];
 
 103     RuntimeDirectory = [ "zfs-usb-backup/%I" ];
 
 106     SyslogIdentifier = "zfs-local-backup-home@%i";
 
 107     ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
 
 110       if zpool status "$DESTPOOL"; then
 
 111         zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
 
 112         zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
 
 113         zpool scrub -p "$DESTPOOL" || true
 
 116     ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
 
 119       # sanoid is quite conservative:
 
 120       # by setting hourly=24, a snapshot must be >24 hours old
 
 121       # and there must been >24 total hourly snapshots,
 
 122       # or nothing is pruned.
 
 123       install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
 
 127         process_children_only=false
 
 129         [$DESTPOOL/${User}/backup/${hostName}/home]
 
 137       ${pkgs.sanoid}/bin/sanoid \
 
 138         --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
 
 139         --configdir /tmp/sanoid \
 
 141         --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
 
 144       for dataset in ${hostName}/home; do
 
 145         ${pkgs.sanoid}/bin/syncoid \
 
 147           --exclude "home/room" \
 
 149           --no-privilege-elevation \
 
 156           "$DESTPOOL"/${User}/backup/"$dataset"
 
 159     ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
 
 162       # Only if the zpool still exists to avoid uninterruptible hanging
 
 163       if zpool status -v "$DESTPOOL"; then
 
 164         # Scrub the zpool 1 minute (in the background)
 
 165         zpool scrub "$DESTPOOL"
 
 168       while zpool status -v "$DESTPOOL"; do
 
 169         zpool scrub -p "$DESTPOOL" || true
 
 171         # Export the zpool (to avoid a forced import later on)
 
 172         zpool export "$DESTPOOL" || true
 
 174       systemctl --no-block stop zfs-term@"$DESTPOOL"
 
 178 programs.bash.interactiveShellInit = ''
 
 179   mount-zfs-backup () {
 
 183     zpool status "$zpool" 2>/dev/null ||
 
 184     sudo zpool import -d /dev/disk/by-id/ "$zpool"
 
 185     trap "sudo zpool export $zpool" EXIT
 
 186     zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
 
 187     grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
 
 188     ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
 
 192 programs.bash.shellAliases = {
 
 193   mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";