The stock systemd user service file for emacs 29.1 looks something like this:
[Unit]Description=Emacs text editorDocumentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/[Service]Type=notifyExecStart=/usr/bin/emacs --fg-daemon# ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"# Emacs will exit with status 15 after having received SIGTERM, which# is the default "KillSignal" value systemd uses to stop services.SuccessExitStatus=15# The location of the SSH auth socket varies by distribution, and some# set it from PAM, so don't override by default.# Environment=SSH_AUTH_SOCK=%t/keyring/sshRestart=on-failure[Install]WantedBy=default.target
However, after my computer starts up and I login, and try to run emacsclient -c
, nothing happens and journalctl -u emacs -f --user
contains an error log:
Authorization required, but no authorization protocol specified
After a restart of emacs (systemctl --user restart emacs
), there is no problem in starting emacs, and the emacsclient window opens up as expected. However, after a reboot, the same problem happens - clearly having to restart emacs immediately up login defeats the purpose of setting it up as a systemd user service.
After various googling, I ended up on https://www.emacswiki.org/emacs/EmacsAsDaemon#h5o-19 (EDIT 1: this link is now dead, see bottom) which says that we need to ensure emacs is started up after Gnome by adding these lines:
[Unit]Requires=org.gnome.Shell@wayland.serviceAfter=org.gnome.Shell@wayland.service
The problem is that Ubuntu 22.04+ (currently running 23.10) does not have a org.gnome.Shell@wayland.service unit. I've tried various things like gdm.service
, user@%U.service
instead of gnome shell in the above section and nothing seems to work. What's the right way here?
I've also had a look at https://emacs.stackexchange.com/questions/74937/systemd-starts-emacs-daemon-without-xauthority-environment-variable-after-upgrad that mentions the same problem on gnome/x11/fedora with a slightly different solution, but that doesn't work for Gnome/Wayland/Ubuntu combination.
I've also asked this question in the Emacs StackExchange and have been redirected here.
EDIT 1:
Emacs Wiki has updated the instructions so that the link above is dead. Updated instructions are (as of this writing) at https://www.emacswiki.org/emacs/EmacsAsDaemon#h5o-1
However, these instructions do not work on Ubuntu 23.10 as well. I still get the same error log as above when starting emacsclient -c
after a reboot (yes I did a daemon-reload
too). Adding a drop-in with:
[Unit]After=graphical-session.target
did not help.