We have a complete example live that you can try out. All you need is a fresh server (such as on Amazon EC2) with at least 16GB memory. (We recommend the latest Ubuntu, with a t3Large instance, with 16GB RAM. Then allow SSH, HTTP traffic, and HTTPS traffic if you plan to build on these examples.) We have two repos:
The sample application
The configuration files
The configuration files install both nginx and the sample app.
After you spin up an instance, install nix for all users:
(Remember, the first time System Manager runs, it takes up to five minutes or so to compile everything.)
Tip
We're specifying a tag in our URL. This is good practice to make sure you get the right version of your flakes. Also, modern Nix supports the use of a protocol called "github", and when you use that protocol, you can specify the tag behind a slash symbol, as we did here for tag v1.0.0.
Tip
If you make changes to your flakes, be sure to create a new tag. Without it, Nix sometimes refuses to load the "latest version" of the repo, and will insist on using whatever version of your repo it used first.
Then, the app should be installed, with nginx sitting in front of it, and you should be able to run:
{"message":"Welcome to the Bun API!","status":"running","endpoints":["/","/health","/random","/cowsay"]}
We even included cowsay in this sample, which you can try at curl localhost/cowsay. Now even though cowsay is meant for fun, the primary reason is this is a TypeScript app that uses bun, and we wanted to demonstrate how easy it is to include npm libraries. bun includes a feature whereby it will install dependency packages from package.json automatically the first time it runs, greatly simplifying the setup.
One thing about the .nix files in this repo is that they in turn pull code (our TypeScript app) from another remote repo. Using this approach, you can separate concerns, placing the deployment .nix files in one repo, and the source app in a separate repo.
# flake.nix{description="Standalone System Manager configuration";inputs={# Specify the source of System Manager and Nixpkgs. nixpkgs.url="github:nixos/nixpkgs/nixos-unstable";system-manager={url="github:numtide/system-manager"; inputs.nixpkgs.follows="nixpkgs";};};outputs={ self, nixpkgs, system-manager,...}:letsystem="x86_64-linux";in{ systemConfigs.default= system-manager.lib.makeSystemConfig {# Specify your system configuration modules here, for example,# the path to your system.nix.modules=[{ nix.settings.experimental-features="nix-command flakes"; services.myapp.enable=true;}./system.nix./nginx.nix./bun-app.nix];# Optionally specify extraSpecialArgs and overlays};};}
Next is the .nix configuration that installs and configures nginx. This is a simple nginx configuration, as it simply routes incoming HTTP traffic directly to the app:
# bun-app.nix{ config, lib, pkgs,...}:let# Fetch the app from GitHubappSource= pkgs.fetchFromGitHub {owner="frecklefacelabs";repo="typescript_app_for_system_manager";rev="v1.0.0";# Use a tagsha256="sha256-TWt/Y2B7cGxjB9pxMOApt83P29uiCBv5nVT3KyycYEA=";};in{config={ nixpkgs.hostPlatform="x86_64-linux";# Install Bun environment.systemPackages=with pkgs;[ bun
];# Simple systemd service - runs Bun directly from Nix store! systemd.services.bunapp={description="Bun TypeScript Application";after=["network.target"];wantedBy=["multi-user.target"];serviceConfig={Type="simple";User="ubuntu";Group="ubuntu";WorkingDirectory="${appSource}";# Bun will auto-install dependencies from package.json on first runExecStart="${pkgs.bun}/bin/bun run index.ts";Restart="always";RestartSec="10s";};environment={NODE_ENV="production";};};};}
importcowsayfrom"cowsay";constmessages=["Hello from System Manager!","Bun is blazingly fast!","Nix + Bun = Easy deployments","Making it happen!","Nix rocks!"];constserver=Bun.serve({port:3000,fetch(req){consturl=newURL(req.url);if(url.pathname==="/"){returnnewResponse(JSON.stringify({message:"Welcome to the Bun API!",status:"running",endpoints:["/","/health","/random","/cowsay"]}),{headers:{"Content-Type":"application/json"}});}if(url.pathname==="/health"){returnnewResponse(JSON.stringify({status:"healthy"}),{headers:{"Content-Type":"application/json"}});}if(url.pathname==="/random"){constrandomMessage=messages[Math.floor(Math.random()*messages.length)];returnnewResponse(JSON.stringify({message:randomMessage,timestamp:newDate().toISOString()}),{headers:{"Content-Type":"application/json"}});}if(url.pathname==="/cowsay"){constcow=cowsay.say({text:"Deployed with System Manager and Nix!"});returnnewResponse(cow,{headers:{"Content-Type":"text/plain"}});}returnnewResponse("Not Found",{status:404});},});console.log(`Server running on http://localhost:${server.port}`);