services.mariadb: init

This commit is contained in:
2023-10-04 10:13:56 +08:00
parent e06623ce79
commit 2cbe5945b7
3 changed files with 46 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ inputs:
./frp.nix
./beesd.nix
./snapper.nix
./mariadb.nix
];
options.nixos.services = let inherit (inputs.lib) mkOption types; in
{

View File

@@ -0,0 +1,43 @@
inputs:
{
options.nixos.services.mariadb = let inherit (inputs.lib) mkOption types; in
{
enable = mkOption { type = types.bool; default = false; };
instances = mkOption
{
type = types.attrsOf (types.submodule (submoduleInputs: { options =
{
database = mkOption { type = types.nonEmptyStr; default = submoduleInputs.config._module.args.name; };
user = mkOption { type = types.nonEmptyStr; default = submoduleInputs.config._module.args.name; };
passwordFile = mkOption { type = types.nullOr types.nonEmptyStr; default = null; };
};}));
default = {};
};
};
config =
let
inherit (inputs.config.nixos.services) mariadb;
inherit (inputs.lib) mkIf;
inherit (inputs.localLib) attrsToList;
inherit (builtins) map listToAttrs filter;
in mkIf mariadb.enable
{
services =
{
mysql =
{
enable = true;
ensureDatabases = map (db: db.value.database) (attrsToList mariadb.instances);
ensureUsers = map (db: { name = db.value.user; }) (attrsToList mariadb.instances);
};
mysqlBackup =
{
enable = true;
databases = map (db: db.value.database) (attrsToList mariadb.instances);
};
};
sops.secrets = listToAttrs (map
(db: { name = "mariadb/${db.value.user}"; value.owner = inputs.config.users.users.mysql.name; })
(filter (db: db.value.passwordFile == null) (attrsToList mariadb.instances)));
};
}

View File

@@ -17,8 +17,8 @@ inputs:
config =
let
inherit (inputs.config.nixos.services) postgresql;
inherit (inputs.lib) mkMerge mkAfter concatStringsSep mkIf;
inherit (inputs.localLib) stripeTabs attrsToList;
inherit (inputs.lib) mkAfter concatStringsSep mkIf;
inherit (inputs.localLib) attrsToList;
inherit (builtins) map listToAttrs filter;
in mkIf postgresql.enable
{