nixos/pkgs/scripts.nix

94 lines
2.9 KiB
Nix

{ pkgs, ... }:
{
tmux-sessions = pkgs.writeShellScriptBin "ts" ''
# use tmux provided through module configuration/global installation
# this is to ensure the right tmux version+configuration/plugins are loaded
if ! which tmux >/dev/null 2>&1; then
printf 'Unable to find tmux, make sure it is installed.'
exit 1;
fi
SOURCES=(
~/Projects/
/persist/etc/nixos/
);
if [[ $# -eq 1 ]]; then
selected="$(${pkgs.coreutils-full}/bin/realpath "$1")";
if [[ ! -d "''${selected}" ]]; then
printf 'Not a directory: %s\n' "''${selected}";
exit 1;
fi
else
selected="$(
${pkgs.findutils}/bin/find "''${SOURCES[@]}" \
-type d \
-name ".git" \
-mindepth 1 \
-maxdepth 5 \
-prune 2>/dev/null \
| ${pkgs.gnused}/bin/sed 's;/\.git$;;' \
| ${pkgs.fzf}/bin/fzf
)";
fi;
if [[ -z "''${selected}" ]]; then
exit 1;
fi;
selected_name="$(${pkgs.coreutils-full}/bin/basename "''${selected}" | ${pkgs.coreutils-full}/bin/tr . _)";
if ! tmux has-session -t="''${selected_name}" 2> /dev/null; then
tmux new-session -ds "''${selected_name}" -c "''${selected}";
fi;
if [[ -z ''${TMUX} ]] && [[ -z ''${tmux_running} ]]; then
tmux -u attach -t="''${selected_name}";
exit 0;
fi;
tmux switch-client -t "''${selected_name}";
exit 0;
'';
mail-sync = pkgs.writeShellScriptBin "mail-sync" ''
MBSYNC=$(${pkgs.procps}/bin/pgrep mbsync);
NOTMUCH=$(${pkgs.procps}/bin/pgrep notmuch);
if [ -f "''${HOME}/.config/pizauth.conf" ]; then
mapfile -t PIZAUTH_ACCOUNTS < <(${pkgs.gnugrep}/bin/grep -i account < "''${HOME}/.config/pizauth.conf" | ${pkgs.gnused}/bin/sed -E 's/^[^"]*"([^"]*).*$/\1/');
else
PIZAUTH_ACCOUNTS=();
fi
if ! which mbsync; then
printf 'Program mbsync not in path. Aborting!\n';
exit 1;
fi
# Load Pizauth if it isn't running
if [ ''${#PIZAUTH_ACCOUNTS[@]} -gt 0 ] && [ -z "$(${pkgs.sysvtools}/bin/pidof pizauth)" ]; then
${pkgs.pizauth}/bin/pizauth server;
${pkgs.libsecret}/bin/secret-tool lookup pizauth keys | ${pkgs.gnupg}/bin/gpg -ad | ${pkgs.pizauth}/bin/pizauth restore;
sleep 5;
fi
if [ -n "$MBSYNC" ] || [ -n "$NOTMUCH" ]; then
echo "Already running one instance of mbsync or notmuch. Exiting...";
exit 1;
fi;
printf 'Deleting messages tagged as *deleted*\n'
${pkgs.notmuch}/bin/notmuch search --format=text0 --output=files tag:deleted | ${pkgs.findutils}/bin/xargs -0 --no-run-if-empty rm -v;
if ! mbsync -a; then
for account in "''${PIZAUTH_ACCOUNTS[@]}"; do
printf 'Refreshing pizauth account "%s".\n' "''${account}";
${pkgs.pizauth}/bin/pizauth refresh "''${account}";
done;
exit 1;
fi;
${pkgs.notmuch}/bin/notmuch new;
exit $?;
'';
}