Skip to content

Commit

Permalink
feat: Add automatic formatting for .nix files
Browse files Browse the repository at this point in the history
  • Loading branch information
Swarsel committed Jul 18, 2024
1 parent 9dc9a1f commit 72d321f
Show file tree
Hide file tree
Showing 46 changed files with 3,162 additions and 3,122 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/flake-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v3
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Check for dead code
- name: Check for dead code in .nix files
run: git ls-files '*.nix' | nix run nixpkgs#deadnix
- name: Check for lints
- name: Check for lints in .nix files
run: nix run nixpkgs#statix -- check
- name: Check formatting in .nix files
run: git ls-files '*.nix' | xargs nix run nixpkgs#alejandra -- --check
55 changes: 28 additions & 27 deletions SwarselSystems.org
Original file line number Diff line number Diff line change
Expand Up @@ -5131,6 +5131,11 @@ Programming languages and default lsp's are defined here: [[#h:0e7e8bea-ec58-499
nmap
lsof

# nix
alejandra
deadnix
statix

# local file sharing
wormhole-rs

Expand Down Expand Up @@ -7524,41 +7529,37 @@ Used in: [[#h:bbcfa895-4d46-4b1d-b84e-f634e982c46e][Centered org-mode Buffers]]

#+end_src

**** org-mode: Auto-tangle and export Configuration Files
**** org-mode: Upon-save actions (Auto-tangle, export to html, formatting)
:PROPERTIES:
:CUSTOM_ID: h:59d4306e-9b73-4b2c-b039-6a6518c357fc
:END:

This section automatically tangles all configuration blocks in this file to the defined Emacs org-file. It also exports the configuration file as html.
This section handles everything that shoudld happen when I save =SwarselSystems.org=. It:

1) automatically tangles all configuration blocks in this file
2) exports the configuration file as html for an easier reading experience with working links and index
3) formats the generated =.nix= files in accordance to the =Alejandra=-style.

We set a hook that runs everytime we save the file. It would be a bit more efficient to only export and format when we enter a magit window for instance (since especially the html export takes times), however, since I cannot be sure to only ever commit from magit (I do indeed sometimes use git from the command line), I prefer this approach.

#+begin_src emacs-lisp

(defun swarsel/org-babel-tangle-config ()
(when (string-equal (buffer-file-name)
swarsel-swarsel-org-filepath)
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-html-export-to-html)
(org-babel-tangle)))
(when (string-equal (buffer-file-name)
swarsel-emacs-org-filepath)
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-html-export-to-html)
(org-babel-tangle)))
(when (string-equal (buffer-file-name)
swarsel-nix-org-filepath)
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-babel-tangle))))

(setq org-html-htmlize-output-type nil)

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'swarsel/org-babel-tangle-config)))


(defun run-alejandra ()
(interactive)
(let ((default-directory (expand-file-name "~/.dotfiles")))
(shell-command "alejandra . -q")))

(defun swarsel/org-babel-tangle-config ()
(when (string-equal (buffer-file-name)
swarsel-swarsel-org-filepath)
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-html-export-to-html)
(org-babel-tangle)
(run-alejandra))))

(setq org-html-htmlize-output-type nil)

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'swarsel/org-babel-tangle-config)))

#+end_src

Expand Down
Loading

0 comments on commit 72d321f

Please sign in to comment.