Deploying with deploy-to-nixos
IHP comes with a standard command calleddeploy-to-nixos. This tool is a little wrapper around nixos-rebuild and allows you to deploy IHP apps to a NixOS server. With deploy-to-nixos you can manage your servers in a fully declarative way and keep the full configuration in your git repository.
AWS EC2 is a good choice for deploying IHP in a professional setup.
AWS Infrastructure Preparation
- Using Terraform
- Manual EC2 Setup
The EC2 instance, RDS database, VPS, subnets, security groups, etc, can be setup automatically using Terraform.
Copy IaC files
Copy the files from the IaC/aws folder from the IaC-aws branch in ihp-boilerplate to your IHP project repo. Run the init command:
Create terraform.tfvars
Create the file
terraform.tfvars with the following content:The two AZs are needed to setup the RDS database. The SSH key-pair should be created in the AWS web interface.
Connecting to the EC2 Instance
After you’ve created the instance, configure your local SSH settings to point to the instance. In your~/.ssh/config add:
The SSH host name must match the NixOS configuration name used in your
flake.nix. Projects generated by ihp-new use production as the default.ssh production.
Configuring the Instance
Projects generated byihp-new already include deployment configuration files under Config/nix/hosts/production/:
Editing configuration.nix
OpenConfig/nix/hosts/production/configuration.nix and update:
Editing hardware-configuration.nix
For AWS EC2 deployments, uncomment the Amazon AMI import:Deploying the App
Now you can deploy the app usingdeploy-to-nixos:
nixosConfigurations key in your flake.nix.
Troubleshooting / Operations
If a deployment goes wrong, login to the EC2 instance:Deploying with Docker
Deploying IHP with docker is a good choice for a professional production setup. IHP has a first party CLI tool calledihp-app-to-docker-image to create Docker images out of your app. This tool is available with IHP Pro and IHP Business.
Creating a Docker Image
To create a Docker image, first install Podman, then run:podman images shows the image:
The
CREATED timestamp shows over 50 years ago because the image is built with nix. For reproducible builds, the timestamp is set to Jan 1970, 00:00 UTC.Starting the App Container
You can start your app container like this:Connecting the DB
You need to connect a postgres database. It’s recommended to use a managed database service like AWS RDS. For a quick setup you can use docker:DATABASE_URL env variable:
Recommended Env Variables
- IHP_SESSION_SECRET
- IHP_BASEURL
- IHP_REQUEST_LOGGER_IP_ADDR_SOURCE
In production setups you want to configure the Use it:
IHP_SESSION_SECRET env variable. It’s a private key used to encrypt your session state.Generate a new secret:TLS Certificates in Docker Images
If your container makes HTTPS requests and you see certificate errors, your image likely doesn’t contain a root CA bundle. Fix by overriding the IHP Docker image to include CA certificates:Deploying on Bare Metal
You can build and deploy your IHP app on your own server without external deployment tools.Install Nix on your server
Copy your project folder to your server
Copy your application source code to the build server. If you’re usinggit, we recommend you use SSH agent forwarding.
Configuration
IHP apps are typically configured using environment variables:- Set
IHP_ENV=Productionto enable production mode - Set
IHP_BASEURL=https://{yourdomain} - Set
DATABASE_URLto your Postgres connection URL - Set
PORTto the port the app will listen on - Configure any custom settings
The database needs the UUID extension:
create extension if not exists "uuid-ossp";Building
Inside your project directory, build your app:result/bin/RunProdServer.
Starting the app
Start your app by running:systemd Integration
Thedeploy-to-nixos tool includes systemd integration to improve reliability:
Key Features
- Systemd Watchdog: The app sends a heartbeat every 30 seconds. If unresponsive, systemd restarts it after 60 seconds.
- Socket Activation: Systemd queues incoming HTTP requests during app startup or restarts, eliminating downtime.
-
Automatic Configuration: The
IHP_SYSTEMDenvironment variable is set to"1"automatically when deploying withdeploy-to-nixos.