4

Problem

I am running Emacs daemon as a user service using Systemd, following the documentation in EmacsWiki. I configured it while using Fedora-36, and everything was working as expected. Once I upgraded to Fedora-37, emacsclient -c started failing with error *ERROR*: Display :0 can't be opened; the Emacs daemon also added Authorization required, but no authorization protocol specified to the systemctl --user status emacs.service output.

Temporary solution.

First I figured out that simply restarting the service using systemctl --user restart emacs.service fixed the problem until I reboot the machine again. But having to restart the service manually every time I start the machine defeated the point of starting it automatically using Systemd.

Debugging attempt.

After some research on the topic, I figured out that the emacs daemon is not getting the XAUTHORITY environment variable when started automatically. Running ps -C emacs eww after a reboot, I can see that the following 14 environment variables are set.

HOME, LANG, LOGNAME, PATH, SHELL, SYSTEMD_EXEC_PID, USER, 
XDG_DATA_DIRS, XDG_RUNTIME_DIR, DBUS_SESSION_BUS_ADDRESS, 
NOTIFY_SOCKET, MANAGERPID, INVOCATION_ID, JOURNAL_STREAM

After restarting the service, I see 43 environment variables. Amount the 29 new variables, there is DISPLAY, GNOME_SETUP_DISPLAY, WAYLAND_DISPLAY, and XAUTHORITY.

So one possibility is that Systemd is starting user services before these variables are available, which causes the problem. If this is the case, how can I fix it?

But I also found that I could just create a soft link named .Xauthority file in my home directory, which points to the actual one, and Emacs seems to work without these environment variables. So maybe these environment variables are not needed and what changed from fedora-36 to fedora-37 is not having the .Xauthority file in the home directory any more. If this is the case, how should I fix this?

1 Answer 1

2

I recently ran into this issue after switching from Plasma X11 to Plasma Wayland on my F37 install. The consensus I've seen from searching the web seems to be what you've found: emacs.service is being started before the graphical environment and DISPLAY variables are available. I didn't have the Authorization required message you had though.

I started with moving the emacs.service file from ~/.config/systemd/user/default.target.wants to ~/.config/systemd/user/graphical-session.target.wants, but that didn't change anything.

Taking a hint from How can I start systemd service units in order?, I copied /usr/lib/systemd/user/emacs.service to ~/.config/systemd/user, linked it into the graphical-session.target.wants folder and added the following lines to the [Unit] section

Requires=plasma-kwin_wayland.service
After=plasma-kwin_wayland.service

After rebooting and logging back in, I verified emacs.service was running, and was able to start up emacsclient as usual.

1
  • 1
    I did something similar. I copied /usr/lib/systemd/user/emacs.service to ~/.config/systemd/user/, added your Requires= and After= lines under the [Unit] section, and changed the WantedBy= target under [Install] to plasma-core.target. I then ran systemctl --user enable emacs to automatically create the appropriate symlink.
    – boltronics
    Sep 12, 2023 at 2:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .