mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 19:46:05 +01:00
parent
6695b1d477
commit
82ee14ff60
124 changed files with 1848 additions and 1891 deletions
|
|
@ -4,27 +4,31 @@
|
|||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
inherit (lib)
|
||||
mkIf
|
||||
mkOption
|
||||
optional
|
||||
optionalAttrs
|
||||
types
|
||||
;
|
||||
|
||||
cfg = config.services.emacs;
|
||||
emacsCfg = config.programs.emacs;
|
||||
emacsBinPath = "${cfg.package}/bin";
|
||||
emacsVersion = getVersion cfg.package;
|
||||
emacsVersion = lib.getVersion cfg.package;
|
||||
|
||||
clientWMClass = if versionAtLeast emacsVersion "28" then "Emacsd" else "Emacs";
|
||||
clientWMClass = if lib.versionAtLeast emacsVersion "28" then "Emacsd" else "Emacs";
|
||||
|
||||
# Workaround for https://debbugs.gnu.org/47511
|
||||
needsSocketWorkaround = versionOlder emacsVersion "28" && cfg.socketActivation.enable;
|
||||
needsSocketWorkaround = lib.versionOlder emacsVersion "28" && cfg.socketActivation.enable;
|
||||
|
||||
# Adapted from upstream emacs.desktop
|
||||
clientDesktopItem = pkgs.writeTextDir "share/applications/emacsclient.desktop" (
|
||||
generators.toINI { } {
|
||||
lib.generators.toINI { } {
|
||||
"Desktop Entry" = {
|
||||
Type = "Application";
|
||||
Exec = "${emacsBinPath}/emacsclient ${concatStringsSep " " cfg.client.arguments} %F";
|
||||
Exec = "${emacsBinPath}/emacsclient ${lib.concatStringsSep " " cfg.client.arguments} %F";
|
||||
Terminal = false;
|
||||
Name = "Emacs Client";
|
||||
Icon = "emacs";
|
||||
|
|
@ -44,15 +48,15 @@ let
|
|||
socketPath = "${socketDir}/server";
|
||||
in
|
||||
{
|
||||
meta.maintainers = [ maintainers.tadfisher ];
|
||||
meta.maintainers = [ lib.maintainers.tadfisher ];
|
||||
|
||||
options.services.emacs = {
|
||||
enable = mkEnableOption "the Emacs daemon";
|
||||
enable = lib.mkEnableOption "the Emacs daemon";
|
||||
|
||||
package = mkOption {
|
||||
type = types.package;
|
||||
default = if emacsCfg.enable then emacsCfg.finalPackage else pkgs.emacs;
|
||||
defaultText = literalExpression ''
|
||||
defaultText = lib.literalExpression ''
|
||||
if config.programs.emacs.enable then config.programs.emacs.finalPackage
|
||||
else pkgs.emacs
|
||||
'';
|
||||
|
|
@ -72,7 +76,7 @@ in
|
|||
};
|
||||
|
||||
client = {
|
||||
enable = mkEnableOption "generation of Emacs client desktop file";
|
||||
enable = lib.mkEnableOption "generation of Emacs client desktop file";
|
||||
arguments = mkOption {
|
||||
type = with types; listOf str;
|
||||
default = [ "-c" ];
|
||||
|
|
@ -86,13 +90,13 @@ in
|
|||
# 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";
|
||||
enable = lib.mkEnableOption "systemd socket activation for the Emacs service";
|
||||
};
|
||||
|
||||
startWithUserSession = mkOption {
|
||||
type = with types; either bool (enum [ "graphical" ]);
|
||||
default = !cfg.socketActivation.enable;
|
||||
defaultText = literalExpression "!config.services.emacs.socketActivation.enable";
|
||||
defaultText = lib.literalExpression "!config.services.emacs.socketActivation.enable";
|
||||
example = "graphical";
|
||||
description = ''
|
||||
Whether to launch Emacs service with the systemd user session. If it is
|
||||
|
|
@ -114,122 +118,124 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable (mkMerge [
|
||||
(mkIf pkgs.stdenv.isLinux {
|
||||
systemd.user.services.emacs =
|
||||
{
|
||||
Unit =
|
||||
{
|
||||
Description = "Emacs text editor";
|
||||
Documentation = "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
|
||||
config = mkIf cfg.enable (
|
||||
lib.mkMerge [
|
||||
(mkIf pkgs.stdenv.isLinux {
|
||||
systemd.user.services.emacs =
|
||||
{
|
||||
Unit =
|
||||
{
|
||||
Description = "Emacs text editor";
|
||||
Documentation = "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
|
||||
|
||||
After = optional (cfg.startWithUserSession == "graphical") "graphical-session.target";
|
||||
PartOf = optional (cfg.startWithUserSession == "graphical") "graphical-session.target";
|
||||
After = optional (cfg.startWithUserSession == "graphical") "graphical-session.target";
|
||||
PartOf = optional (cfg.startWithUserSession == "graphical") "graphical-session.target";
|
||||
|
||||
# Avoid killing the Emacs session, which may be full of
|
||||
# unsaved buffers.
|
||||
X-RestartIfChanged = false;
|
||||
}
|
||||
// optionalAttrs needsSocketWorkaround {
|
||||
# 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;
|
||||
# Avoid killing the Emacs session, which may be full of
|
||||
# unsaved buffers.
|
||||
X-RestartIfChanged = false;
|
||||
}
|
||||
// optionalAttrs needsSocketWorkaround {
|
||||
# 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;
|
||||
};
|
||||
|
||||
Service =
|
||||
{
|
||||
Type = "notify";
|
||||
|
||||
# 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${
|
||||
# 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.
|
||||
lib.optionalString cfg.socketActivation.enable "=${lib.escapeShellArg socketPath}"
|
||||
} ${lib.escapeShellArgs cfg.extraOptions}"'';
|
||||
|
||||
# Emacs will exit with status 15 after having received SIGTERM, which
|
||||
# is the default "KillSignal" value systemd uses to stop services.
|
||||
SuccessExitStatus = 15;
|
||||
|
||||
Restart = "on-failure";
|
||||
}
|
||||
// optionalAttrs needsSocketWorkaround {
|
||||
# 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}";
|
||||
};
|
||||
}
|
||||
// optionalAttrs (cfg.startWithUserSession != false) {
|
||||
Install = {
|
||||
WantedBy = [
|
||||
(if cfg.startWithUserSession == true then "default.target" else "graphical-session.target")
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
Service =
|
||||
{
|
||||
Type = "notify";
|
||||
home = {
|
||||
packages = optional cfg.client.enable (lib.hiPrio clientDesktopItem);
|
||||
|
||||
# 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${
|
||||
# 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 "=${escapeShellArg socketPath}"
|
||||
} ${escapeShellArgs cfg.extraOptions}"'';
|
||||
sessionVariables = mkIf cfg.defaultEditor {
|
||||
EDITOR = lib.getBin (
|
||||
pkgs.writeShellScript "editor" ''exec ${lib.getBin cfg.package}/bin/emacsclient "''${@:---create-frame}"''
|
||||
);
|
||||
};
|
||||
};
|
||||
})
|
||||
|
||||
# Emacs will exit with status 15 after having received SIGTERM, which
|
||||
# is the default "KillSignal" value systemd uses to stop services.
|
||||
SuccessExitStatus = 15;
|
||||
(mkIf (cfg.socketActivation.enable && pkgs.stdenv.isLinux) {
|
||||
systemd.user.sockets.emacs = {
|
||||
Unit = {
|
||||
Description = "Emacs text editor";
|
||||
Documentation = "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
|
||||
};
|
||||
|
||||
Socket = {
|
||||
ListenStream = socketPath;
|
||||
FileDescriptorName = "server";
|
||||
SocketMode = "0600";
|
||||
DirectoryMode = "0700";
|
||||
# This prevents the service from immediately starting again
|
||||
# after being stopped, due to the function
|
||||
# `server-force-stop' present in `kill-emacs-hook', which
|
||||
# calls `server-running-p', which opens the socket file.
|
||||
FlushPending = true;
|
||||
};
|
||||
|
||||
Restart = "on-failure";
|
||||
}
|
||||
// optionalAttrs needsSocketWorkaround {
|
||||
# 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}";
|
||||
};
|
||||
}
|
||||
// optionalAttrs (cfg.startWithUserSession != false) {
|
||||
Install = {
|
||||
WantedBy = [
|
||||
(if cfg.startWithUserSession == true then "default.target" else "graphical-session.target")
|
||||
];
|
||||
WantedBy = [ "sockets.target" ];
|
||||
# Adding this Requires= dependency ensures that systemd
|
||||
# manages the socket file, in the case where the service is
|
||||
# started when the socket is stopped.
|
||||
# The socket unit is implicitly ordered before the service.
|
||||
RequiredBy = [ "emacs.service" ];
|
||||
};
|
||||
};
|
||||
})
|
||||
|
||||
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}"''
|
||||
);
|
||||
};
|
||||
};
|
||||
})
|
||||
|
||||
(mkIf (cfg.socketActivation.enable && pkgs.stdenv.isLinux) {
|
||||
systemd.user.sockets.emacs = {
|
||||
Unit = {
|
||||
Description = "Emacs text editor";
|
||||
Documentation = "info:emacs man:emacs(1) https://gnu.org/software/emacs/";
|
||||
};
|
||||
|
||||
Socket = {
|
||||
ListenStream = socketPath;
|
||||
FileDescriptorName = "server";
|
||||
SocketMode = "0600";
|
||||
DirectoryMode = "0700";
|
||||
# This prevents the service from immediately starting again
|
||||
# after being stopped, due to the function
|
||||
# `server-force-stop' present in `kill-emacs-hook', which
|
||||
# calls `server-running-p', which opens the socket file.
|
||||
FlushPending = true;
|
||||
};
|
||||
|
||||
Install = {
|
||||
WantedBy = [ "sockets.target" ];
|
||||
# Adding this Requires= dependency ensures that systemd
|
||||
# manages the socket file, in the case where the service is
|
||||
# started when the socket is stopped.
|
||||
# The socket unit is implicitly ordered before the service.
|
||||
RequiredBy = [ "emacs.service" ];
|
||||
};
|
||||
};
|
||||
})
|
||||
|
||||
(mkIf pkgs.stdenv.isDarwin {
|
||||
launchd.agents.emacs = {
|
||||
enable = true;
|
||||
config = {
|
||||
ProgramArguments = [
|
||||
"${cfg.package}/bin/emacs"
|
||||
"--fg-daemon"
|
||||
] ++ cfg.extraOptions;
|
||||
RunAtLoad = true;
|
||||
KeepAlive = {
|
||||
Crashed = true;
|
||||
SuccessfulExit = false;
|
||||
(mkIf pkgs.stdenv.isDarwin {
|
||||
launchd.agents.emacs = {
|
||||
enable = true;
|
||||
config = {
|
||||
ProgramArguments = [
|
||||
"${cfg.package}/bin/emacs"
|
||||
"--fg-daemon"
|
||||
] ++ cfg.extraOptions;
|
||||
RunAtLoad = true;
|
||||
KeepAlive = {
|
||||
Crashed = true;
|
||||
SuccessfulExit = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
})
|
||||
]);
|
||||
})
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue