Skip to content

Instantly share code, notes, and snippets.

@bobvanderlinden
Last active March 10, 2023 03:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bobvanderlinden/e0bddba160e28adb7a78fd03d5914cc6 to your computer and use it in GitHub Desktop.
Save bobvanderlinden/e0bddba160e28adb7a78fd03d5914cc6 to your computer and use it in GitHub Desktop.
Running Rails through nix-shell

Use the following to generate gemset.nix:

nix run nixpkgs.bundix --command bundix

Then run:

nix-shell

The shell will start a mysqld instance that listens on a local Unix socket. It will be killed when the shell is killed.

Next you can run:

rspec

or

rails server
let
pkgs = import <nixpkgs> {};
ruby = pkgs.ruby_2_4;
stdenv = pkgs.stdenv;
gems = pkgs.bundlerEnv {
name = "medewerkerportaal-env";
ruby = ruby;
gemset = ./gemset.nix;
gemfile = ./Gemfile;
lockfile = ./Gemfile.lock;
groups = ["default" "development" "test"];
};
in stdenv.mkDerivation {
name = "medewerkerportaal";
buildInputs = [ gems ruby ] ++ (with pkgs; [ bundix mariadb ]);
shellHook = ''
MYSQL_BASEDIR=${pkgs.mariadb}
MYSQL_HOME=$PWD/mysql
MYSQL_DATADIR=$MYSQL_HOME/data
export MYSQL_UNIX_PORT=$MYSQL_HOME/mysql.sock
MYSQL_PID_FILE=$MYSQL_HOME/mysql.pid
mysql_install_db --datadir=$MYSQL_DATADIR --basedir=$MYSQL_BASEDIR --pid-file=$MYSQL_PID_FILE
mysqld --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE --socket=$MYSQL_UNIX_PORT &
MYSQL_PID=$!
finish()
{
mysqladmin --socket=$MYSQL_UNIX_PORT shutdown
kill $MYSQL_PID
wait $MYSQL_PID
}
trap finish EXIT
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment