···379379380380- [`system.stateVersion`](#opt-system.stateVersion) is now validated and must be in the `"YY.MM"` format, ideally corresponding to a prior NixOS release.
381381382382+- `services.mysql` now supports easy cluster setup via [`services.mysql.galeraCluster`](#opt-services.mysql.galeraCluster.enable) option.
383383+384384+ Example:
385385+386386+ ```nix
387387+ services.mysql = {
388388+ enable = true;
389389+ galeraCluster = {
390390+ enable = true;
391391+ localName = "Node 1";
392392+ localAddress = "galera_01";
393393+ nodeAddresses = [ "galera_01" "galera_02" "galera_03"];
394394+ };
395395+ };
396396+ ```
382397383398- [`services.geoclue2`](#opt-services.geoclue2.enable) now has an `enableStatic` option, which allows the NixOS configuration to specify a fixed location for GeoClue to use.
384399
+169-5
nixos/modules/services/databases/mysql.nix
···320320 description = "Port number on which the MySQL master server runs.";
321321 };
322322 };
323323+324324+ galeraCluster = {
325325+ enable = lib.mkEnableOption "MariaDB Galera Cluster";
326326+327327+ package = lib.mkOption {
328328+ type = lib.types.package;
329329+ description = "The MariaDB Galera package that provides the shared library 'libgalera_smm.so' required for cluster functionality.";
330330+ default = lib.literalExpression "pkgs.mariadb-galera";
331331+ };
332332+333333+ name = lib.mkOption {
334334+ type = lib.types.str;
335335+ description = "The logical name of the Galera cluster. All nodes in the same cluster must use the same name.";
336336+ default = "galera";
337337+ };
338338+339339+ sstMethod = lib.mkOption {
340340+ type = lib.types.enum [
341341+ "rsync"
342342+ "mariabackup"
343343+ ];
344344+ description = "Method for the initial state transfer (wsrep_sst_method) when a node joins the cluster. Be aware that rsync needs SSH keys to be generated and authorized on all nodes!";
345345+ default = "rsync";
346346+ example = "mariabackup";
347347+ };
348348+349349+ localName = lib.mkOption {
350350+ type = lib.types.str;
351351+ description = "The unique name that identifies this particular node within the cluster. Each node must have a different name.";
352352+ example = "node1";
353353+ };
354354+355355+ localAddress = lib.mkOption {
356356+ type = lib.types.str;
357357+ description = "IP address or hostname of this node that will be used for cluster communication. Must be reachable by all other nodes.";
358358+ example = "1.2.3.4";
359359+ default = cfg.galeraCluster.localName;
360360+ defaultText = lib.literalExpression "config.services.mysql.galeraCluster.localName";
361361+ };
362362+363363+ nodeAddresses = lib.mkOption {
364364+ type = lib.types.listOf lib.types.str;
365365+ description = "IP addresses or hostnames of all nodes in the cluster, including this node. This is used to construct the default clusterAddress connection string.";
366366+ example = lib.literalExpression ''["10.0.0.10" "10.0.0.20" "10.0.0.30"]'';
367367+ default = [ ];
368368+ };
369369+370370+ clusterPassword = lib.mkOption {
371371+ type = lib.types.str;
372372+ description = "Optional password for securing cluster communications. If provided, it will be used in the clusterAddress for authentication between nodes.";
373373+ example = "SomePassword";
374374+ default = "";
375375+ };
376376+377377+ clusterAddress = lib.mkOption {
378378+ type = lib.types.str;
379379+ description = "Full Galera cluster connection string. If nodeAddresses is set, this will be auto-generated, but you can override it with a custom value. Format is typically 'gcomm://node1,node2,node3' with optional parameters.";
380380+ example = "gcomm://10.0.0.10,10.0.0.20,10.0.0.30?gmcast.seg=1:SomePassword";
381381+ default =
382382+ if (cfg.galeraCluster.nodeAddresses == [ ]) then
383383+ ""
384384+ else
385385+ "gcomm://${builtins.concatStringsSep "," cfg.galeraCluster.nodeAddresses}"
386386+ + lib.optionalString (
387387+ cfg.galeraCluster.clusterPassword != ""
388388+ ) "?gmcast.seg=1:${cfg.galeraCluster.clusterPassword}";
389389+ defaultText = lib.literalExpression ''
390390+ if (config.services.mysql.galeraCluster.nodeAddresses == [ ]) then
391391+ ""
392392+ else
393393+ "gcomm://''${builtins.concatStringsSep \",\" config.services.mysql.galeraCluster.nodeAddresses}"
394394+ + lib.optionalString (config.services.mysql.galeraCluster.clusterPassword != "")
395395+ "?gmcast.seg=1:''${config.services.mysql.galeraCluster.clusterPassword}"
396396+ '';
397397+ };
398398+399399+ };
323400 };
324401325402 };
···327404 ###### implementation
328405329406 config = lib.mkIf cfg.enable {
407407+ assertions = [
408408+ {
409409+ assertion = !cfg.galeraCluster.enable || isMariaDB;
410410+ message = "'services.mysql.galeraCluster.enable' expect services.mysql.package to be an mariadb variant";
411411+ }
412412+ {
413413+ assertion =
414414+ !cfg.galeraCluster.enable
415415+ || (
416416+ cfg.galeraCluster.localAddress != ""
417417+ && (cfg.galeraCluster.nodeAddresses != [ ] || cfg.galeraCluster.clusterAddress != "")
418418+ );
419419+ message = "mariadb galera cluster is enabled but the localAddress and (nodeAddresses or clusterAddress) are not set";
420420+ }
421421+ {
422422+ assertion = !(cfg.galeraCluster.clusterAddress != "" && cfg.galeraCluster.clusterPassword != "");
423423+ message = "mariadb galera clusterPassword is set but overwritten by clusterAddress";
424424+ }
425425+ {
426426+ assertion =
427427+ !(
428428+ cfg.galeraCluster.enable
429429+ && cfg.galeraCluster.nodeAddresses != [ ]
430430+ && cfg.galeraCluster.clusterAddress != ""
431431+ );
432432+ message = "When services.mysql.galeraCluster.clusterAddress is set, setting services.mysql.galeraCluster.nodeAddresses is redundant and will be overwritten by clusterAddress. Choose one approach.";
433433+ }
434434+ ];
330435331436 services.mysql.dataDir = lib.mkDefault (
332437 if lib.versionAtLeast config.system.stateVersion "17.09" then "/var/lib/mysql" else "/var/mysql"
···351456 (lib.mkIf (!isMariaDB) {
352457 plugin-load-add = [ "auth_socket.so" ];
353458 })
459459+ (lib.mkIf cfg.galeraCluster.enable {
460460+ # Ensure Only InnoDB is used as galera clusters can only work with them
461461+ enforce_storage_engine = "InnoDB";
462462+ default_storage_engine = "InnoDB";
463463+464464+ # galera only support this binlog format
465465+ binlog-format = "ROW";
466466+467467+ bind_address = lib.mkDefault "0.0.0.0";
468468+ })
354469 ];
355470471471+ services.mysql.settings.galera = lib.optionalAttrs cfg.galeraCluster.enable {
472472+ wsrep_on = "ON";
473473+ wsrep_debug = lib.mkDefault "NONE";
474474+ wsrep_retry_autocommit = lib.mkDefault "3";
475475+ wsrep_provider = "${cfg.galeraCluster.package}/lib/galera/libgalera_smm.so";
476476+477477+ wsrep_cluster_name = cfg.galeraCluster.name;
478478+ wsrep_cluster_address = cfg.galeraCluster.clusterAddress;
479479+480480+ wsrep_node_address = cfg.galeraCluster.localAddress;
481481+ wsrep_node_name = "${cfg.galeraCluster.localName}";
482482+483483+ # SST method using rsync
484484+ wsrep_sst_method = lib.mkDefault cfg.galeraCluster.sstMethod;
485485+ wsrep_sst_auth = lib.mkDefault "check_repl:check_pass";
486486+487487+ binlog_format = "ROW";
488488+ innodb_autoinc_lock_mode = 2;
489489+ };
490490+356491 users.users = lib.optionalAttrs (cfg.user == "mysql") {
357492 mysql = {
358493 description = "MySQL server user";
···384519385520 unitConfig.RequiresMountsFor = cfg.dataDir;
386521387387- path = [
388388- # Needed for the mysql_install_db command in the preStart script
389389- # which calls the hostname command.
390390- pkgs.nettools
391391- ];
522522+ path =
523523+ [
524524+ # Needed for the mysql_install_db command in the preStart script
525525+ # which calls the hostname command.
526526+ pkgs.nettools
527527+ ]
528528+ # tools 'wsrep_sst_rsync' needs
529529+ ++ lib.optionals cfg.galeraCluster.enable [
530530+ cfg.package
531531+ pkgs.bash
532532+ pkgs.gawk
533533+ pkgs.gnutar
534534+ pkgs.gzip
535535+ pkgs.inetutils
536536+ pkgs.iproute2
537537+ pkgs.netcat
538538+ pkgs.procps
539539+ pkgs.pv
540540+ pkgs.rsync
541541+ pkgs.socat
542542+ pkgs.stunnel
543543+ pkgs.which
544544+ ];
392545393546 preStart =
394547 if isMariaDB then
···581734 })
582735 ];
583736 };
737737+738738+ # Open firewall ports for MySQL (and Galera)
739739+ networking.firewall.allowedTCPPorts = lib.optionals cfg.galeraCluster.enable [
740740+ 3306 # MySQL
741741+ 4567 # Galera Cluster
742742+ 4568 # Galera IST
743743+ 4444 # SST
744744+ ];
745745+ networking.firewall.allowedUDPPorts = lib.optionals cfg.galeraCluster.enable [
746746+ 4567 # Galera Cluster
747747+ ];
584748 };
585749586750 meta.maintainers = [ lib.maintainers._6543 ];