Skip to content

Commit

Permalink
emacs: optionally start service with the session
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DamienCassou authored and rycee committed Jun 18, 2022
1 parent 586ac1f commit 931653b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion modules/services/emacs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,7 +156,7 @@ in {
ExecStopPost =
"${pkgs.coreutils}/bin/chmod --changes +w ${socketDir}";
};
} // optionalAttrs (!cfg.socketActivation.enable) {
} // optionalAttrs (cfg.startWithUserSession) {
Install = { WantedBy = [ "default.target" ]; };
};

Expand Down
2 changes: 2 additions & 0 deletions tests/modules/services/emacs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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"
'';
}

0 comments on commit 931653b

Please sign in to comment.