Run serverless functions on NixOS using faasd - a lightweight & portable faas engine.
If you are new to faasd and OpenFaaS checkout the following resources to get started:
The easiest way to try out faasd-nix is to run a NixOS vm with nixos-shell.
This guide assumes you have the experimental flake commands enabled. To enable them add the following line to
~/.config/nix/nix.conf
:experimental-features = nix-command flakes
-
Start a shell with the tools needed to run the faasd-vm.
$ nix develop github:welteki/faasd-nix#faasd-vm
This will make nixos-shell and the faas-cli available in your shell.
-
Start the faasd-vm.
$ nixos-shell --flake github:welteki/faasd-nix#faasd-vm
This spawns a headless qemu virtual machine with faasd running and provides console access in the same terminal window.
-
Log in as "root" with an empty password.
-
Interact with faasd using the faas-cli.
# Login $ cat /var/lib/faasd/secrets/basic-auth-password | faas-cli login --password-stdin # Deploy a function from the function store $ faas-cli store deploy figlet # Invoke a function $ echo "faasd-nix" | faas-cli invoke figlet
-
Type
Ctrl-a x
to exit the virtual machine or run thepoweroff
command in the virtual machine console.
The faasd NixOS modules include some options to simplify common faasd configuration tasks.
Full reference: faasd NixOS module options
{
services.faasd.enable = true;
}
Adjust the gateway timeouts.
{
services.faasd.gateway = {
writeTimeout = 30;
readTimeout = 30;
upstreamTimeout = 35;
};
}
Add additional function namespaces using the services.faasd.namespaces
option.
{ services.faasd.namespaces = [ "dev" ]; }
All namespaces in this list will be created and labeled openfaas=true
so they can be used with faasd.
Declaratively deploy additional containers.
This is the grafana example taken from the serverless-book.
{
systemd.tmpfiles.rules = [
"d '/var/lib/faasd/grafana'"
];
services.faasd.containers = {
grafana = {
image = "docker.io/grafana/grafana:latest";
environment = [
"GF_AUTH_ANONYMOUS_ORG_ROLE=Admin"
"GF_AUTH_ANONYMOUS_ENABLED=true"
"GF_AUTH_BASIC_ENABLED=false"
];
volumes = [{
type = "bind";
source = "./grafana/";
target = "/etc/grafana/provisioning";
}];
cap_add = [ "CAP_NET_RAW" ];
depends_on = [ "prometheus" ];
ports = [ "3000:3000" ];
};
};
}
Increase the parallelism for async function invocations.
{ services.faasd.defaultQueue.maxInflight = 4; }
Easily deploy and configure multiple queues.
{
services.faasd.queues.slow-queue = {
maxInflight = 1;
natsChannel = "slow-queue";
};
}
Check the OpenFaaS documentation for more info on asynchronous functions.
The bootstrap folder contains an example of how to provision a NixOS instance on hetzner-cloud using terraform and deploy faasd on it.