Commit 63a42b604cff

Vincent Demeester <vincent@sbr.pm>
2020-05-11 19:09:11
home-manager: add services.emacs-server module
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent d04233b
Changed files (3)
modules/profiles/emacs.nix
@@ -7,6 +7,7 @@ let
     #!${pkgs.stdenv.shell}
     emacsclient -s /run/user/1000/emacs/org -n -F '((name . "capture") (width . 150) (height . 90))' -e '(org-capture)'
   '';
+  myEmacs = pkgs.emacs27.override { inherit (pkgs) imagemagick; withXwidgets = cfg.withXwidgets; };
 in
 {
   options = {
@@ -26,173 +27,159 @@ in
       };
     };
   };
-  config = mkIf cfg.enable
+  config = mkIf cfg.enable (mkMerge [
+    {
+      home.file.".local/share/applications/org-protocol.desktop".source = ./assets/xorg/org-protocol.desktop;
+      home.file.".local/share/applications/ec.desktop".source = ./assets/xorg/ec.desktop;
+      home.file.".local/share/applications/capture.desktop".source = ./assets/xorg/capture.desktop;
+      home.packages = with pkgs; [
+        ditaa
+        graphviz
+        pandoc
+        zip
+        hunspell
+        hunspellDicts.en_US-large
+        hunspellDicts.en_GB-ize
+        hunspellDicts.fr-any
+        nixpkgs-fmt
+      ];
+      home.sessionVariables = {
+        EDITOR = "et";
+        ALTERNATE_EDITOR = "et";
+      };
+      programs.emacs = {
+        enable = true;
+        package = myEmacs;
+        extraPackages = epkgs: with epkgs; [
+          ace-window
+          aggressive-indent
+          async
+          avy
+          bbdb
+          beginend
+          pkgs.bookmark-plus
+          company
+          company-emoji
+          company-go
+          dash
+          delight
+          dired-collapse
+          dired-git-info
+          dired-quick-sort
+          dired-narrow
+          dired-rsync
+          pkgs.dired-plus
+          direnv
+          dockerfile-mode
+          easy-kill
+          esup
+          expand-region
+          flycheck
+          flycheck-golangci-lint
+          git-annex
+          git-commit
+          gitattributes-mode
+          gitconfig-mode
+          gitignore-mode
+          github-review
+          goto-last-change
+          hardhat
+          helpful
+          highlight
+          highlight-indentation
+          highlight-numbers
+          ibuffer-vc
+          iedit
+          json-mode
+          markdown-mode
+          #modus-operandi-theme
+          #modus-vivendi-theme
+          mpdel
+          multiple-cursors
+          nixpkgs-fmt
+          no-littering
+          ob-async
+          ob-go
+          ob-http
+          orgit
+          org-plus-contrib
+          org-capture-pop-frame
+          org-gcal
+          org-ref
+          org-super-agenda
+          org-web-tools
+          ox-pandoc
+          pandoc-mode
+          projectile
+          projectile-ripgrep
+          pdf-tools
+          python-mode
+          rainbow-delimiters
+          rainbow-mode
+          region-bindings-mode
+          ripgrep
+          rg
+          try
+          visual-fill-column
+          visual-regexp
+          web-mode
+          wgrep
+          with-editor
+          xterm-color
+          yaml-mode
+          darkroom
+          eshell-prompt-extras
+          esh-autosuggest
+          forge
+          go-mode
+          magit
+          magit-annex
+          magit-popup
+          minions
+          moody
+          mwim
+          nix-buffer
+          nix-mode
+          org-super-agenda
+          org-tree-slide
+          shr-tag-pre-highlight
+          ssh-config-mode
+          smartparens
+          symbol-overlay
+          undo-tree
+          use-package
+          # Highly experimental
+          vterm
+          gotest
+        ];
+      };
+    }
     (
-      mkMerge [
-        {
-          home.file.".local/share/applications/org-protocol.desktop".source = ./assets/xorg/org-protocol.desktop;
-          home.file.".local/share/applications/ec.desktop".source = ./assets/xorg/ec.desktop;
-          home.file.".local/share/applications/capture.desktop".source = ./assets/xorg/capture.desktop;
-          home.packages = with pkgs; [
-            ditaa
-            graphviz
-            pandoc
-            zip
-            hunspell
-            hunspellDicts.en_US-large
-            hunspellDicts.en_GB-ize
-            hunspellDicts.fr-any
-            nixpkgs-fmt
-          ];
-          home.sessionVariables = {
-            EDITOR = "et";
-            ALTERNATE_EDITOR = "et";
-          };
-          programs.emacs = {
-            enable = true;
-            package = pkgs.emacs27.override { inherit (pkgs) imagemagick; withXwidgets = cfg.withXwidgets; };
-            extraPackages = epkgs: with epkgs; [
-              ace-window
-              aggressive-indent
-              async
-              avy
-              bbdb
-              beginend
-              pkgs.bookmark-plus
-              company
-              company-emoji
-              company-go
-              dash
-              delight
-              dired-collapse
-              dired-git-info
-              dired-quick-sort
-              dired-narrow
-              dired-rsync
-              pkgs.dired-plus
-              direnv
-              dockerfile-mode
-              easy-kill
-              esup
-              expand-region
-              flycheck
-              flycheck-golangci-lint
-              git-annex
-              git-commit
-              gitattributes-mode
-              gitconfig-mode
-              gitignore-mode
-              github-review
-              goto-last-change
-              hardhat
-              helpful
-              highlight
-              highlight-indentation
-              highlight-numbers
-              ibuffer-vc
-              iedit
-              json-mode
-              markdown-mode
-              #modus-operandi-theme
-              #modus-vivendi-theme
-              mpdel
-              multiple-cursors
-              nixpkgs-fmt
-              no-littering
-              ob-async
-              ob-go
-              ob-http
-              orgit
-              org-plus-contrib
-              org-capture-pop-frame
-              org-gcal
-              org-ref
-              org-super-agenda
-              org-web-tools
-              ox-pandoc
-              pandoc-mode
-              projectile
-              projectile-ripgrep
-              pdf-tools
-              python-mode
-              rainbow-delimiters
-              rainbow-mode
-              region-bindings-mode
-              ripgrep
-              rg
-              try
-              visual-fill-column
-              visual-regexp
-              web-mode
-              wgrep
-              with-editor
-              xterm-color
-              yaml-mode
-              darkroom
-              eshell-prompt-extras
-              esh-autosuggest
-              forge
-              go-mode
-              magit
-              magit-annex
-              magit-popup
-              minions
-              moody
-              mwim
-              nix-buffer
-              nix-mode
-              org-super-agenda
-              org-tree-slide
-              shr-tag-pre-highlight
-              ssh-config-mode
-              smartparens
-              symbol-overlay
-              undo-tree
-              use-package
-              # Highly experimental
-              vterm
-              gotest
-            ];
-          };
-        }
-        (
-          mkIf config.profiles.emacs.capture {
-            home.packages = with pkgs; [ wmctrl capture ];
-          }
-        )
-        (
-          mkIf config.services.gpg-agent.enable {
-            #services.gpg-agent.extraConfig = ''
-            #  allow-emacs-pinentry
-            #'';
-          }
-        )
-        (
-          mkIf cfg.texlive {
-            home.packages = with pkgs; [ texlive.combined.scheme-full ];
-          }
-        )
-        (
-          mkIf cfg.daemonService {
-            systemd.user.services.emacs = {
-              Unit = {
-                Description = "Emacs: the extensible, self-documenting text editor";
-              };
-              Service = {
-                Environment = ''
-                  PATH=${config.home.homeDirectory}/.nix-profile/bin:${config.home.homeDirectory}/.local/npm/bin:/run/wrappers/bin:/etc/profiles/per-user/vincent/bin:${config.home.profileDirectory}/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:${config.home.homeDirectory}/bin GOPATH=${config.home.homeDirectory} NIX_PATH=${config.home.homeDirectory}/.nix-defexpr/channels:nixpkgs=/home/vincent/.nix-defexpr/channels/nixpkgs ASPELL_CONF=dict-dir=${config.home.homeDirectory}/.nix-profile/lib/aspell SSH_AUTH_SOCK=/run/user/1000/gnupg/S.gpg-agent.ssh NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
-                '';
-                Type = "forking";
-                ExecStart = "${pkgs.bash}/bin/bash -c 'source /etc/profile; exec ${config.home.homeDirectory}/.nix-profile/bin/emacs --dump-file=${config.home.homeDirectory}/.config/emacs/emacs.pdmp --daemon=org'";
-                ExecStop = "${config.home.homeDirectory}/.nix-profile/bin/emacsclient --eval (kill-emacs)";
-                Restart = "always";
-              };
-              Install = {
-                WantedBy = [ "default.target" ];
-              };
-            };
-          }
-        )
-      ]
-    );
+      mkIf config.profiles.emacs.capture {
+        home.packages = with pkgs; [ wmctrl capture ];
+      }
+    )
+    (
+      mkIf config.services.gpg-agent.enable {
+        #services.gpg-agent.extraConfig = ''
+        #  allow-emacs-pinentry
+        #'';
+      }
+    )
+    (
+      mkIf cfg.texlive {
+        home.packages = with pkgs; [ texlive.combined.scheme-full ];
+      }
+    )
+    (
+      mkIf cfg.daemonService {
+        services.emacs-server = {
+          enable = true;
+          package = myEmacs;
+          shell = pkgs.zsh + "/bin/zsh -i -c";
+          extraOptions = "--dump-file=${config.home.homeDirectory}/.config/emacs/emacs.pdmp --daemon=org";
+        };
+      }
+    )
+  ]);
 }
modules/services/emacs-server.nix
@@ -0,0 +1,50 @@
+{ config, lib, pkgs, ... }:
+with lib;
+let
+  cfg = config.services.emacs-server;
+in
+{
+  options.services.emacs-server = {
+    enable = mkEnableOption "the Emacs daemon";
+    package = mkOption {
+      type = types.package;
+      description = "The Emacs package to use for running the daemon.";
+    };
+    shell = mkOption {
+      type = types.str;
+      description = "The shell used for starting Emacs.";
+    };
+    extraOptions = mkOption {
+      type = types.separatedString " ";
+      default = "";
+      description =
+        ''
+          The extra command-line options to pass to
+          <command>emacs</command> daemon.
+        '';
+    };
+  };
+
+  config = mkIf cfg.enable {
+    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 =
+          "${cfg.shell} 'exec ${cfg.package}/bin/emacs --fg-daemon ${cfg.extraOptions}'";
+        ExecStop = "${cfg.package}/bin/emacsclient --eval '(kill-emacs)'";
+        Restart = "on-failure";
+      };
+
+      Install = { WantedBy = [ "default.target" ]; };
+    };
+  };
+}
modules/module-list.nix
@@ -33,5 +33,6 @@
     ./profiles/zsh.nix
     ./programs/podman.nix
     ./services/shairport-sync.nix
+    ./services/emacs-server.nix
   ];
 }