diff --git a/modules/services/emacs.nix b/modules/services/emacs.nix index 9a6ef5e48..8e7ba9fba 100644 --- a/modules/services/emacs.nix +++ b/modules/services/emacs.nix @@ -81,14 +81,14 @@ in { enable = mkEnableOption "systemd socket activation for the Emacs service"; }; - startWithUserSession = lib.mkOption { - type = lib.types.bool; + startWithUserSession = mkOption { + type = with types; either bool (enum [ "graphical" ]); default = !cfg.socketActivation.enable; defaultText = literalExpression "!config.services.emacs.socketActivation.enable"; - example = true; + example = "graphical"; description = '' - Whether to launch Emacs service with the systemd user session. + Whether to launch Emacs service with the systemd user session. If it is true, Emacs service is started by default.target. If it is "graphical", Emacs service is started by graphical-session.target. ''; }; @@ -116,6 +116,11 @@ in { Documentation = "info:emacs man:emacs(1) https://gnu.org/software/emacs/"; + After = optional (cfg.startWithUserSession == "graphical") + "graphical-session.target"; + PartOf = optional (cfg.startWithUserSession == "graphical") + "graphical-session.target"; + # Avoid killing the Emacs session, which may be full of # unsaved buffers. X-RestartIfChanged = false; @@ -156,8 +161,15 @@ in { ExecStopPost = "${pkgs.coreutils}/bin/chmod --changes +w ${socketDir}"; }; - } // optionalAttrs (cfg.startWithUserSession) { - Install = { WantedBy = [ "default.target" ]; }; + } // optionalAttrs (cfg.startWithUserSession != false) { + Install = { + WantedBy = [ + (if cfg.startWithUserSession == true then + "default.target" + else + "graphical-session.target") + ]; + }; }; home = { diff --git a/tests/modules/services/emacs/default.nix b/tests/modules/services/emacs/default.nix index cbbd6f6aa..f3647efd6 100644 --- a/tests/modules/services/emacs/default.nix +++ b/tests/modules/services/emacs/default.nix @@ -1,6 +1,8 @@ { emacs-service-27 = ./emacs-service-27.nix; emacs-service-28 = ./emacs-service-28.nix; + emacs-service-28-after-graphical-session-target = + ./emacs-service-28-after-graphical-session-target.nix; emacs-socket-27 = ./emacs-socket-27.nix; emacs-socket-28 = ./emacs-socket-28.nix; emacs-default-editor = ./emacs-default-editor.nix; diff --git a/tests/modules/services/emacs/emacs-service-28-after-graphical-session-target.nix b/tests/modules/services/emacs/emacs-service-28-after-graphical-session-target.nix new file mode 100644 index 000000000..237e9d146 --- /dev/null +++ b/tests/modules/services/emacs/emacs-service-28-after-graphical-session-target.nix @@ -0,0 +1,40 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + nixpkgs.overlays = [ + (self: super: rec { + emacs = pkgs.writeShellScriptBin "dummy-emacs-28.2" "" // { + outPath = "@emacs@"; + }; + emacsPackagesFor = _: + makeScope super.newScope (_: { emacsWithPackages = _: emacs; }); + }) + ]; + + programs.emacs.enable = true; + services.emacs.enable = true; + services.emacs.client.enable = true; + services.emacs.extraOptions = [ "-f" "exwm-enable" ]; + services.emacs.startWithUserSession = "graphical"; + + nmt.script = '' + assertPathNotExists home-files/.config/systemd/user/emacs.socket + assertFileExists home-files/.config/systemd/user/emacs.service + assertFileExists home-path/share/applications/emacsclient.desktop + + assertFileContent home-files/.config/systemd/user/emacs.service \ + ${ + pkgs.substituteAll { + inherit (pkgs) runtimeShell; + src = + ./emacs-service-emacs-after-graphical-session-target.service; + } + } + assertFileContent home-path/share/applications/emacsclient.desktop \ + ${./emacs-28-emacsclient.desktop} + ''; + }; +} diff --git a/tests/modules/services/emacs/emacs-service-emacs-after-graphical-session-target.service b/tests/modules/services/emacs/emacs-service-emacs-after-graphical-session-target.service new file mode 100644 index 000000000..193327621 --- /dev/null +++ b/tests/modules/services/emacs/emacs-service-emacs-after-graphical-session-target.service @@ -0,0 +1,15 @@ +[Install] +WantedBy=graphical-session.target + +[Service] +ExecStart=@runtimeShell@ -l -c "@emacs@/bin/emacs --fg-daemon '-f' 'exwm-enable'" +Restart=on-failure +SuccessExitStatus=15 +Type=notify + +[Unit] +After=graphical-session.target +Description=Emacs text editor +Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/ +PartOf=graphical-session.target +X-RestartIfChanged=false