-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshell.nix
294 lines (252 loc) · 14.2 KB
/
shell.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
####################################################################
# Importing a cloned Nixpkgs repo (from my home directory), because
# the latest channels don't have Elixir 1.9.
# See https://nixos.org/nix/manual/#idm140737317975776 for the meaning
# of `<nixpkgs>` and `~` in Nix expressions (towards the end of that
# section).
####################################################################
# The Nixpkgs commit used for pinning below is quite old,
#
# Oct 1, 2021, 8:37 PM EDT
# https://github.com/NixOS/nixpkgs/tree/751ce748bd1ebac94442dfeaa8bc3f100d73a9f6
#
# but they can be overridden using `nix-shell`'s `--argstr`
# (never figured out how to use `--arg`):
#
# nix-shell \
# -v \
# -E 'import (builtins.fetchurl "https://raw.githubusercontent.com/toraritte/shell.nixes/main/elixir-phoenix-postgres/shell.nix")' \
# --argstr "nixpkgs_commit" "3ad7b8a7e8c2da367d661df6c3742168c53913fa"
#
# (And all that on one line:
# nix-shell -v -E 'import (builtins.fetchurl "https://raw.githubusercontent.com/toraritte/shell.nixes/main/_composables/postgres_shell.nix")' --argstr "nixpkgs_commit" "3ad7b8a7e8c2da367d661df6c3742168c53913fa"
# )
#
# The rules to compose "raw" GitHub links from the regular view page seems straightforward:
#
# https://github.com/ toraritte/shell.nixes/blob/main/elixir-phoenix-postgres/shell.nix
# https://raw.githubusercontent.com/toraritte/shell.nixes/ main/elixir-phoenix-postgres/shell.nix
{ nixpkgs_commit ? "751ce748bd1ebac94442dfeaa8bc3f100d73a9f6" }:
let
pkgs =
import
# The downloaded archive will be (temporarily?) housed in the Nix store
# e.g., "/nix/store/gk9x7syd0ic6hjrf0fs6y4bsd16zgscg-source"
# (Try any of the `fetchTarball` commands below in `nix repl`, and it
# will print out the path.)
( builtins.fetchTarball "https://github.com/nixos/nixpkgs/tarball/${nixpkgs_commit}" )
{ config = {}; overlays = []; }
;
in
pkgs.mkShell {
buildInputs = with pkgs; [
# beam.packages.erlangR22.elixir_1_9
elixir
# postgresql_11
postgresql
nodejs-12_x
git
# TODO this will won't install on mac
inotify-tools
];
shellHook = ''
######################################################################
# Create a diretory for the generated artifacts #
######################################################################
mkdir .nix-shell
export NIX_SHELL_DIR=$PWD/.nix-shell
######################################################################
# Put the PostgreSQL databases in the project diretory. #
######################################################################
export PGDATA=$NIX_SHELL_DIR/db
####################################################################
# Put any Mix-related data in the project directory
####################################################################
export MIX_HOME="$NIX_SHELL_DIR/.mix"
export MIX_ARCHIVES="$MIX_HOME/archives"
######################################################################
# Clean up after exiting the Nix shell using `trap`. #
# ------------------------------------------------------------------ #
# Idea taken from #
# https://unix.stackexchange.com/questions/464106/killing-background-processes-started-in-nix-shell
# and the answer provides a way more sophisticated solution. #
# #
# The main syntax is `trap ARG SIGNAL` where ARG are the commands to #
# be executed when SIGNAL crops up. See `trap --help` for more. #
######################################################################
trap \
"
########################################################
# Stop PostgreSQL #
########################################################
pg_ctl -D $PGDATA stop
########################################################
# Delete `.nix-shell` directory #
# ---------------------------------- #
# The first step is going back to the project root, #
# otherwise `.nix-shell` won't get deleted. At least #
# it didn't for me when exiting in a subdirectory. #
########################################################
cd $PWD
rm -rf $NIX_SHELL_DIR
" \
EXIT
######################################################################
# If database is not initialized (i.e., $PGDATA directory does not #
# exist), then set it up. Seems superfulous given the cleanup step #
# above, but handy when one had to force reboot the iron. #
######################################################################
if ! test -d $PGDATA
then
########################################################
# Init PostgreSQL #
# #
# NOTE `initdb` vs `createdb` #
# https://stackoverflow.com/questions/50210158/whats-the-difference-between-initdb-usr-local-var-db-and-createdb-db
# https://www.postgresql.org/docs/current/app-initdb.html
########################################################
pg_ctl initdb -D $PGDATA
########################################################
# PORT ALREADY IN USE #
########################################################
# If another `nix-shell` is running with a PostgreSQL #
# instance, the logs will show complaints that the #
# default port 5432 is already in use. Edit the line #
# below with a different port number, uncomment it, #
# and try again. #
########################################################
# sed -i "s|^#port.*$|port = 5433|" $PGDATA/postgresql.conf
fi
########################################################################
# Configure and start PostgreSQL #
# ==================================================================== #
# Setting all necessary configuration options via `pg_ctl` (which #
# is basically a wrapper around `postgres`) instead of editing #
# `postgresql.conf` directly with `sed`. See docs: #
# #
# + https://www.postgresql.org/docs/current/app-pg-ctl.html #
# + https://www.postgresql.org/docs/current/app-postgres.html #
# #
# See more on the caveats at #
# https://discourse.nixos.org/t/how-to-configure-postgresql-declaratively-nixos-and-non-nixos/4063/1
# but recapping out of paranoia: #
# #
# > use `SHOW` commands to check the options because `postgres -C` #
# > "_returns values from postgresql.conf_" (which is not changed by #
# > supplying the configuration options on the command line) and #
# > "_it does not reflect parameters supplied when the cluster was #
# > started._" #
# #
# OPTION SUMMARY #
# -------------------------------------------------------------------- #
# #
# + `unix_socket_directories` #
# #
# PostgreSQL will attempt to create a pidfile in #
# `/run/postgresql` by default, but it will fail as it #
# doesn't exist. By changing the configuration option #
# below, it will get created in $PGDATA. #
# #
# + `listen_addresses` #
# #
# In tandem with edits in `pg_hba.conf` (see #
# `HOST_COMMON` below), it configures PostgreSQL to #
# allow remote connections (otherwise only `localhost` #
# will get authorized and the rest of the traffic #
# will be discarded). #
# #
# NOTE: the edit to `pga_hba.conf` needs to come #
# **before** `pg_ctl start` (or the service #
# needs to be restarted otherwise), because then #
# the changes are not being reloaded. #
# #
# More info on setting up and troubleshooting remote #
# PosgreSQL connections (these are all mirrors of the #
# same text; again, paranoia): #
# #
# + https://stackoverflow.com/questions/24504680/connect-to-postgres-server-on-google-compute-engine
# + https://stackoverflow.com/questions/47794979/connecting-to-postgres-server-on-google-compute-engine
# + https://medium.com/scientific-breakthrough-of-the-afternoon/configure-postgresql-to-allow-remote-connections-af5a1a392a38
# + https://gist.github.com/toraritte/f8c7fe001365c50294adfe8509080201#file-configure-postgres-to-allow-remote-connection-md
#
# + `log*` #
# #
# Setting up basic logging, to see remote connections #
# for example. #
# #
# See the docs for more: #
# https://www.postgresql.org/docs/current/runtime-config-logging.html
########################################################################
# !!!!!!!!!!!! These are only suitable for development.
# ! INSECURE ! (Not sure if running a production server
# !!!!!!!!!!!! from `nix-shell` is a good idea anyway:)
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
####################################################################
# Install Node.js dependencies if not done yet.
####################################################################
if test -d "$PWD/assets/" && ! test -d "$PWD/assets/node_modules/"
then
(cd assets && npm install)
fi
####################################################################
# If $MIX_HOME doesn't exist, set it up.
####################################################################
if ! test -d $MIX_HOME
then
######################################################
# ... but first, test whether there is a `_backup`
# directory. Had issues with installing Hex on NixOS,
# and Hex and Phoenix can be copied from there, just
# in case.
######################################################
if test -d "$PWD/_backup"
then
cp -r _backup/.mix .nix-shell/
else
######################################################
# Install Hex and Phoenix via the network
######################################################
yes | mix local.hex
yes | mix archive.install hex phx_new
fi
fi
if test -f "mix.exs"
then
# These are not in the `if` section above, because of
# the `hex` install glitch, it could be that there is
# already a `$MIX_HOME` folder. See 2019-08-05_0553
mix deps.get
######################################################
# `ecto.setup` is defined in `mix.exs` by default when
# Phoenix project is generated via `mix phx.new`.
# It does `ecto.create`, `ecto.migrate`, and run
# `priv/seeds`.
######################################################
mix ecto.setup
fi
'';
####################################################################
# Without this, almost everything fails with locale issues when
# using `nix-shell --pure` (at least on NixOS).
# See
# + https://github.com/NixOS/nix/issues/318#issuecomment-52986702
# + http://lists.linuxfromscratch.org/pipermail/lfs-support/2004-June/023900.html
####################################################################
LOCALE_ARCHIVE = if pkgs.stdenv.isLinux then "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
}
# vim: set tabstop=2 shiftwidth=2 expandtab: