http-nu
The surprisingly performant, Nushell-scriptable, cross.stream-powered, Datastar-ready HTTP server that fits in your back pocket.
Install
ยท
Reference
ยท
Discord
Install
eget cablehead/http-nu
Homebrew (macOS)
# Homebrew now asks you to trust a third-party tap before installing from it
brew trust --formula cablehead/tap/http-nu
# or if you use a few of cablehead's projects, and trust me ๐, the whole tap
# brew trust cablehead/tap
brew install cablehead/tap/http-nu
cargo
For fast installation using pre-built binaries:
cargo binstall http-nu
Or build from source:
cargo install http-nu --locked
Nix
nix-shell -p http-nu
http-nu is available in nixpkgs. For
packaging and maintenance documentation, see
NIXOS_PACKAGING_GUIDE.md.
Reference
GET: Hello world
$ http-nu :3001 -c '{|req| "Hello world"}'
$ curl -s localhost:3001
Hello world
Or from a file:
$ http-nu :3001 ./serve.nu
Try the live examples or run them
locally with the examples hub:
$ http-nu --datastar :3001 examples/serve.nu
$ http-nu --datastar --store ./store :3001 examples/serve.nu # enables store-dependent examples
UNIX domain sockets
$ http-nu ./sock -c '{|req| "Hello world"}'
$ curl -s --unix-socket ./sock localhost
Hello world
Watch Mode
Use -w / --watch to automatically reload when files change:
$ http-nu :3001 -w ./serve.nu
This watches the script's directory for any changes (including included files)
and hot-reloads the handler. Active SSE connections are
aborted on reload to trigger client reconnection.
[!WARNING]
The watch is recursive: keep only files that should trigger a reload in the
script's directory (serve.nu, templates/, static/, ...). Anything else
that churns there reloads the handler too.
Reading from stdin
Pass - to read the script from stdin:
$ echo '{|req| "hello"}' | http-nu :3001 -
With -w, send null-terminated scripts to hot-reload the handler:
$ (printf '{|req| "v1"}\0'; sleep 5; printf '{|req| "v2"}') | http-nu :3001 - -w
Each \0-terminated script replaces the handler.
POST: echo
$ http-nu :3001 -c '{|req| $in}'
$ curl -s -d Hai localhost:3001
Hai
Request metadata
The Request metadata is passed as an argument to the closure.
$ http-nu :3001 -c '{|req| $req}'
$ curl -s 'localhost:3001/segment?foo=bar&abc=123' # or
$ http get 'http://localhost:3001/segment?foo=bar&abc=123'
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
proto โ HTTP/1.1
method โ GET
uri โ /segment?foo=bar&abc=123
path โ /segment
remote_ip โ 127.0.0.1
remote_port โ 52007
trusted_ip โ 127.0.0.1
โ โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
headers โ host โ localhost:3001
โ user-agent โ curl/8.7.1
โ accept โ */*
โ โโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
โ โโโโโโฌโโโโโ
query โ abc โ 123
โ foo โ bar
โ โโโโโโดโโโโโ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
$ http-nu :3001 -c '{|req| $"hello: ($req.path)"}'
$ http get 'http://localhost:3001/yello'
hello: /yello
Response metadata
Set HTTP response status and headers using nushell's pipeline metadata:
"body" | metadata set { merge {'http.response': {
status: <number> # Optional, defaults to 204 if body is empty, 200 otherwise
headers: { # Optional, HTTP headers
<key>: <value> # Single value: "text/plain"
<key>: [<value>, <value>] # Multiple values: ["cookie1=a", "cookie2=b"]
}
}} }
Header values can be strings or lists of strings. Multiple values (e.g.,
Set-Cookie) are sent as separate HTTP headers per RFC 6265.
$ http-nu :3001 -c '{|req| "sorry, eh" | metadata set { merge {"http.response": {status: 404}} }}'
$ curl -si localhost:3001
HTTP/1.1 404 Not Found
transfer-encoding: chunked
date: Fri, 31 Jan 2025 08:20:28 GMT
sorry, eh
Multi-value headers:
"cookies set" | metadata set { merge {'http.response': {
headers: {
"Set-Cookie": ["session=abc; Path=/", "token=xyz; Secure"]
}
}} }
Content-Type Inference
Content-type is determined in the following order of precedence:
Headers set via http.response metadata:
"body" | metadata set { merge {'http.response': {
headers: {"Content-Type": "text/plain"}
}} }
Pipeline metadata content-type (e.g., from to yaml or
metadata set --content-type)
Inferred from value type:
- Record ->
application/json
- List ->
application/json (JSON array)
- Stream of records ->
application/x-ndjson (JSONL)
- Binary or byte stream ->
application/octet-stream
- Empty (null) -> no Content-Type header
Default: text/html; charset=utf-8
Examples:
# 1. Explicit header takes precedence
{|req| {foo: "bar"} | metadata set { merge {'http.response': {headers: {"Content-Type": "text/plain"}}} } }
# 2. Pipeline metadata
{|req| ls | to yaml } # Returns as application/x-yaml
# 3. Inferred from value type
{|req| {foo: "bar"} } # Record -> application/json
{|req| [{a: 1}, {b: 2}, {c: 3}] } # List -> application/json (array)
{|req| 1..10 | each { {n: $in} } } # Stream of records -> application/x-ndjson
{|req| 0x[deadbeef] } # Binary -> application/octet-stream
{|req| null } # Empty -> no Content-Type header
# 4. Default
{|req| "Hello" } # Returns as text/html; charset=utf-8
To consume a JSONL endpoint from Nushell:
http get http://localhost:3001 | from json --objects | each {|row| ... }
TLS Support
Enable TLS by providing a PEM file containing both certificate and private key:
$ http-nu :3001 --tls combined.pem -c '{|req| "Secure Hello"}'
$ curl -k https://localhost:3001
Secure Hello
Generate a self-signed certificate for testing:
$ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
$ cat cert.pem key.pem > combined.pem
HTTP/2 is automatically enabled for TLS connections:
$ curl -k --http2 -si https://localhost:3001 | head -1
HTTP/2 200
Logging
Control log output with --log-format:
human (default): Live-updating terminal output with startup banner,
per-request progress lines showing timestamp, IP, method, path, status,
timing, and bytes
jsonl: Structured JSON lines with scru128 stamps for log aggregation
Each request emits 3 phases: request (received), response (headers
sent), complete (body finished).
Human format
JSONL format
Events share a request_id for correlation:
$ http-nu --log-format jsonl :3001 '{|req| "hello"}'
{"stamp":"...","message":"started","address":"http://127.0.0.1:3001","startup_ms":42}
{"stamp":"...","message":"request","request_id":"...","method":"GET","path":"/","request":{...}}
{"stamp":"...","message":"response","request_id":"...","status":200,"headers":{...},"latency_ms":1}
{"stamp":"...","message":"complete","request_id":"...","bytes":5,"duration_ms":2}
Lifecycle events: started, reloaded, stopping, stopped, stop_timed_out
The print command outputs to the logging system (appears as message: "print"
in JSONL).
Errors are log events too (message: "error"): a script that fails to load or
to handle a request, connection and store failures, and panics. With
--log-format jsonl they are JSON lines on stdout like everything else, so
stderr is left for bad command-line flags. An error the server can't run past is
written out before it exits.
Trusted Proxies
When behind a reverse proxy, use --trust-proxy to extract client IP from
X-Forwarded-For. Accepts CIDR notation, repeatable:
$ http-nu --trust-proxy 10.0.0.0/8 --trust-proxy 192.168.0.0/16 :3001 '{|req| $req.trusted_ip}'
The trusted_ip field is resolved by parsing X-Forwarded-For right-to-left,
stopping at the first IP not in a trusted range. Falls back to remote_ip when:
- No
--trust-proxy flags provided
- Remote IP is not in trusted ranges
- No
X-Forwarded-For header present
Serving Static Files
You can serve static files from a directory using the .static command. This
command takes two arguments: the root directory path and the request path.
When you call .static, it sets the response to serve the specified file, and
any subsequent output in the closure will be ignored. The content type is
automatically inferred based on the file extension (e.g., text/css for .css
files).
Here's an example:
$ http-nu :3001 -c '{|req| .static "/path/to/static/dir" $req.path}'
For single page applications you can provide a fallback file:
$ http-nu :3001 -c '{|req| .static "/path/to/static/dir" $req.path --fallback "index.html"}'
Streaming responses
Values returned by streaming pipelines (like generate) are sent to the client
immediately as HTTP chunks. This allows real-time data transmission without
waiting for the entire response to be ready.
$ http-nu :3001 -c '{|req|
generate {|_|
sleep 1sec
{out: (date now | to text | $in + "\n") next: true }
} true
}'
$ curl -s localhost:3001
Fri, 31 Jan 2025 03:47:59 -0500 (now)
Fri, 31 Jan 2025 03:48:00 -0500 (now)
Fri, 31 Jan 2025 03:48:01 -0500 (now)
Fri, 31 Jan 2025 03:48:02 -0500 (now)
Fri, 31 Jan 2025 03:48:03 -0500 (now)
...
Use the to sse command to format records for the text/event-stream protocol.
Each input record may contain the optional fields data, id, event, and
retry which will be emitted in the resulting stream.
to sse
Converts {data? id? event? retry?} records into SSE format. Non-string data
values are serialized to JSON.
Auto-sets response headers: content-type: text/event-stream