diff --git a/nix/dev-shell.nix b/nix/dev-shell.nix --- a/nix/dev-shell.nix +++ b/nix/dev-shell.nix @@ -53,6 +53,10 @@ # shell commands to be ran upon entering shell shellHook = '' + if [[ "$OSTYPE" == 'linux'* ]]; then + export MYSQL_UNIX_PORT=''${XDG_RUNTIME_DIR:-/run/user/$UID}/mysql-socket/mysql.sock + fi + echo "Welcome to Comm dev environment! :)" ''; } diff --git a/nix/mysql-down-linux.nix b/nix/mysql-down-linux.nix new file mode 100644 --- /dev/null +++ b/nix/mysql-down-linux.nix @@ -0,0 +1,15 @@ +{ lib +, writeShellScriptBin +}: + +# writeShellScriptBin is a "writer helper" which +# will create an executable shell script located in $out/bin/ +# This shell script will be used to allow for impure+stateful actions +writeShellScriptBin "mysql-down" '' + set -euo pipefail + + echo "Attempting to stop user MySQL server instance" >&2 + systemctl stop --user comm-mysql + + echo "Succesfully killed" >&2 +'' diff --git a/nix/mysql-up-linux.nix b/nix/mysql-up-linux.nix new file mode 100644 --- /dev/null +++ b/nix/mysql-up-linux.nix @@ -0,0 +1,67 @@ +{ lib +, writeShellScriptBin +, writeTextFile +, mysql57 +}: + +let + # This will create a comm-mysql.service meant to be consumed by systemd as a user unit file + # Settings: + # port = 3306 + # datadir = ~/.cache/mysql + # socket = /run/user/1000/mysql-socket/mysql.sock + mysql-user-service-unit = writeTextFile { + name = "comm-mysql"; + text = '' + [Unit] + Description=MySQL Server + + [Service] + ExecStart=${lib.getBin mysql57}/bin/mysqld --port 3306 --datadir=%h/.cache/mysql --socket=%t/mysql-socket/mysql.sock + Restart=on-failure + ProtectHome=read-only + ProtectSystem=strict + PrivateTmp=true + + [Install] + WantedBy=default.target + ''; + destination = "/comm-mysql.service"; + + }; +in + +# writeShellScriptBin is a "writer helper" which +# will create an executable shell script located in $out/bin/ +# This shell script will be used to allow for impure+stateful actions +writeShellScriptBin "mysql-up" '' + set -euo pipefail + + mkdir -p $HOME/.cache/mysql ''${XDG_RUNTIME_DIR:-/run/user/$UID}/mysql-socket + + if [ ! -d ''${XDG_CACHE_HOME:-$HOME/.cache}/mysql/mysql ]; then + # ~/.cache/mysql/mysql should exist if mysql has been initialized + echo "Initializing MySQL database at ''${XDG_CACHE_HOME:-$HOME/.cache}/mysql" >&2 + ${lib.getBin mysql57}/bin/mysqld --initialize-insecure --datadir=''${XDG_CACHE_HOME:-$HOME/.cache}/mysql + fi + + needsReload: + + # check if service was installed + if [ ! -r ''${XDG_CONFIG_DIR:-$HOME/.config}/systemd/user/comm-mysql.service ]; then + needsReload=1 + fi + + # Install the service unit file into the default systemd location + ln -sf ${mysql-user-service-unit}/comm-mysql.service ''${XDG_CONFIG_DIR:-$HOME/.config}/systemd/user/comm-mysql.service + + # Conditionally enable unit file + [ -n needsReload ] && systemctl --user daemon-reload + + # Start mysql + echo "Starting MySQL server as user service: comm-mysql" + systemctl start --user comm-mysql.service + + echo "View MySQL Logs: journalctl -f --user-unit comm-mysql" >&2 + echo "Kill MySQL server: nix run .#mysql-down" >&2 +'' diff --git a/nix/overlay.nix b/nix/overlay.nix --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -16,4 +16,8 @@ protobuf_3_15_cmake = prev.callPackage ./protobuf_3_15.nix { }; devShell = final.callPackage ./dev-shell.nix { }; + + mysql-down = prev.callPackage ./mysql-down-linux.nix { }; + + mysql-up = prev.callPackage ./mysql-up-linux.nix { }; }