Skip to main content


To use a local MariaDB for development with Devbox and Nix, you will need to configure MariaDB to install and manage the configuration locally. This can be done using Environment variables to create the data directory, pidfile, and unix sock locally.

Example Repo

Open In

Adding MariaDB to your Shell

devbox add mariadb, or in your devbox.json add

    "packages": [

Environment Variables

This script should be sourced in the init_hook of our devbox.json. This sets environment variables needed to configure and start MariaDB, as well as setting the data directory to a local folder.

# Environment variables for running MariaDB with local configuration
export MYSQL_BASEDIR=$(which mariadb | sed -r "s/\/bin\/mariadb//g")
export MYSQL_HOME=$PWD/conf/mysql # or another folder in your project directory
# Store DB data in a local folder
# Keep the socket and pidfile in our conf folder, and out of the Nix Store
export MYSQL_UNIX_PORT=$MYSQL_HOME/mysql.sock

Installing the Database

We can check if the MySQL data directory exists, and if not install the database using the environment variables we configured:

    if [ ! -d "$MYSQL_DATADIR" ]; then
# Install the Database
mysql_install_db --auth-root-authentication-method=normal \
--datadir=$MYSQL_DATADIR --basedir=$MYSQL_BASEDIR \

Starting the Daemon

Similarly, we can start the database using mysqld, passing the environment variables where needed

    mysqld --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE \
--socket=$MYSQL_UNIX_PORT 2> $MYSQL_HOME/mysql.log & MYSQL_PID=$!

The daemon can be terminated using mysqladmin:

  mysqladmin -u root --socket=$MYSQL_UNIX_PORT shutdown