summary refs log tree commit diff
path: root/nixos/modules/services/editors/emacs.xml
blob: dfab5ce4a79d39747e72de8d052e2cbae14a4985 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<chapter xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:xi="http://www.w3.org/2001/XInclude"
         version="5.0"
         xml:id="module-services-emacs">

  <title>Emacs</title>

  <!--
    Documentation contributors:
      Damien Cassou @DamienCassou
      Thomas Tuegel @ttuegel
      Rodney Lorrimar @rvl
  -->

  <para>
    <link xlink:href="http://www.gnu.org/software/emacs/">Emacs</link>
    is an extensible, customizable, self-documenting real-time display
    editor  and more. At its core is an interpreter for Emacs Lisp, a
    dialect of the Lisp programming language with extensions to
    support text editing.
  </para>

  <para>
    Emacs runs within a graphical desktop environment using the X
    Window System, but works equally well on a text terminal. Under
    <productname>macOS</productname>, a "Mac port" edition is
    available, which uses Apple's native GUI frameworks.
  </para>

  <para>
    <productname>Nixpkgs</productname> provides a superior environment
    for running <application>Emacs</application>. It's simple to
    create custom builds by overriding the default packages. Chaotic
    collections of Emacs Lisp code and extensions can be brought under
    control using declarative package
    management. <productname>NixOS</productname> even provides a
    <command>systemd</command> user service for automatically
    starting the Emacs daemon.
  </para>

  <section>
    <title>Installing <application>Emacs</application></title>

    <para>
      Emacs can be installed in the normal way for Nix (see
      <xref linkend="sec-package-management" />).
      In addition, a NixOS <emphasis>service</emphasis>
      can be enabled.
    </para>

    <section>
      <title>The Different Releases of Emacs</title>

      <para>
        <productname>Nixpkgs</productname> defines several basic Emacs
        packages. The following are attributes belonging to the
        <varname>pkgs</varname> set:

        <variablelist>
          <varlistentry>
            <term><varname>emacs</varname></term>
            <term><varname>emacs25</varname></term>
            <listitem>
              <para>
                The latest stable version of Emacs 25 using the <link
                xlink:href="http://www.gtk.org">GTK+ 2</link> widget
                toolkit.
              </para>
            </listitem>
          </varlistentry>
          <varlistentry>
            <term><varname>emacs25-nox</varname></term>
            <listitem>
              <para>
                Emacs 25 built without any dependency on X11
                libraries.
              </para>
            </listitem>
          </varlistentry>
          <varlistentry>
            <term><varname>emacsMacport</varname></term>
            <term><varname>emacs25Macport</varname></term>
            <listitem>
              <para>
                Emacs 25 with the "Mac port" patches, providing a more
                native look and feel under macOS.
              </para>
            </listitem>
          </varlistentry>
        </variablelist>
      </para>

      <para>
        If those aren't suitable, then the following imitation Emacs
        editors are also available in Nixpkgs:
        <link xlink:href="https://www.gnu.org/software/zile/">Zile</link>,
        <link xlink:href="http://homepage.boetes.org/software/mg/">mg</link>,
        <link xlink:href="http://yi-editor.github.io/">Yi</link>.
      </para>

    </section>
    <section>
      <title>Adding Packages to Emacs</title>
      <para>
        Emacs includes an entire ecosystem of functionality beyond
        text editing, including a project planner, mail and news
        reader, debugger interface, calendar, and more.
      </para>

      <para>
        Most extensions are gotten with the Emacs packaging system
        (<filename>package.el</filename>) from <link
        xlink:href="https://elpa.gnu.org/">Emacs Lisp Package Archive
        (<acronym>ELPA</acronym>)</link>,
        <link xlink:href="https://melpa.org/"><acronym>MELPA</acronym></link>,
        <link xlink:href="https://stable.melpa.org/">MELPA Stable</link>,
        and <link xlink:href="http://orgmode.org/elpa.html">Org ELPA</link>.
        Nixpkgs is regularly updated to mirror all these archives.
      </para>

      <para>
        Under NixOS, you can continue to use
        <function>package-list-packages</function> and
        <function>package-install</function> to install packages. You
        can also declare the set of Emacs packages you need using the
        derivations from Nixpkgs. The rest of this section discusses
        declarative installation of Emacs packages through nixpkgs.
      </para>

      <note>
        <para>
          This documentation describes the new Emacs packages
          framework in NixOS 16.03
          (<varname>emacsPackagesNg</varname>) which should not be
          confused with the previous and deprecated framework
          (<varname>emacs24Packages</varname>).
        </para>
      </note>

      <para>
        The first step to declare the list of packages you want in
        your Emacs installation is to create a dedicated
        derivation. This can be done in a dedicated
        <filename>emacs.nix</filename> file such as:

      <example xml:id="ex-emacsNix">
        <title>Nix expression to build Emacs with packages (<filename>emacs.nix</filename>)</title>
        <programlisting language="nix">
/*
This is a nix expression to build Emacs and some Emacs packages I like
from source on any distribution where Nix is installed. This will install
all the dependencies from the nixpkgs repository and build the binary files
without interfering with the host distribution.

To build the project, type the following from the current directory:

$ nix-build emacs.nix

To run the newly compiled executable:

$ ./result/bin/emacs
*/
{ pkgs ? import &lt;nixpkgs&gt; {} }: <co xml:id="ex-emacsNix-1" />

let
  myEmacs = pkgs.emacs; <co xml:id="ex-emacsNix-2" />
  emacsWithPackages = (pkgs.emacsPackagesNgGen myEmacs).emacsWithPackages; <co xml:id="ex-emacsNix-3" />
in
  emacsWithPackages (epkgs: (with epkgs.melpaStablePackages; [ <co xml:id="ex-emacsNix-4" />
    magit          # ; Integrate git &lt;C-x g&gt;
    zerodark-theme # ; Nicolas' theme
  ]) ++ (with epkgs.melpaPackages; [ <co xml:id="ex-emacsNix-5" />
    undo-tree      # ; &lt;C-x u&gt; to show the undo tree
    zoom-frm       # ; increase/decrease font size for all buffers %lt;C-x C-+&gt;
  ]) ++ (with epkgs.elpaPackages; [ <co xml:id="ex-emacsNix-6" />
    auctex         # ; LaTeX mode
    beacon         # ; highlight my cursor when scrolling
    nameless       # ; hide current package name everywhere in elisp code
  ]) ++ [
    pkgs.notmuch   # From main packages set <co xml:id="ex-emacsNix-7" />
  ])
</programlisting>
      </example>

      <calloutlist>
        <callout arearefs="ex-emacsNix-1">
          <para>
            The first non-comment line in this file
            (<literal>{ pkgs ? ... }</literal>)
            indicates that the whole file represents a function.
          </para>
        </callout>

        <callout arearefs="ex-emacsNix-2">
          <para>
            The <varname>let</varname> expression below defines a
            <varname>myEmacs</varname> binding pointing to the current
            stable version of Emacs. This binding is here to separate the
            choice of the Emacs binary from the specification of the
            required packages.
          </para>
        </callout>

        <callout arearefs="ex-emacsNix-3">
          <para>
            This generates an <varname>emacsWithPackages</varname>
            function. It takes a single argument: a function from a
            package set to a list of packages (the packages that will
            be available in Emacs).
          </para>
        </callout>

        <callout arearefs="ex-emacsNix-4">
          <para>
            The rest of the file specifies the list of packages to
            install. In the example, two packages
            (<varname>magit</varname> and
            <varname>zerodark-theme</varname>) are taken from MELPA
            stable.
          </para>
        </callout>

        <callout arearefs="ex-emacsNix-5">
          <para>
            Two packages (<varname>undo-tree</varname> and
            <varname>zoom-frm</varname>) are taken from MELPA.
          </para>
        </callout>

        <callout arearefs="ex-emacsNix-6">
          <para>Three packages are taken from GNU ELPA.</para>
        </callout>

        <callout arearefs="ex-emacsNix-7">
          <para>
            <varname>notmuch</varname> is taken from a nixpkgs derivation
            which contains an Emacs mode.
          </para>
        </callout>

      </calloutlist>
      </para>

      <para>
        The result of this configuration will be an
        <command>emacs</command> command which launches Emacs with all
        of your chosen packages in the <varname>load-path</varname>.
      </para>

      <para>
        You can check that it works by executing this in a terminal:

<screen>
$ nix-build emacs.nix
$ ./result/bin/emacs -q
</screen>

        and then typing <literal>M-x package-initialize</literal>.
        Check that you can use all the packages you want in this
        Emacs instance. For example, try switching to the zerodark
        theme through
        <literal>M-x load-theme &lt;RET&gt; zerodark &lt;RET&gt; y</literal>.
      </para>

      <tip>
        <para>
          A few popular extensions worth checking out are: auctex,
          company, edit-server, flycheck, helm, iedit, magit,
          multiple-cursors, projectile, and yasnippet.
        </para>
      </tip>

      <para>
        The list of available packages in the various ELPA
        repositories can be seen with the following commands:
        <example>
          <title>Querying Emacs packages</title>
          <programlisting><![CDATA[
nix-env -f "<nixpkgs>" -qaP -A emacsPackagesNg.elpaPackages
nix-env -f "<nixpkgs>" -qaP -A emacsPackagesNg.melpaPackages
nix-env -f "<nixpkgs>" -qaP -A emacsPackagesNg.melpaStablePackages
nix-env -f "<nixpkgs>" -qaP -A emacsPackagesNg.orgPackages
]]></programlisting>
        </example>
      </para>

      <para>
        If you are on NixOS, you can install this particular Emacs for
        all users by adding it to the list of system packages
        (see <xref linkend="sec-declarative-package-mgmt" />). Simply
        modify your file <filename>configuration.nix</filename> to
        make it contain:
        <example>
          <title>Custom Emacs in <filename>configuration.nix</filename></title>
          <programlisting><![CDATA[
{
 environment.systemPackages = [
   # [...]
   (import /path/to/emacs.nix { inherit pkgs; })
  ];
}
]]></programlisting>
        </example>
      </para>

      <para>
        In this case, the next <command>nixos-rebuild switch</command>
        will take care of adding your <command>emacs</command> to the
        <varname>PATH</varname> environment variable
        (see <xref linkend="sec-changing-config" />).
      </para>

<!-- fixme: i think the following is better done with config.nix
https://nixos.org/nixpkgs/manual/#sec-modify-via-packageOverrides
-->
      <para>
        If you are not on NixOS or want to install this particular
        Emacs only for yourself, you can do so by adding it to your
        <filename>~/.config/nixpkgs/config.nix</filename>
        (see <link xlink:href="http://nixos.org/nixpkgs/manual/#sec-modify-via-packageOverrides">Nixpkgs manual</link>):
        <example>
          <title>Custom Emacs in <filename>~/.config/nixpkgs/config.nix</filename></title>
          <programlisting><![CDATA[
{
  packageOverrides = super: let self = super.pkgs; in {
    myemacs = import /path/to/emacs.nix { pkgs = self; };
  };
}
]]></programlisting>
        </example>
      </para>

      <para>
        In this case, the next
        <literal>nix-env -f '&lt;nixpkgs&gt;' -iA myemacs</literal>
        will take care of adding your emacs to the
        <varname>PATH</varname> environment variable.
      </para>
    </section>

    <section>
      <title>Advanced Emacs Configuration</title>

      <para>
        If you want, you can tweak the Emacs package itself from your
        <filename>emacs.nix</filename>. For example, if you want to
        have a GTK+3-based Emacs instead of the default GTK+2-based
        binary and remove the automatically generated
        <filename>emacs.desktop</filename> (useful is you only use
        <command>emacsclient</command>), you can change your file
        <filename>emacs.nix</filename> in this way:
      </para>

      <example xml:id="ex-emacsGtk3Nix">
        <title>Custom Emacs build</title>
        <programlisting><![CDATA[
{ pkgs ? import <nixpkgs> {} }:
let
  myEmacs = (pkgs.emacs.override {
    # Use gtk3 instead of the default gtk2
    withGTK3 = true;
    withGTK2 = false;
  }).overrideAttrs (attrs: {
    # I don't want emacs.desktop file because I only use
    # emacsclient.
    postInstall = (attrs.postInstall or "") + ''
      rm $out/share/applications/emacs.desktop
    '';
  });
in [...]
]]></programlisting>
      </example>

      <para>
        After building this file as shown in <xref linkend="ex-emacsNix" />,
        you will get an GTK3-based Emacs binary pre-loaded with your
        favorite packages.
      </para>
    </section>
  </section>

<section>
  <title>Running Emacs as a Service</title>
  <para>
    <productname>NixOS</productname> provides an optional
    <command>systemd</command> service which launches
    <link xlink:href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html">
      Emacs daemon
    </link>
    with the user's login session.
  </para>

  <para>
    <emphasis>Source:</emphasis>
    <filename>modules/services/editors/emacs.nix</filename>
  </para>

  <section>
    <title>Enabling the Service</title>

    <para>
      To install and enable the <command>systemd</command>
      user service for Emacs daemon, add the following to your
      <filename>configuration.nix</filename>:

<programlisting>
<xref linkend="opt-services.emacs.enable"/> = true;
<xref linkend="opt-services.emacs.package"/> = import /home/cassou/.emacs.d { pkgs = pkgs; };
</programlisting>
    </para>

    <para>
      The <varname>services.emacs.package</varname> option allows a
      custom derivation to be used, for example, one created by
      <function>emacsWithPackages</function>.
    </para>

    <para>
      Ensure that the Emacs server is enabled for your user's Emacs
      configuration, either by customizing the
      <varname>server-mode</varname> variable, or by adding
      <literal>(server-start)</literal> to
      <filename>~/.emacs.d/init.el</filename>.
    </para>

    <para>
      To start the daemon, execute the following:

<screen>
$ nixos-rebuild switch  # to activate the new configuration.nix
$ systemctl --user daemon-reload        # to force systemd reload
$ systemctl --user start emacs.service  # to start the Emacs daemon
</screen>

      The server should now be ready to serve Emacs clients.
    </para>

  </section>

  <section>
    <title>Starting the client</title>
    <para>
      Ensure that the emacs server is enabled, either by customizing
      the <varname>server-mode</varname> variable, or by adding
      <literal>(server-start)</literal> to
      <filename>~/.emacs</filename>.
    </para>

    <para>
      To connect to the emacs daemon, run one of the following:
      <programlisting><![CDATA[
emacsclient FILENAME
emacsclient --create-frame  # opens a new frame (window)
emacsclient --create-frame --tty  # opens a new frame on the current terminal
]]></programlisting>
    </para>
  </section>

  <section>
    <title>Configuring the <varname>EDITOR</varname> variable</title>
    <!--<title><command>emacsclient</command> as the Default Editor</title>-->

    <para>
      If <xref linkend="opt-services.emacs.defaultEditor"/> is
      <literal>true</literal>, the <varname>EDITOR</varname> variable
      will be set to a wrapper script which launches
      <command>emacsclient</command>.
    </para>

    <para>
      Any setting of <varname>EDITOR</varname> in the shell config
      files will override
      <varname>services.emacs.defaultEditor</varname>.
      To make sure <varname>EDITOR</varname> refers to the Emacs
      wrapper script, remove any existing <varname>EDITOR</varname>
      assignment from <filename>.profile</filename>,
      <filename>.bashrc</filename>, <filename>.zshenv</filename> or
      any other shell config file.
    </para>

    <para>
      If you have formed certain bad habits when editing files,
      these can be corrected with a shell alias to the wrapper
      script:
      <programlisting>alias vi=$EDITOR</programlisting>
    </para>
  </section>

  <section>
    <title>Per-User Enabling of the Service</title>

    <para>
      In general, <command>systemd</command> user services
      are globally enabled by symlinks in
      <filename>/etc/systemd/user</filename>. In the case where
      Emacs daemon is not wanted for all users, it is possible to
      install the service but not globally enable it:

<programlisting>
<xref linkend="opt-services.emacs.enable"/> = false;
<xref linkend="opt-services.emacs.install"/> = true;
</programlisting>
    </para>

    <para>
      To enable the <command>systemd</command> user service for just
      the currently logged in user, run:

      <programlisting>systemctl --user enable emacs</programlisting>

      This will add the symlink
      <filename>~/.config/systemd/user/emacs.service</filename>.
    </para>
  </section>
</section>

<section>
  <title>Configuring Emacs</title>

  <para>
    The Emacs init file should be changed to load the extension
    packages at startup:

    <example>
      <title>Package initialization in <filename>.emacs</filename></title>
      <programlisting><![CDATA[
(require 'package)

;; optional. makes unpure packages archives unavailable
(setq package-archives nil)

(setq package-enable-at-startup nil)
(package-initialize)
]]></programlisting>
    </example>
  </para>

  <para>
    After the declarative emacs package configuration has been
    tested, previously downloaded packages can be cleaned up by
    removing <filename>~/.emacs.d/elpa</filename> (do make a backup
    first, in case you forgot a package).
  </para>

  <!--
      todo: is it worth documenting customizations for
      server-switch-hook, server-done-hook?
  -->

  <section>
    <title>A Major Mode for Nix Expressions</title>

    <para>
      Of interest may be <varname>melpaPackages.nix-mode</varname>,
      which provides syntax highlighting for the Nix language. This is
      particularly convenient if you regularly edit Nix files.
    </para>
  </section>

  <section>
    <title>Accessing man pages</title>
    <para>
      You can use <function>woman</function> to get completion of all
      available man pages. For example, type <literal>M-x woman
      &lt;RET&gt; nixos-rebuild &lt;RET&gt;.</literal>
    </para>
  </section>

  <section xml:id="sec-emacs-docbook-xml">
    <title>Editing DocBook 5 XML Documents</title>
    <para>
      Emacs includes <link
      xlink:href="https://www.gnu.org/software/emacs/manual/html_node/nxml-mode/Introduction.html">nXML</link>,
      a major-mode for validating and editing XML documents.
      When editing DocBook 5.0 documents, such as
      <link linkend="book-nixos-manual">this one</link>,
      nXML needs to be configured with the relevant schema, which is
      not included.
    </para>

    <para>
      To install the DocBook 5.0 schemas, either add
      <varname>pkgs.docbook5</varname> to
      <xref linkend="opt-environment.systemPackages"/> (<link
      linkend="sec-declarative-package-mgmt">NixOS</link>), or run
      <literal>nix-env -i pkgs.docbook5</literal>
      (<link linkend="sec-ad-hoc-packages">Nix</link>).
    </para>

    <para>
      Then customize the variable <varname>rng-schema-locating-files</varname> to include <filename>~/.emacs.d/schemas.xml</filename> and put the following text into that file:
      <example xml:id="ex-emacs-docbook-xml">
        <title>nXML Schema Configuration (<filename>~/.emacs.d/schemas.xml</filename>)</title>
        <programlisting language="xml"><![CDATA[
<?xml version="1.0"?>
<!--
  To let emacs find this file, evaluate:
  (add-to-list 'rng-schema-locating-files "~/.emacs.d/schemas.xml")
-->
<locatingRules xmlns="http://thaiopensource.com/ns/locating-rules/1.0">
  <!--
    Use this variation if pkgs.docbook5 is added to environment.systemPackages
  -->
  <namespace ns="http://docbook.org/ns/docbook"
             uri="/run/current-system/sw/share/xml/docbook-5.0/rng/docbookxi.rnc"/>
  <!--
    Use this variation if installing schema with "nix-env -iA pkgs.docbook5".
  <namespace ns="http://docbook.org/ns/docbook"
             uri="../.nix-profile/share/xml/docbook-5.0/rng/docbookxi.rnc"/>
  -->
</locatingRules>
]]></programlisting>
    </example>
  </para>

  </section>
</section>

</chapter>