2018-12-29 20:11:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.emacs;
|
|
|
|
emacsCfg = config.programs.emacs;
|
2020-11-19 00:19:50 +01:00
|
|
|
emacsBinPath = "${cfg.package}/bin";
|
|
|
|
emacsVersion = getVersion cfg.package;
|
2020-06-08 23:01:17 +02:00
|
|
|
|
2021-05-23 22:24:30 +02:00
|
|
|
clientWMClass =
|
|
|
|
if versionAtLeast emacsVersion "28" then "Emacsd" else "Emacs";
|
|
|
|
|
2019-11-25 14:15:18 +01:00
|
|
|
# Adapted from upstream emacs.desktop
|
2020-10-22 21:52:22 +02:00
|
|
|
clientDesktopItem = pkgs.writeTextDir "share/applications/emacsclient.desktop"
|
|
|
|
(generators.toINI { } {
|
|
|
|
"Desktop Entry" = {
|
|
|
|
Type = "Application";
|
|
|
|
Exec = "${emacsBinPath}/emacsclient ${
|
|
|
|
concatStringsSep " " cfg.client.arguments
|
|
|
|
} %F";
|
|
|
|
Terminal = false;
|
|
|
|
Name = "Emacs Client";
|
|
|
|
Icon = "emacs";
|
|
|
|
Comment = "Edit text";
|
|
|
|
GenericName = "Text Editor";
|
|
|
|
MimeType =
|
|
|
|
"text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;";
|
2021-05-23 22:24:30 +02:00
|
|
|
Categories = "Development;TextEditor;";
|
|
|
|
Keywords = "Text;Editor;";
|
|
|
|
StartupWMClass = clientWMClass;
|
2020-10-22 21:52:22 +02:00
|
|
|
};
|
|
|
|
});
|
2018-12-29 20:11:48 +01:00
|
|
|
|
2020-06-08 23:01:17 +02:00
|
|
|
# Match the default socket path for the Emacs version so emacsclient continues
|
2021-05-23 22:24:30 +02:00
|
|
|
# to work without wrapping it.
|
2021-05-30 04:40:58 +02:00
|
|
|
socketDir = "%t/emacs";
|
|
|
|
socketPath = "${socketDir}/server";
|
2020-06-08 23:01:17 +02:00
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
in {
|
2020-06-11 20:08:40 +02:00
|
|
|
meta.maintainers = [ maintainers.tadfisher ];
|
|
|
|
|
2019-11-25 14:15:18 +01:00
|
|
|
options.services.emacs = {
|
2023-07-01 01:30:13 +02:00
|
|
|
enable = mkEnableOption (lib.mdDoc "the Emacs daemon");
|
2020-06-08 23:01:17 +02:00
|
|
|
|
2020-11-19 00:19:50 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = if emacsCfg.enable then emacsCfg.finalPackage else pkgs.emacs;
|
2021-10-09 11:14:08 +02:00
|
|
|
defaultText = literalExpression ''
|
2020-11-19 00:19:50 +01:00
|
|
|
if config.programs.emacs.enable then config.programs.emacs.finalPackage
|
|
|
|
else pkgs.emacs
|
|
|
|
'';
|
2023-07-01 01:30:13 +02:00
|
|
|
description = lib.mdDoc "The Emacs package to use.";
|
2020-11-19 00:19:50 +01:00
|
|
|
};
|
|
|
|
|
2021-09-29 15:07:38 +02:00
|
|
|
extraOptions = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "-f" "exwm-enable" ];
|
2023-07-01 01:30:13 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra command-line arguments to pass to {command}`emacs`.
|
2021-09-29 15:07:38 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-11-25 14:15:18 +01:00
|
|
|
client = {
|
2023-07-01 01:30:13 +02:00
|
|
|
enable =
|
|
|
|
mkEnableOption (lib.mdDoc "generation of Emacs client desktop file");
|
2019-11-25 14:15:18 +01:00
|
|
|
arguments = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [ "-c" ];
|
2023-07-01 01:30:13 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Command-line arguments to pass to {command}`emacsclient`.
|
2019-11-25 14:15:18 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2020-06-08 23:01:17 +02:00
|
|
|
|
|
|
|
# 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 = {
|
2023-07-01 01:30:13 +02:00
|
|
|
enable = mkEnableOption
|
|
|
|
(lib.mdDoc "systemd socket activation for the Emacs service");
|
2020-06-08 23:01:17 +02:00
|
|
|
};
|
2022-01-10 08:17:13 +01:00
|
|
|
|
2023-05-11 13:58:37 +02:00
|
|
|
startWithUserSession = mkOption {
|
|
|
|
type = with types; either bool (enum [ "graphical" ]);
|
2022-05-06 07:13:20 +02:00
|
|
|
default = !cfg.socketActivation.enable;
|
|
|
|
defaultText =
|
|
|
|
literalExpression "!config.services.emacs.socketActivation.enable";
|
2023-05-11 13:58:37 +02:00
|
|
|
example = "graphical";
|
2023-07-01 01:30:13 +02:00
|
|
|
description = lib.mdDoc ''
|
2023-05-12 23:52:04 +02:00
|
|
|
Whether to launch Emacs service with the systemd user session. If it is
|
2023-07-01 01:30:13 +02:00
|
|
|
`true`, Emacs service is started by
|
|
|
|
`default.target`. If it is
|
|
|
|
`"graphical"`, Emacs service is started by
|
|
|
|
`graphical-session.target`.
|
2022-05-06 07:13:20 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-10 08:17:13 +01:00
|
|
|
defaultEditor = mkOption rec {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = !default;
|
2023-07-01 01:30:13 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to configure {command}`emacsclient` as the default
|
|
|
|
editor using the {env}`EDITOR` environment variable.
|
2022-01-10 08:17:13 +01:00
|
|
|
'';
|
|
|
|
};
|
2019-11-25 14:15:18 +01:00
|
|
|
};
|
2018-12-29 20:11:48 +01:00
|
|
|
|
2020-06-08 23:01:17 +02:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{
|
2021-07-07 23:24:27 +02:00
|
|
|
assertions = [
|
|
|
|
(lib.hm.assertions.assertPlatform "services.emacs" pkgs
|
|
|
|
lib.platforms.linux)
|
|
|
|
];
|
|
|
|
|
2020-06-08 23:01:17 +02:00
|
|
|
systemd.user.services.emacs = {
|
|
|
|
Unit = {
|
2021-05-23 22:24:30 +02:00
|
|
|
Description = "Emacs text editor";
|
2020-06-08 23:01:17 +02:00
|
|
|
Documentation =
|
|
|
|
"info:emacs man:emacs(1) https://gnu.org/software/emacs/";
|
2018-12-29 20:11:48 +01:00
|
|
|
|
2023-05-11 13:58:37 +02:00
|
|
|
After = optional (cfg.startWithUserSession == "graphical")
|
|
|
|
"graphical-session.target";
|
|
|
|
PartOf = optional (cfg.startWithUserSession == "graphical")
|
|
|
|
"graphical-session.target";
|
|
|
|
|
2020-06-08 23:01:17 +02:00
|
|
|
# Avoid killing the Emacs session, which may be full of
|
|
|
|
# unsaved buffers.
|
|
|
|
X-RestartIfChanged = false;
|
2021-05-23 22:24:30 +02:00
|
|
|
} // optionalAttrs (cfg.socketActivation.enable) {
|
|
|
|
# Emacs deletes its socket when shutting down, which systemd doesn't
|
|
|
|
# handle, resulting in a server without a socket.
|
|
|
|
# See https://github.com/nix-community/home-manager/issues/2018
|
|
|
|
RefuseManualStart = true;
|
2020-06-08 23:01:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Service = {
|
2021-05-23 22:24:30 +02:00
|
|
|
Type = "notify";
|
|
|
|
|
2020-06-24 02:17:33 +02:00
|
|
|
# We wrap ExecStart in a login shell so Emacs starts with the user's
|
|
|
|
# environment, most importantly $PATH and $NIX_PROFILES. It may be
|
|
|
|
# worth investigating a more targeted approach for user services to
|
|
|
|
# import the user environment.
|
|
|
|
ExecStart = ''
|
|
|
|
${pkgs.runtimeShell} -l -c "${emacsBinPath}/emacs --fg-daemon${
|
2020-06-08 23:01:17 +02:00
|
|
|
# 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.
|
2020-06-24 02:17:33 +02:00
|
|
|
optionalString cfg.socketActivation.enable
|
|
|
|
"=${escapeShellArg socketPath}"
|
2021-09-29 15:07:38 +02:00
|
|
|
} ${escapeShellArgs cfg.extraOptions}"'';
|
2021-05-23 22:24:30 +02:00
|
|
|
|
|
|
|
# Emacs will exit with status 15 after having received SIGTERM, which
|
|
|
|
# is the default "KillSignal" value systemd uses to stop services.
|
|
|
|
SuccessExitStatus = 15;
|
|
|
|
|
2020-06-08 23:01:17 +02:00
|
|
|
Restart = "on-failure";
|
2021-05-30 04:40:58 +02:00
|
|
|
} // optionalAttrs (cfg.socketActivation.enable) {
|
|
|
|
# Use read-only directory permissions to prevent emacs from
|
|
|
|
# deleting systemd's socket file before exiting.
|
|
|
|
ExecStartPost =
|
|
|
|
"${pkgs.coreutils}/bin/chmod --changes -w ${socketDir}";
|
|
|
|
ExecStopPost =
|
|
|
|
"${pkgs.coreutils}/bin/chmod --changes +w ${socketDir}";
|
2020-06-08 23:01:17 +02:00
|
|
|
};
|
2023-05-11 13:58:37 +02:00
|
|
|
} // optionalAttrs (cfg.startWithUserSession != false) {
|
|
|
|
Install = {
|
|
|
|
WantedBy = [
|
|
|
|
(if cfg.startWithUserSession == true then
|
|
|
|
"default.target"
|
|
|
|
else
|
|
|
|
"graphical-session.target")
|
|
|
|
];
|
|
|
|
};
|
2018-12-29 20:11:48 +01:00
|
|
|
};
|
|
|
|
|
2022-01-10 08:17:13 +01:00
|
|
|
home = {
|
|
|
|
packages = optional cfg.client.enable (hiPrio clientDesktopItem);
|
|
|
|
|
|
|
|
sessionVariables = mkIf cfg.defaultEditor {
|
|
|
|
EDITOR = getBin (pkgs.writeShellScript "editor" ''
|
|
|
|
exec ${
|
|
|
|
getBin cfg.package
|
|
|
|
}/bin/emacsclient "''${@:---create-frame}"'');
|
|
|
|
};
|
|
|
|
};
|
2020-06-08 23:01:17 +02:00
|
|
|
}
|
2019-11-25 14:15:18 +01:00
|
|
|
|
2020-06-08 23:01:17 +02:00
|
|
|
(mkIf cfg.socketActivation.enable {
|
|
|
|
systemd.user.sockets.emacs = {
|
|
|
|
Unit = {
|
2021-05-23 22:24:30 +02:00
|
|
|
Description = "Emacs text editor";
|
2020-06-08 23:01:17 +02:00
|
|
|
Documentation =
|
|
|
|
"info:emacs man:emacs(1) https://gnu.org/software/emacs/";
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket = {
|
|
|
|
ListenStream = socketPath;
|
|
|
|
FileDescriptorName = "server";
|
|
|
|
SocketMode = "0600";
|
|
|
|
DirectoryMode = "0700";
|
|
|
|
};
|
|
|
|
|
|
|
|
Install = { WantedBy = [ "sockets.target" ]; };
|
|
|
|
};
|
|
|
|
})
|
|
|
|
]);
|
2018-12-29 20:11:48 +01:00
|
|
|
}
|