]> git.cworth.org Git - notmuch-wiki/blob - remoteusage.mdwn
Merge branch 'master' of git://notmuchmail.org/git/notmuch-wiki
[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
15 ssh.
16
17 Note that this is all something of a hack, and future versions of
18 notmuch will likely make all of these steps much more
19 transparent. I'll note particularly which things should become
20 unneccessary with future version. At the moment though, this does
21 work, and might enable some of you to use notmuch away from your
22 primary computer.
23
24 ##What you will need##
25 You will need to have the following items in place:
26
27 1.  a working notmuch on one computer (let's call that computer
28 "server"). The notmuch should be at least version 0.2.
29 2.  a working notmuch emacs interface on another computer (let's call
30 that computer "client")
31 3.   password-free login (public key authentication) from client to
32 server. [Here](http://www.debian-administration.org/articles/152) is a
33 good page on how to set it up.
34 4.   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 ##Write a wrapper shell script##
39
40 /!\ Now that notmuch (>=0.5) allows for "raw" downloading of messages, a lot
41 of the hacks in the older script, posted in 2010, are no longer necessary.
42
43 Now we will need to write a simple shell script that does two things:
44
45 1.  replaces the call to the notmuch binary with a call to notmuch
46 over ssh.
47 2.  caches messages when the entire raw message is downloaded. This
48 avoids the need to constantly download large attachments over and
49 over. (NB: this just checks to see if a message with the same id
50 has already been cached. If you delete an attachment on the server,
51 that could lead to an out-of-date cache. It would probably make more
52 sense in the future to concatenate a hash of the message id and a hash 
53 of the message.)
54
55 Note that this shell script also pauses briefly after every message
56 entries. This is currently necessary so that the emacs process-filter
57 doesn't chop off messages. It's an obvious hack, and hopefully won't
58 be necessary in the furture.
59
60     #!/usr/bin/env bash
61     SSH_BIN="/path/to/ssh/on/client"
62     USER="user_name"
63     SSH_HOST="server_name"
64     NOTMUCH_REMOTE_BIN="/path/to/notmuch/on/server"
65     CACHE="${HOME}/.notmuch-cache.d"
66
67     hash_name ()
68     {
69         echo -n ${1} | sha1sum | awk '{print $1}'
70     }
71     
72     check_for_file_name ()
73     {
74         [ -f "${CACHE}/${1}" ]
75     }
76     
77     notmuch_run ()
78     {
79         [ -d "${CACHE}" ] || mkdir -p "${CACHE}"
80         CMD=$1
81         shift
82         # we need to a little sanitizing of msg ids so the shell
83         # doesn't mangle them
84         printf -v ARGS "%q " "$@"       
85         $SSH_BIN $USER@$SSH_HOST $NOTMUCH_REMOTE_BIN ${CMD} ${ARGS}
86     }
87     
88     notmuch_search ()
89     {
90         notmuch_run search $@ |
91         while read line; do
92             sleep 0.02   # Workaround a bug (missing lines) in the emacs interface
93                          # NOTE: This workaround is no longer necessary as of 
94                          # git rev eead2382. You can just run 
95                          # `notmuch_run search $@'                      
96             echo "${line}"
97         done
98     }
99     
100     
101     notmuch_show ()
102     {
103         if [ ${1} = "--format=raw" ]; then 
104             hashed=`hash_name ${2}`
105             check_for_file_name ${hashed} || 
106             notmuch_run show --format=raw ${2} > "${CACHE}/${hashed}"
107             cat "${CACHE}/${hashed}"
108         else 
109             notmuch_run show $@
110         fi
111     }
112     
113     
114     if [ ${1} = "search" ]; then
115         shift
116         notmuch_search $@
117     elif [ ${1} = "show" ]; then
118         shift
119         notmuch_show $@
120     else
121         notmuch_run $@
122     fi
123     
124         
125 Save this to a file, "remote-notmuch.sh", in your path.
126
127 Now you can run "remote-notmuch.sh new". You can call the script
128 anything you like. I actually have $HOME/bin/notmuch linked to that
129 script, so I can transparent
130 usage. (Since I run "new" from an emacs keybinding, I've never
131 bothered with this renaming.)
132
133 ##Configure your emacs client##
134
135 The only thing you need to do is tell your emacs client to use the
136 script. Add the following to your .emacs (this is on your client
137 machine):
138
139     (setq notmuch-command "/path/to/your/remote-notmuch.sh")
140
141
142 ##A tip to speed things up##
143 If you have openssh >= 0.4, you can make use of the "ControlMaster"
144 feature. This allows you to reuse an existing connection. Therefore
145 if you keep a connection open, you won't have to authenticate every
146 time.
147
148 Add the following to your ~/.ssh/config file:
149
150     Host server_name 
151     ControlMaster auto
152     ControlPath ~/.ssh/master-%r@%h:%p
153     
154 You can also se the Host to "*", if you want to use it for all
155 connections. I usually have an interactive ssh connection to my home
156 computer open, so I don't need to do anything more. But if not, you
157 can always run:
158
159     ssh -Nf server_name
160
161 which will open up a background connection, which you can then reuse
162 for all of your notmuch commands.
163
164 ##Problems##
165 Some things won't work perfectly, and there might be some unexpected
166 mismatches between normal usage and this sort of usage. If you're
167 using this approach and run into any problems, please feel free to
168 list them here. And, of course, if you improve on any of these
169 approaches, please do edit this page and let people know!