This tutorial shows how to deploy an API on the Web using FastAPI on a NixOS system.
Add to your /etc/nixos/confirguration.nix
environment.systemPackages = with pkgs; [
direnv
];
git clone [email protected]:nphilou/fastapi-nixos-starter-pack.git
You'll probably get something like
direnv: error /home/.../fastapi-nixos-starter-pack/.envrc is blocked. Run `direnv allow` to approve its content
So you just have to run:
direnv allow
nix-build env.nix -o venv
Then tell PyCharm to use this venv
uvicorn main:app --reload
or using Python:
python main.py
The reload param is optional. If everything is working correctly, you can go to the next step.
- Use a DNS Service (like https://dns.he.net/) to link the domain name to your server
yourdomainname.com -> your_ip_address (A record)
- Create some port forwarding on your router
your_ip_address -> your_local_ip_address (on ports 80 and 443 for example)
- Import
myproject.nix
into your system:
This file contains the backend service, the reverse proxy and the SSL certification system
{ config, pkgs, ... }:
{
imports =
[
/home/.../myproject.nix
];
}
and sudo nixos-rebuild switch
.