1 { config, lib, pkgs, ... }:
4 cfg = config.services.emacs;
6 editorScript = pkgs.writeShellScriptBin "emacseditor" ''
8 exec ${cfg.package}/bin/emacsclient --create-frame --alternate-editor ${cfg.package}/bin/emacs
10 exec ${cfg.package}/bin/emacsclient --alternate-editor ${cfg.package}/bin/emacs "$@"
17 options.services.emacs = {
18 enable = lib.mkOption {
19 type = lib.types.bool;
22 Whether to enable a user service for the Emacs daemon. Use `emacsclient` to connect to the
23 daemon. If `true`, {var}`services.emacs.install` is
24 considered `true`, whatever its value.
28 install = lib.mkOption {
29 type = lib.types.bool;
32 Whether to install a user service for the Emacs daemon. Once
33 the service is started, use emacsclient to connect to the
36 The service must be manually started for each user with
37 "systemctl --user start emacs" or globally through
38 {var}`services.emacs.enable`.
43 package = lib.mkPackageOption pkgs "emacs" { };
45 defaultEditor = lib.mkOption {
46 type = lib.types.bool;
49 When enabled, configures emacsclient to be the default editor
50 using the EDITOR environment variable.
54 startWithGraphical = lib.mkOption {
55 type = lib.types.bool;
56 default = config.services.xserver.enable;
57 defaultText = lib.literalExpression "config.services.xserver.enable";
59 Start emacs with the graphical session instead of any session. Without this, emacs clients will not be able to create frames in the graphical session.
64 config = lib.mkIf (cfg.enable || cfg.install) {
65 systemd.user.services.emacs = {
66 description = "Emacs: the extensible, self-documenting text editor";
70 ExecStart = "${pkgs.runtimeShell} -c 'source ${config.system.build.setEnvironment}; exec ${cfg.package}/bin/emacs --fg-daemon'";
71 ExecStop = "${cfg.package}/bin/emacsclient --eval (kill-emacs)";
75 unitConfig = lib.optionalAttrs cfg.startWithGraphical {
76 After = "graphical-session.target";
78 } // lib.optionalAttrs cfg.enable {
79 wantedBy = if cfg.startWithGraphical then [ "graphical-session.target" ] else [ "default.target" ];
82 environment.systemPackages = [ cfg.package editorScript ];
84 environment.variables.EDITOR = lib.mkIf cfg.defaultEditor (lib.mkOverride 900 "emacseditor");
87 meta.doc = ./emacs.md;