Skip to content

Commit

Permalink
feat: add nixos module for the store
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 12, 2023
1 parent acc3291 commit c55d77f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
} {
imports = [
./nix
./nixos
];
systems = [
"x86_64-linux"
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/store/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Run struct {
Log cli.LogOptions `embed:""`
Nats cli.NatsOptions `embed:""`

ListenAddr string `short:"l" env:"NVIX_STORE_LISTEN_ADDR" default:"localhost:5000"`
MetricsAddr string `short:"m" env:"NVIX_STORE_METRICS_ADDR" default:"localhost:5050"`
ListenAddr string `short:"l" env:"LISTEN_ADDR" default:"localhost:5000"`
MetricsAddr string `short:"m" env:"METRICS_ADDR" default:"localhost:5050"`
}

func (r *Run) Run() error {
Expand Down
4 changes: 2 additions & 2 deletions nix/dev/nvix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
};
working_dir = "$PRJ_DATA_DIR";
environment = {
NVIX_STORE_NATS_CREDENTIALS_FILE = "./nsc/creds/Tvix/Store/Admin.creds";
NATS_CREDENTIALS_FILE = "./nsc/creds/Tvix/Store/Admin.creds";
};
command = "${lib.getExe self'.packages.nvix} store init -v";
};
Expand All @@ -19,7 +19,7 @@
};
working_dir = "$PRJ_DATA_DIR";
environment = {
NVIX_STORE_NATS_CREDENTIALS_FILE = "./nsc/creds/Tvix/Store/Server.creds";
NATS_CREDENTIALS_FILE = "./nsc/creds/Tvix/Store/Server.creds";
};
command = "${lib.getExe self'.packages.nvix} store run -v";
# TODO readiness probe
Expand Down
5 changes: 5 additions & 0 deletions nixos/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
flake.nixosModules = {
store = import ./store.nix;
};
}
81 changes: 81 additions & 0 deletions nixos/store.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
lib,
pkgs,
config,
...
}: let
cfg = config.services.nvix.store;
in {
options.services.nvix.store = with lib; {
enable = mkEnableOption (mdDoc "Enable NVIX Store");
package = mkOption {
type = types.package;
default = pkgs.nvix;
defaultText = literalExpression "pkgs.nvix";
description = mdDoc "Package to use for nits.";
};
listen = {
address = mkOption {
type = types.str;
default = "localhost:5000";
description = "interface and port to listen on";
};
};
metrics = {
address = mkOption {
type = types.str;
default = "localhost:5050";
description = "interface and port to listen on";
};
};
nats = {
url = mkOption {
type = types.str;
example = "nats://localhost:4222";
description = mdDoc "NATS server url.";
};
credentialsFile = mkOption {
type = types.path;
example = "/mnt/shared/user.creds";
description = mdDoc "Path to a file containing a NATS credentials file";
};
};
verbosity = mkOption {
type = types.int;
default = 1;
example = "2";
description = mdDoc "Selects the log verbosity.";
};
};

config = lib.mkIf cfg.enable {
systemd.services.nvix-store = {
after = ["network.target"];
wantedBy = ["sysinit.target"];

description = "NVIX Store";
startLimitIntervalSec = 0;

# the agent will restart itself after a successful deployment
restartIfChanged = false;

environment = lib.filterAttrs (_: v: v != null) {
NATS_URL = cfg.nats.url;
NATS_CREDENTIALS_FILE = cfg.nats.credentialsFile;
LISTEN_ADDRESS = cfg.listen.address;
METRICS_ADDRESS = cfg.metrics.address;
LOG_LEVEL = "${builtins.toString cfg.verbosity}";
};

serviceConfig = with lib; {
Restart = mkDefault "on-failure";
RestartSec = 1;

User = "nvix-store";
DynamicUser = true;
StateDirectory = "nvix-store";
ExecStart = "${cfg.package}/bin/nvix store";
};
};
};
}
2 changes: 1 addition & 1 deletion pkg/cli/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type LogOptions struct {
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"NVIX_LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`
}

func (lo *LogOptions) ConfigureLogger() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type NatsOptions struct {
NatsUrl string `short:"n" env:"NVIX_STORE_NATS_URL" default:"nats://localhost:4222"`
NatsCredentials string `short:"c" env:"NVIX_STORE_NATS_CREDENTIALS_FILE" required:"" type:"path"`
NatsUrl string `short:"n" env:"NATS_URL" default:"nats://localhost:4222"`
NatsCredentials string `short:"c" env:"NATS_CREDENTIALS_FILE" required:"" type:"path"`
}

func (no *NatsOptions) Connect() *nats.Conn {
Expand Down

0 comments on commit c55d77f

Please sign in to comment.