]>
Commit | Line | Data |
---|---|---|
587b9e34 | 1 | { lib, pkgs, config, myconfig, mylibs, ... }: |
4ff90563 IB |
2 | let |
3 | cfg = config.services.myDatabases; | |
4 | in { | |
5 | options.services.myDatabases = { | |
6 | mariadb = { | |
7 | enable = lib.mkOption { | |
8 | default = cfg.enable; | |
9 | example = true; | |
10 | description = "Whether to enable mariadb database"; | |
11 | type = lib.types.bool; | |
12 | }; | |
13 | }; | |
14 | }; | |
15 | ||
16 | config = lib.mkIf cfg.enable { | |
2368a4b7 | 17 | nixpkgs.overlays = [ (self: super: rec { |
4ff90563 | 18 | mariadb = mariadbPAM; |
2368a4b7 | 19 | mariadbPAM = super.mariadb.overrideAttrs(old: rec { |
4ff90563 | 20 | cmakeFlags = old.cmakeFlags ++ [ "-DWITH_AUTHENTICATION_PAM=ON" ]; |
2368a4b7 | 21 | buildInputs = old.buildInputs ++ [ self.pam ]; |
4ff90563 | 22 | }); |
2368a4b7 | 23 | }) ]; |
4ff90563 IB |
24 | |
25 | networking.firewall.allowedTCPPorts = [ 3306 ]; | |
26 | ||
27 | # for adminer, ssl is implemented with mysqli only, which is | |
28 | # currently disabled because it’s not compatible with pam. | |
29 | # Thus we need to generate two users for each 'remote': one remote | |
30 | # with SSL, and one localhost without SSL. | |
31 | # User identified by LDAP: | |
32 | # CREATE USER foo@% IDENTIFIED VIA pam USING 'mysql' REQUIRE SSL; | |
33 | # CREATE USER foo@localhost IDENTIFIED VIA pam USING 'mysql'; | |
34 | services.mysql = rec { | |
35 | enable = cfg.mariadb.enable; | |
36 | package = pkgs.mariadb; | |
37 | extraOptions = '' | |
38 | ssl_ca = ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt | |
39 | ssl_key = /var/lib/acme/mysql/key.pem | |
40 | ssl_cert = /var/lib/acme/mysql/fullchain.pem | |
41 | ''; | |
42 | }; | |
43 | ||
44 | security.acme.certs."mysql" = config.services.myCertificates.certConfig // { | |
45 | user = "mysql"; | |
46 | group = "mysql"; | |
47 | plugins = [ "fullchain.pem" "key.pem" "account_key.json" ]; | |
48 | domain = "db-1.immae.eu"; | |
49 | postRun = '' | |
50 | systemctl restart mysql.service | |
51 | ''; | |
52 | }; | |
53 | ||
85fe2b41 IB |
54 | services.cron = { |
55 | enable = true; | |
56 | systemCronJobs = let | |
57 | mycnf = pkgs.writeText "my.cnf" '' | |
58 | [mysqldump] | |
59 | user = root | |
60 | password = ${myconfig.env.databases.mysql.systemUsers.root} | |
61 | ''; | |
62 | in [ | |
63 | '' | |
64 | 30 1,13 * * * root ${pkgs.mariadb}/bin/mysqldump --defaults-file=${mycnf} --all-databases > /var/lib/mysql/backup.sql | |
65 | '' | |
66 | ]; | |
67 | }; | |
68 | ||
4ff90563 IB |
69 | security.pam.services = let |
70 | pam_ldap = "${pkgs.pam_ldap}/lib/security/pam_ldap.so"; | |
71 | pam_ldap_mysql = with myconfig.env.databases.mysql.pam; | |
72 | pkgs.writeText "mysql.conf" '' | |
73 | host ${myconfig.env.ldap.host} | |
74 | base ${myconfig.env.ldap.base} | |
75 | binddn ${dn} | |
76 | bindpw ${password} | |
77 | pam_filter ${filter} | |
78 | ssl start_tls | |
79 | ''; | |
80 | in [ | |
81 | { | |
82 | name = "mysql"; | |
83 | text = '' | |
84 | # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/ | |
85 | auth required ${pam_ldap} config=${pam_ldap_mysql} | |
86 | account required ${pam_ldap} config=${pam_ldap_mysql} | |
87 | ''; | |
88 | } | |
89 | ]; | |
90 | ||
91 | }; | |
92 | } | |
93 |