2009-10-12 19:27:57 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2008-01-28 15:34:29 +01:00
|
|
|
let
|
|
|
|
|
2008-06-25 23:58:51 +02:00
|
|
|
cfg = config.services.mysql;
|
|
|
|
|
|
|
|
mysql = pkgs.mysql;
|
|
|
|
|
|
|
|
pidFile = "${cfg.pidDir}/mysqld.pid";
|
|
|
|
|
|
|
|
mysqldOptions =
|
|
|
|
"--user=${cfg.user} --datadir=${cfg.dataDir} " +
|
|
|
|
"--log-error=${cfg.logError} --pid-file=${pidFile}";
|
2008-01-28 15:34:29 +01:00
|
|
|
|
|
|
|
in
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.mysql = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether to enable the MySQL server.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
default = "3306";
|
|
|
|
description = "Port of MySQL";
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
default = "mysql";
|
|
|
|
description = "User account under which MySQL runs";
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
2009-10-12 13:30:43 +02:00
|
|
|
default = "/var/mysql"; # !!! should be /var/db/mysql
|
2009-07-15 13:19:11 +02: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-03-06 13:27:00 +01:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
###### implementation
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
config = mkIf config.services.mysql.enable {
|
|
|
|
|
|
|
|
users.extraUsers = singleton
|
2009-03-06 13:27:00 +01:00
|
|
|
{ name = "mysql";
|
|
|
|
description = "MySQL server user";
|
2009-07-15 13:19:11 +02:00
|
|
|
};
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
environment.systemPackages = [mysql];
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
jobs.mysql =
|
2009-10-12 19:27:57 +02:00
|
|
|
{ description = "MySQL server";
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-10-12 13:30:43 +02:00
|
|
|
startOn = "filesystems";
|
|
|
|
stopOn = "shutdown";
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-10-12 13:30:43 +02:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
2009-03-06 13:27:00 +01:00
|
|
|
mkdir -m 0700 -p ${cfg.dataDir}
|
|
|
|
chown -R ${cfg.user} ${cfg.dataDir}
|
|
|
|
${mysql}/bin/mysql_install_db ${mysqldOptions}
|
|
|
|
fi
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-03-06 13:27:00 +01:00
|
|
|
mkdir -m 0700 -p ${cfg.pidDir}
|
|
|
|
chown -R ${cfg.user} ${cfg.pidDir}
|
2009-10-12 13:30:43 +02:00
|
|
|
'';
|
2008-01-28 15:34:29 +01:00
|
|
|
|
2009-10-12 13:30:43 +02:00
|
|
|
exec = "${mysql}/bin/mysqld ${mysqldOptions}";
|
2008-01-28 15:34:29 +01:00
|
|
|
|
2009-10-12 13:30:43 +02:00
|
|
|
postStop =
|
|
|
|
''
|
2009-03-06 13:27:00 +01:00
|
|
|
pid=$(cat ${pidFile})
|
|
|
|
kill "$pid"
|
|
|
|
${mysql}/bin/mysql_waitpid "$pid" 1000
|
2009-10-12 13:30:43 +02:00
|
|
|
'';
|
|
|
|
};
|
2009-07-15 13:19:11 +02:00
|
|
|
|
2009-03-06 13:27:00 +01:00
|
|
|
};
|
2009-07-15 13:19:11 +02:00
|
|
|
|
2008-01-28 15:34:29 +01:00
|
|
|
}
|