Last active
February 19, 2021 14:35
-
-
Save oeb25/a7a804fd0a33df2c09a2189a487e77d1 to your computer and use it in GitHub Desktop.
Elixir/Phoenix Nix and script configuration
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
with import <nixpkgs> {}; | |
stdenv.mkDerivation rec { | |
name = "env"; | |
env = buildEnv { name = name; paths = buildInputs; }; | |
buildInputs = [ | |
yarn | |
nodejs-6_x | |
elixir | |
postgresql96 | |
inotify-tools | |
]; | |
} |
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
#!/bin/bash | |
set -e | |
PGDATA="`dirname $0`/db" | |
tools='yarn mix postgres pg_ctl' | |
for tool in $tools; do | |
if [[ `which $tool` = "" ]]; then | |
missing_tools="$missing_tools, $tool" | |
fi | |
done | |
if [[ "$1" != "nix" ]]; then | |
if [[ "$missing_tools" != "" ]]; then | |
echo "[-] Missing tools: ${missing_tools:2}. Trying running in nix:" | |
echo "" | |
echo " $ $0 nix $@" | |
echo "" | |
exit | |
fi | |
fi | |
if [[ $1 == "setup" ]]; then | |
echo "Starting db... Please Wait..." | |
initdb -U postgres $PGDATA > /dev/null | |
pg_ctl -D $PGDATA -l logfile start > /dev/null | |
cd assets && yarn && ./node_modules/.bin/brunch build && cd .. | |
mix do phx.digest, ecto.create, ecto.migrate | |
elif [[ $1 == "nix" ]]; then | |
if [[ $2 == "nix" ]]; then | |
echo "[-] You gave me '$0 $@', looks like you want to run nix inside nix, why?" | |
exit | |
fi | |
nix-shell --command "$0 $2" | |
elif [[ $1 == "dev" ]]; then | |
echo "[-] Starting database..." | |
pg_ctl -D $PGDATA -l logfile start > /dev/null | |
echo "[-] Starting phoenix server..." | |
mix phx.server | |
elif [[ $1 == "prod" ]]; then | |
echo "[-] Starting database..." | |
pg_ctl -D $PGDATA -l logfile start > /dev/null | |
echo "[-] Getting dependencies and compiling..." | |
mix local.hex --force | |
mix deps.get --only prod | |
cd assets && ./node_modules/.bin/brunch build --production && cd .. | |
MIX_ENV=prod mix do phx.digest, ecto.create, ecto.migrate | |
echo "[-] Starting phoenix server in production mode..." | |
MIX_ENV=prod PORT=4000 mix phx.server | |
elif [[ $1 == "stop" ]]; then | |
echo "[-] Stopping db... Please Wait..." | |
PGDATA=$PGDATA pg_ctl stop | |
elif [[ $1 == "clear" ]]; then | |
$0 stop | |
echo "[-] Clearing db... Please Wait..." | |
rm -rf $PGDATA && \ | |
echo "[-] Database has been removed" | |
else | |
echo "[-] Invalid argument, or none found. Options are:" | |
echo "$0 setup - create (if needed) and start the database, then run migrations" | |
echo "$0 nix [cmd] - run the passed command inside the nix enviorment" | |
echo "$0 dev - start database and phoenix server" | |
echo "$0 prod - start database and phoenix server in production mode" | |
echo "$0 stop - to stop the database" | |
echo "$0 clear - to remove the database entirely" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment