]> git.cworth.org Git - notmuch-wiki/blob - remoteusage.mdwn
Link to alternate remote wrapper script
[notmuch-wiki] / remoteusage.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 #Using notmuch remotely#
3
4 ##Why?##
5 It is hard to keep nomuch tags in sync across multiple instances of
6 notmuch, on multiple computers. Though you can do this with "notmuch
7 dump" and "notmuch restore", it is often preferable to be able to use
8 notmuch on a remote computer as if it were present on a local
9 computer.
10
11 The following guidelines show how I have accomplished this. It isn't
12 perfect, but it works pretty well, and allows me to access notmuch on
13 my home computer, using only an emacs client on my netbook or work
14 computer, a trivial shell script, a few settings in my .emacs, and a
15 couple of common unix utilities (ssh and dtach).
16
17 ##What you will need##
18 You will need to have the following items in place:
19
20 1.  a working notmuch on one computer (let's call that computer
21 "server").
22
23 2.  a working notmuch emacs interface on another computer (let's call
24 that computer "client")
25
26 3.  `ssh` and `dtach` on your client computer. (TODO: Make dtach
27 optional, or allow screen or tmux to be used instead.
28 [[Here|remotewrapper]] is a version that does not require dtach.)
29
30 4.  password-free login (public key authentication) from client to
31 server. [Here](http://www.debian-administration.org/articles/152) is a
32 good page on how to set it up.
33
34 5.  a reasonably fast connection. (This isn't really *neccessary*, but
35 if your connection is too slow, this won't be very pleasant to use,
36 and certainly won't seem transparent.)
37
38
39 ##Write a wrapper shell script##
40
41 Now we will need to write a simple shell script that does two things:
42
43 1.  replaces the call to the notmuch binary with a call to notmuch
44 over ssh.
45
46 2.  sets up a running, detached, ssh connection to the server, so that
47 future calls can reuse the socket.
48
49         #!/usr/bin/env bash
50
51         SSH_BIN="ssh"
52         USER="example_user"
53         SSH_HOST="example.com"
54         SOCKET_DIR="/tmp/notmuch_$(id -u)"
55         SSH_SOCKET="${SOCKET_DIR}/ssh.socket"
56         NOTMUCH_REMOTE_BIN="notmuch"
57         DTACH="/usr/bin/dtach"
58         DTACH_SOCKET="${SOCKET_DIR}/dtach.socket"
59         
60         check_for_socket_dir ()
61         {
62             [ -d "${SOCKET_DIR}" ]
63         }
64         
65         check_socket_dir_owner_and_perm ()
66         {
67             [ "$(stat -c %U ${SOCKET_DIR})" = "$(whoami)" ] &&
68             [ "$(stat -c %a ${SOCKET_DIR})" = "700" ]
69         }
70         
71         create_socket_dir ()
72         {
73             mkdir "${SOCKET_DIR}"
74             chmod 700 "${SOCKET_DIR}"
75         }
76         
77         check_for_socket ()
78         {
79             [ -S "${SSH_SOCKET}" ]
80         }
81         
82         start_socket ()
83         {
84             dtach_command="${DTACH} -n ${DTACH_SOCKET} ${SSH_BIN} -M -S ${SSH_SOCKET} ${USER}@${SSH_HOST}"
85             command -v ${DTACH} &>/dev/null && ${dtach_command}
86         }
87         
88         notmuch_run ()
89         {
90             if check_for_socket_dir; then
91                 if check_socket_dir_owner_and_perm; then
92                     if ! check_for_socket; then
93                         start_socket
94                     fi
95                 else echo "Wrong permissions of ${SOCKET_DIR}" >&2
96                     exit 1
97                 fi
98             elif create_socket_dir; then
99                 start_socket
100             else
101                 exit 1
102             fi
103             printf -v ARGS "%q " "$@"
104             $SSH_BIN -S $SSH_SOCKET $USER@$SSH_HOST $NOTMUCH_REMOTE_BIN ${ARGS}
105         }
106         
107         notmuch_run "$@"
108         
109 Save this to a file, "remote-notmuch.sh", in your path.
110
111 Now you can run "remote-notmuch.sh new". You can call the script
112 anything you like. I actually have $HOME/bin/notmuch linked to that
113 script, so I can have transparent
114 usage. 
115
116 ##Configure your emacs client##
117
118 The only thing you need to do is tell your emacs client to use the
119 script. Add the following to your .emacs (this is on your client
120 machine):
121
122     (setq notmuch-command "/path/to/your/remote-notmuch.sh")
123
124 ##Problems##
125 Some things probably won't work perfectly, and there might be some unexpected
126 mismatches between normal usage and this sort of usage. If you're
127 using this approach and run into any problems, please feel free to
128 list them here. And, of course, if you improve on any of these
129 approaches, please do edit this page and let people know!