Emacs systemd service

Since upgrading to Ubuntu 15.04, I’ve been living with systemd. I’ve been a Debian user mainly and was wary of the doom-and-gloom comments about systemd but actually, I’ve been a bit impressed with it so far. One thing that is undeniably easier with systemd is writing what System V init sytems called init scripts. In systemd they are known as service files.

Anyway, I usually run emacs as daemon and I had the idea that emacs could run as a systemd service! Immediately I fell into the trap that Umberto Eco describes:

there are two laws no human being can escape: the first idea that comes into a person’s mind will be the most obvious one; and, having had an obvious idea, nobody ever thinks that others may have had the same idea before.

So yes, there plenty of systmed emacs service files out there, but this one is mine.

[Unit]
Description=Emacs daemon
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(progn (setq kill-emacs-hook 'nil) (kill-emacs))"
Restart=always
Environment=SSH_AUTH_SOCK=/run/user/1000/keyring/ssh GPG_AGENT_INFO=/run/user/1000/keyring/gpg:0:1
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

The main difference from my config is that I’ve added the GPG_AGENT_INFO data so that emacs can uses GPG agent. I run keychain so my ssh-agent and gpg-agent are launched at login. There are probably nicer ways to pass the environment variables. This only works if you user ID is 1000, which it most likely will be if you are the first user. Just check the output of id -u and change the 1000 to whatever it says and it will probably work.

Lastly, I run this service as a user so I put the file in ~/.config/systemd/user/emacs.service and then did:

systemctl --user enable emacs.service   
systemctl --user start emacs.service

I use emacsclient to connect to the server, which I’ve aliased to just e, so I’ll type e -nc to open a new instance for example.