In this blog post I share my experiences with getting an example site up and running with Haskell, Yesod and Fay.
Developing and maintaining software is costly. Reducing those costs means that less is spent on development of the software and/or that it takes less to maintain it. In other words: there are great merits in increasing the efficiency of software development and maintenance. An important intangible benefit of this is that software developers love “to do more in less time, and to do it properly”.
At Hoppinger we are currently evaluating how we can use Haskell to better serve our customers, with happier developers and smaller development budgets.
At Hoppinger we like web-frameworks; Symfony2 on PHP, Rails on Ruby… and Yesod on Haskell.
Yesod nicely brings the speed and type-safety of Haskell to the world of web application development, and features most of the conveniences that we have come to know and love from using Rails.
When using Fay it is possible to share code —such as data structures and templating functions— between the server-side and the client-side. This allows the type-checker to help developers to avoid whole categories of bugs when writing web applications. Fay has build in serialization/deserialization of Haskell data structures to/from JSON, which reliefs developers from creating and maintaining a significant amount of error-prone boilerplate code that is otherwise needed by most modern web applications.
The following guide should get a basic Yesod+Fay app started in development mode on any recent version of Ubuntu.
First make sure to be switched to the users that you want to use for development, and install Haskell Platform and cabal (which is, amongst other things, Haskell’s package manager).
sudo apt-get update
sudo apt-get install haskell-platform
With the following command we can check the version of cabal.
If this is not 1.18.x of larger we need to upgrade it with the following commands.
cabal install Cabal cabal-install # the library and the binary
At this point make sure ~/.cabal/bin is in your $PATH. You can do so with export ~/.cabal/bin:$PATH for your current session, or add it to your ~/.profile for all future sessions.
Now we’re installing Yesod.
cabal update && cabal install yesod-bin
Since the scaffold application also makes used of Postgres we need to install that too.
sudo apt-get install libpq-dev postgresql
To avoid the “peer athentication error” do we need to fix the following config.
sudo sed -Ei "s/^(local\s+all\s+all\s+)peer$/\1md5/" \
And finally create a user and database for this project.
sudo -u postgres createuser -d -R -P \
sudo -u postgres createdb -O \
Next is to initialize (scaffold) a new Yesod project. To do so go to the directory you want it to be created in (~/repos in my case).
Now run the interactive yesod init command. Set the project name to yesod-fay-example and choose the pf (Postgress+Fay) app scaffold.
Next is installing (downloading and compiling) all the projects dependencies into a cabal sandbox. Cabal provides sandboxes as a way to localize packages to a project; this to avoid conflicting dependencies when working on several projects concurrently.
cabal sandbox init
cabal install --enable-tests --max-backjumps=-1 \
. yesod-platform yesod-bin fay-base
We need to set an environment variable to point to the right path in the local sandbox directory. This is workaround needed up to cabal version 1.20.
export HASKELL_PACKAGE_SANDBOX=`echo .cabal-sandbox/*conf.d/`
That was it. You should be able to get your development environment up and running with the following command.
When pointing your browser to http://localhost:3000 you should be able to see the scaffolded site in action.
It is minimal, but (for me) it works… And from here I was able to extend it into a larger application.
When looking at the code in the project directory we can observe several interesting things:
While this guide shows how little is involved in getting Yesod and Fay up and running, it is possible to get started with just Yesod much faster. The company FPComplete has a web-base IDE named Haskell Center (demo video) for which you do not have to install any software at all — and it has a community edition which is free. But at this moment it is not possible (yet) to use Fay on this platform.
Fay is not the only way to run Haskell in the browser, there are more roads to Rome. Another interesting approach —that I yet have to look at more closely— is Haste. When I have some experience with this I will certainly write another blog on it.
Image credit: Bendan Giffen (@bendangiffen). It shows data harvested from Wikipedia on the influences that programming languages have on each other. Whilst relatively unknown Haskell is shown to have been very influential thus far.