|
| 1 | +#################################################################### |
| 2 | +# Create a diretory for the generated artifacts |
| 3 | +#################################################################### |
| 4 | + |
| 5 | +mkdir .nix-shell |
| 6 | +export NIX_SHELL_DIR=$PWD/.nix-shell |
| 7 | + |
| 8 | +#################################################################### |
| 9 | +# Put the PostgreSQL databases in the project diretory. |
| 10 | +#################################################################### |
| 11 | + |
| 12 | +export PGDATA=$NIX_SHELL_DIR/db |
| 13 | + |
| 14 | +#################################################################### |
| 15 | +# Clean up after exiting the Nix shell using `trap`. |
| 16 | +# ------------------------------------------------------------------ |
| 17 | +# Idea taken from |
| 18 | +# https://unix.stackexchange.com/questions/464106/killing-background-processes-started-in-nix-shell |
| 19 | +# and the answer provides a way more sophisticated solution. |
| 20 | +# |
| 21 | +# The main syntax is `trap ARG SIGNAL` where ARG are the commands to |
| 22 | +# be executed when SIGNAL crops up. See `trap --help` for more. |
| 23 | +#################################################################### |
| 24 | + |
| 25 | +trap \ |
| 26 | + " |
| 27 | + ###################################################### |
| 28 | + # Stop PostgreSQL |
| 29 | + ###################################################### |
| 30 | +
|
| 31 | + pg_ctl -D $PGDATA stop |
| 32 | +
|
| 33 | + ###################################################### |
| 34 | + # Delete `.nix-shell` directory |
| 35 | + # ---------------------------------- |
| 36 | + # The first step is going back to the project root, |
| 37 | + # otherwise `.nix-shell` won't get deleted. At least |
| 38 | + # it didn't for me when exiting in a subdirectory. |
| 39 | + ###################################################### |
| 40 | +
|
| 41 | + cd $PWD |
| 42 | + rm -rf "$NIX_SHELL_DIR" |
| 43 | + " \ |
| 44 | + EXIT |
| 45 | + |
| 46 | +#################################################################### |
| 47 | +# If database is not initialized (i.e., $PGDATA directory does not |
| 48 | +# exist), then set it up. Seems superfulous given the cleanup step |
| 49 | +# above, but handy when one gets to force reboot the iron. |
| 50 | +#################################################################### |
| 51 | + |
| 52 | +if ! test -d $PGDATA |
| 53 | +then |
| 54 | + |
| 55 | + ###################################################### |
| 56 | + # Init PostgreSQL |
| 57 | + ###################################################### |
| 58 | + |
| 59 | + pg_ctl initdb -D $PGDATA |
| 60 | + |
| 61 | + ###################################################### |
| 62 | + # PORT ALREADY IN USE |
| 63 | + ###################################################### |
| 64 | + # If another `nix-shell` is running with a PostgreSQL |
| 65 | + # instance, the logs will show complaints that the |
| 66 | + # default port 5432 is already in use. Edit the line |
| 67 | + # below with a different port number, uncomment it, |
| 68 | + # and try again. |
| 69 | + ###################################################### |
| 70 | + |
| 71 | + # sed -i "s|^#port.*$|port = 5433|" $PGDATA/postgresql.conf |
| 72 | + |
| 73 | +fi |
| 74 | + |
| 75 | +#################################################################### |
| 76 | +# Start PostgreSQL |
| 77 | +# ================================================================== |
| 78 | +# Setting all necessary configuration options via `pg_ctl` (which |
| 79 | +# is basically a wrapper around `postgres`) instead of editing |
| 80 | +# `postgresql.conf` directly with `sed`. See docs: |
| 81 | +# |
| 82 | +# + https://www.postgresql.org/docs/current/app-pg-ctl.html |
| 83 | +# + https://www.postgresql.org/docs/current/app-postgres.html |
| 84 | +# |
| 85 | +# See more on the caveats at |
| 86 | +# https://discourse.nixos.org/t/how-to-configure-postgresql-declaratively-nixos-and-non-nixos/4063/1 |
| 87 | +# but recapping out of paranoia: |
| 88 | +# |
| 89 | +# > use `SHOW` commands to check the options because `postgres -C` |
| 90 | +# > "_returns values from postgresql.conf_" (which is not changed by |
| 91 | +# > supplying the configuration options on the command line) and |
| 92 | +# > "_it does not reflect parameters supplied when the cluster was |
| 93 | +# > started._" |
| 94 | +# |
| 95 | +# OPTION SUMMARY |
| 96 | +# -------------------------------------------------------------------- |
| 97 | +# |
| 98 | +# + `unix_socket_directories` |
| 99 | +# |
| 100 | +# > PostgreSQL will attempt to create a pidfile in |
| 101 | +# > `/run/postgresql` by default, but it will fail as it |
| 102 | +# > doesn't exist. By changing the configuration option |
| 103 | +# > below, it will get created in $PGDATA. |
| 104 | +# |
| 105 | +# + `listen_addresses` |
| 106 | +# |
| 107 | +# > In tandem with edits in `pg_hba.conf` (see |
| 108 | +# > `HOST_COMMON` below), it configures PostgreSQL to |
| 109 | +# > allow remote connections (otherwise only `localhost` |
| 110 | +# > will get authenticated and the rest of the traffic |
| 111 | +# > discarded). |
| 112 | +# > |
| 113 | +# > NOTE: the edit to `pga_hba.conf` needs to come |
| 114 | +# > **before** `pg_ctl start` (or the service |
| 115 | +# > needs to be restarted otherwise), because then |
| 116 | +# > the changes are not being reloaded. |
| 117 | +# > |
| 118 | +# > More info on setting up and troubleshooting remote |
| 119 | +# > PosgreSQL connections (these are all mirrors of the |
| 120 | +# > same text; again, paranoia): |
| 121 | +# > |
| 122 | +# > + https://stackoverflow.com/questions/24504680/connect-to-postgres-server-on-google-compute-engine |
| 123 | +# > + https://stackoverflow.com/questions/47794979/connecting-to-postgres-server-on-google-compute-engine |
| 124 | +# > + https://medium.com/scientific-breakthrough-of-the-afternoon/configure-postgresql-to-allow-remote-connections-af5a1a392a38 |
| 125 | +# > + https://gist.github.com/toraritte/f8c7fe001365c50294adfe8509080201#file-configure-postgres-to-allow-remote-connection-md |
| 126 | + |
| 127 | +HOST_COMMON="host\s\+all\s\+all" |
| 128 | +sed -i "s|^$HOST_COMMON.*127.*$|host all all 0.0.0.0/0 trust|" $PGDATA/pg_hba.conf |
| 129 | +sed -i "s|^$HOST_COMMON.*::1.*$|host all all ::/0 trust|" $PGDATA/pg_hba.conf |
| 130 | + |
| 131 | + # + `log*` |
| 132 | + # |
| 133 | + # > Setting up basic logging, to see remote connections |
| 134 | + # > for example. |
| 135 | + # > |
| 136 | + # > See the docs for more: |
| 137 | + # > https://www.postgresql.org/docs/current/runtime-config-logging.html |
| 138 | + |
| 139 | +pg_ctl \ |
| 140 | + -D $PGDATA \ |
| 141 | + -l $PGDATA/postgres.log \ |
| 142 | + -o "-c unix_socket_directories='$PGDATA'" \ |
| 143 | + -o "-c listen_addresses='*'" \ |
| 144 | + -o "-c log_destination='stderr'" \ |
| 145 | + -o "-c logging_collector=on" \ |
| 146 | + -o "-c log_directory='log'" \ |
| 147 | + -o "-c log_filename='postgresql-%Y-%m-%d_%H%M%S.log'" \ |
| 148 | + -o "-c log_min_messages=info" \ |
| 149 | + -o "-c log_min_error_statement=info" \ |
| 150 | + -o "-c log_connections=on" \ |
| 151 | + start |
| 152 | + |
| 153 | +#################################################################### |
| 154 | +# Create necessary databases & users. |
| 155 | +# ------------------------------------------------------------------ |
| 156 | +# Basically, for this config, I set all credentials to "temp", |
| 157 | +# you're welcomed to change it to what the fuckever as you wish. |
| 158 | +# |
| 159 | +# Also, before bootstrapping credentials, it creates database |
| 160 | +# for current running user as it doesn't get done by default. |
| 161 | +#################################################################### |
| 162 | + |
| 163 | +export DB_USERNAME=temp |
| 164 | +export DB_PASSWORD=temp |
| 165 | +export DB_DATABASE=temp |
| 166 | + |
| 167 | +createdb $(whoami) --host=$PGDATA |
| 168 | + |
| 169 | +createdb $DB_DATABASE --host=$PGDATA |
| 170 | +createuser $DB_USERNAME --host=$PGDATA |
| 171 | +psql --host=$PGDATA --username=$(whoami) --dbname=$(whoami) --port=5432 \ |
| 172 | + -c "alter user $DB_USERNAME with encrypted password '$DB_PASSWORD';" |
| 173 | +psql --host=$PGDATA --username=$(whoami) --dbname=$(whoami) --port=5432 \ |
| 174 | + -c "grant all privileges on database $DB_DATABASE to $DB_USERNAME;" |
| 175 | + |
| 176 | +#################################################################### |
| 177 | +# Prepare .env file for the server |
| 178 | +#################################################################### |
| 179 | + |
| 180 | +if [ ! -f .env ]; then |
| 181 | + |
| 182 | + ###################################################### |
| 183 | + # Generate and warn user about defaulted .env file |
| 184 | + ###################################################### |
| 185 | + |
| 186 | + touch .env |
| 187 | + echo "I'm setting temporary local dev settings for now." |
| 188 | + echo "If any changes wanted, feel free to change or do what the fuckever you want with .env file!"; |
| 189 | + |
| 190 | + ###################################################### |
| 191 | + # Write defaulted settings to .env file |
| 192 | + ###################################################### |
| 193 | + |
| 194 | + echo "# Generated with shell.nix (L251 for more)" > .env; |
| 195 | + echo "URL=127.0.0.1" > .env; |
| 196 | + echo "PORT=8001" > .env; |
| 197 | + echo "DATABASE_URL=postgres://$(whoami):$(whoami)@localhost/temp" > .env; |
| 198 | + |
| 199 | +else |
| 200 | + source .env; |
| 201 | +fi |
0 commit comments