Is there a way to run mysql via nixpkgs on non nixos distros like ubuntu

Hi,

I’m trying to run mysql80 package from nixpkgs on ubuntu, I have initialized the data directory and ran the mysqld command with --initialize-insecure option but I still can’t login to mysql, (shows access denied for root@localhost).

I think this maybe related to the fact that mysql is usually run using the mysql user.
Just wondering if someone’s figured out a way to run mysql like this

I’m also looking for the same thing.

Basically I would like to be able to run “services” from a flake file. I’m investigating.

I think we have to check the nixosModule of the flake file.

There’s actually two different questions here. I’m not familiar enough with MySQL to tell why it’s not letting you log in, but I am familiar enough with how Nix works to tell you you can’t really run “proper” system services from a flake, especially on non-NixOS. It’s entirely possible to have a completely user-level Nix install that does not have any sort of root access, and thus can’t manage “real” system services for something like systemd. The only real option here is to use a userspace process manager like supervisord and configure everything to run as the current user, without assuming you can get any level of system access.

1 Like

If you have generous cgroups settings, you might be able to use imperative nixos containers built from a config file: https://nixos.org/manual/nixos/stable/#ch-containers

But yes, as @K900 says, nix on other distros is purely a package manager. NixOS modules make a lot of assumptions about the underlying system, even if some services could in theory work the same on other distros it’d be far too difficult to make sure all of them do, and either way the entire symlink-based infra would be hard to force into a ubuntu host.

home-manager is an example of how you could nonetheless write a deployment manager, but the existing service configurations won’t be of use to you.

I don’t get this either. Homebrew can give me a running postgres install rather easily but with Nix I’m hunting around for weird documentation and have to patch something together myself?

Because homebrew is specifically designed for MacOS, while Nix is not. You can try nix-darwin if you want NixOS-like modules for MacOS.

It looks like mysqld is built with defaults for datadir and socket that might need to be overridden. Just guessed at this from the help docs and error messages.

I just built mysql80, ran these commands:

mkdir -p /tmp/mysqld/datadir
./result/bin/mysqld --datadir /tmp/mysqld/datadir --initialize-insecure
./result/bin/mysqld --socket /tmp/mysqld/socket --datadir /tmp/mysqld/datadir
# mysqld is running
# new terminal window:
./result/bin/mysql --socket /tmp/mysqld/socket --user root
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

Thanks. That’s great. I just wish that nix would print that out or that would be on the package page.

Homebrew spits out a message how to run a particular service. I have done this once too often and don’t really want to have to bother figuring out again.

So if I pack that into apps I can do nix run and have it go.

OK. That does not go.

error: app program './result/bin/mysqld --datadir `pwd`/mysqld/datadir --initialize-insecure; ./result/bin/mysqld --socket `pwd`/mysqld/socket --datadir /tmp/mysqld/datadir;' is not in the Nix store

./result is the symlink resulting from my nix build ... invocation.

You’d want to use ${pkgs.mysql80}/bin/mysqld ... if you’re trying to use this via a nix run.

I’m at:

default = {
          type = "app";
          program = "${pkgs.mysql80}/bin/mysqld --socket /Users/alper/Code/contrib/nestjs-realworld-example-app/mysqld/socket --datadir /Users/alper/Code/contrib/nestjs-realworld-example-app/mysqld/datadir";
        };

        initialize_db = {
          type = "app";
          program = "${pkgs.mysql80}/bin/mysqld --datadir /Users/alper/Code/contrib/nestjs-realworld-example-app/mysqld/datadir --initialize-insecure";
        };

but it seems that program has to be just that a program. Is the fact that you can’t pass arguments to things you run with nix run an oversight?

That mysql command also can’t be stopped cleanly so then on next run it goes:

2023-09-21T15:40:30.219816Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 35

I can say it’s incredibly grating to spend lots of time getting things to work that work out of the box with homebrew.

I find the whole app concept in flake.nix to be a bit odd, partly for what you’re finding. I can’t speak to the particular design choices.

The only times I’ve used it, I’ve used pkgs.writeShellScript to do what you’re trying to do. For example, something like (riffing here, I have a slightly different setup and didn’t test this snippet):

  initialize_db = {
    type = "app";
    program = (pkgs.writeShellScript ''
      ${pkgs.mysql80}/bin/mysqld --datadir /Users/alper/Code/contrib/nestjs-realworld-example-app/mysqld/datadir --initialize-insecure
    '').outPath;
  };
1 Like

I think there should be a type = "script" for exactly this use case. Or a suite of types around development processes. But that would probably not make it into nix, given nix doesn’t really have a good way to integrate something like writeShellScript.

Honestly, Nix should aim to have at least parity with Homebrew otherwise I don’t really see what the point here is.

I’ve come across this when using nix (the package manager). My solution was to run a podman vm and expose a port like this:

pkgs.mkShell {
name=“php74dev-env”;
packages = [
pkgs.qemu
pkgs.podman
];

shellHook=''
    podman machine init
    podman machine start
    podman run -p 3306:3306  --env MARIADB_PASSWORD=pass --env MARIADB_USER=dev-user --env MARIADB_ROOT_PASSWORD=pass --env MARIADB_DATABASE=mydb mariadb:latest 

You might use any mysql/mariadb version you want by creating a docker image using the nix docker tools: Building and running Docker images — nix.dev documentation

Alternatively you might try to thinker with systemd by adding the service file directly to systemd services

Turns out that there is a facility in devenv to run mysql and other services relatively easily.

I just don’t think it’s reasonable to ask me to install two more dependencies (Cachix + devenv) to use this. Can’t .services be a part of the flake format?

The output schema already contains lots of stuff.

If a project uses devenv, it should automatically add devenv/whatever to its dev shell (the one that’s sourced with direnv ideally), then you don’t need to install anything on top.

Can’t .services be a part of the flake format?

Wouldn’t that mean that all the magic behind this would need to be backed into nix as well?

I mean, you can add services to the output already, flakes don’t prohibit arbitrary output attributes.

So there’s an RFC out now that aims to address the issue of long running services.

Not sure how to get that out fast, but it would be great to have this.

You might want to have a look at services-flake, only pre-requisite is a nix installation. Have a look at the example sub-dir to see how to use it.

Note: I am a maintainer for this repo

2 Likes