-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters