Hello, I am Siddharth, a novice to the NixOS world. I switched from Arch Linux to NixOS recently.
I’m learning MySQL/Mariadb for my placements and I need to setup Mariadb on my system.
I tried adding the below expression to my /etc/nixos/configuration.nix
file.
services.mysql.enable = true;
services.mysql.package = pkgs.mariadb;
And was able to install mariadb successfully! Simply running mariadb
launches the mariadb server on the terminal.
Then attempting to create users and granting the privileges does’nt seems to work. Gives error
So I ran this command mariadb -u root -p
where i was asked password and trying bunch of valid passwords from my system i was unable to connect to the server.
Then through AI/ChatGPT assistance I came to know creating a module in the same directory as configuration file
Therefore I created modules/mariadb-config.nix
file and imported it in my configuration.nix
file with this content
{ config, pkgs, ... }:
{
services.mysql = {
enable = true;
package = pkgs.mariadb;
ensureDatabases = [ "leetcode_db" ];
ensureUsers = [
{
name = "siddharth";
ensurePermissions = {
"leetcode_db.*" = "ALL PRIVILEGES";
};
}
];
};
systemd.services.setdbpass = {
description = "MySQL/Mariadb database password setup";
wants = [ "mysql.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = ''
${pkgs.mariadb}/bin/mysql -e "GRANT ALL PRIVILEGES ON leetcode_db.* TO siddharth@localhost IDENTIFIED BY 'sidsql';"
'';
User = "root";
PermissionsStartOnly = true;
RemainAfterExit = true;
};
};
}
This gives me this error
Please help , What is the correct and secure way to setup mariadb?