Add Clickhouse service (#85)

This commit is contained in:
Shivaraj B H 2024-01-24 20:54:26 +05:30 committed by GitHub
parent 8c396234dc
commit af4af40169
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,27 @@
# Clickhouse
## Getting Started
```nix
# In `perSystem.process-compose.<name>`
{
services.clickhouse."clickhouse-1".enable = true;
}
```
## Tips & Tricks
### Change the HTTP default port
Clickhouse has [HTTP Interface](https://clickhouse.com/docs/en/interfaces/http) that is enabled by default on port 8123. To change the default port, use the `extraConfig` option:
```nix
{
services.clickhouse."clickhouse-1" = {
enable = true;
extraConfig = ''
http_port: 9050
'';
};
}
```

View File

@ -8,6 +8,7 @@ short-title: Services
> This list denotes the progress of documentation, not implementation. See full list of implemented services at [gh].
- [ ] Apache Kafka
- [ ] [[clickhouse]]#
- [ ] Elasticsearch
- [ ] MySQL
- [ ] Nginx

87
nix/clickhouse.nix Normal file
View File

@ -0,0 +1,87 @@
# Based on: https://github.com/cachix/devenv/blob/main/src/modules/services/clickhouse.nix
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
in
{
options = {
enable = lib.mkEnableOption name;
package = lib.mkOption {
type = types.package;
description = "Which package of clickhouse to use";
default = pkgs.clickhouse;
defaultText = lib.literalExpression "pkgs.clickhouse";
};
port = lib.mkOption {
type = types.int;
description = "Which port to run clickhouse on. This port is for `clickhouse-client` program";
default = 9000;
};
dataDir = lib.mkOption {
type = types.str;
default = "./data/${name}";
description = "The clickhouse data directory";
};
extraConfig = lib.mkOption {
type = types.lines;
description = "Additional configuration to be appended to `clickhouse-config.yaml`.";
default = "";
};
outputs.settings = lib.mkOption {
type = types.deferredModule;
internal = true;
readOnly = true;
default = {
processes = {
"${name}" =
let
clickhouseConfig = pkgs.writeText "clickhouse-config.yaml" ''
logger:
level: warning
console: 1
tcp_port: ${toString config.port}
default_profile: default
default_database: default
path: ${config.dataDir}/clickhouse
tmp_path: ${config.dataDir}/clickhouse/tmp
user_files_path: ${config.dataDir}/clickhouse/user_files
format_schema_path: ${config.dataDir}/clickhouse/format_schemas
user_directories:
users_xml:
path: ${config.package}/etc/clickhouse-server/users.xml
${config.extraConfig}
'';
startScript = pkgs.writeShellApplication {
name = "start-clickhouse";
runtimeInputs = [ config.package ];
text = ''
clickhouse-server --config-file=${clickhouseConfig}
'';
};
in
{
command = "${lib.getExe startScript}";
readiness_probe = {
exec.command = ''${config.package}/bin/clickhouse-client --query "SELECT 1" --port ${builtins.toString config.port}'';
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
namespace = name;
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability.restart = "on_failure";
};
};
};
};
};
}

35
nix/clickhouse_test.nix Normal file
View File

@ -0,0 +1,35 @@
{ pkgs, config, ... }: {
services.clickhouse."clickhouse" = {
enable = true;
extraConfig = ''
http_port: 9050
'';
};
settings.processes.test =
let
cfg = config.services.clickhouse."clickhouse";
in
{
command = pkgs.writeShellApplication {
runtimeInputs = [ cfg.package pkgs.gnugrep pkgs.curl ];
text =
let
# Tests based on: https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/clickhouse.nix
tableDDL = pkgs.writeText "ddl.sql" "CREATE TABLE `demo` (`value` FixedString(10)) engine = MergeTree PARTITION BY value ORDER BY tuple();";
insertQuery = pkgs.writeText "insert.sql" "INSERT INTO `demo` (`value`) VALUES ('foo');";
selectQuery = pkgs.writeText "select.sql" "SELECT * from `demo`";
in
''
clickhouse-client < ${tableDDL}
clickhouse-client < ${insertQuery}
clickhouse-client < ${selectQuery} | grep foo
# Test clickhouse http port
curl http://localhost:9050 | grep Ok
'';
name = "clickhouse-test";
};
depends_on."clickhouse".condition = "process_healthy";
};
}

View File

@ -5,6 +5,7 @@ in
{
imports = builtins.map multiService [
./apache-kafka.nix
./clickhouse.nix
./elasticsearch.nix
./mysql.nix
./nginx.nix

View File

@ -38,6 +38,7 @@
in
builtins.listToAttrs (builtins.map mkPackageFor [
../nix/apache-kafka_test.nix
../nix/clickhouse_test.nix
../nix/elasticsearch_test.nix
../nix/mysql_test.nix
../nix/nginx_test.nix