Skip to content

Commit

Permalink
feat(voyager): introduce voyager nixos module
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed May 27, 2024
1 parent dbaedcf commit 38b5c93
Showing 1 changed file with 112 additions and 1 deletion.
113 changes: 112 additions & 1 deletion voyager/voyager.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ ... }: {
{ self, ... }: {
perSystem = { self', pkgs, system, config, crane, stdenv, dbg, ... }:
let
voyager = crane.buildWorkspaceMember {
Expand All @@ -21,4 +21,115 @@
};
checks = voyager.checks;
};

flake.nixosModules.voyager = { lib, pkgs, config, ... }:
with lib;
let
cfg = config.services.voyager;
in
{
options.services.voyager = {
enable = mkEnableOption "Voyager service";
package = mkOption {
type = types.package;
default = self.packages.${pkgs.system}.voyager;
};
chains = mkOption {
# The configuration design is breaking quite often, would be a waste
# of effort to fix the type for now.
type = types.attrs;
};
workers = mkOption {
type = types.int;
default = 20;
};
runtime-max-secs = mkOption {
type = types.int;
default = 1800;
};
db-url = mkOption {
type = types.str;
default = "postgres://voyager:voyager@localhost/voyager";
};
db-min-conn = mkOption {
type = types.int;
default = 20;
};
db-max-conn = mkOption {
type = types.int;
default = 20;
};
log-level = mkOption {
type = types.str;
default = "info";
description = "RUST_LOG passed to voyager";
example = "voyager=debug";
};
log-format = mkOption {
type = types.enum [ "json" "text" ];
default = "json";
example = "text";
};
};

config =
let
configJson = pkgs.writeText "config.json" (builtins.toJSON {
chain = cfg.chains;
voyager = {
num_workers = cfg.workers;
queue = {
type = "pg-queue";
database_url = cfg.db-url;
min_connections = cfg.db-min-conn;
max_connections = cfg.db-max-conn;
idle_timeout = null;
max_lifetime = null;
};
};
});
in
mkIf cfg.enable {
systemd.services.voyager-migration = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
description = "Voyager Migration";
serviceConfig = {
Type = "oneshot";
ExecStart = ''
${pkgs.lib.meta.getExe cfg.package} \
--config-file-path ${configJson} \
-l ${cfg.log-format} \
run-migrations
'';
};
environment = {
RUST_LOG = "debug";
RUST_BACKTRACE = "full";
};
};
systemd.services.voyager = {
wantedBy = [ "multi-user.target" ];
after = [ "voyager-migration.service" ];
partOf = [ "voyager-migration.service" ];
requires = [ "voyager-migration.service" ];
description = "Voyager";
serviceConfig = {
Type = "simple";
ExecStart = ''
${pkgs.lib.getExe cfg.package} \
--config-file-path ${configJson} \
-l ${cfg.log-format} \
relay
'';
Restart = mkForce "always";
RestartSec = 10;
RuntimeMaxSec = cfg.runtime-max-secs;
};
environment = {
RUST_LOG = "${cfg.log-level}";
};
};
};
};
}

0 comments on commit 38b5c93

Please sign in to comment.