Home Explore Blog Models CI



nixpkgs

1st chunk of `doc/packages/emacs.section.md`
106bfecea5e967d00fc95f3fbec2695a306721d7893b6c1400000001000008b5
# 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 those 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 package 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 Packages and Settings with Nix
Summary
This section explains how to configure Emacs using Nix, focusing on managing Emacs Lisp packages (like those from ELPA) and custom configurations. It details using `emacs.pkgs.withPackages` in a `~/.config/nixpkgs/config.nix` override to install packages such as `company`, `counsel`, `flycheck`, `ivy`, `magit`, `projectile`, and `use-package`. Furthermore, it shows how to apply custom Emacs configurations by creating a Nix package that provides a `default.el` file in `/share/emacs/site-start/`, which Emacs automatically loads on startup. An example `default.el` snippet is provided, demonstrating basic configurations for `company`, `counsel`, and `flycheck` using `use-package`.