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.
This commit is contained in:
Damien Cassou 2022-05-06 07:13:20 +02:00 committed by Robert Helgesson
parent 586ac1fd58
commit 931653b99f
No known key found for this signature in database
GPG Key ID: 36BDAA14C2797E89
3 changed files with 41 additions and 1 deletions

View File

@ -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" ]; };
};

View File

@ -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;
}

View File

@ -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"
'';
}