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 description = ''
25 Whether to enable a user service for the Emacs daemon. Use <literal>emacsclient</literal> to connect to the
26 daemon. If <literal>true</literal>, <varname>services.emacs.install</varname> is
27 considered <literal>true</literal>, whatever its value.
28 '';
29 };
30
31 install = mkOption {
32 type = types.bool;
33 default = false;
34 description = ''
35 Whether to install a user service for the Emacs daemon. Once
36 the service is started, use emacsclient to connect to the
37 daemon.
38
39 The service must be manually started for each user with
40 "systemctl --user start emacs" or globally through
41 <varname>services.emacs.enable</varname>.
42 '';
43 };
44
45
46 package = mkOption {
47 type = types.package;
48 default = pkgs.emacs;
49 defaultText = "pkgs.emacs";
50 description = ''
51 emacs derivation to use.
52 '';
53 };
54
55 defaultEditor = mkOption {
56 type = types.bool;
57 default = false;
58 description = ''
59 When enabled, configures emacsclient to be the default editor
60 using the EDITOR environment variable.
61 '';
62 };
63 };
64
65 config = mkIf (cfg.enable || cfg.install) {
66 systemd.user.services.emacs = {
67 description = "Emacs: the extensible, self-documenting text editor";
68
69 serviceConfig = {
70 Type = "forking";
71 ExecStart = "${pkgs.bash}/bin/bash -c 'source ${config.system.build.setEnvironment}; exec ${cfg.package}/bin/emacs --daemon'";
72 ExecStop = "${cfg.package}/bin/emacsclient --eval (kill-emacs)";
73 Restart = "always";
74 };
75 } // optionalAttrs cfg.enable { wantedBy = [ "default.target" ]; };
76
77 environment.systemPackages = [ cfg.package editorScript ];
78
79 environment.variables = {
80 # This is required so that GTK applications launched from Emacs
81 # get properly themed:
82 GTK_DATA_PREFIX = "${config.system.path}";
83 } // (if cfg.defaultEditor then {
84 EDITOR = mkOverride 900 "${editorScript}/bin/emacseditor";
85 } else {});
86 };
87
88 meta.doc = ./emacs.xml;
89}