Emacs service in Home-manager


I’ve attempted to enable emacs service ( https://github.com/efim/dotfiles/blob/8f95d971eb4bbaf9899647c27e9b5808c6b511b2/nixpkgs/my-emacs.nix#L16 ) with my home manager setup and encounter a problem that I can’t fully understand

It seems that on system startup emacs process is started without proper XAUTHORITY
$ grep -az XAUTHORITY= /proc/$(pidof emacs)/environ :: returned empty string

$ emacsclient --create-frame :: returns
Error: Display 0.0 can’t be opened

After killing server emacs process with
$ emacsclient -t --eval ‘(progn (server-save-buffers-kill-terminal 1) (save-buffers-kill-emacs 1))’

On the new emacs process XAUTHORITY is set:
$ grep -az XAUTHORITY= /proc/$(pidof emacs)/environ :: returnes

And new graphical frames are opened correctly

I am on nixos 20.09, with home-manager installed from nixpkgs

You should additionally use home-manager xsession manager (xsession.enable) which injects some env vars into the proper places (XAUTHORITY is already in that list). Or do something similar yourself.

I already have xsession.enable in my home.nix

Currently in the managed .xprofile config there’s

I suspect this is where emacs server gets XAUTHORITY after restart

But I still don’t understand why wouldn’t it get correct value on system startup:

Additional note (after reading comments in x11 - emacsclient can't open display :0 - Emacs Stack Exchange )

Could it be that .xprofile and .xsessions aren’t used on me loggig in?
I am using xmonad configured in home-manager with

  xsession = {
    enable = true;

    windowManager.xmonad = {
      enable = true;
      enableContribAndExtras = true;
      config = ../xmonad/xmonad.hs;

And don’t think that I reference .xsession or .xpofile files in the xmonad config in any way

Edit: confirm that this problem is only when I try to log into xmonad
When I try to log into Gnome after system startup the emacsclient works well.

I have .xprofile that exports these variables, and .xprofile is managed by nix
It is sourced from /etc/gdm/Xprofile which is also managed by nix

I don’t know why wouldn’t emacs server get these variables when I log into xmonad after system start, still stuck.

This nixer article outlines load order for x session configurations

Could it be that GDM when I select “xmonad” doesn’t load it’s /etc/gdm/Xprofile
and loads directly ~/.xsession? How could I check that?

If someone knows how to fix that - please leave a note, I’m still unsure where to look for solution

What I’ve noticed is that emacs doesn’t depend on any units (unlike other services like e.g. https://github.com/nix-community/home-manager/blob/91bd34620d73340be03642279ee0d1c64110ee6c/modules/services/dunst.nix#L136). So you can try to inject something like this into your config:

systemd.user.services.emacs.Unit = {
          After = [ "graphical-session-pre.target" ];
          PartOf = [ "graphical-session.target" ];
1 Like

Thank you very much for the response!
At the time (about a year ago) I tried to figure it out and apply to my situation, but didn’t fully get it.

Currently I upon revisiting same problem, I actually understand what you meant and, yes, I suspect that it might be a way to solve this.

I still sometimes encounter this problem, when gdm fails to start “none+xmonad”
and this might be because emacs server is being started, but I only connect to X session after retry and it’s a different one?

My current hack that helps with the problem is to do “systemctl --user restart emacs”

I suspect that adding that requirement would help
One reason I’m not trying it now is that I suspect that I will more often use emacs from tty when X doesn’t start after an upgrade
And adding requirement for graphical session would mean that I’d probably need to manually start/restart emacs service when log into tty without graphics

This didn’t work for me with Gnome Shell.
The Emacs daemon worked without a problem on qtile before.

As reported restarting the daemon after logging in works but is a bit annoying to do.
emacsclient -tc works as well but then I would’ve to find a workaround for the clipboard.

I’m having this same issue with gnome. I even tried to put it after network-online.target, but still no luck. Have to manually restart the service once for this.