Created
January 26, 2025 01:00
-
-
Save maikelthedev/fc09159f4a66974c5eb07b75bbcc9d0b to your computer and use it in GitHub Desktop.
Oh my God I'm using Flakes now 🤯
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 = "Basic flake for the dev environment of indie"; | |
inputs = { | |
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |
}; | |
outputs = { self, nixpkgs }: | |
let | |
system = "x86_64-linux"; | |
pkgs = nixpkgs.legacyPackages.${system}; | |
in { | |
devShells.${system}.default = pkgs.mkShell { | |
# 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 | |
buildInputs = with pkgs; [ | |
elixir_1_18 | |
elixir-ls | |
erlang | |
rebar3 | |
nodejs | |
yarn | |
fish | |
gnumake | |
postgresql | |
]; | |
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" | |
# Color-coded functions for output | |
red() { echo -e "\e[31m🚨 $1\e[0m"; } | |
green() { echo -e "\e[32m✅ $1\e[0m"; } | |
yellow() { echo -e "\e[33m⚙️ $1\e[0m"; } | |
blue() { echo -e "\e[34m🔷 $1\e[0m"; } | |
# Step headers | |
step_header() { | |
echo -e "\e[35m\n======> $1\e[0m\n"; | |
} | |
# Create the .iex.exs file if it doesn't exist | |
iexfile() { | |
step_header "Setting up .iex.exs file" | |
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 | |
green ".iex.exs created successfully!" | |
else | |
green ".iex.exs already exists!" | |
fi | |
} | |
# Fetch dependencies | |
get_dependencies() { | |
step_header "Fetching Dependencies" | |
green "Checking dependencies..." | |
[ ! -d "deps" ] && yellow "Running mix deps.get" && mix deps.get || green "Dependencies are already fetched." | |
[ ! -d "_build" ] && yellow "Running mix deps.compile" && mix deps.compile || green "Dependencies are already compiled." | |
} | |
# Cleanup and setup directories | |
cleanup() { | |
step_header "Cleaning up and setting directories" | |
yellow "Resetting PostgreSQL data directories..." | |
rm -rf $PGDATA | |
mkdir -p $PGDATA | |
mkdir -p $PGLOCKDIR | |
chmod 700 $PGDATA | |
chmod 700 $PGLOCKDIR | |
green "Directories are ready!" | |
} | |
# Run PostgreSQL server | |
run_postgres() { | |
step_header "Starting PostgreSQL server" | |
yellow "Initializing PostgreSQL..." | |
pg_ctl initdb -o "-U postgres" | |
green "PostgreSQL initialized." | |
yellow "Starting PostgreSQL server..." | |
pg_ctl -D $PGDATA -l $PGLOG -o "-p $PGPORT -h localhost -k $PGLOCKDIR" start | |
green "PostgreSQL is running on port $PGPORT!" | |
} | |
# Populate the database | |
populate_db() { | |
step_header "Populating the database" | |
yellow "Resetting the database..." | |
mix ecto.reset | |
green "Database populated successfully!" | |
} | |
# Start the Phoenix development server | |
dev() { | |
step_header "Starting the Phoenix development server" | |
iex -S mix phx.server | |
} | |
# Main execution flow | |
iexfile | |
get_dependencies | |
cleanup | |
run_postgres | |
populate_db | |
# Clean up PostgreSQL process on shell exit | |
trap 'killall -s KILL postgres && red "PostgreSQL has been stopped 🥳!"' EXIT | |
''; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment