Automatic program start up on login with Xorg

Hi. I am trying to create a service to automatically open Emacs to the desktop on user login. I have tried two main methods.

Systsemd Service using home-manager

  systemd.user = {
    startServices = "sd-switch";
    services = {
      emacs.service = {
        enable = true;
        script = ''emacs -fs &'';
        wantedBy = [ "graphical-session.target" ];
      };
    };
  };

Full home-manager file
This doesn’t seem to do anything. I tested it by replacing emacs -fs & with touch ~/test.txt and it still didn’t work. So I am assuming I am missing something fundamental to configuring systemd services.


Using the service.emacs option

services.emacs = {
  enable = true;
  startWithUserSession = "graphical";
  extraOptions = [
    "-fs"
    ];
  };

Full Emacs config file
This “works” but doesn’t do what I was intending it to. Instead opening a server in the background witch doesn’t seem to use my init.el, and conflicts with any Emacs instance I open normally.

I would prefer to get the systemd service working if possible as that would be a useful way to solve a few other problems I am currently working on. For context I am tryning to start Emacs like this so EXWM can automatically take over as the window manager inside an XFCE display environment.

I had wasted lots time on it, may be it will help someone.
Some apps can disattach from the service and service will exit killing the app.
Fix is add: RemainAfterExit = true;

systemd.user.services.synology-drive = {
        description = "Synology Cloud Sync";
        enable = true;
        serviceConfig.PassEnvironment = "DISPLAY";
#       wantedBy = ["multi-user.target"];       #afer login
        wantedBy = ["graphical-session.target"];
        partOf   = ["graphical-session.target"];
        path     = [ pkgs.synology-drive-client ];

        script = ''
                #!/run/current-system/sw/bin/bash

                sleep 30
                cd ${pkgs.synology-drive-client}/bin && synology-drive 
        '';
        
        serviceConfig = {
                RemainAfterExit = true;
        };

  };


1 Like

Funny how this is the exact same application I need to autostart. Thanks a bunch.

btw this is the home-manager variant/module I ended up with:

{ pkgs, ... }:
{
  home.packages = with pkgs; [
    synology-drive-client
  ];

  systemd.user.services.synology-drive = {
    Unit = {
      Description = "Synology Cloud Sync";
    };

    Install = {
      WantedBy = [ "graphical-session.target" ];
    };

    Service = {
      Environment = "PATH=/run/wrappers/bin:/etc/profiles/per-user/root/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin";
      ExecStart = "${pkgs.synology-drive-client}/bin/synology-drive";
      RemainAfterExit = true;
    };
  };
}