Blog»2012-08-28-why-a-static-site-using-hakyll

2012-08-28-why-a-static-site-using-hakyll

--- title: Why Have a Static Site Using Hakyll? tags: hakyll, haskell, blogging, meta ---

As my "hello world" for this site I thought I'd talk a little bit about the technology running the site and my reasons for choosing it.

This website is a collection of static files served by Nginx. There is no database, and no php, python or any other programming language performing any kind of content creation or request routing.

Choosing to run a static site in this age of wordpress, tumblr and Google+ may seem like an odd decision. But static sites have certain advantages and there are some new techniques and technologies which make working with them much easier.

I will first answer the question "why a static site?" and then move onto explaining my choice of site generator (Hakyll).

1 Why a Static Site?

Static websites have a couple of advantages over dynamic, database driven applications:

  • No need to setup a database which makes them simpler to run and host
  • Easier to scale. Nginx can deal with thousands of concurrent connections when serving static files.
  • More security. There is much less that can go wrong with a static website; the attack surface is smaller.

All these are good reasons, but they were not enough to convince me. At the kind of scale I operate at worrying about thousands of users is premature optimisation and I am already running databases and dynamic applications elsewhere.

Two things turned me on to the idea of a static website. Then when I found out that a static website could still look good and be easy to update I was sold. The two things were:

  • Codename: Svbtle by dcurtis made me realise how blogging and content creation workflows can improve both the quality of content and the ease with which it is produced.
  • Org Mode is primarily the world's best todo list app and this is how I first came across it. However, it may also be the world's best tool for any sort of writing. Period. I find using org mode for drafting and organising very easy and very productive.

I had found my desired work flow: I want to blog using org mode.

At the time, I was using Zotonic partly to learn a bit of Erlang and partly because I really quite liked it as a CMS. I tried to figure out a way to use org-publish (an org mode utility to convert org mode files to HTML and other formats) and the Zotonic API to publish text files straight to Zotonic. This turned out to be beyond me.

Next I looked for another CMS which would allow me to publish from org mode. Googling turned up two main candidates:

  • org-publish which is part of org mode. The easiest to integrate, but I wasn't sure how flexible it would be.
  • Jekyll

Both of these solutions were static site generators.

In the end I chose neither of these and went with a third option: Hakyll, a Haskell library for generating static sites.

2 Why Hakyll?

The main reason is that customising Hakyll uses Haskell which is the programming language I know best. This meant that I felt confident that I would (eventually) be able to get the site to do what I wanted.

3 How it all works

I have a folder called "eanalytica" within which are several subfolders:

  • Folders containing "static" files like images, css and javascript
  • A templates folder
  • A posts folder

The main folder also contains a Haskell program called "site" that is written using the Hakyll library.

When I run the "site" program it uses the contents of the posts folder and the templates to generate the static HTML files which it places in a folder called "site". Then all I have to do is upload the _site folder to my web server. You can read the source code to my Hakyll site generator if you want to find out a bit more about how this works. The official Hakyll site also has some documentation.

So now my blogging workflow looks like this:

  1. Create a new blog post using org mode.
  2. Use org-publish to turn this post into HTML and place it in the "posts" directory of my Hakyll site folder
  3. Run the "site" program to update all the _site folder
  4. Upload any changes to the _site folder to my web server.

In fact, it is even simpler than that because three of the above four steps can be automated.

I'm only one post in, but already I like my new workflow.

Authored by Richard Fergie