├── .github ├── FUNDING.yml └── workflows │ └── test.yml ├── Makefile ├── README.md └── nixpkgs-fmt.el /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: sanityinc 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - '**.md' 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | emacs_version: 14 | - 24.3 15 | - 24.5 16 | - 25.1 17 | - 25.3 18 | - 26.1 19 | - 26.3 20 | - 27.1 21 | - 27.2 22 | - 28.1 23 | - 28.2 24 | - snapshot 25 | steps: 26 | - uses: purcell/setup-emacs@master 27 | with: 28 | version: ${{ matrix.emacs_version }} 29 | 30 | - uses: actions/checkout@v2 31 | - name: Run tests 32 | run: make 33 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | EMACS ?= emacs 2 | 3 | # A space-separated list of required package names 4 | NEEDED_PACKAGES = package-lint reformatter 5 | 6 | INIT_PACKAGES="(progn \ 7 | (require 'package) \ 8 | (push '(\"melpa\" . \"https://melpa.org/packages/\") package-archives) \ 9 | (package-initialize) \ 10 | (dolist (pkg '(${NEEDED_PACKAGES})) \ 11 | (unless (package-installed-p pkg) \ 12 | (unless (assoc pkg package-archive-contents) \ 13 | (package-refresh-contents)) \ 14 | (package-install pkg))) \ 15 | )" 16 | 17 | all: compile package-lint clean-elc 18 | 19 | package-lint: 20 | ${EMACS} -Q --eval ${INIT_PACKAGES} -batch -f package-lint-batch-and-exit nixpkgs-fmt.el 21 | 22 | compile: clean-elc 23 | ${EMACS} -Q --eval ${INIT_PACKAGES} -L . -batch -f batch-byte-compile *.el 24 | 25 | clean-elc: 26 | rm -f f.elc 27 | 28 | .PHONY: all compile clean-elc package-lint 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Melpa Status](http://melpa.org/packages/nixpkgs-fmt-badge.svg)](http://melpa.org/#/nixpkgs-fmt) 2 | [![Melpa Stable Status](http://stable.melpa.org/packages/nixpkgs-fmt-badge.svg)](http://stable.melpa.org/#/nixpkgs-fmt) 3 | [![Build Status](https://github.com/purcell/emacs-nixpkgs-fmt/actions/workflows/test.yml/badge.svg)](https://github.com/purcell/emacs-nixpkgs-fmt/actions/workflows/test.yml) 4 | Support me 5 | 6 | nixpkgs-fmt.el 7 | ============== 8 | 9 | This Emacs library provides commands and a minor mode for easily reformatting 10 | Nix source code using the [nixpkgs-fmt][nixpkgs-fmt] command. 11 | 12 | Installation 13 | ============= 14 | 15 | If you choose not to use one of the convenient 16 | packages in [MELPA][melpa], you'll need to 17 | add the directory containing `nixpkgs-fmt.el` to your `load-path`, and 18 | then `(require 'nixpkgs-fmt)`. 19 | 20 | Usage 21 | ===== 22 | 23 | Customise the `nixpkgs-fmt-command` variable as desired, then call 24 | `nixpkgs-fmt-buffer` or `nixpkgs-fmt-region` as convenient. 25 | 26 | Enable `nixpkgs-fmt-on-save-mode` in Nix buffers like this: 27 | 28 | ```el 29 | (add-hook 'nix-mode-hook 'nixpkgs-fmt-on-save-mode) 30 | ``` 31 | 32 | or locally to your project with a form in your .dir-locals.el like 33 | this: 34 | 35 | ```el 36 | ((nix-mode 37 | (mode . nixpkgs-fmt-on-save))) 38 | ``` 39 | 40 | You might like to bind `nixpkgs-fmt` or `nixpkgs-fmt-buffer` to a key, 41 | e.g. with: 42 | 43 | ```el 44 | (define-key 'nix-mode-map (kbd "C-c C-f") 'nixpkgs-fmt) 45 | ``` 46 | 47 | [melpa]: http://melpa.org 48 | [nixpkgs-fmt]: https://github.com/nix-community/nixpkgs-fmt 49 | 50 |
51 | 52 | [💝 Support this project and my other Open Source work](https://www.patreon.com/sanityinc) 53 | 54 | [💼 LinkedIn profile](https://uk.linkedin.com/in/stevepurcell) 55 | 56 | [✍ sanityinc.com](http://www.sanityinc.com/) 57 | -------------------------------------------------------------------------------- /nixpkgs-fmt.el: -------------------------------------------------------------------------------- 1 | ;;; nixpkgs-fmt.el --- Reformat Nix using nixpkgs-fmt -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2020 Steve Purcell 4 | 5 | ;; Author: Steve Purcell 6 | ;; Keywords: languages 7 | ;; URL: https://github.com/purcell/emacs-nixpkgs-fmt 8 | ;; Package-Requires: ((emacs "24") (reformatter "0.3")) 9 | ;; Version: 0 10 | 11 | ;; This program is free software; you can redistribute it and/or modify 12 | ;; it under the terms of the GNU General Public License as published by 13 | ;; the Free Software Foundation, either version 3 of the License, or 14 | ;; (at your option) any later version. 15 | 16 | ;; This program is distributed in the hope that it will be useful, 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | ;; GNU General Public License for more details. 20 | 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with this program. If not, see . 23 | 24 | ;;; Commentary: 25 | 26 | ;; Provides commands and a minor mode for easily reformatting Nix code 27 | ;; using the external "nixpkgs-fmt" program. 28 | 29 | ;; Call `nixpkgs-fmt-buffer' or `nixpkgs-fmt-region' as convenient. 30 | 31 | ;; Enable `nixpkgs-fmt-on-save-mode' in Nix buffers like this: 32 | 33 | ;; (add-hook 'nix-mode-hook 'nixpkgs-fmt-on-save-mode) 34 | 35 | ;; or locally to your project with a form in your .dir-locals.el like 36 | ;; this: 37 | 38 | ;; ((nix-mode 39 | ;; (mode . nixpkgs-fmt-on-save))) 40 | 41 | ;; You might like to bind `nixpkgs-fmt-region' or `nixpkgs-fmt-buffer' to a key, 42 | ;; e.g. with: 43 | 44 | ;; (define-key 'nix-mode-map (kbd "C-c C-f") 'nixpkgs-fmt) 45 | 46 | ;;; Code: 47 | 48 | 49 | ;; Minor mode and customisation 50 | 51 | (require 'reformatter) 52 | 53 | (defgroup nixpkgs-fmt nil 54 | "Reformat Nix using nixpkgs-fmt." 55 | :group 'languages) 56 | 57 | (defcustom nixpkgs-fmt-command "nixpkgs-fmt" 58 | "Command used for reformatting." 59 | :type 'string) 60 | 61 | 62 | ;; Commands for reformatting 63 | 64 | ;;;###autoload (autoload 'nixpkgs-fmt-buffer "nixpkgs-fmt" nil t) 65 | ;;;###autoload (autoload 'nixpkgs-fmt-region "nixpkgs-fmt" nil t) 66 | ;;;###autoload (autoload 'nixpkgs-fmt-on-save-mode "nixpkgs-fmt" nil t) 67 | (reformatter-define nixpkgs-fmt 68 | :program nixpkgs-fmt-command 69 | :lighter " NixPkgFmt" 70 | :group 'nixpkgs-fmt) 71 | 72 | 73 | (provide 'nixpkgs-fmt) 74 | ;;; nixpkgs-fmt.el ends here 75 | --------------------------------------------------------------------------------