hazine 0.2.4

Runs a DuckDB Quack server for DuckLake on localhost, reached through an SSH tunnel.
Documentation
<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="assets/hazine-bus-dark.png">
    <img src="assets/hazine-bus.png" alt="hazine: a double-decker bus" width="200">
  </picture>
</p>

# hazine

hazine runs a [DuckDB](https://duckdb.org) server that speaks the Quack
protocol, for a DuckLake catalog, on **localhost only**. Clients reach it
through an SSH tunnel.

It follows the pattern of DuckDB's own Quack deployment
([duckdb-quack-infra](https://github.com/duckdb/duckdb-quack-infra)): the
DuckDB CLI runs an init file that ends in `quack_serve`, and keeps serving for
as long as its stdin stays open. hazine replaces `sleep infinity | duckdb
-init init.sql` with a supervisor that:

- refuses any non-loopback listen address,
- checks that the CLI is DuckDB 2.0 or later,
- holds DuckDB's stdin open, and closes it to stop DuckDB cleanly on SIGTERM
  (killing it after `--stop-timeout`),
- forwards DuckDB's output to its log (text or JSON), with the token and
  other secrets redacted,
- restarts DuckDB with backoff, and exits non-zero after `--max-failures`
  failed starts in a row, so systemd sees the failure.

nginx and TLS are not needed: the SSH tunnel provides authentication and
encryption, and Quack checks a token on top.

The DuckLake catalog lives in a Postgres server and the data in
S3-compatible object storage, both elsewhere. The catalog is DuckDB's main
database, so every Quack session uses it without `USE` or qualified names.

## Requirements

- Linux with systemd, for `hazine setup`
- A Postgres database for the catalog, and an S3 bucket for the data
- Rust ≥ 1.85 to build (`cargo install hazine`)

## Server setup

`hazine setup` does everything as root:

1. installs hazine as `/usr/local/bin/hazine`
2. downloads the DuckDB CLI (2.0.1 by default) as `/usr/local/bin/duckdb`
3. creates the `hazine` system user, home `/var/lib/hazine`
4. installs the `quack`, `ducklake`, `postgres` and `httpfs` extensions for it
5. attaches the lake once, to check the settings, creating the DuckLake
   catalog with its data path the first time
6. writes `/etc/hazine/init.sql`, `/etc/hazine/hazine.env` (secrets, `0640
   root:hazine`) and `/etc/systemd/system/hazine.service`, sized to the
   machine's CPUs and RAM
7. enables and starts the service

Setup asks for anything it needs that isn't given as a flag:

- the Postgres host
- the data path
- the S3 endpoint, region and URL style
- the Postgres password and the S3 keys, without echoing them

It offers the values from the last run as defaults. For Hetzner Object
Storage, the endpoint is `<location>.your-objectstorage.com`; setup then
suggests the location as the region (e.g. `fsn1`) and `path` as the URL style.
For unattended runs, pass the secrets as `HAZINE_PG_PASSWORD`,
`HAZINE_S3_KEY_ID` and `HAZINE_S3_SECRET` in the environment. Flags work too,
but other users can see them in the process list.

```sh
sudo ~/.cargo/bin/hazine setup --pg-database ducklake --pg-user hazine
```

Add `--dry-run` to see the plan and the files first (secrets redacted, no root
needed). Rerun setup to change settings. Secrets and the Quack token are kept
from the existing `hazine.env` unless you give new ones. A changed file is
kept as `<file>.bak`. If the lake check fails, nothing is written. The
service restarts only if something changed.

Postgres connections use `--pg-sslmode require` by default. The data path is
stored in the catalog the first time and cannot be changed by rerunning
setup.

## Connecting

```sh
ssh -N -L 1294:127.0.0.1:1294 <host>
```

```sql
LOAD quack;
FROM quack_query('quack:127.0.0.1:1294', 'SELECT count(*) FROM my_table',
                 token = '<QUACK_TOKEN>', disable_ssl = true);
```

Get the token on the server with
`sudo sed -n 's/^QUACK_TOKEN=//p' /etc/hazine/hazine.env`.

### In the DuckDB UI

On your own machine (macOS or Linux, with the DuckDB 2.0 CLI and `cargo
install hazine`):

```sh
hazine ui hermione.front-matter.de
```

This opens the SSH tunnel and starts the [DuckDB UI](https://duckdb.org/docs/stable/core_extensions/ui)
in your browser, with the server attached as a database named after the host
(`hermione`). Browse its tables in the UI, or query them as
`hermione.my_table`. Quit DuckDB in the terminal (`.exit` or Ctrl-D) to close
the tunnel; Ctrl-C cancels a running query.

The token is read from the server's `/etc/hazine/hazine.env` over SSH, which
needs `sudo` without a password there; otherwise set `HAZINE_TOKEN`. Options:
`--name`, `--local-port` (tunnel, default 1294), `--ui-port` (default 4213;
use another if a DuckDB UI is already open), `--database` (a local file to
open alongside), `--no-open` (print the UI's URL instead of opening it).

## OpenAlex

hazine can import the [OpenAlex](https://openalex.org) snapshot (CC0), which
OpenAlex publishes quarterly as Parquet in the public bucket
`s3://openalex/data/parquet/`. Each release replaces the previous one there,
so import a release to keep it. On the server, as root, in `tmux` (the full
release is about 800 GB and takes hours):

```sh
sudo hazine openalex import --dry-run   # the release, its tables and sizes
sudo hazine openalex import             # all entities, works last
sudo hazine openalex setup              # point the openalex views at it
```

`import` reads exactly the files in the release's manifest into the schema
`openalex_<release date>` (e.g. `openalex_2026_10_14`), one table per entity,
and checks each table's row count against the manifest. It resumes: rerun it
after an interruption, and complete tables are skipped. It stops if OpenAlex
publishes a new release meanwhile. `--entities works,authors` imports only
some; `--threads` and `--memory-limit` (default: half the CPUs, a fifth of
RAM) keep room for hazine serve.

`setup` creates views in the `openalex` schema on the newest imported release
(or `--release 2026-10-14`), so queries don't name a release:

```sql
FROM openalex.release;   -- which release the views show
SELECT publication_year, count(*) FROM openalex.works GROUP BY ALL ORDER BY 1;
```

After importing the next release, run `setup` again to switch; drop old
release schemas when you no longer need them.

## Configuration

Precedence: flags > environment > defaults. See `hazine setup --help` and
`hazine serve --help`. These are the `serve` settings; `setup` writes them to
`hazine.env` and the unit.

| Flag              | Environment            | Default                |
|-------------------|------------------------|------------------------|
| `--duckdb`        | `HAZINE_DUCKDB`        | `duckdb` (from `PATH`) |
| `--init`          | `HAZINE_INIT`          | `/etc/hazine/init.sql` |
| `--database`      | `HAZINE_DATABASE`      | in memory; setup: `ducklake:postgres:` |
| `--quack-addr`    | `HAZINE_QUACK_ADDR`    | `127.0.0.1:1294`       |
| `--ready-timeout` | `HAZINE_READY_TIMEOUT` | `60` seconds           |
| `--stop-timeout`  | `HAZINE_STOP_TIMEOUT`  | `30` seconds           |
| `--max-failures`  | `HAZINE_MAX_FAILURES`  | `5`                    |
| `--log-format`    | `HAZINE_LOG_FORMAT`    | `text` (or `json`)     |

The log level comes from `RUST_LOG` (default `info`).

## License

MIT