Hakyll on Nix

Posted on April 10, 2017 by Lucas Paul

Today I set up my website. Because I can’t procrastinate on it any longer. The hard part is, of course, writing something worth reading. But there is also a technical challenge, and I do like a technical challenge.

I hate the overcomplicated nature of the web. So for my site, I wanted to keep things simple. On the other hand, writing raw HTML doesn’t appeal to me. So I decided to use Hakyll to build my site out of markdown files. But I also use Nix. And I don’t like installing packages system-wide or even profile-wide. I want a project to depend on its tools and take responsibility for fulfilling those dependencies during its build, regardless of what I happen to have installed.

Fortunately, Utku Demir has similar tastes, and has blogged about his solution. I was able to reuse much of his code for my own. Thanks, Utku!

# Lucas' Yale website v. 2017-03-02

# Some code copied and/or modified from
# https://utdemir.com/posts/hakyll-on-nixos.html
let
  pkgs = import <nixpkgs> {};
  stdenv = pkgs.stdenv;
  generator = pkgs.stdenv.mkDerivation {
    name = "lucasPaulWebsiteGenerator";
    src = ./generator;
    phases = "unpackPhase buildPhase";
    buildInputs = [
      (pkgs.haskellPackages.ghcWithPackages (p: with p; [ hakyll ]))
    ];
    buildPhase = ''
      mkdir -p $out/bin
      ghc -O2 -dynamic --make site.hs -o $out/bin/generate-site
    '';
  };
in rec {
  lucasPaulWebsite = stdenv.mkDerivation rec {
    name = "lucasPaulWebsite";
    src = ./site;
    phases = "unpackPhase buildPhase";
    version = "0.1";
    buildInputs = [ generator ];
    buildPhase = ''
      export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive";
      export LANG=en_US.UTF-8
      generate-site build

      mkdir $out
      cp -r _site/* $out
    '';
  };
}