Is it possible to install MariaDB through a flake?

I tried adding mariadb to devShell buildInputs, and when I tried to run the binary, it failed to create the necessary files, which is understandable.

So I googled on how to install mariadb on nixos, and it turned out that adding

        services.mysql.package = pkgs.mariadb;
        services.mysql.enable = true;

is supposed to resolve the issue. Which it did, when I used it in my ./configuration.nix

So, I tried putting it into the flake I was creating

{
  inputs = {
    nixpkgs.url = "nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        packages = with pkgs; [ mariadb ];
      in {
        devShell = pkgs.mkShell { buildInputs = packages; };
        services.mysql.package = pkgs.mariadb;
        services.mysql.enable = true;
      });
}

But, that didn’t do anything, so I tried following the same “pattern” that I have in my systems flake.nix, which has the configuration.nix as a module, which resulted in this ?

{
  inputs = {
    nixpkgs.url = "nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        packages = with pkgs; [ mariadb ];
        user = "albert";
        system = "x86_64-linux";
      in {
        devShell = pkgs.mkShell { buildInputs = packages; };
        nixosConfigurations = {
          ${user} = nixpkgs.lib.nixosSystem {

            inherit system;

            modules = [{
              services.mysql.package = pkgs.mariadb;
              services.mysql.enable = true;
            }];
          };
        };
      });
}

At the end of the day, that didn’t work, and I am confused to why. Could anyone help me ?
I am sorry, I am relatively new to nix, and it appears some basic concepts are escaping me :frowning:

If you want mariadb to be available and runnable as your user, you need to manually set the configuration in the dev shell to use some user writeable data dir.

I do not know maria well enough to know whterh that would be via env vars or flags.

Another way would be to use devenv, which allows to define a shell in a similar way to a system configuration, which can then start related services via devenv up.

I do not know if there is direct suport for mariadb, all examples I found are using plain mysql.

1 Like

I am doing that in selfoss: Just adding pkgs.mariadb to mkShell’s inputs:

and then running it with a temporary directory for data:

but devenv will probably be easier these days.

2 Likes

Thank you! It worked pretty nicely ! This ended up being my full flake for development if anyone is struggling with this:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    systems.url = "github:nix-systems/default";
    devenv.url = "github:cachix/devenv";
  };

  outputs = { self, nixpkgs, devenv, systems, ... }@inputs:
    let forEachSystem = nixpkgs.lib.genAttrs (import systems);
    in {
      devShells = forEachSystem (system:
        let pkgs = nixpkgs.legacyPackages.${system};
        in {
          default = devenv.lib.mkShell {
            inherit inputs pkgs;
            modules = [{
              packages = with pkgs; [
                mariadb
                nodePackages.pnpm
                nodePackages.prisma
                nodejs-slim_20
                tmux
              ];
              enterShell = with pkgs; ''
                export PRISMA_MIGRATION_ENGINE_BINARY="${prisma-engines}/bin/migration-engine"
                export PRISMA_QUERY_ENGINE_BINARY="${prisma-engines}/bin/query-engine"
                export PRISMA_QUERY_ENGINE_LIBRARY="${prisma-engines}/lib/libquery_engine.node"
                export PRISMA_INTROSPECTION_ENGINE_BINARY="${prisma-engines}/bin/introspection-engine"
                export PRISMA_FMT_BINARY="${prisma-engines}/bin/prisma-fmt"
              '';
              # https://devenv.sh/reference/options/
              services.mysql.package = pkgs.mariadb;
              services.mysql.enable = true;
            }];
          };
        });
    };
}

Thank you for explaining how to do it without devenv! I learned a lot from your repo, have my star :star: