Skip to content

Instantly share code, notes, and snippets.

@ScottFreeCode
Created July 25, 2023 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottFreeCode/a0e78181faff634d85e322bf8df5fc38 to your computer and use it in GitHub Desktop.
Save ScottFreeCode/a0e78181faff634d85e322bf8df5fc38 to your computer and use it in GitHub Desktop.
Haskell+Nix, Monorepo
dist/
dist-*/
result
result-*
cabal.project.local
Setup
*~
*#*

Fixup (work around gist limitations)

mkdir -p app/src lib/src ; git mv app{_slash_,/}example.cabal ; git mv app{_slash_src_slash_,/src/}Main.hs ; git mv lib{_slash_,/}lib.cabal ; git mv lib{_slash_src_slash_,/src/}MyLib.hs

Commands

Clean full build

nix-build

Dev mode

nix-shell

Then you can build with cabal build app or build the library and then the app separately via cd lib && cabal v1-install && cd ../app && cabal v1-build – with or without --nix=True (or $HOME/.cabal/config containing nix: True).

cabal-version: 3.0
name: example
version: 0.1.0.0
build-type: Simple
executable example
main-is: Main.hs
build-depends:
base
, example-lib
hs-source-dirs: src
default-language: Haskell2010
module Main where
import MyLib (myFunction)
main = myFunction
packages: */*.cabal
{ pkgs ? import <nixpkgs> {} }:
let
gitignore = pkgs.nix-gitignore.gitignoreSourcePure [ ./.gitignore ];
lib = pkgs.haskellPackages.callCabal2nix "" (gitignore ./lib) {};
app = gitignore ./app;
in { lib = lib; app = pkgs.haskellPackages.callCabal2nix "" app { example-lib = lib; }; }
cabal-version: 3.0
name: example-lib
version: 0.1.0.0
build-type: Simple
library
exposed-modules:
MyLib
build-depends:
base
hs-source-dirs: src
default-language: Haskell2010
module MyLib (myFunction) where
myFunction = putStrLn "Hello, world!"
{ pkgs ? import <nixpkgs> {} }:
pkgs.haskellPackages.shellFor {
packages = p: builtins.attrValues (import ./. { inherit pkgs; });
buildInputs = [
pkgs.cabal-install
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment