]> git.cworth.org Git - notmuch-wiki/blob - remoteusage.mdwn
Cleaned up some markdown.
[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://sial.org/howto/openssh/publickey-auth/) 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         ARGS=`echo $@ | sed 's/[\\$\\*\\!]/\\\&/g'`
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
93             echo "${line}"
94         done
95     }
96     
97     
98     notmuch_show ()
99     {
100         if [ ${1} = "--format=raw" ]; then 
101             hashed=`hash_name ${2}`
102             check_for_file_name ${hashed} || 
103             notmuch_run show --format=raw ${2} > "${CACHE}/${hashed}"
104             cat "${CACHE}/${hashed}"
105         else 
106             notmuch_run show $@
107         fi
108     }
109     
110     
111     if [ ${1} = "search" ]; then
112         shift
113         notmuch_search $@
114     elif [ ${1} = "show" ]; then
115         shift
116         notmuch_show $@
117     else
118         notmuch_run $@
119     fi
120     
121         
122 Save this to a file, "remote-notmuch.sh", in your path.
123
124 Now you can run "remote-notmuch.sh new". You can call the script
125 anything you like. I actually have $HOME/bin/notmuch linked to that
126 script, so I can transparent
127 usage. (Since I run "new" from an emacs keybinding, I've never
128 bothered with this renaming.)
129
130 ##Configure your emacs client##
131
132 The only thing you need to do is tell your emacs client to use the
133 script. Add the following to your .emacs (this is on your client
134 machine):
135
136     (setq notmuch-command "/path/to/your/remote-notmuch.sh")
137
138
139 ##A tip to speed things up##
140 If you have openssh >= 0.4, you can make use of the "ControlMaster"
141 feature. This allows you to reuse an existing connection. Therefore
142 if you keep a connection open, you won't have to authenticate every
143 time.
144
145 Add the following to your ~/.ssh/config file:
146
147     Host server_name 
148     ControlMaster auto
149     ControlPath ~/.ssh/master-%r@%h:%p
150     
151 You can also se the Host to "*", if you want to use it for all
152 connections. I usually have an interactive ssh connection to my home
153 computer open, so I don't need to do anything more. But if not, you
154 can always run:
155
156     ssh -Nf server_name
157
158 which will open up a background connection, which you can then reuse
159 for all of your notmuch commands.
160
161 ##Problems##
162 Some things won't work perfectly, and there might be some unexpected
163 mismatches between normal usage and this sort of usage. If you're
164 using this approach and run into any problems, please feel free to
165 list them here. And, of course, if you improve on any of these
166 approaches, please do edit this page and let people know!