diff --git a/pkgs/nixpkgs.nix b/pkgs/nixpkgs.nix index 412a3c7..4dd312d 100644 --- a/pkgs/nixpkgs.nix +++ b/pkgs/nixpkgs.nix @@ -22,7 +22,7 @@ in theme_sddm_midnight = callPackage ./sddm_midnight { }; xdiskusage = callPackage ./xdiskusage { }; kanagawa = callPackage ./kanagawa { }; - }; + } // import ./scripts.nix { inherit pkgs; }; }; overlays = [ (final: prev: { diff --git a/pkgs/scripts.nix b/pkgs/scripts.nix new file mode 100644 index 0000000..d2d7351 --- /dev/null +++ b/pkgs/scripts.nix @@ -0,0 +1,94 @@ +{ 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 $?; + ''; +} diff --git a/pkgsets/mail_utils.nix b/pkgsets/mail_utils.nix index cf418e0..e7c2f9e 100644 --- a/pkgsets/mail_utils.nix +++ b/pkgsets/mail_utils.nix @@ -12,5 +12,7 @@ (isync.override { withCyrusSaslXoauth2 = true; }) mailutils pandoc # to convert messages + # defined in ../pkgs/scripts.nix + mail-sync ]; } diff --git a/services/tmux.nix b/services/tmux.nix index 73e0408..ecde932 100644 --- a/services/tmux.nix +++ b/services/tmux.nix @@ -77,4 +77,8 @@ lib.mkIf (lib.elem "tmux" config.machine.services) { set -g set-titles-string "tmux: #S / #W" ''; }; + environment.systemPackages = with pkgs; [ + # defined in ../pkgs/scripts.nix + tmux-sessions + ]; }