2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 17:27:57 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
let
|
|
|
|
|
2008-06-25 21:58:51 +00:00
|
|
|
cfg = config.services.mysql;
|
|
|
|
|
2020-08-12 01:09:41 +00:00
|
|
|
isMariaDB = lib.getName cfg.package == lib.getName pkgs.mariadb;
|
2024-02-11 22:57:32 +00:00
|
|
|
isOracle = lib.getName cfg.package == lib.getName pkgs.mysql80;
|
|
|
|
# Oracle MySQL has supported "notify" service type since 8.0
|
|
|
|
hasNotify = isMariaDB || (isOracle && versionAtLeast cfg.package.version "8.0");
|
2019-11-24 17:22:28 +00:00
|
|
|
|
2008-06-25 21:58:51 +00:00
|
|
|
mysqldOptions =
|
2020-08-12 01:09:41 +00:00
|
|
|
"--user=${cfg.user} --datadir=${cfg.dataDir} --basedir=${cfg.package}";
|
2008-01-28 14:34:29 +00:00
|
|
|
|
2021-12-19 02:00:04 +00:00
|
|
|
format = pkgs.formats.ini { listsAsDuplicateKeys = true; };
|
|
|
|
configFile = format.generate "my.cnf" cfg.settings;
|
2020-03-07 00:03:32 +00:00
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
in
|
2008-06-25 21:58:51 +00:00
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
{
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
2020-08-12 01:17:02 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "pidDir" ] "Don't wait for pidfiles, describe dependencies through systemd.")
|
2019-12-10 01:51:19 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "rootPassword" ] "Use socket authentication or set the password outside of the nix store.")
|
2021-12-19 02:00:04 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "extraOptions" ] "Use services.mysql.settings.mysqld instead.")
|
2021-12-19 02:26:47 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "bind" ] "Use services.mysql.settings.mysqld.bind-address instead.")
|
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "port" ] "Use services.mysql.settings.mysqld.port instead.")
|
2019-12-10 01:51:19 +00:00
|
|
|
];
|
2009-07-15 11:19:11 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
services.mysql = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2020-05-14 11:08:41 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "MySQL server");
|
2009-07-15 11:19:11 +00:00
|
|
|
|
2009-12-16 15:24:15 +00:00
|
|
|
package = mkOption {
|
2014-02-26 12:54:04 +00:00
|
|
|
type = types.package;
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression "pkgs.mariadb";
|
2022-08-14 03:16:55 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-11-14 01:08:15 +00:00
|
|
|
Which MySQL derivation to use. MariaDB packages are supported too.
|
2022-08-14 03:16:55 +00:00
|
|
|
'';
|
2009-12-16 14:54:36 +00:00
|
|
|
};
|
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
user = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.str;
|
2009-07-15 11:19:11 +00:00
|
|
|
default = "mysql";
|
2021-12-19 01:55:03 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
User account under which MySQL runs.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
If left as the default value this user will automatically be created
|
|
|
|
on system activation, otherwise you are responsible for
|
|
|
|
ensuring the user exists before the MySQL service starts.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2021-12-19 01:55:03 +00:00
|
|
|
'';
|
2009-07-15 11:19:11 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 01:12:59 +00:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mysql";
|
2021-12-19 01:55:03 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Group account under which MySQL runs.
|
|
|
|
|
|
|
|
::: {.note}
|
2021-12-19 02:35:50 +00:00
|
|
|
If left as the default value this group will automatically be created
|
2021-12-19 01:55:03 +00:00
|
|
|
on system activation, otherwise you are responsible for
|
|
|
|
ensuring the user exists before the MySQL service starts.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2021-12-19 01:55:03 +00:00
|
|
|
'';
|
2020-08-12 01:12:59 +00:00
|
|
|
};
|
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
dataDir = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.path;
|
2017-05-19 22:30:51 +00:00
|
|
|
example = "/var/lib/mysql";
|
2021-12-19 01:50:48 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The data directory for MySQL.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
If left as the default value of `/var/lib/mysql` this directory will automatically be created before the MySQL
|
|
|
|
server starts, otherwise you are responsible for ensuring the directory exists with appropriate ownership and permissions.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2021-12-19 01:50:48 +00:00
|
|
|
'';
|
2009-07-15 11:19:11 +00:00
|
|
|
};
|
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
configFile = mkOption {
|
|
|
|
type = types.path;
|
2021-12-19 02:00:04 +00:00
|
|
|
default = configFile;
|
|
|
|
defaultText = ''
|
|
|
|
A configuration file automatically generated by NixOS.
|
|
|
|
'';
|
2020-03-07 00:03:32 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Override the configuration file used by MySQL. By default,
|
|
|
|
NixOS generates one automatically from {option}`services.mysql.settings`.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-03-07 00:03:32 +00:00
|
|
|
pkgs.writeText "my.cnf" '''
|
|
|
|
[mysqld]
|
|
|
|
datadir = /var/lib/mysql
|
|
|
|
bind-address = 127.0.0.1
|
|
|
|
port = 3336
|
|
|
|
|
|
|
|
!includedir /etc/mysql/conf.d/
|
|
|
|
''';
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
2021-12-19 02:00:04 +00:00
|
|
|
type = format.type;
|
2020-03-07 00:03:32 +00:00
|
|
|
default = {};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
MySQL configuration. Refer to
|
|
|
|
<https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html>,
|
|
|
|
<https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html>,
|
|
|
|
and <https://mariadb.com/kb/en/server-system-variables/>
|
|
|
|
for details on supported values.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
MySQL configuration options such as `--quick` should be treated as
|
|
|
|
boolean options and provided values such as `true`, `false`,
|
|
|
|
`1`, or `0`. See the provided example below.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2020-03-07 00:03:32 +00:00
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-03-07 00:03:32 +00:00
|
|
|
{
|
|
|
|
mysqld = {
|
|
|
|
key_buffer_size = "6G";
|
|
|
|
table_cache = 1600;
|
|
|
|
log-error = "/var/log/mysql_err.log";
|
|
|
|
plugin-load-add = [ "server_audit" "ed25519=auth_ed25519" ];
|
|
|
|
};
|
|
|
|
mysqldump = {
|
|
|
|
quick = true;
|
|
|
|
max_allowed_packet = "16M";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-18 16:19:04 +00:00
|
|
|
initialDatabases = mkOption {
|
2019-03-12 17:08:23 +00:00
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The name of the database to create.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
schema = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The initial schema of the database; if null (the default),
|
|
|
|
an empty database is created.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2009-11-18 16:19:04 +00:00
|
|
|
default = [];
|
2017-05-20 21:57:07 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
List of database names and their initial schemas that should be used to create databases on the first startup
|
|
|
|
of MySQL. The schema attribute is optional: If not specified, an empty database is created.
|
|
|
|
'';
|
2022-11-03 14:52:19 +00:00
|
|
|
example = literalExpression ''
|
|
|
|
[
|
|
|
|
{ name = "foodatabase"; schema = ./foodatabase.sql; }
|
|
|
|
{ name = "bardatabase"; }
|
|
|
|
]
|
|
|
|
'';
|
2009-11-18 16:19:04 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2010-04-16 18:06:23 +00:00
|
|
|
initialScript = mkOption {
|
2019-04-01 19:08:47 +00:00
|
|
|
type = types.nullOr types.path;
|
2010-04-16 18:06:23 +00:00
|
|
|
default = null;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "A file containing SQL statements to be executed on the first startup. Can be used for granting certain permissions on the database.";
|
2010-04-16 18:06:23 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2017-09-10 15:58:52 +00:00
|
|
|
ensureDatabases = mkOption {
|
2019-03-12 17:08:23 +00:00
|
|
|
type = types.listOf types.str;
|
2017-09-10 15:58:52 +00:00
|
|
|
default = [];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Ensures that the specified databases exist.
|
|
|
|
This option will never delete existing databases, especially not when the value of this
|
|
|
|
option is changed. This means that databases created once through this option or
|
|
|
|
otherwise have to be removed manually.
|
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"nextcloud"
|
2018-01-16 17:44:27 +00:00
|
|
|
"matomo"
|
2017-09-10 15:58:52 +00:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
ensureUsers = mkOption {
|
2019-03-12 17:08:23 +00:00
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Name of the user to ensure.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
ensurePermissions = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = {};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Permissions to ensure for the user, specified as attribute set.
|
|
|
|
The attribute names specify the database and tables to grant the permissions for,
|
|
|
|
separated by a dot. You may use wildcards here.
|
|
|
|
The attribute values specfiy the permissions to grant.
|
|
|
|
You may specify one or multiple comma-separated SQL privileges here.
|
|
|
|
|
|
|
|
For more information on how to specify the target
|
|
|
|
and on which privileges exist, see the
|
|
|
|
[GRANT syntax](https://mariadb.com/kb/en/library/grant/).
|
|
|
|
The attributes are used as `GRANT ''${attrName} ON ''${attrValue}`.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2019-03-12 17:08:23 +00:00
|
|
|
{
|
|
|
|
"database.*" = "ALL PRIVILEGES";
|
|
|
|
"*.*" = "SELECT, LOCK TABLES";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2017-09-10 15:58:52 +00:00
|
|
|
default = [];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Ensures that the specified users exist and have at least the ensured permissions.
|
|
|
|
The MySQL users will be identified using Unix socket authentication. This authenticates the Unix user with the
|
|
|
|
same name only, and that without the need for a password.
|
|
|
|
This option will never delete existing users or remove permissions, especially not when the value of this
|
|
|
|
option is changed. This means that users created and permissions assigned once through this option or
|
|
|
|
otherwise have to be removed manually.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2019-03-12 17:08:23 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name = "nextcloud";
|
|
|
|
ensurePermissions = {
|
|
|
|
"nextcloud.*" = "ALL PRIVILEGES";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "backup";
|
|
|
|
ensurePermissions = {
|
|
|
|
"*.*" = "SELECT, LOCK TABLES";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2017-09-10 15:58:52 +00:00
|
|
|
};
|
|
|
|
|
2011-07-13 20:58:48 +00:00
|
|
|
replication = {
|
|
|
|
role = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.enum [ "master" "slave" "none" ];
|
2012-01-18 15:01:44 +00:00
|
|
|
default = "none";
|
2016-10-02 20:34:29 +00:00
|
|
|
description = lib.mdDoc "Role of the MySQL server instance.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-07-13 20:58:48 +00:00
|
|
|
serverId = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.int;
|
2012-01-18 15:01:44 +00:00
|
|
|
default = 1;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "Id of the MySQL server instance. This number must be unique for each instance.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masterHost = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.str;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "Hostname of the MySQL master server.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 00:15:55 +00:00
|
|
|
slaveHost = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.str;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "Hostname of the MySQL slave server.";
|
2016-07-21 00:15:55 +00:00
|
|
|
};
|
|
|
|
|
2012-01-18 15:01:44 +00:00
|
|
|
masterUser = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.str;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "Username of the MySQL replication user.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masterPassword = mkOption {
|
2016-10-02 20:34:29 +00:00
|
|
|
type = types.str;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "Password of the MySQL replication user.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masterPort = mkOption {
|
2021-12-19 02:35:50 +00:00
|
|
|
type = types.port;
|
2012-01-18 15:01:44 +00:00
|
|
|
default = 3306;
|
2020-08-12 01:17:02 +00:00
|
|
|
description = lib.mdDoc "Port number on which the MySQL master server runs.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
2011-07-13 20:58:48 +00:00
|
|
|
};
|
2009-07-15 11:19:11 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
};
|
|
|
|
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2009-07-15 11:19:11 +00:00
|
|
|
###### implementation
|
2008-06-25 21:58:51 +00:00
|
|
|
|
2021-12-19 02:35:50 +00:00
|
|
|
config = mkIf cfg.enable {
|
2009-07-15 11:19:11 +00:00
|
|
|
|
2017-05-19 22:30:51 +00:00
|
|
|
services.mysql.dataDir =
|
2018-07-25 20:22:54 +00:00
|
|
|
mkDefault (if versionAtLeast config.system.stateVersion "17.09" then "/var/lib/mysql"
|
2017-05-19 22:30:51 +00:00
|
|
|
else "/var/mysql");
|
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
services.mysql.settings.mysqld = mkMerge [
|
|
|
|
{
|
|
|
|
datadir = cfg.dataDir;
|
2021-12-19 02:26:47 +00:00
|
|
|
port = mkDefault 3306;
|
2020-03-07 00:03:32 +00:00
|
|
|
}
|
|
|
|
(mkIf (cfg.replication.role == "master" || cfg.replication.role == "slave") {
|
|
|
|
log-bin = "mysql-bin-${toString cfg.replication.serverId}";
|
|
|
|
log-bin-index = "mysql-bin-${toString cfg.replication.serverId}.index";
|
|
|
|
relay-log = "mysql-relay-bin";
|
|
|
|
server-id = cfg.replication.serverId;
|
2020-05-07 19:47:56 +00:00
|
|
|
binlog-ignore-db = [ "information_schema" "performance_schema" "mysql" ];
|
2020-03-07 00:03:32 +00:00
|
|
|
})
|
2020-05-05 07:22:52 +00:00
|
|
|
(mkIf (!isMariaDB) {
|
2020-08-12 01:54:51 +00:00
|
|
|
plugin-load-add = "auth_socket.so";
|
2020-05-05 07:22:52 +00:00
|
|
|
})
|
2020-03-07 00:03:32 +00:00
|
|
|
];
|
|
|
|
|
2020-08-12 01:12:59 +00:00
|
|
|
users.users = optionalAttrs (cfg.user == "mysql") {
|
|
|
|
mysql = {
|
|
|
|
description = "MySQL server user";
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.mysql;
|
|
|
|
};
|
2013-08-26 13:20:25 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 01:12:59 +00:00
|
|
|
users.groups = optionalAttrs (cfg.group == "mysql") {
|
|
|
|
mysql.gid = config.ids.gids.mysql;
|
|
|
|
};
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2020-08-12 01:09:41 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
environment.etc."my.cnf".source = cfg.configFile;
|
2018-12-01 12:10:27 +00:00
|
|
|
|
2021-12-19 02:35:50 +00:00
|
|
|
systemd.services.mysql = {
|
|
|
|
description = "MySQL Server";
|
|
|
|
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
restartTriggers = [ cfg.configFile ];
|
|
|
|
|
|
|
|
unitConfig.RequiresMountsFor = cfg.dataDir;
|
|
|
|
|
|
|
|
path = [
|
|
|
|
# Needed for the mysql_install_db command in the preStart script
|
|
|
|
# which calls the hostname command.
|
|
|
|
pkgs.nettools
|
|
|
|
];
|
|
|
|
|
|
|
|
preStart = if isMariaDB then ''
|
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
|
|
|
${cfg.package}/bin/mysql_install_db --defaults-file=/etc/my.cnf ${mysqldOptions}
|
|
|
|
touch ${cfg.dataDir}/mysql_init
|
|
|
|
fi
|
|
|
|
'' else ''
|
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
|
|
|
${cfg.package}/bin/mysqld --defaults-file=/etc/my.cnf ${mysqldOptions} --initialize-insecure
|
|
|
|
touch ${cfg.dataDir}/mysql_init
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
# https://mariadb.com/kb/en/getting-started-with-mariadb-galera-cluster/#systemd-and-galera-recovery
|
|
|
|
if test -n "''${_WSREP_START_POSITION}"; then
|
|
|
|
if test -e "${cfg.package}/bin/galera_recovery"; then
|
|
|
|
VAR=$(cd ${cfg.package}/bin/..; ${cfg.package}/bin/galera_recovery); [[ $? -eq 0 ]] && export _WSREP_START_POSITION=$VAR || exit 1
|
2021-02-17 19:16:26 +00:00
|
|
|
fi
|
2021-12-19 02:35:50 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# The last two environment variables are used for starting Galera clusters
|
|
|
|
exec ${cfg.package}/bin/mysqld --defaults-file=/etc/my.cnf ${mysqldOptions} $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION
|
|
|
|
'';
|
|
|
|
|
|
|
|
postStart = let
|
|
|
|
# The super user account to use on *first* run of MySQL server
|
|
|
|
superUser = if isMariaDB then cfg.user else "root";
|
|
|
|
in ''
|
2024-02-11 22:57:32 +00:00
|
|
|
${optionalString (!hasNotify) ''
|
2021-12-19 02:35:50 +00:00
|
|
|
# Wait until the MySQL server is available for use
|
|
|
|
while [ ! -e /run/mysqld/mysqld.sock ]
|
|
|
|
do
|
|
|
|
echo "MySQL daemon not yet started. Waiting for 1 second..."
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
''}
|
|
|
|
|
|
|
|
if [ -f ${cfg.dataDir}/mysql_init ]
|
|
|
|
then
|
|
|
|
# While MariaDB comes with a 'mysql' super user account since 10.4.x, MySQL does not
|
|
|
|
# Since we don't want to run this service as 'root' we need to ensure the account exists on first run
|
|
|
|
( echo "CREATE USER IF NOT EXISTS '${cfg.user}'@'localhost' IDENTIFIED WITH ${if isMariaDB then "unix_socket" else "auth_socket"};"
|
|
|
|
echo "GRANT ALL PRIVILEGES ON *.* TO '${cfg.user}'@'localhost' WITH GRANT OPTION;"
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
2021-02-17 19:16:26 +00:00
|
|
|
|
2020-08-12 02:04:16 +00:00
|
|
|
${concatMapStrings (database: ''
|
2021-12-19 02:35:50 +00:00
|
|
|
# Create initial databases
|
|
|
|
if ! test -e "${cfg.dataDir}/${database.name}"; then
|
|
|
|
echo "Creating initial database: ${database.name}"
|
|
|
|
( echo 'create database `${database.name}`;'
|
|
|
|
|
|
|
|
${optionalString (database.schema != null) ''
|
|
|
|
echo 'use `${database.name}`;'
|
|
|
|
|
|
|
|
# TODO: this silently falls through if database.schema does not exist,
|
|
|
|
# we should catch this somehow and exit, but can't do it here because we're in a subshell.
|
|
|
|
if [ -f "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}
|
|
|
|
elif [ -d "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}/mysql-databases/*.sql
|
|
|
|
fi
|
|
|
|
''}
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
fi
|
|
|
|
'') cfg.initialDatabases}
|
|
|
|
|
|
|
|
${optionalString (cfg.replication.role == "master")
|
|
|
|
''
|
|
|
|
# Set up the replication master
|
|
|
|
|
|
|
|
( echo "use mysql;"
|
|
|
|
echo "CREATE USER '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}' IDENTIFIED WITH mysql_native_password;"
|
|
|
|
echo "SET PASSWORD FOR '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}' = PASSWORD('${cfg.replication.masterPassword}');"
|
|
|
|
echo "GRANT REPLICATION SLAVE ON *.* TO '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}';"
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (cfg.replication.role == "slave")
|
|
|
|
''
|
|
|
|
# Set up the replication slave
|
|
|
|
|
|
|
|
( echo "stop slave;"
|
|
|
|
echo "change master to master_host='${cfg.replication.masterHost}', master_user='${cfg.replication.masterUser}', master_password='${cfg.replication.masterPassword}';"
|
|
|
|
echo "start slave;"
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (cfg.initialScript != null)
|
|
|
|
''
|
|
|
|
# Execute initial script
|
|
|
|
# using toString to avoid copying the file to nix store if given as path instead of string,
|
|
|
|
# as it might contain credentials
|
|
|
|
cat ${toString cfg.initialScript} | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
''}
|
|
|
|
|
|
|
|
rm ${cfg.dataDir}/mysql_init
|
|
|
|
fi
|
|
|
|
|
|
|
|
${optionalString (cfg.ensureDatabases != []) ''
|
|
|
|
(
|
|
|
|
${concatMapStrings (database: ''
|
|
|
|
echo "CREATE DATABASE IF NOT EXISTS \`${database}\`;"
|
|
|
|
'') cfg.ensureDatabases}
|
|
|
|
) | ${cfg.package}/bin/mysql -N
|
|
|
|
''}
|
|
|
|
|
|
|
|
${concatMapStrings (user:
|
|
|
|
''
|
|
|
|
( echo "CREATE USER IF NOT EXISTS '${user.name}'@'localhost' IDENTIFIED WITH ${if isMariaDB then "unix_socket" else "auth_socket"};"
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (database: permission: ''
|
|
|
|
echo "GRANT ${permission} ON ${database} TO '${user.name}'@'localhost';"
|
|
|
|
'') user.ensurePermissions)}
|
2020-08-12 02:04:16 +00:00
|
|
|
) | ${cfg.package}/bin/mysql -N
|
2021-12-19 02:35:50 +00:00
|
|
|
'') cfg.ensureUsers}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = mkMerge [
|
|
|
|
{
|
2024-02-11 22:57:32 +00:00
|
|
|
Type = if hasNotify then "notify" else "simple";
|
2021-12-19 02:35:50 +00:00
|
|
|
Restart = "on-abort";
|
|
|
|
RestartSec = "5s";
|
|
|
|
|
|
|
|
# User and group
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
# Runtime directory and mode
|
|
|
|
RuntimeDirectory = "mysqld";
|
|
|
|
RuntimeDirectoryMode = "0755";
|
|
|
|
# Access write directories
|
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
|
|
|
# Capabilities
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# Security
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
# Sandboxing
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
# System Call Filtering
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
}
|
|
|
|
(mkIf (cfg.dataDir == "/var/lib/mysql") {
|
|
|
|
StateDirectory = "mysql";
|
|
|
|
StateDirectoryMode = "0700";
|
|
|
|
})
|
|
|
|
];
|
|
|
|
};
|
2009-03-06 12:27:00 +00:00
|
|
|
};
|
2008-01-28 14:34:29 +00:00
|
|
|
}
|