aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/services/emacs.nix
blob: 5b0e88db72d7a2b59f2c70559fa6fd583d263482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.emacs;
  emacsCfg = config.programs.emacs;
  emacsBinPath = "${emacsCfg.finalPackage}/bin";

in {
  options.services.emacs = { enable = mkEnableOption "the Emacs daemon"; };

  config = mkIf cfg.enable {
    assertions = [{
      assertion = emacsCfg.enable;
      message = "The Emacs service module requires"
        + " 'programs.emacs.enable = true'.";
    }];

    systemd.user.services.emacs = {
      Unit = {
        Description = "Emacs: the extensible, self-documenting text editor";
        Documentation =
          "info:emacs man:emacs(1) https://gnu.org/software/emacs/";

        # Avoid killing the Emacs session, which may be full of
        # unsaved buffers.
        X-RestartIfChanged = false;
      };

      Service = {
        ExecStart =
          "${pkgs.runtimeShell} -l -c 'exec ${emacsBinPath}/emacs --fg-daemon'";
        ExecStop = "${emacsBinPath}/emacsclient --eval '(kill-emacs)'";
        Restart = "on-failure";
      };

      Install = { WantedBy = [ "default.target" ]; };
    };
  };
}