Home Explore Blog Models CI



nixpkgs

4th chunk of `nixos/modules/services/editors/emacs.md`
42bd99050c2b2c436807d4c33243eb68edf52b85d8a654c20000000100000889
    }).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
[
  # ...
]
```
:::

After building this file as shown in [](#ex-emacsNix), you
will get an GTK 3-based Emacs binary pre-loaded with your favorite packages.

## Running Emacs as a Service {#module-services-emacs-running}

NixOS provides an optional
{command}`systemd` service which launches
[Emacs daemon](https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html)
with the user's login session.

*Source:* {file}`modules/services/editors/emacs.nix`

### Enabling the Service {#module-services-emacs-enabling}

To install and enable the {command}`systemd` user service for Emacs
daemon, add the following to your {file}`configuration.nix`:
```nix
{ services.emacs.enable = true; }
```

The {var}`services.emacs.package` option allows a custom
derivation to be used, for example, one created by
`emacsWithPackages`.

Ensure that the Emacs server is enabled for your user's Emacs
configuration, either by customizing the {var}`server-mode`
variable, or by adding `(server-start)` to
{file}`~/.emacs.d/init.el`.

To start the daemon, execute the following:
```ShellSession
$ 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
```
The server should now be ready to serve Emacs clients.

### Starting the client {#module-services-emacs-starting-client}

Ensure that the Emacs server is enabled, either by customizing the
{var}`server-mode` variable, or by adding
`(server-start)` to {file}`~/.emacs`.

To connect to the Emacs daemon, run one of the following:
```
emacsclient FILENAME
emacsclient --create-frame  # opens a new frame (window)
emacsclient --create-frame --tty  # opens a new frame on the current terminal
```

### Configuring the {var}`EDITOR` variable {#module-services-emacs-editor-variable}

<!--<title>{command}`emacsclient` as the Default Editor</title>-->

Title: Running Emacs as a `systemd` Service and Client on NixOS
Summary
This chunk details how to configure and run Emacs as a daemon using a `systemd` user service on NixOS. It covers enabling the service in `configuration.nix`, using custom Emacs derivations, and ensuring the Emacs server is enabled within the user's Emacs configuration. Instructions are provided for activating and starting the Emacs daemon, as well as connecting to it using `emacsclient` for various use cases (e.g., opening files, creating new frames). The chunk concludes by introducing a section on configuring the `EDITOR` variable.