开发速记-emacs篇: haskell+nix开发模式

安装nix并配置channel:

sudo install -d -m755 -o $(id -u) -g $(id -g) /nix
curl https://nixos.org/nix/install | sh

nix-channel --add https://nixos.org/channels/nixos-20.03 nixos-20.03
nix-channel --update nixos-20.03

安装cachix 并配置iohk缓存

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA cachix
cachix use iohk

安装cabal

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA cabal-install

安装emacs

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA emacs

配置emacs:

创建并拷贝github文件内容至~/.emacs.d/init.el
https://github.com/clojurians-org/my-emacs/blob/master/init.el
相关代码片断讲解

  1. 安装dante-mode
(dolist (package '(paredit restclient json-mode typescript-mode
                   dap-mode lsp-java gradle-mode
                   dante cider
                   ))
  (unless (package-installed-p package)
    (package-install package)))
  1. 配置dante-mode
(use-package dante
  :ensure t
  :after haskell-mode
  :commands 'dante-mode
  :init
  (add-hook 'haskell-mode-hook 'flycheck-mode)
  (add-hook 'haskell-mode-hook 'dante-mode)
  )

创建cabal项目:

mkdir my-app && cd my-app 
cabal init --libandexe --source-dir=src --application-dir=app
cabal new-configure

添加stack版本支持(仅创建文件即可)

larrys-MacBook-Pro:my-app larluo$ cat stack.yaml 
resolver: lts-14.27
packages:
- .

添加依赖

larrys-MacBook-Pro:my-app larluo$ cat my-app.cabal
cabal-version:       >=1.10
-- Initial package description 'my-app.cabal' generated by 'cabal init'.
-- For further documentation, see http://haskell.org/cabal/users-guide/

name:                my-app
version:             0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
-- license:
license-file:        LICENSE
author:              clojurians-org
maintainer:          [email protected]
-- copyright:
-- category:
build-type:          Simple
extra-source-files:  CHANGELOG.md

library
  exposed-modules:     MyLib
  -- other-modules:
  -- other-extensions:
  build-depends:       
      base
    , bytestring
    , cryptonite
                      
  hs-source-dirs:      src
  default-language:    Haskell2010

executable my-app
  main-is:             Main.hs
  -- other-modules:
  -- other-extensions:
  build-depends:       base, my-app
  hs-source-dirs:      app
  default-language:    Haskell2010

切换至nix集成模式

[ haskell.nix: https://github.com/input-output-hk/haskell.nix ]

如使用iohk缓存,请参考 https://github.com/input-output-hk/haskell.nix/blob/master/ci.nix中的nixpkgs版本,替换~/.nix-defexpr/channels/nixos-20.03

larrys-MacBook-Pro:my-app larluo$ cat default.nix
{ pkgs ? import ~/.nix-defexpr/channels/nixos-20.03 (import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz))
}:
  pkgs.haskell-nix.stackProject {
    src = pkgs.haskell-nix.haskellLib.cleanGit { src = ./.; };
  }

进入nix-shell开发环境

nix-shell -A shellFor

分别为library及application添加emacs配置

[nix-shell:~/my-tmp/my-app]$ cat src/.dir-locals.el 
((nil . (
  (dante-methods . (new-build))
  (dante-target . "lib:my-app")
                  )))

[nix-shell:~/my-tmp/my-app]$ cat app/.dir-locals.el 
((nil . (
  (dante-methods . (new-build))
  (dante-target . "exe:my-app")
                  )))

使用emacs 编码

emacs -nw src/MyLib.hs

使用[C-c, !, l] 打开flycheck显示错误提示

image.png

使用[C-c, "]运行代码得到结果

image.png

image.png

退出nix-shell开发模式,使用nix进行构建

larrys-MacBook-Pro:my-app larluo$ touch LICENSE 
larrys-MacBook-Pro:my-app larluo$ nix build -f . my-app.components.exes.my-app
trace: gitSource.nix: /Users/larluo/my-tmp/my-app does not seem to be a git repository,
assuming it is a clean checkout.
trace: [alex-plan-to-nix-pkgs] cabal new-configure --with-ghc=ghc --with-ghc-pkg=ghc-pkg
trace: [happy-plan-to-nix-pkgs] cabal new-configure --with-ghc=ghc --with-ghc-pkg=ghc-pkg
trace: [hscolour-plan-to-nix-pkgs] cabal new-configure --with-ghc=ghc --with-ghc-pkg=ghc-pkg
trace: Get `stack-sha256` with `nix-hash --base32 --type sha256 /nix/store/gcrvfhw1sm60xfj8db5k3sfv4ywx9lc3-stack-to-nix-pkgs/`
[5 built, 0.0 MiB DL]

larrys-MacBook-Pro:my-app larluo$ ls -l ./result
lrwxr-xr-x 1 larluo staff 69 Mar  2 15:05 ./result -> /nix/store/4z0l3y59mlv3a1dbdsx7299i21kgy611-my-app-0.1.0.0-exe-my-app

larrys-MacBook-Pro:my-app larluo$ ./result/bin/my-app 
Hello, Haskell!
someFunc
78dbae4188d22f8a2f308e8d18c88733

你可能感兴趣的:(开发速记-emacs篇: haskell+nix开发模式)