Home Explore Blog Models CI



nixpkgs

3rd chunk of `nixos/modules/services/editors/emacs.md`
a632ea05bb270b83ab2ed05285bb5c0c9773f5dc0f698c590000000100000cfe
nix-env -f "<nixpkgs>" -qaP -A emacs.pkgs.melpaPackages
nix-env -f "<nixpkgs>" -qaP -A emacs.pkgs.melpaStablePackages
nix-env -f "<nixpkgs>" -qaP -A emacs.pkgs.orgPackages
```
:::

If you are on NixOS, you can install this particular Emacs for all users by
putting the `emacs.nix` file in `/etc/nixos` and adding it to the list of
system packages (see [](#sec-declarative-package-mgmt)). Simply modify your
file {file}`configuration.nix` to make it contain:
::: {.example #module-services-emacs-configuration-nix}
### Custom Emacs in `configuration.nix`

```nix
{
  environment.systemPackages = [
    # [...]
    (import ./emacs.nix { inherit pkgs; })
  ];
}
```
:::

In this case, the next {command}`nixos-rebuild switch` will take
care of adding your {command}`emacs` to the {var}`PATH`
environment variable (see [](#sec-changing-config)).

<!-- fixme: i think the following is better done with config.nix
https://nixos.org/nixpkgs/manual/#sec-modify-via-packageOverrides
-->

If you are not on NixOS or want to install this particular Emacs only for
yourself, you can do so by putting `emacs.nix` in `~/.config/nixpkgs` and
adding it to your {file}`~/.config/nixpkgs/config.nix` (see
[Nixpkgs manual](https://nixos.org/nixpkgs/manual/#sec-modify-via-packageOverrides)):
::: {.example #module-services-emacs-config-nix}
### Custom Emacs in `~/.config/nixpkgs/config.nix`

```nix
{
  packageOverrides =
    super:
    let
      self = super.pkgs;
    in
    {
      myemacs = import ./emacs.nix { pkgs = self; };
    };
}
```
:::

In this case, the next `nix-env -f '<nixpkgs>' -iA
myemacs` will take care of adding your emacs to the
{var}`PATH` environment variable.

### Advanced Emacs Configuration {#module-services-emacs-advanced}

If you want, you can tweak the Emacs package itself from your
{file}`emacs.nix`. 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 {file}`emacs.desktop` (useful if you
only use {command}`emacsclient`), you can change your file
{file}`emacs.nix` in this way:

::: {.example #ex-emacsGtk3Nix}
### Custom Emacs build

```nix
{
  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
[
  # ...
]
```
:::

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

Title: Custom Emacs Installation and Service Management with Nix and NixOS
Summary
This chunk details various methods for installing a custom Emacs build (defined in `emacs.nix`) on NixOS for all users via `configuration.nix`, or for individual users by modifying `~/.config/nixpkgs/config.nix`. It further explains how to apply advanced customizations to the Emacs package itself, such as overriding the GTK version or modifying post-installation steps. Finally, it introduces how to enable and configure Emacs to run as a `systemd` user service on NixOS.