#973432 emacs: When run as a systemd user service, emacs does not pick up environment settings from the desktop

Package:
emacs
Source:
emacs
Submitter:
Zack Weinberg
Date:
2025-12-17 14:47:02 UTC
Severity:
normal
Tags:
#973432#5
Date:
2020-10-30 13:43:09 UTC
From:
To:
I’ve been experimenting with running emacs as a systemd “user
service”, which is an upstream-supported configuration (see
/usr/lib/systemd/user/emacs.service).  I use GNOME for my desktop.

At about the same time that emacs 27.1 landed in unstable, I noticed
emacs start prompting me to unlock my ssh key via the mini-buffer
instead of via the desktop-wide password prompt.  It turns out that
this is because emacs is no longer aware of the desktop-wide ssh
agent, because it’s not picking up all the environment variable
settings that it ought to.  Specifically, these environment variables
are set for a random GUI application invoked from the GNOME launcher,
but not for emacs running as a user service:

DESKTOP_SESSION=gnome
GDMSESSION=gnome
GDM_LANG=en_US.UTF-8
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_SETUP_DISPLAY=:1
IM_CONFIG_PHASE=1
QT_IM_MODULE=ibus
SESSION_MANAGER=local/moxana:@/tmp/.ICE-unix/2195,unix/moxana:/tmp/.ICE-unix/2195
SSH_AGENT_LAUNCHER=openssh
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
WAYLAND_DISPLAY=wayland-0
XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.UOX3S0
XDG_CURRENT_DESKTOP=GNOME
XDG_MENU_PREFIX=gnome-
XDG_SESSION_CLASS=user
XDG_SESSION_DESKTOP=gnome
XDG_SESSION_TYPE=wayland
XMODIFIERS=@im=ibus

SSH_AUTH_SOCK is the one causing my immediate problem, but several
others look like they could also be trouble for Emacs users, such
as everything to do with input methods.

zw

#973432#10
Date:
2020-10-30 13:59:49 UTC
From:
To:
All of the missing environment variables are present in the output of
`systemctl --user show-environment` run from a gnome-terminal window,
so the problem *might* just be that emacs.service is getting started
too early, in which case it could probably be fixed with some After=
directives, but I don't know what those should be.

zw

#973432#17
Date:
2025-08-27 12:03:32 UTC
From:
To:
 >

I had the exactly same problem. Most prominently
(getenv "PATH") gave me the root path (including the sbin stuff).

As this not only happens for the emacs service but in general I reported
this as https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1104693

I don't understand how systemd user startup works and how environment
variables coming from .profile etc get injected (should they at all?),
but i managed to get emacs.service working correctly with the following

=== .config/systemd/user/emacs.service.d/override.conf ===
[Service]
Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=on-failure

[Unit]
After=gnome-terminal-server
==========================================================

The “After=gnome-terminal-server” did the trick for me.

I hope this could help you (and others) too (there may be a better
service to rely on than gnome-terminal-server, I am not sure about this).

Greetings,

Klaus

#973432#22
Date:
2025-08-27 17:14:11 UTC
From:
To:
after posting this). I played around with Gnome on X11 vs. Gnome on
Wayland and the given file was not enough.

Especially when running under Wayland it had problems with Xauthority.

I ended up with creating a new file
(by copying /usr/lib/systemd/user/emacs.service)
as follows:

=== .config/systemd/user/graphical-emacs.service ===
[Unit]
Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

[Service]
Type=notify
ExecStart=/usr/bin/emacs --fg-daemon

# 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/ssh
Restart=on-failure

[Install]
WantedBy=graphical-session.target

[Unit]
After=gnome-session-x11-services.target
=====================================================

I am not sure this could have been done with overrides, I just did not
manage to get “systemctl --user enable graphical-emacs” to create the
correct link.

Don't forget to
systemctl --user disable emacs
systemctl --user enable graphical-emacs
systemctl --user stop emacs
systemctl --user start graphical-emacs

I hope this works for you and others.
If it does (and does not have other drawbacks I don't understand with my
limited knowledge of systemd) providing a patched version of this file
in future releases could be helpful.

The EmacsWiki already knows about this:
<https://www.emacswiki.org/emacs/EmacsAsDaemon> (I learned from this)

I basically stole the above from here:
<https://emacs.stackexchange.com/questions/74937/systemd-starts-emacs-daemon-without-xauthority-environment-variable-after-upgrad>.

This also was somehow helpful:
<https://askubuntu.com/questions/1499139/how-to-run-emacs-daemon-as-a-systemd-service-with-wayland-on-ubuntu-22-04>.

Klaus

#973432#27
Date:
2025-12-17 14:35:55 UTC
From:
To:
It's nice the emacs-common includes an emacs.service file for systemd. Two
in fact,

/usr/share/emacs/30.2/etc/emacs.service
/usr/lib/systemd/user/emacs.service

The second of those is actually used when you go

$ systemctl --user start emacs.service

It works okay, except that if you enable it and have "lingering" set,
you'll get a no-graphics server even if you log into a desktop. Or if you
ssh in and then log into a desktop, the desktop will see the non-graphical
server. (Why is it possible to have a non-graphical server when the server
doesn't actually do any graphics, and can connect to multiple displays
using the env info passed to emacsclient? I dunno but apparently that's
really hard to fix.)

In any case, according to https://www.emacswiki.org/emacs/EmacsAsDaemon
that emacs.service file should include

[Unit]
After=graphical-session.target

This would make the server more likely to start in graphical mode.