A static website generator written in nix. Inspired by hugo.
Getting started
{ description = "My website generated using nixtml." ; inputs = { nixpkgs . url = "github:nixos/nixpkgs/nixos-unstable" ; flake-utils . url = "github:numtide/flake-utils" ; nixtml . url = "github:arnarg/nixtml" ; } ; outputs = { self , nixpkgs , flake-utils , nixtml , } : ( flake-utils . lib . eachDefaultSystem ( system : let pkgs = import nixpkgs { inherit system ; } ; in { packages . blog = nixtml . lib . mkWebsite { inherit pkgs ; name = "my-blog" ; baseURL = "https://my-blog.com" ; # Arbitrary metdata to be used in # templates. metadata = { lang = "en" ; title = "My Blog" ; description = "This is my blog" ; } ; # Walk a directory of markdown files # and create a page for each of them. content . dir = ./content ; # Copy an entire directory and symlink # in the final website derivation. static . dir = ./static ; # Collections are for paginating content # and generating RSS feeds. collections . blog = { path = "posts" ; # Posts in the collection should be # grouped by optional tags in posts' # frontmatter. taxonomies = [ "tags" ] ; } ; # Import any nixtml modules (good for # "themes"). imports = [ ./theme.nix ] ; } ; # Quickly build and serve website with # `nix run .#serve`. apps . serve = { type = "app" ; program = ( pkgs . writeShellScript "serve-blog" '' ${ pkgs . python3 } /bin/python -m http.server -d ${ self . packages . ${ system } . blog } 8080 '' ) . outPath ; } ; } ) ) ; }
Examples
Templates
Templates should be defined in modules under website.layouts . All templates should be a function to a string (or list of strings, that is automatically coerced to a string).
Nix functional HTML
In nixtml's lib there are functions for most commonly used HTML tags which can be used like this:
{ lib , ... } : let inherit ( lib . tags ) html head body div ; inherit ( lib ) attrs ; in { website . layouts . base = { path , content , ... } @ context : "
" + html [ ( attrs . lang metadata . lang ) ] [ ( head [ ] [ ( partials . head context ) ] ) ( body [ ( attrs . classes [ "font-sans" "bg-white" ] ) ] [ ( div [ ( attrs . classes [ "container" ] ) ] [ content ] ) ] ) ] ; }
... continue reading