]>
Commit | Line | Data |
---|---|---|
1 | { lib, pkgs, config, ... }: | |
2 | let | |
3 | cfg = config.myServices.databases.mariadb; | |
4 | in { | |
5 | options.myServices.databases = { | |
6 | mariadb = { | |
7 | enable = lib.mkOption { | |
8 | default = false; | |
9 | example = true; | |
10 | description = "Whether to enable mariadb database"; | |
11 | type = lib.types.bool; | |
12 | }; | |
13 | package = lib.mkOption { | |
14 | type = lib.types.package; | |
15 | default = pkgs.mariadb; | |
16 | description = '' | |
17 | Mariadb package to use. | |
18 | ''; | |
19 | }; | |
20 | credentials = lib.mkOption { | |
21 | default = {}; | |
22 | description = "Credentials"; | |
23 | type = lib.types.attrsOf lib.types.str; | |
24 | }; | |
25 | ldapConfig = lib.mkOption { | |
26 | description = "LDAP configuration to allow PAM identification via LDAP"; | |
27 | type = lib.types.submodule { | |
28 | options = { | |
29 | host = lib.mkOption { type = lib.types.str; }; | |
30 | base = lib.mkOption { type = lib.types.str; }; | |
31 | dn = lib.mkOption { type = lib.types.str; }; | |
32 | password = lib.mkOption { type = lib.types.str; }; | |
33 | filter = lib.mkOption { type = lib.types.str; }; | |
34 | }; | |
35 | }; | |
36 | }; | |
37 | replicationLdapConfig = lib.mkOption { | |
38 | description = "LDAP configuration to allow replication"; | |
39 | type = lib.types.submodule { | |
40 | options = { | |
41 | host = lib.mkOption { type = lib.types.str; }; | |
42 | base = lib.mkOption { type = lib.types.str; }; | |
43 | dn = lib.mkOption { type = lib.types.str; }; | |
44 | password = lib.mkOption { type = lib.types.str; }; | |
45 | }; | |
46 | }; | |
47 | }; | |
48 | dataDir = lib.mkOption { | |
49 | type = lib.types.path; | |
50 | default = "/var/lib/mysql"; | |
51 | description = '' | |
52 | The directory where Mariadb stores its data. | |
53 | ''; | |
54 | }; | |
55 | # Output variables | |
56 | socketsDir = lib.mkOption { | |
57 | type = lib.types.path; | |
58 | default = "/run/mysqld"; | |
59 | description = '' | |
60 | The directory where Mariadb puts sockets. | |
61 | ''; | |
62 | }; | |
63 | sockets = lib.mkOption { | |
64 | type = lib.types.attrsOf lib.types.path; | |
65 | default = { | |
66 | mysqld = "${cfg.socketsDir}/mysqld.sock"; | |
67 | }; | |
68 | readOnly = true; | |
69 | description = '' | |
70 | Mariadb sockets | |
71 | ''; | |
72 | }; | |
73 | }; | |
74 | }; | |
75 | ||
76 | config = lib.mkIf cfg.enable { | |
77 | networking.firewall.allowedTCPPorts = [ 3306 ]; | |
78 | ||
79 | # for adminer, ssl is implemented with mysqli only, which is | |
80 | # currently disabled because it’s not compatible with pam. | |
81 | # Thus we need to generate two users for each 'remote': one remote | |
82 | # with SSL, and one localhost without SSL. | |
83 | # User identified by LDAP: | |
84 | # CREATE USER foo@% IDENTIFIED VIA pam USING 'mysql' REQUIRE SSL; | |
85 | # CREATE USER foo@localhost IDENTIFIED VIA pam USING 'mysql'; | |
86 | ||
87 | # To create a user (host) for replication: | |
88 | # CREATE USER 'host'@'%' IDENTIFIED VIA pam USING 'mysql_replication' REQUIRE SSL; | |
89 | # GRANT REPLICATION SLAVE, REPLICATION CLIENT, RELOAD, LOCK TABLES, SELECT, SHOW VIEW ON *.* TO 'host'@'%'; | |
90 | # (the lock/select grant permits to let the replication host handle | |
91 | # the initial fetch of the database) | |
92 | # % should be valid for both localhost (for cron dumps) and the origin host. | |
93 | services.mysql = { | |
94 | enable = true; | |
95 | package = cfg.package; | |
96 | dataDir = cfg.dataDir; | |
97 | settings = { | |
98 | mysqld = { | |
99 | ssl_ca = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; | |
100 | ssl_key = "${config.security.acme.certs.mysql.directory}/key.pem"; | |
101 | ssl_cert = "${config.security.acme.certs.mysql.directory}/fullchain.pem"; | |
102 | ||
103 | # for replication | |
104 | log-bin = "mariadb-bin"; | |
105 | server-id = "1"; | |
106 | ||
107 | # this introduces a small delay before storing on disk, but | |
108 | # makes it order of magnitudes quicker | |
109 | innodb_flush_log_at_trx_commit = "0"; | |
110 | }; | |
111 | }; | |
112 | }; | |
113 | ||
114 | users.users.mysql.extraGroups = [ "keys" ]; | |
115 | security.acme.certs."mysql" = config.myServices.databasesCerts // { | |
116 | group = "mysql"; | |
117 | domain = "db-1.immae.eu"; | |
118 | postRun = '' | |
119 | systemctl restart mysql.service | |
120 | ''; | |
121 | }; | |
122 | ||
123 | secrets.keys = { | |
124 | "mysql/mysqldump" = { | |
125 | permissions = "0400"; | |
126 | user = "root"; | |
127 | group = "root"; | |
128 | text = '' | |
129 | [mysqldump] | |
130 | user = root | |
131 | password = ${cfg.credentials.root} | |
132 | ''; | |
133 | }; | |
134 | "mysql/pam" = { | |
135 | permissions = "0400"; | |
136 | user = "mysql"; | |
137 | group = "mysql"; | |
138 | text = with cfg.ldapConfig; '' | |
139 | host ${host} | |
140 | base ${base} | |
141 | binddn ${dn} | |
142 | bindpw ${password} | |
143 | pam_filter ${filter} | |
144 | ssl start_tls | |
145 | ''; | |
146 | }; | |
147 | "mysql/pam_replication" = { | |
148 | permissions = "0400"; | |
149 | user = "mysql"; | |
150 | group = "mysql"; | |
151 | text = with cfg.replicationLdapConfig; '' | |
152 | host ${host} | |
153 | base ${base} | |
154 | binddn ${dn} | |
155 | bindpw ${password} | |
156 | pam_login_attribute cn | |
157 | ssl start_tls | |
158 | ''; | |
159 | }; | |
160 | }; | |
161 | ||
162 | security.pam.services = let | |
163 | pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so"; | |
164 | in { | |
165 | mysql = { | |
166 | text = '' | |
167 | # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/ | |
168 | auth required ${pam_ldap} config=${config.secrets.fullPaths."mysql/pam"} | |
169 | account required ${pam_ldap} config=${config.secrets.fullPaths."mysql/pam"} | |
170 | ''; | |
171 | }; | |
172 | mysql_replication = { | |
173 | text = '' | |
174 | auth required ${pam_ldap} config=${config.secrets.fullPaths."mysql/pam_replication"} | |
175 | account required ${pam_ldap} config=${config.secrets.fullPaths."mysql/pam_replication"} | |
176 | ''; | |
177 | }; | |
178 | }; | |
179 | ||
180 | }; | |
181 | } |