1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.emacs;
8
9 editorScript = pkgs.writeScriptBin "emacseditor" ''
10 #!${pkgs.stdenv.shell}
11 if [ -z "$1" ]; then
12 exec ${cfg.package}/bin/emacsclient --create-frame --alternate-editor ${cfg.package}/bin/emacs
13 else
14 exec ${cfg.package}/bin/emacsclient --alternate-editor ${cfg.package}/bin/emacs "$@"
15 fi
16 '';
17
18in {
19
20 options.services.emacs = {
21 enable = mkOption {
22 type = types.bool;
23 default = false;
24 example = true;
25 description = ''
26 Whether to enable a user service for the Emacs daemon. Use <literal>emacsclient</literal> to connect to the
27 daemon. If <literal>true</literal>, <varname>services.emacs.install</varname> is
28 considered <literal>true</literal>, whatever its value.
29 '';
30 };
31
32 install = mkOption {
33 type = types.bool;
34 default = false;
35 example = true;
36 description = ''
37 Whether to install a user service for the Emacs daemon. Once
38 the service is started, use emacsclient to connect to the
39 daemon.
40
41 The service must be manually started for each user with
42 "systemctl --user start emacs" or globally through
43 <varname>services.emacs.enable</varname>.
44 '';
45 };
46
47
48 package = mkOption {
49 type = types.package;
50 default = pkgs.emacs;
51 defaultText = "pkgs.emacs";
52 description = ''
53 emacs derivation to use.
54 '';
55 };
56
57 defaultEditor = mkOption {
58 type = types.bool;
59 default = false;
60 example = true;
61 description = ''
62 When enabled, configures emacsclient to be the default editor
63 using the EDITOR environment variable.
64 '';
65 };
66 };
67
68 config = mkIf (cfg.enable || cfg.install) {
69 systemd.user.services.emacs = {
70 description = "Emacs: the extensible, self-documenting text editor";
71
72 serviceConfig = {
73 Type = "forking";
74 ExecStart = "${pkgs.bash}/bin/bash -c 'source ${config.system.build.setEnvironment}; exec ${cfg.package}/bin/emacs --daemon'";
75 ExecStop = "${cfg.package}/bin/emacsclient --eval (kill-emacs)";
76 Restart = "always";
77 };
78 } // optionalAttrs cfg.enable { wantedBy = [ "default.target" ]; };
79
80 environment.systemPackages = [ cfg.package editorScript ];
81
82 environment.variables = if cfg.defaultEditor then {
83 EDITOR = mkOverride 900 "${editorScript}/bin/emacseditor";
84 } else {};
85 };
86
87 meta.doc = ./emacs.xml;
88}