# Bubblegum — a nix CGI programming framework

**URL:** https://discourse.nixos.org/t/bubblegum-a-nix-cgi-programming-framework/12259
**Category:** Announcements
**Created:** [April 1, 2021, 7:27pm UTC](https://discourse.nixos.org/t/bubblegum-a-nix-cgi-programming-framework/12259 "2021-04-01T19:27:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sternenseemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/sternenseemann/32/3809_2.png) [@sternenseemann](https://discourse.nixos.org/u/sternenseemann)
#### Post date: [April 1, 2021, 7:27pm UTC](https://discourse.nixos.org/t/bubblegum-a-nix-cgi-programming-framework/12259/1 "2021-04-01T19:27:24Z")

</div>

Nix may not be a general purpose programming language, but it is a pretty decent special purpose programming language. It is pretty good at describing managing sets of package descriptions and — as it turns out — CGI programming. To be precise the following properties make it pretty feasible:

- `builtins.getEnv` allows us very easy access to the environment which is the main way the web server communicates with a CGI application
- The powerful string interpolation Nix offers makes using a templating engine practically obsolete and rendering HTML not feel like a chore.
- Attribute set can be used to make routing logic very simple and comprehensible, both in expression and implementation. For example: `route = path: routes."${path}" or notFound`.

With [bubblegum](https://cs.tvl.fyi/depot/-/blob/web/bubblegum/README.md) a simple CGI application looks like this:

```nix
{ depot, ... }:

let
  inherit (depot.web.bubblegum)
    respond
    ;
in

respond "OK" {
  "Content-type" = "text/html";
  # further headers…
} ''
  <!doctype html>
  <html>
    <head>
      <meta charset="utf-8">
      <title>hello world</title>
    </head>
    <body>
      hello world!
    </body>
  </html>
''

```

Of course applications are somewhat limited due to the nature of nix:

- It is pretty slow: Currently it uses a [simple wrapper around `nix-instantiate`](https://cs.tvl.fyi/depot/-/blob/users/sterni/nint/README.md) to interpret CGI scripts which already takes between 200ms and 300ms for trivial scripts and well over 1.5s for scripts using import from derivation where the output is already present in `/nix/store`.
- We can’t cause any effects except for adding files and directories to the nix store, building derivations and performing GET requests using `builtins.fetchurl` (although this probably only works with tarball TTL 0 and would be very slow).
- Error handling is very limited as `builtins.tryEval` doesn’t catch all errors nix evaluation generates.

However even in spite of that `bubblegum` can be used to write useful CGI applications. If you want to see for yourself you can run a few example scripts:

```auto
> git clone https://code.tvl.fyi/ depot && cd depot
> nix-build -A web.bubblegum && ./result
# navigate to http://localhost:9000

```

This distribution of examples contains a [working file based blog](https://cs.tvl.fyi/depot/-/blob/web/bubblegum/examples/blog.nix) and a [SVG which is built on demand from a derivation](https://cs.tvl.fyi/depot/-/blob/web/bubblegum/examples/derivation-svg.nix).

Future improvements would be supporting request bodies and thus `POST` and the introduction of streaming (maybe even multiplexed) I/O using a custom interpreter and emulated lazy lists via attribute sets.

Full source code can be [browsed in cgit here](https://code.tvl.fyi/tree/web/bubblegum).

---

<div class="post-metadata">

### Author: ![aanderse](https://discourse.nixos.org/user_avatar/discourse.nixos.org/aanderse/32/1420_2.png) [@aanderse](https://discourse.nixos.org/u/aanderse)
#### Post date: [April 1, 2021, 8:01pm UTC](https://discourse.nixos.org/t/bubblegum-a-nix-cgi-programming-framework/12259/2 "2021-04-01T20:01:40Z")

</div>

**lol**

I really love this.

---

<div class="post-metadata">

### Author: ![picnoir](https://discourse.nixos.org/user_avatar/discourse.nixos.org/picnoir/32/4186_2.png) [@picnoir](https://discourse.nixos.org/u/picnoir)
#### Post date: [April 1, 2021, 8:04pm UTC](https://discourse.nixos.org/t/bubblegum-a-nix-cgi-programming-framework/12259/3 "2021-04-01T20:04:06Z")

</div>

The more you dig into this, the better it gets.

> `nint` is a shebang compatible interpreter for nix. It is currently  
> implemented as a fairly trivial wrapper around `nix-instantiate --eval`.  
> It allows to run nix expressions as command line tools if they conform  
> to the following calling convention

\<3

This is amazing! Massive kudos.

---

<div class="post-metadata">

### Author: ![fricklerhandwerk](https://discourse.nixos.org/user_avatar/discourse.nixos.org/fricklerhandwerk/32/2029_2.png) [@fricklerhandwerk](https://discourse.nixos.org/u/fricklerhandwerk)
#### Post date: [April 3, 2021, 1:04pm UTC](https://discourse.nixos.org/t/bubblegum-a-nix-cgi-programming-framework/12259/4 "2021-04-03T13:04:44Z")

</div>

This is so insane and impractical that it’s hilarious, in every positive way. Great stuff!
