安裝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:
創(chuàng)建并拷貝github文件內(nèi)容至~/.emacs.d/init.el
https://github.com/clojurians-org/my-emacs/blob/master/init.el
相關(guān)代碼片斷講解
- 安裝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)))
- 配置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)
)
創(chuàng)建cabal項(xiàng)目:
mkdir my-app && cd my-app
cabal init --libandexe --source-dir=src --application-dir=app
cabal new-configure
添加stack版本支持(僅創(chuàng)建文件即可)
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: larluo@clojurians.org
-- 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緩存川梅,請(qǐng)參考 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 = ./.; };
}
進(jìn)入nix-shell開(kāi)發(fā)環(huán)境
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] 打開(kāi)flycheck顯示錯(cuò)誤提示
使用[C-c, "]運(yùn)行代碼得到結(jié)果
退出nix-shell開(kāi)發(fā)模式,使用nix進(jìn)行構(gòu)建
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