From ddac798a5a8cf52f820e0da180f870452714f075 Mon Sep 17 00:00:00 2001 From: Konstantin Bogdanov Date: Tue, 4 Nov 2025 21:25:45 +0100 Subject: [PATCH] nixos/clickhouse: allow configuring with Nix attribute set --- doc/release-notes/rl-2511.section.md | 1 + .../modules/services/databases/clickhouse.nix | 112 +++++++++++++++++- 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/doc/release-notes/rl-2511.section.md b/doc/release-notes/rl-2511.section.md index ddb0f3a36b34..21a9b41636e5 100644 --- a/doc/release-notes/rl-2511.section.md +++ b/doc/release-notes/rl-2511.section.md @@ -279,6 +279,7 @@ - `cloudflare-ddns`: Added package cloudflare-ddns. +- `clickhouse`: Added `serverConfig`, `usersConfig` configuration options accepting Nix attribute sets. Also added `extraServerConfig` and `extraUsersConfig` options accepting plain text (expecting XML configuration). - [`homebox` 0.20.0](https://github.com/sysadminsmedia/homebox/releases/tag/v0.20.0) changed how assets are stored and hashed. It is recommended to back up your database before this update. In particular, `--storage-data` was replaced with `--storage-conn-string` and `--storage-prefix-path`. If your configuration set `HBOX_STORAGE_DATA` manually, you must migrate it to `HBOX_STORAGE_CONN_STRING` and `HBOX_STORAGE_PREFIX_PATH`. diff --git a/nixos/modules/services/databases/clickhouse.nix b/nixos/modules/services/databases/clickhouse.nix index 18251d2e4d77..de9371de513a 100644 --- a/nixos/modules/services/databases/clickhouse.nix +++ b/nixos/modules/services/databases/clickhouse.nix @@ -6,9 +6,15 @@ }: let cfg = config.services.clickhouse; + format = pkgs.formats.yaml { }; + + serverConfigFile = format.generate "config.yaml" cfg.serverConfig; + usersConfigFile = format.generate "users.yaml" cfg.usersConfig; in { + meta.maintainers = [ "thevar1able" ]; + ###### interface options = { @@ -17,7 +23,86 @@ in enable = lib.mkEnableOption "ClickHouse database server"; - package = lib.mkPackageOption pkgs "clickhouse" { }; + package = lib.mkPackageOption pkgs "clickhouse" { + example = "pkgs.clickhouse-lts"; + }; + + serverConfig = lib.mkOption { + type = format.type; + default = { }; + example = lib.literalExpression '' + { + http_port = 8123; + tcp_port = 9000; + + remote_servers = { + default = { + shard = { + replica = [ + { host = "::"; port = "9000"; } + { host = "::"; port = "9001"; } + { host = "::"; port = "9002"; } + ]; + }; + }; + }; + } + ''; + description = '' + Your {file}`config.yaml` as a Nix attribute set. + Check the [documentation](https://clickhouse.com/docs/operations/configuration-files) + for possible options. + ''; + }; + + usersConfig = lib.mkOption { + type = format.type; + default = { }; + example = lib.literalExpression '' + { + profiles = {}; + + users = { + default = { + profile = "default"; + password_sha256_hex = "36dd292533174299fb0c34665df468bb881756ca9eaf9757d0cfde38f9ededa1"; # `echo -n verysecret | sha256sum` + }; + }; + } + ''; + description = '' + Your {file}`users.yaml` as a Nix attribute set. + Check the [documentation](https://clickhouse.com/docs/operations/configuration-files#user-settings) + for possible options. + ''; + }; + + extraServerConfig = lib.mkOption { + type = lib.types.lines; + default = ""; + example = '' + + 500 + 3 + + ''; + description = "Additional raw XML configuration for ClickHouse server."; + }; + + extraUsersConfig = lib.mkOption { + type = lib.types.lines; + default = ""; + example = '' + + + + readonly + + + + ''; + description = "Additional raw XML configuration for ClickHouse server."; + }; }; @@ -40,18 +125,15 @@ in description = "ClickHouse server"; wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; serviceConfig = { Type = "notify"; User = "clickhouse"; Group = "clickhouse"; - ConfigurationDirectory = "clickhouse-server"; AmbientCapabilities = "CAP_SYS_NICE"; StateDirectory = "clickhouse"; - LogsDirectory = "clickhouse"; - ExecStart = "${cfg.package}/bin/clickhouse-server --config-file=/etc/clickhouse-server/config.xml"; + ExecStart = "${cfg.package}/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml"; TimeoutStartSec = "infinity"; }; @@ -69,6 +151,26 @@ in "clickhouse-server/users.xml" = { source = "${cfg.package}/etc/clickhouse-server/users.xml"; }; + + "clickhouse-server/config.d/100-nixos-module-config.yaml" = lib.mkIf (cfg.serverConfig != { }) { + source = serverConfigFile; + }; + + "clickhouse-server/users.d/100-nixos-module-config.yaml" = lib.mkIf (cfg.usersConfig != { }) { + source = usersConfigFile; + }; + + "clickhouse-server/config.d/200-nixos-module-extra-config.xml" = + lib.mkIf (cfg.extraServerConfig != "") + { + text = cfg.extraServerConfig; + }; + + "clickhouse-server/users.d/200-nixos-module-extra-config.xml" = + lib.mkIf (cfg.extraUsersConfig != "") + { + text = cfg.extraUsersConfig; + }; }; environment.systemPackages = [ cfg.package ];