| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2009-10-12 17:27:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02: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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 15:24:15 +00:00
										 |  |  |   mysql = cfg.package; | 
					
						
							| 
									
										
										
										
											2008-06-25 21:58:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 07:54:04 -05:00
										 |  |  |   is55 = mysql.mysqlVersion == "5.5"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mysqldDir = if is55 then "${mysql}/bin" else "${mysql}/libexec"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-25 21:58:51 +00:00
										 |  |  |   pidFile = "${cfg.pidDir}/mysqld.pid"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mysqldOptions = | 
					
						
							| 
									
										
										
										
											2014-03-14 11:56:54 -04:00
										 |  |  |     "--user=${cfg.user} --datadir=${cfg.dataDir} --basedir=${mysql} " + | 
					
						
							| 
									
										
										
										
											2013-07-14 01:47:40 +02:00
										 |  |  |     "--pid-file=${pidFile}"; | 
					
						
							| 
									
										
										
										
											2008-01-28 14:34:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-13 20:58:48 +00:00
										 |  |  |   myCnf = pkgs.writeText "my.cnf" | 
					
						
							|  |  |  |   ''
 | 
					
						
							|  |  |  |     [mysqld] | 
					
						
							| 
									
										
										
										
											2014-04-30 10:21:29 -04:00
										 |  |  |     port = ${toString cfg.port} | 
					
						
							| 
									
										
										
										
											2011-07-13 20:58:48 +00:00
										 |  |  |     ${optionalString (cfg.replication.role == "master" || cfg.replication.role == "slave") "log-bin=mysql-bin"} | 
					
						
							|  |  |  |     ${optionalString (cfg.replication.role == "master" || cfg.replication.role == "slave") "server-id = ${toString cfg.replication.serverId}"} | 
					
						
							| 
									
										
										
										
											2014-02-26 07:54:04 -05:00
										 |  |  |     ${optionalString (cfg.replication.role == "slave" && !is55) | 
					
						
							| 
									
										
										
										
											2011-07-13 20:58:48 +00:00
										 |  |  |     ''
 | 
					
						
							|  |  |  |       master-host = ${cfg.replication.masterHost} | 
					
						
							|  |  |  |       master-user = ${cfg.replication.masterUser} | 
					
						
							|  |  |  |       master-password = ${cfg.replication.masterPassword} | 
					
						
							|  |  |  |       master-port = ${toString cfg.replication.masterPort} | 
					
						
							|  |  |  |     ''}
 | 
					
						
							| 
									
										
										
										
											2013-07-14 01:47:40 +02:00
										 |  |  |     ${cfg.extraOptions} | 
					
						
							| 
									
										
										
										
											2011-07-13 20:58:48 +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
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 11:19:11 +00:00
										 |  |  |       enable = mkOption { | 
					
						
							|  |  |  |         default = false; | 
					
						
							|  |  |  |         description = "
 | 
					
						
							|  |  |  |           Whether to enable the MySQL server. | 
					
						
							|  |  |  |         ";
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 15:24:15 +00:00
										 |  |  |       package = mkOption { | 
					
						
							| 
									
										
										
										
											2014-02-26 07:54:04 -05:00
										 |  |  |         type = types.package; | 
					
						
							|  |  |  |         example = literalExample "pkgs.mysql"; | 
					
						
							| 
									
										
										
										
											2009-12-16 14:54:36 +00:00
										 |  |  |         description = "
 | 
					
						
							|  |  |  |           Which MySQL derivation to use. | 
					
						
							|  |  |  |         ";
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 11:19:11 +00:00
										 |  |  |       port = mkOption { | 
					
						
							|  |  |  |         default = "3306"; | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  |         description = "Port of MySQL"; | 
					
						
							| 
									
										
										
										
											2009-07-15 11:19:11 +00:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       user = mkOption { | 
					
						
							|  |  |  |         default = "mysql"; | 
					
						
							|  |  |  |         description = "User account under which MySQL runs"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       dataDir = mkOption { | 
					
						
							| 
									
										
										
										
											2009-10-12 11:30:43 +00:00
										 |  |  |         default = "/var/mysql"; # !!! should be /var/db/mysql | 
					
						
							| 
									
										
										
										
											2009-07-15 11:19:11 +00:00
										 |  |  |         description = "Location where MySQL stores its table files"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       pidDir = mkOption { | 
					
						
							|  |  |  |         default = "/var/run/mysql"; | 
					
						
							|  |  |  |         description = "Location of the file which stores the PID of the MySQL server"; | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-14 01:47:40 +02:00
										 |  |  |       extraOptions = mkOption { | 
					
						
							|  |  |  |         default = ""; | 
					
						
							|  |  |  |         example = ''
 | 
					
						
							|  |  |  |           key_buffer_size = 6G | 
					
						
							|  |  |  |           table_cache = 1600 | 
					
						
							|  |  |  |           log-error = /var/log/mysql_err.log | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Provide extra options to the MySQL configuration file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           Please note, that these options are added to the | 
					
						
							|  |  |  |           <literal>[mysqld]</literal> section so you don't need to explicitly | 
					
						
							|  |  |  |           state it again. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"; | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |         example = [ | 
					
						
							| 
									
										
										
										
											2013-10-23 20:06:39 +02:00
										 |  |  |           { name = "foodatabase"; schema = literalExample "./foodatabase.sql"; } | 
					
						
							|  |  |  |           { name = "bardatabase"; schema = literalExample "./bardatabase.sql"; } | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  |         default = null; | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |         description = "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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  |       # FIXME: remove this option; it's a really bad idea. | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |       rootPassword = mkOption { | 
					
						
							|  |  |  |         default = null; | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |         description = "Path to a file containing the root password, modified on the first startup. Not specifying a root password will leave the root password empty."; | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-13 20:58:48 +00:00
										 |  |  |       replication = { | 
					
						
							|  |  |  |         role = mkOption { | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |           default = "none"; | 
					
						
							|  |  |  |           description = "Role of the MySQL server instance. Can be either: master, slave or none"; | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-13 20:58:48 +00:00
										 |  |  |         serverId = mkOption { | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |           default = 1; | 
					
						
							|  |  |  |           description = "Id of the MySQL server instance. This number must be unique for each instance"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         masterHost = mkOption { | 
					
						
							|  |  |  |           description = "Hostname of the MySQL master server"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         masterUser = mkOption { | 
					
						
							|  |  |  |           description = "Username of the MySQL replication user"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         masterPassword = mkOption { | 
					
						
							|  |  |  |           description = "Password of the MySQL replication user"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         masterPort = mkOption { | 
					
						
							|  |  |  |           default = 3306; | 
					
						
							|  |  |  |           description = "Port number on which the MySQL master server runs"; | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 11:19:11 +00:00
										 |  |  |   config = mkIf config.services.mysql.enable { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 15:20:25 +02:00
										 |  |  |     users.extraUsers.mysql = { | 
					
						
							|  |  |  |       description = "MySQL server user"; | 
					
						
							|  |  |  |       group = "mysql"; | 
					
						
							|  |  |  |       uid = config.ids.uids.mysql; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     users.extraGroups.mysql.gid = config.ids.gids.mysql; | 
					
						
							| 
									
										
										
										
											2009-03-06 12:27:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 11:19:11 +00:00
										 |  |  |     environment.systemPackages = [mysql]; | 
					
						
							| 
									
										
										
										
											2009-03-06 12:27:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-16 12:33:18 +01:00
										 |  |  |     systemd.services.mysql = | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  |       { description = "MySQL Server"; | 
					
						
							| 
									
										
										
										
											2008-06-25 21:58:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  |         wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         unitConfig.RequiresMountsFor = "${cfg.dataDir}"; | 
					
						
							| 
									
										
										
										
											2008-06-25 21:58:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-12 11:30:43 +00: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} | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                 touch /tmp/mysql_init | 
					
						
							| 
									
										
										
										
											2009-03-06 12:27:00 +00:00
										 |  |  |             fi | 
					
						
							| 
									
										
										
										
											2008-06-25 21:58:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-06 12:27:00 +00:00
										 |  |  |             mkdir -m 0700 -p ${cfg.pidDir} | 
					
						
							|  |  |  |             chown -R ${cfg.user} ${cfg.pidDir} | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  |           '';
 | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 07:54:04 -05:00
										 |  |  |         serviceConfig.ExecStart = "${mysqldDir}/mysqld --defaults-extra-file=${myCnf} ${mysqldOptions}"; | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         postStart = | 
					
						
							|  |  |  |           ''
 | 
					
						
							| 
									
										
										
										
											2009-11-18 16:19:04 +00:00
										 |  |  |             # Wait until the MySQL server is available for use | 
					
						
							|  |  |  |             count=0 | 
					
						
							|  |  |  |             while [ ! -e /tmp/mysql.sock ] | 
					
						
							|  |  |  |             do | 
					
						
							|  |  |  |                 if [ $count -eq 30 ] | 
					
						
							|  |  |  |                 then | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                     echo "Tried 30 times, giving up..." | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                     exit 1 | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                 fi | 
					
						
							| 
									
										
										
										
											2009-11-18 16:19:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                 echo "MySQL daemon not yet started. Waiting for 1 second..." | 
					
						
							|  |  |  |                 count=$((count++)) | 
					
						
							|  |  |  |                 sleep 1 | 
					
						
							| 
									
										
										
										
											2009-11-18 16:19:04 +00:00
										 |  |  |             done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |             if [ -f /tmp/mysql_init ] | 
					
						
							|  |  |  |             then | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  |                 ${concatMapStrings (database: | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                   ''
 | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                     # Create initial databases | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                     if ! test -e "${cfg.dataDir}/${database.name}"; then | 
					
						
							|  |  |  |                         echo "Creating initial database: ${database.name}" | 
					
						
							|  |  |  |                         ( echo "create database ${database.name};" | 
					
						
							|  |  |  |                           echo "use ${database.name};" | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                           if [ -f "${database.schema}" ] | 
					
						
							|  |  |  |                           then | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                               cat ${database.schema} | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                           elif [ -d "${database.schema}" ] | 
					
						
							|  |  |  |                           then | 
					
						
							|  |  |  |                               cat ${database.schema}/mysql-databases/*.sql
 | 
					
						
							|  |  |  |                           fi | 
					
						
							|  |  |  |                         ) | ${mysql}/bin/mysql -u root -N | 
					
						
							| 
									
										
										
										
											2010-04-16 18:06:23 +00:00
										 |  |  |                     fi | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  |                   '') cfg.initialDatabases}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 07:54:04 -05:00
										 |  |  |                 ${optionalString (cfg.replication.role == "slave" && is55) | 
					
						
							|  |  |  |                   ''
 | 
					
						
							|  |  |  |                     # Set up the replication master | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     ( 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;" | 
					
						
							|  |  |  |                     ) | ${mysql}/bin/mysql -u root -N | 
					
						
							|  |  |  |                   ''}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                 ${optionalString (cfg.initialScript != null) | 
					
						
							|  |  |  |                   ''
 | 
					
						
							|  |  |  |                     # Execute initial script | 
					
						
							|  |  |  |                     cat ${cfg.initialScript} | ${mysql}/bin/mysql -u root -N | 
					
						
							|  |  |  |                   ''}
 | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                 ${optionalString (cfg.rootPassword != null) | 
					
						
							|  |  |  |                   ''
 | 
					
						
							|  |  |  |                     # Change root password | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-18 15:01:44 +00:00
										 |  |  |                     ( echo "use mysql;" | 
					
						
							|  |  |  |                       echo "update user set Password=password('$(cat ${cfg.rootPassword})') where User='root';" | 
					
						
							|  |  |  |                       echo "flush privileges;" | 
					
						
							|  |  |  |                     ) | ${mysql}/bin/mysql -u root -N | 
					
						
							|  |  |  |                   ''}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               rm /tmp/mysql_init | 
					
						
							|  |  |  |             fi | 
					
						
							| 
									
										
										
										
											2012-12-18 13:40:04 +01:00
										 |  |  |           ''; # */
 | 
					
						
							| 
									
										
										
										
											2009-10-12 11:30:43 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-28 14:34:29 +00:00
										 |  |  | } |