Home Explore Blog CI



nixpkgs

5th chunk of `nixos/modules/services/editors/emacs.md`
59e44794ce1a71eae0b065ec2447160e7d4768b38b39516f0000000100000c59
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>-->

If [](#opt-services.emacs.defaultEditor) is
`true`, the {var}`EDITOR` variable will be set
to a wrapper script which launches {command}`emacsclient`.

Any setting of {var}`EDITOR` in the shell config files will
override {var}`services.emacs.defaultEditor`. To make sure
{var}`EDITOR` refers to the Emacs wrapper script, remove any
existing {var}`EDITOR` assignment from
{file}`.profile`, {file}`.bashrc`,
{file}`.zshenv` or any other shell config file.

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

### Per-User Enabling of the Service {#module-services-emacs-per-user}

In general, {command}`systemd` user services are globally enabled
by symlinks in {file}`/etc/systemd/user`. In the case where
Emacs daemon is not wanted for all users, it is possible to install the
service but not globally enable it:
```nix
{
  services.emacs.enable = false;
  services.emacs.install = true;
}
```

To enable the {command}`systemd` user service for just the
currently logged in user, run:
```
systemctl --user enable emacs
```
This will add the symlink
{file}`~/.config/systemd/user/emacs.service`.

## Configuring Emacs {#module-services-emacs-configuring}

If you want to only use extension packages from Nixpkgs, you can add
`(setq package-archives nil)` to your init file.

After the declarative Emacs package configuration has been tested,
previously downloaded packages can be cleaned up by removing
{file}`~/.emacs.d/elpa` (do make a backup first, in case you
forgot a package).

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

### A Major Mode for Nix Expressions {#module-services-emacs-major-mode}

Of interest may be {var}`melpaPackages.nix-mode`, which
provides syntax highlighting for the Nix language. This is particularly
convenient if you regularly edit Nix files.

### Accessing man pages {#module-services-emacs-man-pages}

You can use `woman` to get completion of all available
man pages. For example, type `M-x woman <RET> nixos-rebuild <RET>.`


Title: Configuring and Managing the Emacs Systemd Service and Integrating with Nix
Summary
This section details how to configure the Emacs systemd service, including setting the EDITOR variable to use emacsclient. It also describes enabling the service per-user and configuring Emacs, such as using extension packages from Nixpkgs. Additionally, it mentions nix-mode for syntax highlighting of Nix expressions and using woman to access man pages within Emacs.