Htnl: Nix library for making websites

Hey. I don’t know that this is a good idea. But I did it. At least some of it. Probably much of it. But there must be things I haven’t thought about. I did think about performance, so I’ve tried to minimize string allocations. But I didn’t benchmark anything. It seems to work, but is not used for any challenging amount of content.

Also, see issues.

Also, it’s almost midnight and this is pushed out now to impress the folks at Thaiger Sprint tomorrow.

13 Likes

I thought about this a few hours ago “hmm I wonder if I could render HTML nicely with Nix” and now this announcement comes! :grin:

2 Likes

this reminds me a bit of htmnix - i think that was the one briefly presented on at nixcon last year.

the fediversity website used to use nix-generated html as well (until the new person in charge of content at the time turned out to like wordpress better :sweat_smile:).

3 Likes

With nix to be an universal builder for literally anything why not HTML :joy: :fire:

2 Likes

Very neat. For additional ergonomics, would it be possible to do something like the following?

let
  h = htnl.polymorphic.element;
  e = builtins.listToAttrs (map (name: { inherit name; value = h name;
 }) ["br", "span"])
in {
  expr = e.span (e.br { }) |> serialize;
  expected = "<span><br></span>";
}
1 Like

In ngipkgs, we use module system to generate html. See ngipkgs/overview/content-types at main · ngi-nix/ngipkgs · GitHub .

4 Likes

Such functions are already provided under htnl.polymorphic.partials!

2 Likes

Is htnl missing any features compared to that?

htnl now includes a web bundler, believe it or not. And assets can be included as derivations in attribute value position, such as

let
  asset = pkgs.writeText "file.txt" "contents";
in
a { href = asset; } "Download"

Assets end up in the bundle store, /nix/store/<hash>-file.txt and the attribute value is resolved correctly.

So browser cache invalidation is easy to set up, because the asset paths are immutable hashes.

Next up: non-root base paths.

Submitted overlooking a pool, sauna and ice bath during Thaiger Sprint.

2 Likes