Skip to content

Instantly share code, notes, and snippets.

@maikelthedev
Created January 24, 2025 19:21
Show Gist options
  • Save maikelthedev/b14530eabf4bc86a30b1f3c47019a838 to your computer and use it in GitHub Desktop.
Save maikelthedev/b14530eabf4bc86a30b1f3c47019a838 to your computer and use it in GitHub Desktop.
Now with cleaner variable interpolation
# On Nixos running this from the project folder with nix-shell will create a shell with all the necessary dependencies
# and run postgresql in an ephemeral mode for development.
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
elixir_1_18
elixir-ls
erlang
rebar3
nodejs
yarn
fish
gnumake
postgresql
];
# Environment variables
MIX_ENV = "dev";
NIX_ENFORCE_PURITY = "0"; # Without this bcrypt does not compile
IEX_FILE = "${builtins.getEnv "PWD"}/.iex.exs";
PROJECT_DIR = "/tmp/indie";
PGPORT = "5432"; # Use quotes to ensure consistency in shell environments
# Hook to automatically switch to Fish when entering the shell
shellHook = ''
# Environment variables that need interpolation
export PGDATA=$PROJECT_DIR/data
export PGLOCKDIR=$PROJECT_DIR/lock
export PGLOG=$PROJECT_DIR/postgress.log
alias quit="killall -s KILL postgres && exit"
red() { echo -e "\e[31m🚀 $1\e[0m"; }
green() { echo -e "\e[32m🚀 $1\e[0m"; }
iexfile() {
# Create the .iex.exs file if it doesn't exist
if [ ! -f "$IEX_FILE" ]; then
red "Creating .iex.exs with common aliases..."
cat <<EOF > "$IEX_FILE"
IEx.configure(auto_reload: true)
alias Indie.Repo
alias Indie.Accounts.{User, Address}
alias Indie.Commerce.Order
alias Indie.Domains.Domain
EOF
else
green ".iex.exs already exists!"
fi
}
get_dependencies () {
# Run mix deps.get and mix deps.compile if necessary
green "Getting Dependencies"
[ ! -d "deps" ] && red "Running mix deps.get" && mix deps.get
[ ! -d "_build" ] && red "Running mix deps.compile" && mix deps.compile
}
cleanup () {
# Ensure the custom directories exist
green "Cleaning and setting up ephemeral directories"
rm -r $PGDATA
mkdir -p $PGDATA
mkdir -p $PGLOCKDIR
chmod 700 $PGDATA
chmod 700 $PGLOCKDIR
}
run_postgres () {
green "Running Postgress"
pg_ctl initdb -o "-U postgres"
pg_ctl -D $PGDATA -l $PGLOG -o "-p $PGPORT -h localhost -k $PGLOCKDIR" start
}
populate_db () {
green "Populating database"
mix ecto.reset
}
dev () {
iex -S mix phx.server
}
iexfile
get_dependencies
cleanup
run_postgres
populate_db
#trap 'killall -s KILL postgres && echo Postgress is now DEAD 🥳!' EXIT
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment