[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/nix-mode ae2c44c160 250/500: Add nix-shell-haskell.el
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/nix-mode ae2c44c160 250/500: Add nix-shell-haskell.el |
Date: |
Sat, 29 Jan 2022 08:27:11 -0500 (EST) |
branch: elpa/nix-mode
commit ae2c44c160cf2e03dde6431212dd69cf436fec15
Author: Matthew Bauer <mjbauer95@gmail.com>
Commit: Matthew Bauer <mjbauer95@gmail.com>
Add nix-shell-haskell.el
---
Cask | 1 +
nix-shell-haskell.el | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 108 insertions(+)
diff --git a/Cask b/Cask
index 1e13441a88..0a1ae1d212 100644
--- a/Cask
+++ b/Cask
@@ -17,6 +17,7 @@
"nix-search.el"
"nix-shebang.el"
"nix.el"
+ "nix-shell-haskell.el"
)
(development
diff --git a/nix-shell-haskell.el b/nix-shell-haskell.el
new file mode 100644
index 0000000000..01e56c21c0
--- /dev/null
+++ b/nix-shell-haskell.el
@@ -0,0 +1,107 @@
+;;; nix-shell-haskell.el -- run nix commands in Emacs -*- lexical-binding: t
-*-
+
+;; Author: Matthew Bauer <mjbauer95@gmail.com>
+;; Homepage: https://github.com/NixOS/nix-mode
+;; Keywords: nix
+
+;; This file is NOT part of GNU Emacs.
+
+;;; Commentary:
+
+;; You can use nix-shell-haskell as a hook to haskell-mode.
+;; Just run:
+
+;; (require 'nix-shell-haskell)
+;; (add-hook 'haskell-mode 'nix-shell-haskell)
+
+;;; Code:
+
+(require 'nix)
+(require 'nix-instantiate)
+(require 'nix-shell)
+(require 'haskell)
+(require 'haskell-cabal)
+(require 'haskell-session)
+(require 'flycheck)
+
+(defun nix-shell-haskell--callback (buffer cabal-file drv &rest _)
+ "Run the nix-shell callback to setup the buffer.
+The BUFFER to run in.
+The CABAL-FILE to load from.
+The DRV file to use."
+ (let* ((env (alist-get 'env drv))
+ (stdenv (alist-get 'stdenv env))
+ (system (alist-get 'system env))
+ (inputs (remove nil
+ (apply 'append
+ (mapcar (lambda (prop)
+ (split-string (alist-get prop env)))
+ nix-shell-inputs)))))
+
+ ;; Prevent accidentally rebuilding the world.
+ (unless (file-directory-p stdenv)
+ (error
+ "Your stdenv at %s has not been built. Please run: nix-store -r %s"
+ stdenv stdenv))
+
+ ;; Make sure this .drv file can actually be built here.
+ (unless (string= system (nix-system))
+ (error
+ "Your system (%s) does not match .drv’s build system (%s)"
+ (nix-system) system))
+
+ (with-current-buffer buffer
+ (make-local-variable 'exec-path)
+
+ (dolist (input inputs)
+ (when (and (not (file-directory-p input))
+ nix-shell-auto-realise)
+ (nix-store-realise input))
+
+ (add-to-list 'exec-path (expand-file-name "bin" input)))
+
+ (when flycheck-mode
+ (flycheck-buffer)))
+
+ (when (and (eq major-mode 'haskell-mode) (not (haskell-session-maybe)))
+ (message "here")
+ (unless (haskell-session-lookup cabal-file)
+ (let ((session (haskell-session-make cabal-file)))
+ (haskell-session-set-cabal-dir (file-name-directory cabal-file))
+ (haskell-process-start session)))
+ (haskell-session-assign cabal-file))))
+
+;;;###autoload
+(defun nix-shell-haskell (&optional cabal-file)
+ "Setup Nix shell from the .cabal file."
+ (interactive)
+ (unless cabal-file
+ (setq cabal-file (haskell-cabal-find-file)))
+ (when (and (called-interactively-p) (not cabal-file))
+ (error "Cannot find a cabal file in %s" default-directory))
+ (when cabal-file
+ (setq cabal-file (expand-file-name cabal-file))
+ (let ((file (concat (file-name-sans-extension cabal-file) ".nix"))
+ (project-dir (locate-dominating-file cabal-file "default.nix")))
+ (with-temp-file file
+ (insert "let\n")
+ (insert (format " haskellPackages = %s.packages"
+ (if project-dir (format "(import %s {})"
+ (expand-file-name
+ "default.nix" project-dir))
+ "{}")))
+ (insert " or (import <nixpkgs> {}).haskellPackages;\n")
+ (insert "in\n")
+ (insert (format " (haskellPackages.callCabal2nix \"%s\" \"%s\" {}).env"
+ (file-name-base cabal-file) cabal-file))
+ (insert " .overrideAttrs (attrs: {\n")
+ (insert " nativeBuildInputs = (attrs.nativeBuildInputs or [])\n")
+ (insert " ++ (with haskellPackages; [
cabal-install ]);\n")
+ (insert " })\n"))
+ (nix-instantiate-async (apply-partially 'nix-shell-haskell--callback
+ (current-buffer)
+ cabal-file)
+ file))))
+
+(provide 'nix-shell-haskell)
+;;; nix-shell-haskell.el ends here
- [nongnu] elpa/nix-mode 5a8b334c75 212/500: Merge pull request #42 from serokell/master, (continued)
- [nongnu] elpa/nix-mode 5a8b334c75 212/500: Merge pull request #42 from serokell/master, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode b8aed79f89 211/500: No need to indent on = and ; (ruins your day), ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 81680dd7ac 213/500: nix-repl.el: make it work with newer nix repl command, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode a8af6e9203 218/500: Find nix executables at start, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode d6286ce43b 235/500: Add pcomplete support to nix.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode dd49386cc7 240/500: Remove global-nix-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1be3ca4666 245/500: Clean up nix-shell.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 4ef5d77214 246/500: Add user manual, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 2bc99a4542 247/500: Add new CHANGELOG entry., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 14d1bbb936 248/500: Delete dir-locals.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode ae2c44c160 250/500: Add nix-shell-haskell.el,
ELPA Syncer <=
- [nongnu] elpa/nix-mode c462eb400b 251/500: Add alias for nix-repl-show, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 47cdb44e2e 255/500: Improve indentation robustness, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1c772ad539 253/500: Remove recommended config, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 295cd6729b 261/500: Add nix-edit.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode fde8c8e38c 268/500: Merge pull request #48 from dustinlacewell/master, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 90ac0a74b2 273/500: Merge pull request #55 from teozkr/issue-54, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 563f6bdb03 272/500: Fixed `nix-mode-comments` matching everything, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode abdeae0f23 276/500: Make sure nix-instantiate-executable exists before running it, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 57ea981cb2 278/500: Use eval-when-compile for subr-x, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1512d02830 279/500: Formatting, ELPA Syncer, 2022/01/29