Skip to content

Instantly share code, notes, and snippets.

@glendmaatita
Last active February 22, 2024 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save glendmaatita/9380d3f403e20c9adc08204235c943c7 to your computer and use it in GitHub Desktop.
Save glendmaatita/9380d3f403e20c9adc08204235c943c7 to your computer and use it in GitHub Desktop.
OnXP Python Web Development
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = { allowUnfree = true; }; overlays = []; };
git = pkgs.git.overrideAttrs (oldAttrs: rec {
version = "2.42.0";
});
podman = pkgs.podman.overrideAttrs (oldAttrs: rec {
version = "4.7.2";
});
postgresql = pkgs.postgresql_15.overrideAttrs (oldAttrs: rec {
version = "15.4";
});
in
pkgs.mkShell {
packages = with pkgs; [
git
podman
nodejs_20
python311
python311Packages.pip
pkgs.pdm
redis
pkgs.postgresql
];
shellHook = ''
alias docker=podman
export NIX_SHELL_DIR=$PWD/.nix-shell
export LC_ALL=C
export LANG=C.utf8
export PGDATA=$NIX_SHELL_DIR/db
export PGHOST=localhost
export PGUSER=postgres
export PGPASSWORD=password
# Setup PostgreSQL
mkdir $NIX_SHELL_DIR
trap \
"
pg_ctl -D $PGDATA stop
pkill redis-server
" \
EXIT
if ! test -d $PGDATA
then
initdb -D $PGDATA --no-locale --encoding=UTF8
fi
HOST_COMMON="host\s\+all\s\+all"
sed -i "s|^$HOST_COMMON.*127.*$|host all all 0.0.0.0/0 trust|" $PGDATA/pg_hba.conf
sed -i "s|^$HOST_COMMON.*::1.*$|host all all ::/0 trust|" $PGDATA/pg_hba.conf
pg_ctl \
-D $PGDATA \
-l $PGDATA/postgres.log \
-o "-c unix_socket_directories='$PGDATA'" \
-o "-c listen_addresses='*'" \
-o "-c log_destination='stderr'" \
-o "-c logging_collector=on" \
-o "-c log_directory='log'" \
-o "-c log_filename='postgresql-%Y-%m-%d_%H%M%S.log'" \
-o "-c log_min_messages=info" \
-o "-c log_min_error_statement=info" \
-o "-c log_connections=on" \
start
echo "Setup database.. To access DB: psql -U $PGUSER -d postgres"
if ! psql -U $(whoami) -tAc "SELECT 1 FROM pg_database WHERE datname='development'" | grep -q 1; then
createuser -U $(whoami)
psql -U $(whoami) -d postgres -c "CREATE DATABASE $(whoami) OWNER $(whoami);" || true
psql -U $(whoami) -d postgres -c "ALTER ROLE $PGUSER SUPERUSER;"
psql -U "$PGUSER" -d postgres -c "CREATE DATABASE development" || true
fi
echo "Run redis.. See log on $NIX_SHELL_DIR/redis.log"
nohup redis-server > $NIX_SHELL_DIR/redis.log 2>&1 &
'';
}
@andirkh
Copy link

andirkh commented Feb 17, 2024

I've got an error when running "CREATE DATABASE $(whoami) OWNER $(whoami);" because my computer's username is using hypens -

@glendmaatita , Would you mind to update the code to be "CREATE DATABASE \"$(whoami)\" OWNER \"$(whoami)\" ?
Because psql doesn't allow hyphens in identifiers unless they're quoted.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment