Home Explore Blog CI



nixpkgs

3rd chunk of `nixos/modules/services/editors/emacs.md`
8a2939acd42af6b5a5a2e47f4fc452945cbd254c6b905f7a0000000100000cea
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
derivation to be used, for example, one created by

Title: Installing and Configuring Custom Emacs on NixOS and Non-NixOS Systems
Summary
This section details how to install a custom-built Emacs with specific packages on NixOS by integrating the `emacs.nix` file into `/etc/nixos/configuration.nix`. It also explains how to achieve the same on non-NixOS systems or for user-specific installations by modifying `~/.config/nixpkgs/config.nix`. Additionally, it demonstrates how to tweak the Emacs package itself, such as enabling GTK 3 support. Finally, it discusses how to run Emacs as a systemd service on NixOS, enabling the Emacs daemon for a user's login session.