2009-10-12 18:27:57 +01:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
let
|
|
|
|
|
2008-06-25 22:58:51 +01:00
|
|
|
cfg = config.services.mysql;
|
|
|
|
|
2009-12-16 14:54:36 +00:00
|
|
|
mysql = cfg.mysql;
|
2008-06-25 22:58:51 +01:00
|
|
|
|
|
|
|
pidFile = "${cfg.pidDir}/mysqld.pid";
|
|
|
|
|
|
|
|
mysqldOptions =
|
|
|
|
"--user=${cfg.user} --datadir=${cfg.dataDir} " +
|
|
|
|
"--log-error=${cfg.logError} --pid-file=${pidFile}";
|
2008-01-28 14:34:29 +00:00
|
|
|
|
|
|
|
in
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.mysql = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether to enable the MySQL server.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-12-16 14:54:36 +00:00
|
|
|
mysql = mkOption {
|
|
|
|
default = pkgs.mysql;
|
|
|
|
description = "
|
|
|
|
Which MySQL derivation to use.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
port = mkOption {
|
|
|
|
default = "3306";
|
|
|
|
description = "Port of MySQL";
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
default = "mysql";
|
|
|
|
description = "User account under which MySQL runs";
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
2009-10-12 12:30:43 +01:00
|
|
|
default = "/var/mysql"; # !!! should be /var/db/mysql
|
2009-07-15 12:19:11 +01:00
|
|
|
description = "Location where MySQL stores its table files";
|
|
|
|
};
|
|
|
|
|
|
|
|
logError = mkOption {
|
|
|
|
default = "/var/log/mysql_err.log";
|
|
|
|
description = "Location of the MySQL error logfile";
|
|
|
|
};
|
|
|
|
|
|
|
|
pidDir = mkOption {
|
|
|
|
default = "/var/run/mysql";
|
|
|
|
description = "Location of the file which stores the PID of the MySQL server";
|
|
|
|
};
|
2009-11-18 16:19:04 +00:00
|
|
|
|
|
|
|
initialDatabases = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = "List of database names and their initial schemas that should be used to create databases on the first startup of MySQL";
|
|
|
|
example = [
|
|
|
|
{ name = "foodatabase"; schema = ./foodatabase.sql; }
|
|
|
|
{ name = "bardatabase"; schema = ./bardatabase.sql; }
|
|
|
|
];
|
|
|
|
};
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
###### implementation
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
config = mkIf config.services.mysql.enable {
|
|
|
|
|
|
|
|
users.extraUsers = singleton
|
2009-03-06 12:27:00 +00:00
|
|
|
{ name = "mysql";
|
|
|
|
description = "MySQL server user";
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
environment.systemPackages = [mysql];
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2009-10-12 19:09:34 +01:00
|
|
|
jobs.mysql =
|
2009-10-12 18:27:57 +01:00
|
|
|
{ description = "MySQL server";
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-11-23 13:26:33 +00:00
|
|
|
startOn = "started network-interfaces";
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-10-12 12:30:43 +01:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
2009-03-06 12:27:00 +00:00
|
|
|
mkdir -m 0700 -p ${cfg.dataDir}
|
|
|
|
chown -R ${cfg.user} ${cfg.dataDir}
|
|
|
|
${mysql}/bin/mysql_install_db ${mysqldOptions}
|
|
|
|
fi
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-03-06 12:27:00 +00:00
|
|
|
mkdir -m 0700 -p ${cfg.pidDir}
|
|
|
|
chown -R ${cfg.user} ${cfg.pidDir}
|
2009-10-12 12:30:43 +01:00
|
|
|
'';
|
2008-01-28 14:34:29 +00:00
|
|
|
|
2009-11-07 12:43:32 +00:00
|
|
|
exec = "${mysql}/libexec/mysqld ${mysqldOptions}";
|
2009-11-18 16:19:04 +00:00
|
|
|
|
|
|
|
postStart =
|
|
|
|
''
|
|
|
|
# Wait until the MySQL server is available for use
|
|
|
|
count=0
|
|
|
|
while [ ! -e /tmp/mysql.sock ]
|
|
|
|
do
|
|
|
|
if [ $count -eq 30 ]
|
|
|
|
then
|
|
|
|
echo "Tried 30 times, giving up..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "MySQL daemon not yet started. Waiting for 1 second..."
|
|
|
|
count=$((count++))
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
# Create initial databases
|
|
|
|
|
|
|
|
${concatMapStrings (database:
|
|
|
|
''
|
|
|
|
if ! test -e "${cfg.dataDir}/${database.name}"; then
|
|
|
|
echo "Creating initial database: ${database.name}"
|
|
|
|
( echo "create database ${database.name};"
|
|
|
|
echo "use ${database.name};"
|
2009-11-18 21:56:47 +00:00
|
|
|
if [ -f "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}
|
|
|
|
elif [ -d "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}/mysql-databases/*.sql
|
|
|
|
fi
|
|
|
|
) | ${mysql}/bin/mysql -u root -N
|
2009-11-18 16:19:04 +00:00
|
|
|
fi
|
|
|
|
'') cfg.initialDatabases}
|
|
|
|
'';
|
2008-01-28 14:34:29 +00:00
|
|
|
|
2009-11-07 12:43:32 +00:00
|
|
|
# !!! Need a postStart script to wait until mysqld is ready to
|
|
|
|
# accept connections.
|
|
|
|
|
|
|
|
extraConfig = "kill timeout 60";
|
2009-10-12 12:30:43 +01:00
|
|
|
};
|
2009-07-15 12:19:11 +01:00
|
|
|
|
2009-03-06 12:27:00 +00:00
|
|
|
};
|
2009-07-15 12:19:11 +01:00
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
}
|