{ pkgs, lib, config, osConfig, ... }:
let
  fs-diff = with pkgs;
    writeShellApplication {
      name = "fs-diff";
      text = ''
        #!/usr/bin/env bash
        # fs-diff.sh
        set -euo pipefail

        sudo mkdir -p /btrfs_tmp
        sudo mount -o subvol=/ /dev/mapper/crypted /btrfs_tmp

        OLD_TRANSID=$(sudo btrfs subvolume find-new /btrfs_tmp/root-blank 9999999)
        OLD_TRANSID=''${OLD_TRANSID#transid marker was }

        sudo btrfs subvolume find-new "/btrfs_tmp/root" "$OLD_TRANSID" |
        sed '$d' |
        cut -f17- -d' ' |
        sort |
        uniq |
        while read -r path; do
          path="/$path"
          if [ -L "$path" ]; then
            : # The path is a symbolic link, so is probably handled by NixOS already
          elif [ -d "$path" ]; then
            : # The path is a directory, ignore
          else
            echo "$path"
          fi
        done
        sudo umount /btrfs_tmp
      '';
    };
in with lib; {
  config = mkIf osConfig.mods.impermanence.enable {
    home.packages = [ fs-diff ];
    # home.persistence."/persist/home/muon" = {
    #   directories = [
    #     "documents"
    #     "downloads"
    #     "src"
    #     ".gnupg"
    #     ".ssh"
    #     ".config/sops"
    #     ".local/share/direnv"
    #   ];
    # };
  };
}
