Emacs integration

pedantix ships an Emacs package, pedantix.el, that formats Nix buffers by calling pedantix. It provides two main commands plus a format-on-save minor mode.

The package requires Emacs 27.1 or newer.

Commands and customization

Commands

CommandDescription
M-x pedantix-format-bufferFormat the current buffer with pedantix.
M-x pedantix-format-on-save-modeRun ‘pedantix-format-buffer’ before saving the buffer.
M-x pedantix-format-regionFormat the region from BEG to END with pedantix.

Customization

VariableDefaultDescription
pedantix-argumentsnilExtra command line arguments passed to pedantix.
pedantix-program"pedantix"Name or path of the pedantix executable.
pedantix-show-errorstWhether to pop up a buffer with stderr output when formatting fails.

Why the region must be self-contained. pedantix and the base formatters parse complete expressions. A bare run of bindings like b = 1; a = 2; is not a valid expression on its own and cannot be formatted — select the enclosing { … } instead. If formatting a region fails, the error buffer hints at exactly this.

Usage

(require 'pedantix)

(add-hook 'nix-mode-hook    #'pedantix-format-on-save-mode)
(add-hook 'nix-ts-mode-hook #'pedantix-format-on-save-mode)

Or with use-package:

(use-package pedantix
  :hook ((nix-mode nix-ts-mode) . pedantix-format-on-save-mode))

Installing with Nix

The cleanest way is the overlays.emacs overlay, which adds pedantix to every emacs package set (this works with e.g. emacs-overlay).

Add the overlay, then:

programs.emacs.extraPackages = epkgs: [ epkgs.pedantix ];

Without the overlay, use lib.emacsPackage:

programs.emacs.extraPackages = epkgs: [ (inputs.pedantix.lib.emacsPackage epkgs) ];

Both install pedantix.el with pedantix-program already patched to the wrapped binary's store path, so the executable and the base formatters are on hand without extra PATH setup.

Installing manually

If you install pedantix.el by any other means, make sure the pedantix binary is on PATH (or set pedantix-program to its path):