Nix + Bun Architecture
Architecture Overview: This application demonstrates a hybrid architecture where Nix generates HTML content and Bun serves it, along with handling API endpoints and database operations.
System Information
Nix System: x86_64-linux
Nix Version: 2.24.12
Subscribe to Updates
This form demonstrates how Nix-generated content can interact with Bun's API and database:
Interaction Example:
- The email form above is a Web Component defined in
static/js/app.js
- Nix includes this JavaScript in the page using
builtins.readFile
- When you submit an email, the component sends a POST request to
/api/emails
- Bun's API handler validates the email and stores it in the SQLite database
- The component shows success or error messages based on the response
How This Architecture Works
Request Flow:
- Your browser makes a request to the server (e.g.,
/nix
)
- Bun receives the request and routes it to the appropriate handler
- For content pages like this one, Bun executes:
nix eval --arg route "/nix" --raw -f ./app get
- Nix evaluates the expression in
app/default.nix
, which imports app/get.nix
- Nix reads HTML templates from
static/html/
and JS from static/js/
- Nix generates the HTML content you're viewing right now
- Bun returns the generated HTML to your browser
Technology Roles:
Nix's Role
- Generates HTML content (what you're reading now)
- Imports and embeds JavaScript
- Handles routing across different URL paths
- Provides reproducible environment through flakes
Bun's Role
- Serves as the HTTP server (listening on port 3001)
- Routes requests to appropriate handlers
- Executes Nix expressions to get HTML content
- Provides API endpoints (
/api/emails
)
- Manages the SQLite database for email storage
- Offers health check endpoints (
/health/status
and /health/ready
)