1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 # Using notmuch remotely #
6 It is hard to keep notmuch tags in sync across multiple instances of
7 notmuch, on multiple computers. Though you can do this with "notmuch
8 dump" and "notmuch restore", it is often preferable to be able to use
9 notmuch on a remote computer as if it were present on a local computer.
11 The following guidelines show how to accomplished this. It isn't
12 perfect, but it works pretty well, and allows one to access notmuch on
13 one computer, using only an Emacs client, a trivial shell script, and
14 some Emacs and ssh settings on another computer.
16 ## What you will need ##
18 You will need to have the following items in place:
20 1. a working notmuch and `sshd` on one computer (let's call that
23 2. a working notmuch emacs interface (of the same version as on the
24 server), `bash`, and `ssh` on another computer (let's call that computer
27 3. password-free login (public key authentication) from client to
28 server. [Here](http://www.debian-administration.org/articles/152) is a
29 good page on how to set it up (<del>3</del>).
31 4. a reasonably fast connection. (This isn't really *necessary*, but if
32 your connection is too slow, this won't be very pleasant to use, and
33 certainly won't seem transparent.)
35 (<del>3</del>) If you don't want / cannot use password-free login,
36 [[This|remoteusage/124]] page provides yet another alternative.
38 ## Configure `ssh` on the client computer ##
40 Add this to your `~/.ssh/config`:
46 ControlPath ~/.ssh/master-%h@%p:%r
48 IdentityFile ~/.ssh/example.com.id_rsa
50 Replace `example.com` with your server. Replace `remoteuser` with the
51 username on the server.
53 The `Control*` options keep the connection open in the background to not
54 require authentication every time. The `ControlPersist` option defines
55 the connection timeout. These aren't strictly necessary, but reduce
58 The `IdentityFile` option may not be necessary depending on how you set
59 up public key authentication. This assumes you set up a separate key;
60 set the filename accordingly.
62 Please refer to `ssh_config(5)` man page for details.
64 ## Set up a wrapper shell script on the client computer ##
66 Save this to a file, for example `remote-notmuch.sh`, in your `PATH`:
69 printf -v ARGS "%q " "$@"
70 exec ssh notmuch notmuch ${ARGS}
72 and give it execute permissions: `chmod +x remote-notmuch.sh`
74 Now you can run `remote-notmuch.sh new`, or other notmuch commands. You
75 can call the script anything you like. (You could also call it `notmuch`
76 or symlink `~/bin/notmuch` to it for transparent usage.)
78 ## Configure Emacs on the client computer ##
80 Add this to your `~/.emacs` to tell the Emacs client to use the wrapper
83 (setq notmuch-command "/path/to/your/remote-notmuch.sh")
85 If you use Fcc and Notmuch older than 0.23, you may want to do something like
86 this on the client, to Bcc mails to yourself:
88 (setq notmuch-fcc-dirs nil)
89 (add-hook 'message-header-setup-hook
90 (lambda () (insert (format "Bcc: %s <%s>\n"
92 (notmuch-user-primary-email))))))
94 Starting from 0.23, Fcc is also done through notmuch-command.
96 ## Additional Emacs remote-notmuch configuration ##
98 To prevent you from having to maintain your GPG private keys on the remote
99 server, you can add advice to `notmuch-show-view-raw-message` to enable epa
100 inline decryption from notmuch raw message views.
103 ;; enable gpg decryption in raw view
104 (defadvice notmuch-show-view-raw-message
105 (after notmuch-show-view-raw-message-after activate)
109 When using remote-notmuch in an environment that brings the ssh tunnel up and
110 down often (e.g. laptop suspends), it's helpful to have an Emacs process
111 sentinel in place that will monitor the process state of your remote-notmuch
115 (defvar my/ssh-tunnel-notmuch-proc nil)
117 (defun my/ssh-tunnel-notmuch ()
118 "Start and monitor ssh session for remote-notmuch."
119 (my/ssh-tunnel-with-proc
120 (proc "~/.ssh/config" "notmuch")
121 (set-process-sentinel
123 #'(lambda (proc string)
124 (when (buffer-live-p (process-buffer proc))
125 (kill-buffer (process-buffer proc)))
126 (when (yes-or-no-p "Restart notmuch control master? ")
127 (setq my/ssh-tunnel-notmuch-proc (my/ssh-tunnel-notmuch)))))
131 (before notmuch-before activate)
132 (unless (process-live-p my/ssh-tunnel-notmuch-proc)
133 (message "Starting notmuch control master")
134 (setq my/ssh-tunnel-notmuch-proc (my/ssh-tunnel-notmuch))))
141 (cl-defmacro my/ssh-tunnel-with-proc ((proc ssh-tunnel-config-path ssh-tunnel-config-name) &body body)
142 "Bind PROC with an ssh process for SSH-TUNNEL-CONFIG-NAME from SSH-TUNNEL-CONFIG-PATH for BODY.
146 (defun my/ssh-tunnel-start ()
147 \"returns active process or nil\"
148 (my/ssh-tunnel-with-proc (proc \"~/my-ssh-configs/someconfig.ssh\"
150 ;; BODY with process bound to proc
153 (let ((ssh-tunnel-process (gensym "ssh-tunnel-process")))
155 `(let ((,ssh-tunnel-process nil)
156 (ssh-tunnel-buffer-name (format "*%s*" ,ssh-tunnel-config-name))
157 (ssh-tunnel-config ,ssh-tunnel-config-name))
158 (if (not (process-live-p ,ssh-tunnel-process))
159 (let ((process (start-process
160 ,ssh-tunnel-config-name
161 (generate-new-buffer ssh-tunnel-buffer-name)
166 (format "%s" (expand-file-name ,ssh-tunnel-config-path))
167 ,ssh-tunnel-config-name)))
168 (if (process-live-p process)
170 (setq ,ssh-tunnel-process process)
173 #'(lambda (proc string)
174 (when (and (process-live-p proc)
175 (buffer-live-p (process-buffer proc)))
176 (if (string-match-p tramp-password-prompt-regexp string)
177 (process-send-string proc (concat (read-passwd string) "\n"))
178 (princ (format "%s" string)
179 (process-buffer proc))))))
180 (set-process-sentinel
182 #'(lambda (proc string)
183 (message "%s-sentinel: %s" ,ssh-tunnel-config-name string)))
184 (message "Started ssh config: %s" ,ssh-tunnel-config-name))
186 (message "Could not start ssh config: %s" ,ssh-tunnel-config-name)))
187 (message "%s already running" ,ssh-tunnel-config-name))
189 (let ((,proc ,ssh-tunnel-process))
196 Some things probably won't work perfectly, and there might be some
197 unexpected mismatches between normal usage and this sort of usage. If
198 you're using this approach and run into any problems, please feel free
199 to list them here. And, of course, if you improve on any of these
200 approaches, please do edit this page and let people know!
202 If you have issues, you may want to try the
203 [[old remote usage instructions|remoteusage/old]] or
204 [[yet another alternative|remoteusage/124]].