aboutsummaryrefslogtreecommitdiff
path: root/modules/home-manager/i3/rofi-passmenu.nix
blob: 006fcb34d0e90fc1ac866df563aed55d81f0e872 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
    perSystem =
        { pkgs, ... }:
        {
            packages.rofi-passmenu = pkgs.callPackage (
                {
                    pkgs
                }:

                pkgs.writeShellApplication {
                  name = "rofi-passmenu";

                  runtimeInputs = with pkgs; [
                    pass
                    rofi
                    xclip
                    xdotool
                    gnugrep
                    gawk
                    coreutils
                    libnotify
                  ];

                  text = ''
    #!/usr/bin/env bash

    store_dir="''${PASSWORD_STORE_DIR:-$HOME/.password-store}"

    get_passwords() {
        echo "[+] Add New Password"
        if [ -d "$store_dir" ]; then
            find "$store_dir" -name "*.gpg" | sort | while IFS= read -r file; do
                pass="''${file#"$store_dir"/}"
                echo "''${pass%.gpg}"
            done
        fi
    }

    selected=$(get_passwords | rofi -dmenu -i -p "pass")
    [[ -z "$selected" ]] && exit 0

    if [[ "$selected" == "[+] Add New Password" ]]; then
        new_path=$(rofi -dmenu -p "Enter path/name for new password:")
        [[ -z "$new_path" ]] && exit 0
        
        choice=$(echo -e "Generate\nManual" | rofi -dmenu -p "Password method:")
        if [[ "$choice" == "Generate" ]]; then
            pass generate "$new_path" 20
            notify-send "Pass" "Generated password for $new_path"
        elif [[ "$choice" == "Manual" ]]; then
            new_pass=$(rofi -dmenu -p "Enter password:" -password)
            [[ -n "$new_pass" ]] && echo "$new_pass" | pass insert -m "$new_path"
        fi
        exit 0
    fi

    action=$(echo -e "1. Type Password\n2. Type Username\n3. Copy Password\n4. Copy Username\n5. Show Password" | rofi -dmenu -i -p "$selected")

    case "$action" in
        "1. Type Password")
            pass show "$selected" | head -n 1 | tr -d '\n' | xdotool type --clearmodifiers -fi -
            ;;
        "2. Type Username")
            pass show "$selected" | grep -i "^username:" | sed 's/^[^:]*:[[:space:]]*//' | tr -d '\n' | xdotool type --clearmodifiers -fi -
            ;;
        "3. Copy Password")
            pass show -c "$selected"
            ;;
        "4. Copy Username")
            pass show "$selected" | grep -i "^username:" | sed 's/^[^:]*:[[:space:]]*//' | xclip -selection clipboard
            ;;
        "5. Show Password")
            pass show "$selected" | head -n 1 | rofi -dmenu -p "Password for $selected"
            ;;
    esac
  '';
                }
            ) { };
        };
}