From 931653b99f876c6ddee0f9b864707cf228d162f1 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Fri, 6 May 2022 07:13:20 +0200 Subject: [PATCH] emacs: optionally start service with the session Add services.emacs.startWithUserSession boolean to indicate that Emacs must be started with the systemd user session. This is true by default unless socket activation is also true. In the past, the user had to choose between socket activation (to get the Emacs service started when the user uses emacsclient) and immediate start with the user session. When choosing immediate start over socket activation and if the Emacs service is stopped at some point, using emacsclient would start a new Emacs daemon but the service would still be turned off. This situation would prevent `home-manager switch` from completing successfully because it wouldn't be able to start the Emacs service as Emacs is already running. This new setting makes it possible to have both socket activation and immediate start at the same time. In this scenario, Emacs is started with the user session and, after the Emacs service is stopped, using emacsclient starts the service again. This new settings also makes it possible to have neither socket activation nor immediate start. --- modules/services/emacs.nix | 13 ++++++++- tests/modules/services/emacs/default.nix | 2 ++ .../emacs-socket-and-startWithUserSession.nix | 27 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/modules/services/emacs/emacs-socket-and-startWithUserSession.nix diff --git a/modules/services/emacs.nix b/modules/services/emacs.nix index 38267151..9a6ef5e4 100644 --- a/modules/services/emacs.nix +++ b/modules/services/emacs.nix @@ -81,6 +81,17 @@ in { enable = mkEnableOption "systemd socket activation for the Emacs service"; }; + startWithUserSession = lib.mkOption { + type = lib.types.bool; + default = !cfg.socketActivation.enable; + defaultText = + literalExpression "!config.services.emacs.socketActivation.enable"; + example = true; + description = '' + Whether to launch Emacs service with the systemd user session. + ''; + }; + defaultEditor = mkOption rec { type = types.bool; default = false; @@ -145,7 +156,7 @@ in { ExecStopPost = "${pkgs.coreutils}/bin/chmod --changes +w ${socketDir}"; }; - } // optionalAttrs (!cfg.socketActivation.enable) { + } // optionalAttrs (cfg.startWithUserSession) { Install = { WantedBy = [ "default.target" ]; }; }; diff --git a/tests/modules/services/emacs/default.nix b/tests/modules/services/emacs/default.nix index 47493207..cbbd6f6a 100644 --- a/tests/modules/services/emacs/default.nix +++ b/tests/modules/services/emacs/default.nix @@ -4,4 +4,6 @@ emacs-socket-27 = ./emacs-socket-27.nix; emacs-socket-28 = ./emacs-socket-28.nix; emacs-default-editor = ./emacs-default-editor.nix; + emacs-socket-and-startWithUserSession = + ./emacs-socket-and-startWithUserSession.nix; } diff --git a/tests/modules/services/emacs/emacs-socket-and-startWithUserSession.nix b/tests/modules/services/emacs/emacs-socket-and-startWithUserSession.nix new file mode 100644 index 00000000..5f47089e --- /dev/null +++ b/tests/modules/services/emacs/emacs-socket-and-startWithUserSession.nix @@ -0,0 +1,27 @@ +{ lib, pkgs, ... }: + +with lib; + +{ + services.emacs = { + enable = true; + socketActivation.enable = true; + startWithUserSession = true; + }; + + nixpkgs.overlays = [ + (self: super: rec { + emacs = pkgs.writeShellScriptBin "dummy-emacs-28.0.5" "" // { + outPath = "@emacs@"; + }; + emacsPackagesFor = _: + makeScope super.newScope (_: { emacsWithPackages = _: emacs; }); + }) + ]; + + nmt.script = '' + assertFileContains \ + home-files/.config/systemd/user/emacs.service \ + "WantedBy=default.target" + ''; +}