• Stars
    star
    272
  • Rank 151,235 (Top 3 %)
  • Language
    Shell
  • Created about 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Deploy Haskell apps to Heroku

Styleguide Rails Logo

Styleguide Rails Logo


Deploy Haskell apps to Heroku


This buildpack supports frameworks like Yesod, Snap, and Happstack with the latest stable GHC binaries. Putting Haskell web applications online should be easy, and now it is. Try it for yourself.


**Note**, this buildpack does a basic build from scratch. For a faster build, try [mietek/haskell-on-heroku](https://github.com/mietek/haskell-on-heroku) which detects your project dependencies and fetches a custom pre-built sandbox.

Example: deploying a Snap app

Here's how to go from zero to "hello world" on Heroku. You'll need to install the Haskell Platform and the Heroku Toolbelt on your local machine, then do this:

# Generate a barebones snap app called snapdemo

mkdir snapdemo && cd $_
cabal sandbox init
cabal install snap
cabal exec snap init barebones

# Tell Heroku how to start the server

echo 'web: cabal run -- -p $PORT' > Procfile

# Create a git repo and deploy!

git init .
echo "dist\n.cabal-sandbox\ncabal.sandbox.config" > .gitignore
git add *
git commit -m 'Initial commit'

heroku create --stack=cedar-14 --buildpack https://github.com/begriffs/heroku-buildpack-ghc.git
git push heroku master

The first deploy is slowest as the environment downloads and bootstraps. Subsequent deploys use cached binaries and cached cabal packages to go faster.

Beating the Fifteen-Minute Build Limit

The first time you try to deploy a big framework like Yesod the compilation can take so long that Heroku cuts it off. If this happens fear not, you can build your app with an Anvil server.

# Enable Anvil builds
heroku plugins:install https://github.com/ddollar/heroku-anvil

# Move big build artifacts out of the way or else the upload
# to Anvil will be very slow
mkdir -p /tmp/deploy-stash ; mv .cabal-sandbox /tmp/deploy-stash  ; mv dist /tmp/deploy-stash

# Build your slug and cache without any time limits
heroku build -r -b https://github.com/begriffs/heroku-buildpack-ghc.git

# Use Anvil-generated cache next time we do a regular git push to Heroku
heroku config:set EXTERNAL_CACHE=$(cat .anvil/cache)

# Bring your sandbox etc back
mv /tmp/deploy-stash/.cabal-sandbox . ; mv /tmp/deploy-stash/dist .

After the first deploy using Anvil you can go back to the regular deploy process. This is because the cabal sandbox etc are cached by Anvil and will be retrieved, making future builds incremental and fast.

Remember to heroku config:unset EXTERNAL_CACHE after your first successful regular (post-Anvil) git push.

Locking Package Versions

Cabal sometimes gets confused on Heroku and tries installing outdated packages. If you have your app working locally you can constrain the remote package versions to match your local environment. Just do this:

cabal freeze
git add cabal.config

# commit and push to fix remote build

Configuring the Build

You can change build settings through Heroku environment variables.

# set the variable of your choice
heroku config:set VARIABLE=value

Here are the options

namedescriptiondefault
CLEAR_CACHE Force everything to reinstall from scratch by setting to 1. 0
EXTERNAL_CACHE Url of replacement buildpack cache tarball. Useful for Anvil.
GHC_VER GHC version to download or build 7.10.3
CABAL_VER Version of cabal-install 1.22.8.0
PREBUILT Base url for the prebuilt binary cache https://s3.amazonaws.com/heroku-ghc
PRE_SCRIPT Path to an executable, relative to checkout root. If present, this will be chmodded (+x) and ran before anything else.
POST_SCRIPT Path to an executable, relative to checkout root. If present, this will be chmodded (+x) and ran after anything else.

Interacting with a running app

heroku run bash         # shell access
heroku run cabal repl   # Haskell repl with all app modules loaded

Benefits of this buildpack

  • Latest binaries: GHC 7.10.3, cabal-install 1.22.8.0
  • Uses cabal >=1.20 features to run the app and repl
  • Exposes Haskell platform binaries to your app and scripts
  • Uses prebuilt binaries for speed but...
  • ...can fall back to building the standard GHC distribution

Contributing

There are a number of ways to improve this buildpack. Please see the Github issues for ideas.

In order to contribute to the build script it will help to understand how Heroku's deployment process works, and how that affects GHC. Heroku provides three areas for storing files during build: a cache directory, a working directory, and a build directory.

The cache, called $CACHE_DIR in the script, persists between deployments. We use it to avoid building binaries more than once. The working directory, called $WORKING_HOME, does not persist between builds, or even after the build script is done. Seems we should avoid this area, right? Well GHC has some idiosyncracies that make this area quite useful as you will see. Finally the build destination directory, $BUILD_DIR, holds the git repo the user pushes and gets copied into what will be /app in the deployed application. During build it lives in a weird nonce filename.

We want to use GHC binaries at build time to compile the app, and we would also like those binaries to be available in the app environment after deployment (so people can use runhaskell or cabal repl). Seems like we should install directly to $BUILD_DIR. There's one problem: GHC breaks if you move it to a new path after installation because many of its binaries are just scripts with hard-coded full paths in them to other GHC files. And as you remember, Heroku is going to move things in $BUILD_DIR to /app. So the trick will be to install to /app in the working directory, use GHC there, then copy that installation to the build directory which will be renamed in the deployed application and not notice it has been moved.

GHC is also sensitive to having libgmp named just right. We don't have privileges to adjust /usr/lib in the deployed app so we create a symbolic link in a place we are permitted and set linker variables in the shell so that everything can build.

External dependencies

Some packages have external dependencies (i.e. non-Haskell dependencies which cannot be satisfied by cabal). If you come across such a package, check in contribs to see if someone has already created patches for the dependencies. If they have, you should be able to

patch -p0 < contribs/DEP.patch

to patch the buildpack and proceed. If not, you'll need to modify bin/compile yourself. Contributing these changes back as patches is appreciated.

Building new binaries for Heroku

As new versions of GHC and Cabal are released we should build them for Heroku and put them on S3 to speed up future deploys for everyone. Luckily the buildpack can do the building too.

Adjust the GHC_VER and CABAL_VER environment vars and then deploy. It will build the new binaries from the standard GHC distribution. Then copy the results to S3 like this:

heroku run bash
# now SSH'd into the server

cd /app/vendor

url "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
awscli-bundle/install

~/.local/lib/aws/bin/aws configure
# ^^^ answer the configuration questions

tar zcf heroku-ghc-[VERSION].tar.gz ghc-[VERSION]/
tar zcf heroku-cabal-install-[VERSION].tar.gz cabal-install-[VERSION]/

~/.local/lib/aws/bin/aws s3 cp heroku-ghc-[VERSION].tar.gz s3://[BUCKET]
~/.local/lib/aws/bin/aws s3 cp heroku-cabal-install-[VERSION].tar.gz s3://[BUCKET]

Thanks

Thanks to Brian McKenna and others for their work on heroku-buildpack-haskell which inspired and informed this buildpack. For a history of that project's contributions and ideas see [this article] (http://blog.begriffs.com/2013/08/haskell-on-heroku-omg-lets-get-this.html).

More Repositories

1

css-ratiocinator

because your CSS is garbage
JavaScript
1,027
star
2

haskell-vim-now

One-line Haskell Vim install
Shell
987
star
3

angular-paginate-anything

Γ€ la carte server-side pagination
JavaScript
517
star
4

lucre

Let people pay you for any or no reason.
Ruby
506
star
5

pg_rational

Precise fractional arithmetic for PostgreSQL
PLpgSQL
229
star
6

objgrep

Find strings inside complicated javascript objects
JavaScript
160
star
7

immutube

Youtube + functors, a most unlikely combo
JavaScript
141
star
8

pg_listen

Trigger shell command from NOTIFY
C
83
star
9

gitftp

Browse git over anonymous FTP
C
82
star
10

mimedown

markdown to multipart mime
C
79
star
11

c-mix

Demo of C project with Haskell functions
C
77
star
12

microservice-template

Basic architecture for running and monitoring workers
Shell
70
star
13

postgrest-example

Migrations for an example conference API
PLpgSQL
70
star
14

styleguide_rails

generate a living styleguide with one command
JavaScript
69
star
15

libderp

C collections. Easy to build, boring algorithms. Dumb is good.
C
45
star
16

haskell-pair

Haskell pair programming server via Vagrant
Shell
41
star
17

react-showpiece

Eminently styleable markup
CSS
37
star
18

obsd

Redacted config files
Vim Script
36
star
19

clean_pagination

API pagination the way RFC7233 intended it
Ruby
33
star
20

vimrc

An old plugin-heavy vim config (I do things differently nowadays)
Vim Script
31
star
21

algorithm-freezer

Know your algorithms cold!
Haskell
29
star
22

utofu

Unicode Trust on First Use (TOFU)
C
28
star
23

haskell-circle-example

Exemplary Stack-based template for Circle CI
Haskell
16
star
24

groupthink

obey the masses with realtime voting
JavaScript
15
star
25

autolytics

An embarrassment of analytics riches
12
star
26

wc

Beating haskell with C
C
11
star
27

haskell-postgres-examples

A cookbook for Postgres in Haskell
Haskell
10
star
28

picobounce

Experiment to build an IRC bouncer out of independent programs connected by named pipes. Only half done.
C
8
star
29

mother-structures

Programming via abstract math
CoffeeScript
8
star
30

randln

Fast, flexible, portable way to get random lines out of files
C
6
star
31

lexyacc

Learning to parse
Lex
6
star
32

dumbus

🚌 Hyperefficient bus planning for dumbphones
Haskell
6
star
33

aeson-t

Transform JSON
Haskell
6
star
34

libc

Implementing the C standard library
C
5
star
35

posix-man

Man pages for POSIX (sections 0,1,3; issues 6 and 7)
Roff
5
star
36

clache

Run Lazy K programs in the cloud
Ruby
5
star
37

aws_pipes

AWS queues Γ  la Unix
Ruby
5
star
38

libscorm

Create SCORM 2004 courses in Flash or HTML
JavaScript
5
star
39

findrss

Search (un)common paths to find the Atom or RSS feed of a site
Shell
5
star
40

lancelot

Your knight in shining ASCII armor
Haskell
4
star
41

sinatra-sql

Easy PostgreSQL access with migrations in Sinatra
Ruby
4
star
42

generator-omni-module

Write JS modules that run everywhere
JavaScript
4
star
43

micro-scraper

Microservice to download pages
Haskell
4
star
44

wchar-conformance

Test ISO 10646 conformance of wchar_t
C
4
star
45

ordinary

Transfinite arithmetic in Ruby
Ruby
4
star
46

kr

Quick 'n dirty K&R exercises
C
3
star
47

incl

Preprocessor to include files into output stream
C
3
star
48

philosoraptor

Logic programming in JavaScript
3
star
49

qwertywords

Words that are fun to type like figment ajangle
C
3
star
50

jsobject

Nicer JavaScript objects through ExternalInterface in ActionScript 3
ActionScript
3
star
51

itertools

Python itertools for Ruby
Ruby
3
star
52

turing

There are many like it, but this one is mine
C
2
star
53

float

Experiments in number representations, a collaboration with Eric Bavier
C
2
star
54

endoxa-graph

everything's connected, man
JavaScript
2
star
55

doublekill

Weird experiments with signals
C
2
star
56

twittective

Some screen scraping in Haskell
Haskell
2
star
57

quickcheck-simple

Project template for doing Haskell exercises
Haskell
2
star
58

angular-http-batch

Leap multiple $http requests in a single bound
JavaScript
2
star
59

retags

Keep ctags up to date with minimal overhead
Shell
2
star
60

pthread-test

Exercises from Butenhof's book
C
2
star
61

semln

Filter to add semantic line breaks in many languages
C
2
star
62

hasql-stress

Trying to reproduce deadlock
Haskell
2
star
63

git-zophrenic

Finds the secret government messages in your Git hashes
Shell
1
star
64

stalk27

When will route 27 actually arrive? No more lies.
Shell
1
star
65

jvscrpt

Drop the function arguments and write less code
JavaScript
1
star
66

multiparse

Drafts for my parsing article
Yacc
1
star
67

ringfifo

Nonblocking multi-use named pipes
C
1
star
68

begriffs.com

my site
CSS
1
star
69

CopernicusJS

A revolution in CSS positioning
JavaScript
1
star
70

angular-patience

Efficiently wait for long server processing
JavaScript
1
star
71

flexicode

Tools scanning Unicode in Flex
C
1
star
72

stm32f411

STM32F4 (STM32F411CEU6) "black pill" development with non-proprietary tools
C
1
star
73

scheme48hrs

Write yourself a Scheme in 48 hours exercises
Haskell
1
star
74

getclojure-ui

A front-end for devn/getclojure
Ruby
1
star
75

apue

Advanced Programming in the UNIX Environment
C
1
star
76

1up

Project page for my one-year challenge
Ruby
1
star
77

warp-async-test

Can warp handle multiple requests at once?
Haskell
1
star
78

rustybank

Concurrency project created by "mob programming" at a meetup
Rust
1
star
79

crapyesod

Haskell
1
star
80

semiquandle

Experiments in classification
Python
1
star
81

mathnotes

Graduate mathematics notes in TeX
1
star
82

cmsis_nucleo

Experiments with CMSIS (+device family pack) and RTOS with static allocation
C
1
star
83

enigma

Totally random dynamic CSS
JavaScript
1
star
84

preview-ics

Preview iCal invitations from the command line (or mutt)
1
star
85

heroku-buildpack-ghc-test-yesod

Test yesod server deployment
CSS
1
star
86

motif

Trying examples from "Motif Programming" by Marshall Brain
C
1
star
87

heroku-buildpack-ghc-test-snap

Test snap server deployment
Haskell
1
star
88

slow-warp

Why isn't warp responding concurrently?
Haskell
1
star
89

decaying-accumulator

A number that tends toward zero but can be nudged
JavaScript
1
star
90

githhub-care-package

Daily list of repos that need your help
1
star