Skip to content

Commit

Permalink
build: nixOS service changes
Browse files Browse the repository at this point in the history
Include cardano-graphql-background in build of cardano-graphql

 and include cardano-graphql-background-service.nix in module-list.nix

fix cfg attr for cardano-graphql-background svc
  • Loading branch information
rhyslbw committed Feb 1, 2023
1 parent d669346 commit f8fdcd1
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 54 deletions.
6 changes: 6 additions & 0 deletions nix/cardano-graphql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ in stdenv.mkDerivation {
exec ${nodejs}/bin/node $out/packages/server/dist/index.js
EOF
chmod +x $out/bin/cardano-graphql
cat <<EOF > $out/bin/cardano-graphql-background
#!${runtimeShell}
exec ${nodejs}/bin/node $out/packages/api-cardano-db-hasura/dist/background.js
EOF
chmod +x $out/bin/cardano-graphql-background
'';
}
121 changes: 121 additions & 0 deletions nix/nixos/cardano-graphql-background-service.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

{ lib, pkgs, config, ... }:
let
cfg = config.services.cardano-graphql-background;
selfPkgs = import ../pkgs.nix {};
in {
options = {
services.cardano-graphql-background = {
enable = lib.mkEnableOption "cardano-explorer graphql background service";

assetMetadataUpdateInterval = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};

dbHost = lib.mkOption {
type = lib.types.str;
default = "/run/postgresql";
};

dbPassword = lib.mkOption {
type = lib.types.str;
default = ''""'';
};

dbPort = lib.mkOption {
type = lib.types.int;
default = 5432;
};

dbUser = lib.mkOption {
type = lib.types.str;
default = "cexplorer";
};

db = lib.mkOption {
type = lib.types.str;
default = "cexplorer";
};

enginePort = lib.mkOption {
type = lib.types.int;
default = 9999;
};

loggerMinSeverity = lib.mkOption {
type = lib.types.str;
default = "info";
};

hasuraIp = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
};

hasuraProtocol = lib.mkOption {
type = lib.types.str;
default = "http";
};

metadataServerUri = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};

ogmiosHost = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
};

ogmiosPort = lib.mkOption {
type = lib.types.int;
default = 1337;
};
};
};
config = let
boolToNodeJSEnv = bool: if bool then "true" else "false";
frontend = selfPkgs.packages.cardano-graphql;
persistgraphql = selfPkgs.packages.persistgraphql;
hasura-cli = selfPkgs.packages.hasura-cli;
hasura-cli-ext = selfPkgs.packages.hasura-cli-ext;
hasuraBaseUri = "${cfg.hasuraProtocol}://${cfg.hasuraIp}:${toString cfg.enginePort}";
pluginLibPath = pkgs.lib.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
];
installHasuraCLI = ''
# always start with no plugins so future upgrades will work
rm -rf ~/.hasura/plugins
mkdir -p ~/.hasura/plugins/store/cli-ext/v${hasura-cli-ext.version}
ln -s ${hasura-cli-ext}/bin/cli-ext-hasura-linux ~/.hasura/plugins/store/cli-ext/v${hasura-cli-ext.version}/cli-ext-hasura-linux
'';
in lib.mkIf cfg.enable {
systemd.services.cardano-graphql = {
wantedBy = [ "multi-user.target" ];
wants = [ "graphql-engine.service" ];
after = [ "graphql-engine.service" ];
environment = lib.filterAttrs (k: v: v != null) {
HASURA_CLI_PATH = hasura-cli + "/bin/hasura";
HASURA_GRAPHQL_ENABLE_TELEMETRY = toString false;
HASURA_URI = hasuraBaseUri;
LOGGER_MIN_SEVERITY = cfg.loggerMinSeverity;
POSTGRES_DB = cfg.db;
POSTGRES_HOST = cfg.dbHost;
POSTGRES_PASSWORD = cfg.dbPassword;
POSTGRES_PORT = toString cfg.dbPort;
POSTGRES_USER = cfg.dbUser;
} //
(lib.optionalAttrs (cfg.assetMetadataUpdateInterval != null) { ASSET_METADATA_UPDATE_INTERVAL = toString cfg.assetMetadataUpdateInterval; }) //
(lib.optionalAttrs (cfg.metadataServerUri != null) { METADATA_SERVER_URI = toString cfg.metadataServerUri; });
path = with pkgs; [ netcat curl postgresql frontend hasura-cli glibc.bin patchelf ];
preStart = ''
set -exuo pipefail
${installHasuraCLI}
'';
script = ''
exec cardano-graphql-background
'';
};
};
}
55 changes: 1 addition & 54 deletions nix/nixos/cardano-graphql-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,6 @@ in {
services.cardano-graphql = {
enable = lib.mkEnableOption "cardano-explorer graphql service";

dbHost = lib.mkOption {
type = lib.types.str;
default = "/run/postgresql";
};

dbPassword = lib.mkOption {
type = lib.types.str;
default = ''""'';
};

dbPort = lib.mkOption {
type = lib.types.int;
default = 5432;
};

dbUser = lib.mkOption {
type = lib.types.str;
default = "cexplorer";
};

db = lib.mkOption {
type = lib.types.str;
default = "cexplorer";
};

enginePort = lib.mkOption {
type = lib.types.int;
default = 9999;
Expand Down Expand Up @@ -105,18 +80,10 @@ in {
default = null;
description = "Source directory or file to generate allow-list from";
};
metadataServerUri = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
pollingIntervalAdaSupply = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};
assetMetadataUpdateInterval = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};
maxQueryComplexity = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
Expand All @@ -128,36 +95,22 @@ in {
boolToNodeJSEnv = bool: if bool then "true" else "false";
frontend = selfPkgs.packages.cardano-graphql;
persistgraphql = selfPkgs.packages.persistgraphql;
hasura-cli = selfPkgs.packages.hasura-cli;
hasura-cli-ext = selfPkgs.packages.hasura-cli-ext;
hasuraBaseUri = "${cfg.hasuraProtocol}://${cfg.hasuraIp}:${toString cfg.enginePort}";
pluginLibPath = pkgs.lib.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
];
installHasuraCLI = ''
# always start with no plugins so future upgrades will work
rm -rf ~/.hasura/plugins
mkdir -p ~/.hasura/plugins/store/cli-ext/v${hasura-cli-ext.version}
ln -s ${hasura-cli-ext}/bin/cli-ext-hasura-linux ~/.hasura/plugins/store/cli-ext/v${hasura-cli-ext.version}/cli-ext-hasura-linux
'';
in lib.mkIf cfg.enable {
systemd.services.cardano-graphql = {
wantedBy = [ "multi-user.target" ];
wants = [ "graphql-engine.service" ];
after = [ "graphql-engine.service" ];
environment = lib.filterAttrs (k: v: v != null) {
CARDANO_NODE_CONFIG_PATH = cfg.cardanoNodeConfigPath;
HASURA_CLI_PATH = hasura-cli + "/bin/hasura";
HASURA_GRAPHQL_ENABLE_TELEMETRY = toString false;
HASURA_URI = hasuraBaseUri;
LOGGER_MIN_SEVERITY = cfg.loggerMinSeverity;
OGMIOS_HOST = cfg.ogmiosHost;
OGMIOS_PORT = toString cfg.ogmiosPort;
POSTGRES_DB = cfg.db;
POSTGRES_HOST = cfg.dbHost;
POSTGRES_PASSWORD = cfg.dbPassword;
POSTGRES_PORT = toString cfg.dbPort;
POSTGRES_USER = cfg.dbUser;
PROMETHEUS_METRICS = boolToNodeJSEnv cfg.enablePrometheus;
TRACING = boolToNodeJSEnv (cfg.enableTracing || cfg.enablePrometheus);
ALLOW_INTROSPECTION = boolToNodeJSEnv cfg.allowIntrospection;
Expand All @@ -166,17 +119,11 @@ in {
} //
(lib.optionalAttrs (cfg.allowedOrigins != null) { ALLOWED_ORIGINS = cfg.allowedOrigins; }) //
(lib.optionalAttrs (cfg.listenAddress != null) { LISTEN_ADDRESS = cfg.listenAddress; }) //
(lib.optionalAttrs (cfg.metadataServerUri != null) { METADATA_SERVER_URI = toString cfg.metadataServerUri; }) //
(lib.optionalAttrs (cfg.pollingIntervalAdaSupply != null) { POLLING_INTERVAL_ADA_SUPPLY = toString cfg.pollingIntervalAdaSupply; }) //
(lib.optionalAttrs (cfg.assetMetadataUpdateInterval != null) { ASSET_METADATA_UPDATE_INTERVAL = toString cfg.assetMetadataUpdateInterval; }) //
(lib.optionalAttrs (cfg.queryDepthLimit != null) { QUERY_DEPTH_LIMIT = toString cfg.queryDepthLimit; }) //
(lib.optionalAttrs (cfg.allowListPath != null) { ALLOW_LIST_PATH = cfg.allowListPath; }) //
(lib.optionalAttrs (cfg.maxQueryComplexity != null) { MAX_QUERY_COMPLEXITY = toString cfg.maxQueryComplexity; });
path = with pkgs; [ netcat curl postgresql frontend hasura-cli glibc.bin patchelf ];
preStart = ''
set -exuo pipefail
${installHasuraCLI}
'';
path = with pkgs; [ netcat curl frontend glibc.bin patchelf ];
script = ''
exec cardano-graphql
'';
Expand Down
1 change: 1 addition & 0 deletions nix/nixos/module-list.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
./graphql-engine-service.nix
./cardano-graphql-service.nix
./cardano-graphql-background-service.nix
]

0 comments on commit f8fdcd1

Please sign in to comment.