Home Explore Blog CI



nixpkgs

1st chunk of `doc/packages/emacs.section.md`
79e8a5f5f3c1975e41a6cdb9cf020dd8e024b2d26d2aaeaf00000001000008b3
# Emacs {#sec-emacs}

## Configuring Emacs {#sec-emacs-config}

The Emacs package comes with some extra helpers to make it easier to configure. `emacs.pkgs.withPackages` allows you to manage packages from ELPA. This means that you will not have to install that packages from within Emacs. For instance, if you wanted to use `company` `counsel`, `flycheck`, `ivy`, `magit`, `projectile`, and `use-package` you could use this as a `~/.config/nixpkgs/config.nix` override:

```nix
{
  packageOverrides =
    pkgs: with pkgs; {
      myEmacs = emacs.pkgs.withPackages (
        epkgs:
        (with epkgs.melpaStablePackages; [
          company
          counsel
          flycheck
          ivy
          magit
          projectile
          use-package
        ])
      );
    };
}
```

You can install it like any other packages via `nix-env -iA myEmacs`. However, this will only install those packages. It will not `configure` them for us. To do this, we need to provide a configuration file. Luckily, it is possible to do this from within Nix! By modifying the above example, we can make Emacs load a custom config file. The key is to create a package that provides a `default.el` file in `/share/emacs/site-start/`. Emacs knows to load this file automatically when it starts.

```nix
{
  packageOverrides = pkgs: {
    myEmacsConfig = pkgs.writeText "default.el" ''
      (eval-when-compile
        (require 'use-package))

      ;; load some packages

      (use-package company
        :bind ("<C-tab>" . company-complete)
        :diminish company-mode
        :commands (company-mode global-company-mode)
        :defer 1
        :config
        (global-company-mode))

      (use-package counsel
        :commands (counsel-descbinds)
        :bind (([remap execute-extended-command] . counsel-M-x)
               ("C-x C-f" . counsel-find-file)
               ("C-c g" . counsel-git)
               ("C-c j" . counsel-git-grep)
               ("C-c k" . counsel-ag)
               ("C-x l" . counsel-locate)
               ("M-y" . counsel-yank-pop)))

      (use-package flycheck
        :defer 2
        :config (global-flycheck-mode))

      (use-package ivy
        :defer 1
        :bind (("C-c C-r" . ivy-resume)

Title: Configuring Emacs with Nix
Summary
The Emacs package in Nix provides helpers for easier configuration, such as `emacs.pkgs.withPackages` for managing ELPA packages. A `~/.config/nixpkgs/config.nix` override example shows how to install packages like `company`, `counsel`, `flycheck`, `ivy`, `magit`, `projectile`, and `use-package` with Nix. Furthermore, a custom configuration file can be loaded by creating a package with a `default.el` file in `/share/emacs/site-start/`, which Emacs automatically loads on startup. An example demonstrates how to write the `default.el` file using `pkgs.writeText` and configure packages using `use-package`.