Skip to content

Instantly share code, notes, and snippets.

@adetokunbo
Last active February 26, 2021 00:14
Show Gist options
  • Save adetokunbo/c20b82c2a24774e6e8ead79aa6f19217 to your computer and use it in GitHub Desktop.
Save adetokunbo/c20b82c2a24774e6e8ead79aa6f19217 to your computer and use it in GitHub Desktop.
.spacemacs enabling dante mode/nix/cabal in spacemacs main branch
;;; ...
;;; ...
;; List of additional packages that will be installed without being
;; wrapped in a layer. If you need some configuration for these
;; packages, then consider creating a layer. You can also put the
;; configuration in `dotspacemacs/user-config'.
;; Cabal/Nix haskell setup
dotspacemacs-additional-packages '(dante nix-sandbox)
;;; ...
;;; ...
;; Derived from
;; https://www.tuicool.com/articles/ZBv6ju
;; and https://github.com/travisbhartwell/nix-emacs#flycheck
;;
;; Configure flycheck to use Nix
;; Requires `nix-sandbox` package added to dotspacemacs-additional-packages
(defun flycheck-haskell-set-nix-executables ()
;; Find any executables flycheck needs in the nix sandbox
(make-local-variable 'flycheck-command-wrapper-function)
(make-local-variable 'flycheck-executable-find)
(setq flycheck-command-wrapper-function
(lambda (cmd) (apply 'nix-shell-command (nix-current-sandbox) cmd))
flycheck-executable-find
(lambda (cmd) (nix-executable-find (nix-current-sandbox) cmd)))
;; Explicitly set the ghc and hlint buffer-local executable values
(make-local-variable 'flycheck-haskell-ghc-executable)
(make-local-variable 'flycheck-haskell-hlint-executable)
(setq flycheck-haskell-ghc-executable
(nix-executable-find (nix-current-sandbox) "ghc")
flycheck-haskell-hlint-executable
(nix-executable-find (nix-current-sandbox) "hlint"))
;; Make the executable-find a local function that uses nix
(make-local-variable 'executable-find)
(setq executable-find
(lambda (cmd) (nix-executable-find (nix-current-sandbox) cmd)))
(message "set flycheck-haskell-ghc-executable to: %S"
flycheck-haskell-ghc-executable))
;; Setup the dante project values according to the proposed layout for
;; shared common code, i.e
;;
;; dante-project-root === <immediate folder with a shell.nix>
;; dante-repl-command-line === cabal new-repl <dante-target> --buildir=dist/dante
(defun reflex-set-dante-locals ()
(make-local-variable 'dante-project-root)
(make-local-variable 'dante-repl-command-line)
(setq dante-project-root
(locate-dominating-file buffer-file-name "shell.nix"))
(if dante-target
(let ((cabal-cmd
(concat "cabal new-repl " dante-target " --builddir=dist/dante")))
(setq dante-repl-command-line (list "nix-shell" "--run" cabal-cmd)))
nil))
(defun flycheck-haskell-setup-nix-locals ()
;; disable the haskell-stack-ghc checker
(add-to-list 'flycheck-disabled-checkers 'haskell-stack-ghc)
(add-hook 'hack-local-variables-hook #'reflex-set-dante-locals
nil 'local)
(add-hook 'hack-local-variables-hook #'flycheck-haskell-set-nix-executables
nil 'local))
(defun dotspacemacs/user-config ()
"Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."
;; Set up Cabal/Nix Haskell - enable dante mode/flycheck/nix hooks
(add-hook 'haskell-mode-hook 'dante-mode)
(add-hook 'dante-mode-hook
'(lambda () (flycheck-add-next-checker 'haskell-dante
'(warning . haskell-hlint))))
(add-hook 'dante-mode-hook #'flycheck-haskell-setup-nix-locals)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment