~abcdw/rde

rde/rde/features/password-utils.scm -rw-r--r-- 6.6 KiB
e98d6a8eAndrew Tropin rde: gnupg: Add copyright preamble. 19 hours ago
                                                                                
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;;; rde --- Reproducible development environment.
;;;
;;; Copyright © 2021, 2022 Andrew Tropin <andrew@trop.in>
;;; Copyright © 2021 Demis Balbach <db@minikn.xyz>
;;;
;;; This file is part of rde.
;;;
;;; rde is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; rde is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with rde.  If not, see <http://www.gnu.org/licenses/>.

(define-module (rde features password-utils)
  #:use-module (rde features)
  #:use-module (rde features emacs)
  #:use-module (rde features predicates)
  #:use-module (gnu home-services password-utils)
  #:use-module (gnu home-services state)
  #:use-module (gnu packages emacs-xyz)
  #:use-module (gnu packages password-utils)
  #:use-module (gnu services)

  #:use-module (guix gexp)

  #:export (feature-password-store))


(define* (feature-password-store
          #:key
          (password-store password-store)
          (remote-password-store-url #f)
          (default-pass-prompt? #t))
  "Setup and configure password manager."
  ;; (ensure-pred maybe-url? remote-password-store-url)
  (ensure-pred file-like? password-store)
  (ensure-pred boolean? default-pass-prompt?)

  (define emacs-f-name 'pass)
  (define f-name (symbol-append 'emacs- emacs-f-name))

  (define (emacs-pass-prompt config)
    (require-value 'emacs config)
    (emacs-minibuffer-program
     (get-value 'emacs-client-create-frame config)
     "pass-prompt" "pass" 'rde-consult-pass
     #:height (get-value 'standalone-minibuffer-height config 10)))

  (define (password-store-home-services config)
    "Returns home services related to password-store."
    (require-value 'gpg-primary-key config)
    (require-value 'home-directory config)
    (list (service home-password-store-service-type
                   (home-password-store-configuration
                    (package password-store)))
          (simple-service
           'add-password-store-git-state
           home-state-service-type
           (list
            (state-git
            ;;; TODO: Rewrite it to xdg-state-home or rework states.
             (string-append
              (get-value 'home-directory config)
              "/.local/var/lib/password-store")
             remote-password-store-url)))

          (when (get-value 'emacs config)
            (emacs-xdg-service
             'pass
             "Emacs (Client) [pass]"
             (emacs-pass-prompt config)))

          (when (get-value 'emacs config)
            (let ((emacs-embark (get-value 'emacs-embark config))
                  (emacs-consult (get-value 'emacs-consult config)))
              (rde-elisp-configuration-service
               emacs-f-name
               config
               `((eval-when-compile (require 'pass))
                 ;; TODO: Fix tree view in pass.el
                 ,@(if (get-value 'emacs-advanced-user? config)
                       '((setq pass-show-keybindings nil))
                       '())
                 (add-hook 'pass-mode-hook (lambda () (setq truncate-lines t)))

                 (require 'configure-rde-keymaps)
                 (define-key rde-app-map (kbd "p") 'pass)

                 (with-eval-after-load
                  'auth-source
                  (require 'auth-source-pass)
                  (add-to-list 'auth-sources 'password-store))

                 ;; Source:
                 ;; https://github.com/hlissner/doom-emacs/blob/develop/modules/tools/pass/autoload/consult.el
                 ,@(if emacs-consult
                       `((autoload 'password-store-dir "password-store")
                         (autoload 'password-store-list "password-store")
                         (autoload 'consult--read "consult")

                         (defun rde-consult-pass (arg pass)
                           "Interactively search the password store."
                           (interactive
                            (list current-prefix-arg
                                  (consult--read (password-store-list)
                                                 :prompt "Pass entry: "
                                                 :sort nil
                                                 :require-match nil
                                                 :category 'pass)))
                           (funcall (if arg
                                        'password-store-url
                                        'password-store-copy)
                                    pass))

                         (define-key global-map (kbd "M-g p") 'rde-consult-pass))
                       '())

                 ,@(if emacs-embark
                       `((eval-when-compile
                          (require 'embark))

                         (with-eval-after-load
                          'embark
                          (require 'password-store)
                          (embark-define-keymap
                           embark-pass-actions
                           "Keymap for actions for pass entries."
                           ("f" password-store-copy-field)
                           ("b" password-store-url)
                           ("e" password-store-edit)
                           ("g" password-store-generate)
                           ("r" password-store-rename)
                           ("d" password-store-remove))

                          (add-to-list 'embark-keymap-alist
                                       '(pass . embark-pass-actions))))
                       '()))
               #:summary "\
Password store emacs interfaces"
               #:commentary "\
Keybinding for `rde-consult-pass' and embark actions for it."
               #:keywords '(convenience)
               #:elisp-packages
               (append
                (list emacs-pass emacs-password-store emacs-password-store-otp
                      (get-value 'emacs-configure-rde-keymaps config))
                (if emacs-embark (list emacs-embark) '())
                (if emacs-consult (list emacs-consult) '())))))))

  (feature
   (name 'password-store)
   (values `((pass . #t)
             (password-store . ,password-store)
             ,@(if default-pass-prompt?
                   `((default-pass-prompt-fn . ,emacs-pass-prompt))
                   '())))
   (home-services-getter password-store-home-services)))