aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorTad Fisher <tadfisher@gmail.com>2020-06-08 14:01:17 -0700
committerRobert Helgesson <robert@rycee.net>2020-06-11 20:39:49 +0200
commit3815248786cb7b911ae1a72d2ec769f93c0b27b6 (patch)
tree11bf1373d5e6597d751129d905daaa6a195350d4 /modules
parent02c1f8d416d55d8bc8d4de62f65f62fef40e5e80 (diff)
emacs: Support socket activation via systemd
Add 'services.emacs.socketActivation.enable' for generating an 'emacs.socket' systemd unit. Emacs since version 26 has supported socket activation, whereby an external process manager such as systemd listens on a socket and passes it to the Emacs daemon when the manager launches it. This improves startup time of the user session and avoids launching the daemon when not needed, for example when launching the user session via SSH. This implementation hard-codes the socket path to the default for the version of 'programs.emacs.finalPackage', because systemd does not perform shell expansion in the socket unit's 'ListenStream' parameter and it seems like an advanced use-case to change the socket path. Shell expansion would be desirable as the socket path usually resides in directories such as $XDG_RUNTIME_DIR or $TMPDIR. Tests were added to verify behavior in the following cases: - Emacs service with socket activation disabled - Emacs 26 with socket activation enabled - Emacs 27 with socket activation enabled PR #1314
Diffstat (limited to 'modules')
-rw-r--r--modules/misc/news.nix10
-rw-r--r--modules/services/emacs.nix113
2 files changed, 97 insertions, 26 deletions
diff --git a/modules/misc/news.nix b/modules/misc/news.nix
index 4f962eaddcf..65b2545b01d 100644
--- a/modules/misc/news.nix
+++ b/modules/misc/news.nix
@@ -1542,6 +1542,16 @@ in
programs.ssh.matchBlocks.<name>.serverAliveCountMax
'';
}
+
+ {
+ time = "2020-06-11T18:06:37+00:00";
+ condition = config.services.emacs.enable;
+ message = ''
+ The Emacs service now supports systemd socket activation.
+
+ It can be enabled through the option 'services.emacs.socketActivation.enable'.
+ '';
+ }
];
};
}
diff --git a/modules/services/emacs.nix b/modules/services/emacs.nix
index c027626253d..7b91bcc2923 100644
--- a/modules/services/emacs.nix
+++ b/modules/services/emacs.nix
@@ -7,6 +7,8 @@ let
cfg = config.services.emacs;
emacsCfg = config.programs.emacs;
emacsBinPath = "${emacsCfg.finalPackage}/bin";
+ emacsVersion = getVersion emacsCfg.finalPackage;
+
# Adapted from upstream emacs.desktop
clientDesktopItem = pkgs.makeDesktopItem rec {
name = "emacsclient";
@@ -27,9 +29,26 @@ let
'';
};
+ # Match the default socket path for the Emacs version so emacsclient continues
+ # to work without wrapping it. It might be worthwhile to allow customizing the
+ # socket path, but we would want to wrap emacsclient in the user profile to
+ # connect to the alternative socket by default for Emacs 26, and set
+ # EMACS_SOCKET_NAME for Emacs 27.
+ #
+ # As systemd doesn't perform variable expansion for the ListenStream param, we
+ # would also have to solve the problem of matching the shell path to the path
+ # used in the socket unit, which would likely involve templating. It seems of
+ # little value for the most common use case of one Emacs daemon per user
+ # session.
+ socketPath = if versionAtLeast emacsVersion "27" then
+ "%t/emacs/server"
+ else
+ "%T/emacs%U/server";
+
in {
options.services.emacs = {
enable = mkEnableOption "the Emacs daemon";
+
client = {
enable = mkEnableOption "generation of Emacs client desktop file";
arguments = mkOption {
@@ -40,36 +59,78 @@ in {
'';
};
};
+
+ # Attrset for forward-compatibility; there may be a need to customize the
+ # socket path, though allowing for such is not easy to do as systemd socket
+ # units don't perform variable expansion for 'ListenStream'.
+ socketActivation = {
+ enable = mkEnableOption "systemd socket activation for the Emacs service";
+ };
};
- config = mkIf cfg.enable {
- assertions = [{
- assertion = emacsCfg.enable;
- message = "The Emacs service module requires"
- + " 'programs.emacs.enable = true'.";
- }];
-
- systemd.user.services.emacs = {
- Unit = {
- Description = "Emacs: the extensible, self-documenting text editor";
- Documentation =
- "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
-
- # Avoid killing the Emacs session, which may be full of
- # unsaved buffers.
- X-RestartIfChanged = false;
- };
+ config = mkIf cfg.enable (mkMerge [
+ {
+ assertions = [
+ {
+ assertion = emacsCfg.enable;
+ message = "The Emacs service module requires"
+ + " 'programs.emacs.enable = true'.";
+ }
+ {
+ assertion = cfg.socketActivation.enable
+ -> versionAtLeast emacsVersion "26";
+ message = "Socket activation requires Emacs 26 or newer.";
+ }
+ ];
+
+ systemd.user.services.emacs = {
+ Unit = {
+ Description = "Emacs: the extensible, self-documenting text editor";
+ Documentation =
+ "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
- Service = {
- ExecStart =
- "${pkgs.runtimeShell} -l -c 'exec ${emacsBinPath}/emacs --fg-daemon'";
- ExecStop = "${emacsBinPath}/emacsclient --eval '(kill-emacs)'";
- Restart = "on-failure";
+ # Avoid killing the Emacs session, which may be full of
+ # unsaved buffers.
+ X-RestartIfChanged = false;
+ };
+
+ Service = {
+ ExecStart = "${emacsBinPath}/emacs --fg-daemon${
+ # In case the user sets 'server-directory' or 'server-name' in
+ # their Emacs config, we want to specify the socket path explicitly
+ # so launching 'emacs.service' manually doesn't break emacsclient
+ # when using socket activation.
+ optionalString cfg.socketActivation.enable ''="${socketPath}"''
+ }";
+ # We use '(kill-emacs 0)' to avoid exiting with a failure code, which
+ # would restart the service immediately.
+ ExecStop = "${emacsBinPath}/emacsclient --eval '(kill-emacs 0)'";
+ Restart = "on-failure";
+ };
+ } // optionalAttrs (!cfg.socketActivation.enable) {
+ Install = { WantedBy = [ "default.target" ]; };
};
- Install = { WantedBy = [ "default.target" ]; };
- };
+ home.packages = optional cfg.client.enable clientDesktopItem;
+ }
- home.packages = optional cfg.client.enable clientDesktopItem;
- };
+ (mkIf cfg.socketActivation.enable {
+ systemd.user.sockets.emacs = {
+ Unit = {
+ Description = "Emacs: the extensible, self-documenting text editor";
+ Documentation =
+ "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
+ };
+
+ Socket = {
+ ListenStream = socketPath;
+ FileDescriptorName = "server";
+ SocketMode = "0600";
+ DirectoryMode = "0700";
+ };
+
+ Install = { WantedBy = [ "sockets.target" ]; };
+ };
+ })
+ ]);
}