Emacs service in Home-manager

Hello!

I’ve attempted to enable emacs service ( dotfiles/my-emacs.nix at 8f95d971eb4bbaf9899647c27e9b5808c6b511b2 · efim/dotfiles · GitHub ) 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

And
$ 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
XAUTHORITY=/run/user/1000/gdm/Xauthority

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
systemctl --user import-environment DBUS_SESSION_BUS_ADDRESS DISPLAY SSH_AUTH_SOCK XAUTHORITY XDG_DATA_DIRS XDG_RUNTIME_DIR XDG_SESSION_ID

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. home-manager/dunst.nix at 91bd34620d73340be03642279ee0d1c64110ee6c · nix-community/home-manager · GitHub). 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" ];
};