Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Clickhouse service #85

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions doc/services-flake/clickhouse.md
Original file line number Diff line number Diff line change
@@ -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
'';
};
}
```
1 change: 1 addition & 0 deletions doc/services-flake/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions nix/clickhouse.nix
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions nix/clickhouse_test.nix
Original file line number Diff line number Diff line change
@@ -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";
};
}
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ in
{
imports = builtins.map multiService [
./apache-kafka.nix
./clickhouse.nix
./elasticsearch.nix
./mysql.nix
./nginx.nix
Expand Down
1 change: 1 addition & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down