Skip to content

Instantly share code, notes, and snippets.

@chaorace
Created October 20, 2023 19:19
Show Gist options
  • Save chaorace/5b1ef9df454600defbe4947a80b6c601 to your computer and use it in GitHub Desktop.
Save chaorace/5b1ef9df454600defbe4947a80b6c601 to your computer and use it in GitHub Desktop.
Sync systemd/D-Bus environment variable updates w/ Emacs daemon
;; Ensures that any change pushed to the systemd environment (e.g.: via dbus-update-activation-environment) immediately applies to the Emacs daemon process without needing to kill it first
;; This is useful for example if you run Emacs as a systemd user service and it happens to start before your WM, since this normally causes DISPLAY / WAYLAND_DISPLAY to be missing
;; Ensuring the variable is synced will fix issues like GUI apps not launching when invoked by emacs (e.g.: via xdg-open/shell mode)
;; Obviously, this code depends on D-Bus!
(dbus-register-monitor
:session
(lambda (&rest args)
(unless (null args)
(mapc
(lambda (var-expression)
(when (stringp var-expression)
(let ((splitted (split-string var-expression "=")))
(setenv (car splitted) (string-join (cdr splitted) "=")))))
(car args))))
:path "/org/freedesktop/systemd1"
:interface "org.freedesktop.systemd1.Manager"
:member "SetEnvironment")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment