forked from nixcloud/nixcloud-webservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.nix
176 lines (162 loc) · 5.69 KB
/
mysql.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{ lib, mkUnique, pkgs, config, ... }:
let
filterDb = lib.const (db: db.type == "mysql");
dbs = lib.filterAttrs filterDb config.database;
inherit (config.mysql) package dataDir;
isMariaDb = lib.hasPrefix "mariadb" package.name;
serverName = if isMariaDb then "MariaDB" else "MySQL";
progName = if isMariaDb then "mariadb" else "mysql";
authMethod = if isMariaDb then "unix_socket" else "auth_socket";
escapeSql = val: "'${lib.escape ["'" "\\"] val}'";
assertSqlIdent = v: assert builtins.match "[0-9a-zA-Z$_]+" v != null; v;
mysqlShell = user: database: let
prog = "${package}/bin/mysql";
args = [
"--user=${user}"
"--socket=${configuration.socket}"
database
];
in "${prog} " + lib.concatMapStringsSep " " lib.escapeShellArg args;
dbservices = lib.listToAttrs (lib.concatMap (cfg: let
dbuser = mkUnique cfg.user;
mkStateFile = action: let
filename = ".database-${action}-${cfg.name}";
in "${config.stateDir}/${filename}";
in [
{ name = "database-${cfg.name}";
value = {
instance.requiredBy = [ "database-${cfg.name}.target" ];
instance.before = [ "database-${cfg.name}.target" ];
instance.after = [ "mysql.service" ];
script = let
createDb = pkgs.writeText "create-${cfg.name}.sql" ''
CREATE USER IF NOT EXISTS ${escapeSql dbuser}@'localhost'
IDENTIFIED WITH ${authMethod};
CREATE DATABASE `${assertSqlIdent cfg.name}`;
GRANT ALL ON `${assertSqlIdent cfg.name}`.* TO
${escapeSql dbuser}@'localhost';
'';
in "${mysqlShell (mkUnique "mysql") "mysql"} < ${createDb}";
postStart = "touch ${lib.escapeShellArg (mkStateFile "create")}";
unitConfig.ConditionPathExists = "!${mkStateFile "create"}";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
PermissionsStartOnly = true;
User = "mysql";
Group = "mysql";
};
};
}
{ name = "database-${cfg.name}-post-create";
value = {
instance.requiredBy = [ "database-${cfg.name}.target" ];
instance.before = [ "database-${cfg.name}.target" ];
instance.after = [
"mysql.service"
"database-${cfg.name}.service"
];
script = cfg.postCreate;
path = lib.singleton (pkgs.writeScriptBin "sqlsh" ''
#!${pkgs.stdenv.shell}
exec ${mysqlShell (mkUnique cfg.user) cfg.name} "$@"
'');
postStart = "touch ${lib.escapeShellArg (mkStateFile "post-create")}";
unitConfig.ConditionPathExists = "!${mkStateFile "post-create"}";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
PermissionsStartOnly = true;
User = cfg.user;
};
};
}
]) (lib.attrValues dbs));
configuration = {
basedir = package;
datadir = dataDir;
log-output = "NONE";
skip-networking = true;
socket = "${config.runtimeDir}/.mysql.sock";
plugin-load-add = "auth_socket.so";
};
cfgVals = let
mkCfgVal = name: val: let
flag = "--${name}" + lib.optionalString (val != true) "=${val}";
in lib.escapeShellArg flag;
in lib.mapAttrsToList mkCfgVal configuration;
cmdLine = lib.concatStringsSep " " cfgVals;
in {
options.mysql = {
package = lib.mkOption {
type = lib.types.package;
default = pkgs.mariadb;
example = lib.literalExample "pkgs.mysql57";
description = "MySQL package to use.";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "${config.stateDir}/mysql";
readOnly = true;
description = "Data directory for MySQL";
};
};
config = lib.mkIf (dbs != {}) {
users.mysql = {
group = "mysql";
description = "${serverName} Server User";
};
groups.mysql = {};
systemd.services = {
mysql-initdb = {
description = "Initialize ${serverName} Server";
instance.requiredBy = [ "mysql.service" ];
instance.before = [ "mysql.service" ];
preStart = let
escDataDir = lib.escapeShellArg dataDir;
escUserGroup = lib.escapeShellArg (mkUnique "mysql");
in ''
mkdir -m 0711 -p ${escDataDir}
chmod 0700 ${escDataDir}
chown ${escUserGroup}:${escUserGroup} ${escDataDir}
'';
unitConfig.ConditionPathExists = "!${dataDir}/mysql";
serviceConfig = {
ExecStart = "${package}/bin/mysql_install_db ${cmdLine}";
Type = "oneshot";
RemainAfterExit = true;
PermissionsStartOnly = true;
User = "mysql";
Group = "mysql";
};
};
mysql = {
description = "${serverName} Server";
instance.requiredBy = [ "db-server.target" ];
instance.before = [ "db-server.target" ];
after = [ "network.target" ];
postStart = let
superuser = escapeSql (mkUnique "mysql");
dropRoot = pkgs.writeText "drop-root.sql" ''
CREATE USER ${superuser}@'localhost' IDENTIFIED WITH ${authMethod};
GRANT ALL ON *.* TO ${superuser}@'localhost' WITH GRANT OPTION;
DROP DATABASE test;
DELETE FROM mysql.user WHERE user IN ('root', ''');
FLUSH PRIVILEGES;
'';
in ''
if [ ! -e ${lib.escapeShellArg dataDir}/.root-dropped ]; then
${mysqlShell "root" "mysql"} < ${dropRoot}
touch ${lib.escapeShellArg dataDir}/.root-dropped
fi
'';
serviceConfig = {
ExecStart = "@${package}/bin/mysqld ${progName} ${cmdLine}";
User = "mysql";
Group = "mysql";
Type = "notify";
};
};
} // dbservices;
};
}