about summary refs log tree commit diff
path: root/modules/common/emacs/default.nix
blob: 6d1b549ae882e3a564304759b1f2b6df227e21ea (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{
  config,
  inputs,
  lib,
  localUsername ? lib.my.username,
  pkgs,
  this,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.emacs;
in {
  options.nixfiles.modules.emacs.enable = mkEnableOption "GNU Emacs";

  config = mkIf cfg.enable {
    secrets.authinfo = {
      file = "${inputs.self}/secrets/authinfo";
      owner = localUsername;
    };

    nixfiles.modules = {
      fonts.enable = true;
      git.client.enable = true;
      gnupg.enable = true;
      password-store.enable = true;
      profiles.dev.enable = true;
      profiles.email.enable = true;
    };

    hm = {
      xdg.configFile = mapAttrs (_: value:
        value
        // {
          onChange = with config.hm.programs; ''
            export EMACSDIR="''${XDG_CONFIG_HOME:-$HOME/.config}/emacs"
            export DOOMDIR="''${XDG_CONFIG_HOME:-$HOME/.config}/doom"

            if [[ ! -d "$EMACSDIR/.git" ]]; then
              ${getExe git.package} clone --depth=1 --branch=master \
                "https://github.com/doomemacs/doomemacs" "$EMACSDIR"
            fi

            if [[ ! -d "$DOOMDIR/snippets" ]]; then
              mkdir -p "$DOOMDIR/snippets"
            fi

            if [[ -x "$EMACSDIR/bin/doom" ]]; then
              if [[ ! -d "$EMACSDIR/.local" ]]; then
                PATH="''${PATH:-/bin:/usr/bin:/usr/local/bin}:${emacs.package}/bin:${git.package}/bin" \
                  "$EMACSDIR/bin/doom" install --force --verbose
              fi

              PATH="''${PATH:-/bin:/usr/bin:/usr/local/bin}:${emacs.package}/bin:${git.package}/bin" \
                "$EMACSDIR/bin/doom" sync -e -p --force --verbose
            fi
          '';
        }) {
        "doom/init.el".source = ./doom/init.el;
        "doom/packages.el".source = ./doom/packages.el;
        "doom/config.el" = {
          text = concatLines [
            (
              let
                extraBins = with pkgs;
                  [
                    (aspellWithDicts (p: with p; [en ru])) # :checkers (spell +aspell)
                    # wordnet # :tools (lookup +dictionary +offline)
                    asmfmt # :editor format
                    cargo # :lang rust
                    clang-tools # :lang (cc +lsp) :editor format
                    cmake-format # :lang cc :editor format
                    cmigemo # :lang japanese
                    config.hm.programs.emacs.package # !doom
                    config.nix.package # !doom
                    dockerfile-language-server-nodejs # :tools (docker +lsp)
                    dockfmt # :tools docker :editor format
                    editorconfig-core-c # :tools editorconfig
                    fd # doom!
                    gcc # :lang cc
                    ghc # :lang haskell
                    gnuplot # :lang (org +gnuplot)
                    gnutar # :tools tree-sitter
                    gnutls # doom! :app irc
                    go # :lang go
                    gocode # :lang go org
                    godef # :lang go
                    gomodifytags # :lang go
                    gopls # :lang (go +lsp)
                    gore # :lang go
                    gotests # :lang go
                    gotools # :lang go
                    graphviz # :lang (org +roam2) :lang plantuml
                    gzip # :tools tree-sitter
                    haskellPackages.cabal-fmt # :lang haskell :editor format
                    haskellPackages.cabal-install # :lang haskell
                    haskellPackages.haskell-language-server # :lang (haskell +lsp)
                    haskellPackages.hoogle # :lang haskell
                    haskellPackages.ormolu # :lang haskell :editor format
                    html-tidy # :lang web :editor format
                    jdk # :lang java :lang plantuml :checkers grammar
                    languagetool # :checkers grammar
                    libxml2 # :lang data :editor format
                    markdownlint-cli # :lang markdown
                    nil # :lang (nix +lsp)
                    nodePackages.bash-language-server # :lang (sh +lsp)
                    nodePackages.eslint # :lang (json +lsp)
                    nodePackages.js-beautify # :lang web
                    nodePackages.prettier # :editor format
                    nodePackages.stylelint # :lang web
                    nodePackages.vscode-css-languageserver-bin # lang (web +lsp)
                    nodePackages.vscode-html-languageserver-bin # lang (web +lsp)
                    nodePackages.vscode-json-languageserver-bin # lang (json +lsp)
                    nodejs # :tools debugger
                    pandoc # :lang org markdown latex
                    pinentry-emacs # doom!
                    pipenv # :lang python
                    pre-commit # :tools magit
                    python3 # :lang python
                    python3Packages.black # :lang python :editor format
                    python3Packages.isort # :lang python :editor format
                    python3Packages.nose # :lang python
                    python3Packages.pyflakes # :lang python :editor format
                    python3Packages.pytest # :lang python
                    python3Packages.python-lsp-server # :lang python :editor format
                    ripgrep # doom!
                    rust-analyzer # :lang (rust +lsp)
                    rustc # :lang rust
                    rustfmt # :lang rust
                    shellcheck # :lang sh
                    shfmt # :lang sh :editor format
                    sqlite # :lang (org +roam2) :tools lookup
                    terraform-ls # :tools (terraform +lsp)
                    texlab # lang (tex +lsp)
                    texlive.combined.scheme-full # :lang org tex
                    unzip # :tools debugger
                    yaml-language-server # :lang (yaml +lsp)
                    zig # :lang zig :editor format
                    zls # :lang (zig +lsp)
                    zstd # :emacs undo
                  ]
                  ++ (
                    # GDB doesn't support[1] Apple Silicon.
                    #
                    # [1]: https://inbox.sourceware.org/gdb/6b48224b-9e2e-518d-793b-df4fc5514884@arm.com/
                    if (this.system != "aarch64-darwin")
                    then [gdb] # :tools debugger
                    else [lldb] # :tools debugger
                  );

                parinferRustLibrary =
                  if (hasSuffix "linux" this.system)
                  then "${pkgs.parinfer-rust}/lib/libparinfer_rust.so"
                  else "${pkgs.parinfer-rust}/lib/libparinfer_rust.dylib";
              in ''
                ;; Integrate packages which are required by various modules
                ;; without polluting the user's profile.
                (setq exec-path (append exec-path '(${
                  concatMapStringsSep " " (x: ''"${x}/bin"'') extraBins
                })))
                (setenv "PATH" (concat (getenv "PATH") ":${
                  concatMapStringsSep ":" (x: "${x}/bin") extraBins
                }"))

                ;; HACK Explicitly load specific Emacs packages from Nixpkgs.
                ;; For some reason providing them as "extraPackages" doesn't
                ;; work.
                (add-to-list 'load-path "${pkgs.mu.mu4e}/share/emacs/site-lisp/mu4e")
                (add-to-list 'load-path "${pkgs.emacsPackages.vterm}/share/emacs/site-lisp/elpa/vterm-${pkgs.emacsPackages.vterm.version}")

                (appendq! auth-sources '(("${config.secrets.authinfo.path}")))

                ;; The font must be set to n+2, otherwise it looks too small.
                (setq doom-font (font-spec
                                  :family "${config.fontScheme.monospaceFont.family}"
                                  :size ${toString (config.fontScheme.monospaceFont.size + 2)})
                      doom-unicode-font doom-font)

                ;; :input japanese
                (setq migemo-dictionary "${pkgs.cmigemo}/share/migemo/utf-8/migemo-dict"
                      skk-large-jisyo "${pkgs.skk-dicts}/share/SKK-JISYO.L")

                ;; :editor parinfer
                (setq parinfer-rust-auto-download nil
                      parinfer-rust-library "${parinferRustLibrary}")

                ;; :lang nix
                ;; HACK Trick `nix-mode' to use alejandra instead of nixfmt.
                (setq nix-nixfmt-bin "${pkgs.writeShellScript "nixfmt" ''
                  ${getExe pkgs.alejandra} "$@"
                ''}")

                ;; :lang (org +roam2) :email mu4e
                (setq emacsql-sqlite-executable "${getExe pkgs.emacsql-sqlite}")

                ;; :lang plantuml
                (setq plantuml-jar-path "${pkgs.plantuml}/lib/plantuml.jar"
                      plantuml-executable-path "${getExe' pkgs.plantuml "plantuml"}"
                      org-plantuml-jar-path plantuml-jar-path
                      org-plantuml-executable-path plantuml-executable-path)


                ;; :app irc
                (setq circe-default-nick "${my.username}"
                      circe-default-realname "${my.email}"
                      circe-default-user circe-default-nick)
              ''
            )
            (with config.hm.accounts.email; let
              mu4eAccounts = let
                muAccounts = filter (a: a.mu.enable) (attrValues accounts);
              in
                concatMapStringsSep "\n"
                (a:
                  with a; let
                    personalAddresses = concatMapStringsSep " " (v: ''"${v}"'') aliases;
                  in ''
                    (set-email-account! "${name}"
                      '((user-full-name           . "${realName}")
                        (user-mail-address        . "${address}")
                        (mu4e-inbox-folder        . "/${name}/${folders.inbox}")
                        (mu4e-sent-folder         . "/${name}/${folders.sent}")
                        (mu4e-drafts-folder       . "/${name}/${folders.drafts}")
                        (mu4e-trash-folder        . "/${name}/${folders.trash}")
                        (mu4e-refile-folder       . "/${name}/Archive")
                        (+mu4e-personal-addresses . (${personalAddresses})))
                      t)
                  '')
                muAccounts;
            in ''
              (setq mu4e-root-maildir "${maildirBasePath}")

              ${mu4eAccounts}
            '')
            (builtins.readFile ./doom/config.el)
          ];
        };
      };

      programs = {
        emacs = {
          enable = true;
          package = pkgs.emacs29;
        };

        bash.initExtra = mkAfter ''
          export PATH="$PATH:$XDG_CONFIG_HOME/emacs/bin"

          # https://github.com/akermu/emacs-libvterm
          if [[ "$INSIDE_EMACS" = vterm ]] && [[ -n "$EMACS_VTERM_PATH" ]] && [[ -f "$EMACS_VTERM_PATH/etc/emacs-vterm-bash.sh" ]]; then
              source "$EMACS_VTERM_PATH/etc/emacs-vterm-bash.sh"

              message() {
                  vterm_cmd message "%s" "$*"
              }

              find-file() {
                  vterm_cmd find-file "$(realpath "''${@:-.}")"
              }
          fi

          # Not sourced from inside Emacs for some reason. Maybe it's not
          # considered an interactive shell?
          [[ -f ~/.profile ]] && . ~/.profile
        '';
      };
    };
  };
}

Consider giving Nix/NixOS a try! <3