Rails development environment powered by Nix + Direnv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use flake | |
# Use binstubs directly | |
PATH_add bin | |
# Web server port | |
export PORT=3000 | |
# MySQL config directory | |
export MYSQL_HOME=./config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Location: bin/dev | |
#!/usr/bin/env sh | |
exec foreman start -f Procfile.dev "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
description = "dev environment"; | |
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; | |
inputs.flake-utils.url = "github:numtide/flake-utils"; | |
outputs = { self, nixpkgs, flake-utils }: | |
flake-utils.lib.eachDefaultSystem (system: | |
let pkgs = nixpkgs.legacyPackages.${system}; | |
in with pkgs; { | |
devShells.default = mkShell { | |
packages = [ | |
# Runtimes | |
ruby_2_7 | |
nodejs-16_x | |
# Databases | |
mysql80 | |
redis | |
# Tools | |
chromedriver | |
imagemagick | |
yarn | |
# Dependencies | |
cmake | |
icu | |
libffi | |
libiconv | |
openssl | |
pkg-config | |
zlib | |
zstd | |
]; | |
shellHook = '' | |
MYSQL_DATA_DIR=$PWD/mysql_data | |
MYSQL_SOCKET_FILE=mysql.sock | |
# Initialize MySQL data directory | |
if [ ! -d "$MYSQL_DATA_DIR" ]; then | |
mkdir -p $MYSQL_DATA_DIR | |
mysqld --initialize-insecure \ | |
--datadir=$MYSQL_DATA_DIR \ | |
--socket=$MYSQL_SOCKET_FILE | |
fi | |
# Create database.yml if not present | |
DATABASE_FILE=$PWD/config/database.yml | |
if [ ! -f "$DATABASE_FILE" ]; then | |
cp -v config/database.yml.example $DATABASE_FILE | |
fi | |
# Create my.cnf if not present | |
MYSQL_CONFIG_FILE=$PWD/config/my.cnf | |
if [ ! -f "$MYSQL_CONFIG_FILE" ]; then | |
cp -v config/my.cnf.example $MYSQL_CONFIG_FILE | |
fi | |
# Create Procfile.dev if not present | |
PROCFILE=$PWD/Procfile.dev | |
if [ ! -f "$PROCFILE" ]; then | |
cp -v Procfile.example $PROCFILE | |
fi | |
''; | |
}; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### This file is located in ./config/my.cnf | |
[client] | |
port=3306 | |
socket=./mysql_data/mysql.sock | |
[mysqld] | |
port=3306 | |
bind-address=127.0.0.1 | |
datadir=./mysql_data | |
socket=mysql.sock | |
character-set-server=utf8mb4 | |
collation-server=utf8mb4_unicode_ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: bundle exec puma -C ./config/puma.rb | |
mysql: mysqld | |
redis: redis-server | |
js: ./bin/webpacker -w | |
dj: bundle exec rake jobs:work | |
sidekiq: bundle exec sidekiq -C config/sidekiq.yml |
mysqld is already in the Procfile?
@alper Yes, it is contained in the mysql80
package. All packages are installed once direnv is enabled via direnv allow
.
I mean:
In one shell, start MySQL server
I don't think you have to start it separately if it's in the procfile?
@alper You're right, one can start MySQL with Procfile and then create database. Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirement
Steps
All necessary packages for development are installed and the MySQL data directory is created in
./mysql_data
.That's all. Open http://localhost:3000 to visit.
You may need to setup database in another shell:
bundle exec rails db:setup
Enjoy the power of functional programming and plain old shell script!