Skip to content

Commit

Permalink
feat(nats-server): Add service
Browse files Browse the repository at this point in the history
  • Loading branch information
misuzu committed Jan 3, 2025
1 parent 032d358 commit 02aec92
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/nats-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nats-server

[NATS](https://nats.io) is a simple, secure and performant communications system for digital systems, services and devices.

## Usage example

<https://github.com/juspay/services-flake/blob/main/nix/services/nats-server_test.nix>
1 change: 1 addition & 0 deletions doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ short-title: Services
- [[minio]]#
- [[mongodb]]#
- [[mysql]]#
- [[nats-server]]#
- [[nginx]]#
- [[ollama]]#
- [[open-webui]]#
Expand Down
1 change: 1 addition & 0 deletions nix/services/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ in
./grafana.nix
./memcached.nix
./minio.nix
./nats-server.nix
./prometheus.nix
./pgadmin.nix
./cassandra.nix
Expand Down
122 changes: 122 additions & 0 deletions nix/services/nats-server.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{ pkgs
, lib
, name
, config
, ...
}:
let
settingsFormat = pkgs.formats.json { };
configFile = settingsFormat.generate "nats.conf" config.settings;
validateConfig =
file:
pkgs.runCommand "validated-nats.conf" { nativeBuildInputs = [ config.package ]; } ''
nats-server --config "${file}" -t
ln -s "${file}" "$out"
'';
in
{
options = {
package = lib.mkPackageOption pkgs "nats-server" { };
settings = lib.mkOption {
default = { };
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
server_name = lib.mkOption {
default = name;
example = "n1-c3";
type = lib.types.str;
description = ''
Name of the NATS server, must be unique if clustered.
'';
};
port = lib.mkOption {
default = 4222;
type = lib.types.port;
description = ''
Port on which to listen.
'';
};
monitor_port = lib.mkOption {
default = 8222;
type = lib.types.port;
description = ''
HTTP monitoring port.
'';
};
};
};
example = lib.literalExpression ''
{
port = 14222;
monitor_port = 18222;
accounts = {
"$SYS" = {
users = [
{ user = "admin"; pass = "admin"; }
];
};
js = {
jetstream = "enabled";
users = [
{ user = "js"; pass = "js"; }
];
};
};
cluster = {
name = "cluster";
port = 14248;
routes = [
"nats://localhost:14248"
"nats://localhost:24248"
"nats://localhost:34248"
];
};
jetstream = {
max_mem = "1G";
max_file = "10G";
};
};
'';
description = ''
Declarative NATS configuration. See the
[
NATS documentation](https://docs.nats.io/nats-server/configuration) for a list of options.
'';
};
};

config = {
outputs.settings.processes."${name}" = {
command = "${lib.getExe config.package} -c ${validateConfig configFile} -sd ${config.dataDir}";
readiness_probe =
let
# https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#health-healthz
nats-ready = pkgs.writeShellApplication {
runtimeInputs = with pkgs; [
curl
gnugrep
jq
];
name = "nats-ready";
text = ''
curl -sSfN http://localhost:${toString config.settings.monitor_port}/healthz | jq '.status' | grep "ok"
'';
};
in
{
exec.command = lib.getExe nats-ready;
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability = {
restart = "on_failure";
max_restarts = 5;
};
};
};
}
86 changes: 86 additions & 0 deletions nix/services/nats-server_test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{ lib, pkgs, ... }:
let
commonClusterSettings = {
accounts."$SYS".users = [
{
user = "admin";
pass = "admin";
}
];
accounts.js = {
jetstream = "enabled";
users = [
{
user = "js";
pass = "js";
}
];
};
jetstream.max_file = "128M";
cluster.name = "default-cluster";
cluster.routes = [
"nats://localhost:14248"
"nats://localhost:24248"
"nats://localhost:34248"
];
};
in
{
services.nats-server."nats".enable = true;

services.nats-server."nats-1" = {
enable = true;
settings = lib.recursiveUpdate commonClusterSettings {
port = 14222;
monitor_port = 18222;
cluster.port = 14248;
};
};
services.nats-server."nats-2" = {
enable = true;
settings = lib.recursiveUpdate commonClusterSettings {
port = 24222;
monitor_port = 28222;
cluster.port = 24248;
};
};
services.nats-server."nats-3" = {
enable = true;
settings = lib.recursiveUpdate commonClusterSettings {
port = 34222;
monitor_port = 38222;
cluster.port = 34248;
};
};

settings.processes.test = {
command = pkgs.writeShellApplication {
runtimeInputs = [ pkgs.natscli ];
text = ''
# standalone
nats account info -s nats://localhost:4222
# nats cluster with jetstream enabled
nats server info -s nats://admin:admin@localhost:14222
export NATS_URL=nats://js:js@localhost:14222
nats account info
# https://docs.nats.io/nats-concepts/jetstream/js_walkthrough
nats stream add my_stream --subjects=foo --storage=memory --replicas=3 --defaults
nats stream info my_stream
nats pub foo --count=5 "publication #{{Count}} @ {{TimeStamp}}"
nats consumer add my_stream pull_consumer --pull --replicas=3 --defaults
nats consumer next my_stream pull_consumer --count 5
nats stream rm -f my_stream
'';
name = "nats-test";
};
depends_on = {
"nats".condition = "process_healthy";
"nats-1".condition = "process_healthy";
"nats-2".condition = "process_healthy";
"nats-3".condition = "process_healthy";
};
};
}
1 change: 1 addition & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"${inputs.services-flake}/nix/services/minio_test.nix"
"${inputs.services-flake}/nix/services/mongodb_test.nix"
"${inputs.services-flake}/nix/services/mysql/mysql_test.nix"
"${inputs.services-flake}/nix/services/nats-server_test.nix"
"${inputs.services-flake}/nix/services/nginx/nginx_test.nix"
"${inputs.services-flake}/nix/services/ollama_test.nix"
"${inputs.services-flake}/nix/services/open-webui_test.nix"
Expand Down

0 comments on commit 02aec92

Please sign in to comment.