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