[nixos] Add nixos-format-on-save (#14982)

* [nixos] Add nixos-format-on-save
This commit is contained in:
Aaron Zeng 2021-08-19 23:09:00 -04:00 committed by GitHub
parent 3a52081c23
commit fdeb770103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 5 deletions

View File

@ -4,11 +4,12 @@
[[file:img/nixos.png]]
* Table of Contents :TOC_5_gh:noexport:
* Table of Contents :TOC_5_gh:noexport:
- [[#description][Description]]
- [[#features][Features:]]
- [[#install][Install]]
- [[#layer][Layer]]
- [[#format-on-save][Format on save]]
- [[#configuration][Configuration]]
- [[#key-bindings][Key bindings]]
- [[#nixos-options][NixOS Options]]
@ -18,6 +19,7 @@ This layer adds tools for better integration of Emacs in NixOS.
** Features:
- Nix-mode using [[https://github.com/NixOS/nix-mode][nix-mode]]
- Automatic formatting via [[https://github.com/serokell/nixfmt][nixfmt]]
- Auto-completion of NixOS Options using [[https://github.com/travisbhartwell/nix-emacs/blob/master/company-nixos-options.el][company-nixos-options]]
- Helm Lookup for NixOS Options [[https://github.com/travisbhartwell/nix-emacs/blob/master/helm-nixos-options.el][helm-nixos-options]]
@ -27,6 +29,14 @@ To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =nixos= to the existing =dotspacemacs-configuration-layers= list in this
file.
** Format on save
To enable automatic formatting on save, set the layer variable
~nixos-format-on-save~ to ~t~:
#+begin_src elisp
(nixos :variables nixos-format-on-save t)
#+end_src
* Configuration
On some systems, =company-nixos-options= may be very slow. If this is the case,
see the section on disabling the [[https://github.com/syl20bnr/spacemacs/blob/develop/doc/DOCUMENTATION.org#disabling-layer-services-in-other-layers][disabling auto-complete]] for the =nixos= layer.
@ -34,6 +44,7 @@ see the section on disabling the [[https://github.com/syl20bnr/spacemacs/blob/de
* Key bindings
** NixOS Options
| Key binding | Description |
|-------------+-------------------------|
| ~SPC h >~ | Call helm-nixos-options |
| Key binding | Description |
|-------------+----------------------------|
| ~SPC h >~ | Call helm-nixos-options |
| ~SPC m = =~ | Format buffer using nixfmt |

View File

@ -0,0 +1,27 @@
;;; config.el --- nixos Layer Configuration File for Spacemacs
;;
;; Copyright (c) 2012-2021 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; variables
(defvar nixos-format-on-save nil
"If non-nil, nixfmt before saving.")

View File

@ -56,7 +56,9 @@
(progn
(add-to-list 'spacemacs-indent-sensitive-modes 'nix-mode)
(spacemacs/set-leader-keys-for-major-mode 'nix-mode
"==" 'nix-format-buffer))
"==" 'nix-format-buffer)
(when nixos-format-on-save
(add-hook 'before-save-hook 'nix-format-before-save)))
:config
(electric-indent-mode -1)))