-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
405 lines (355 loc) · 13.3 KB
/
flake.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{
description = "Phone Database";
inputs = {
nixpkgs = {url = "github:NixOS/nixpkgs/nixos-24.11";};
flake-utils = {url = "github:numtide/flake-utils";};
devenv = {url = "github:cachix/devenv";};
};
outputs = inputs @ {
self,
nixpkgs,
flake-utils,
devenv,
}:
flake-utils.lib.eachDefaultSystem (system: let
inherit (pkgs.lib) optional optionals;
pkgs = nixpkgs.legacyPackages.${system};
elixir = pkgs.beam.packages.erlang.elixir;
beamPackages = pkgs.beam.packagesWith pkgs.beam.interpreters.erlang;
src = ./.;
version = "0.0.0";
pname = "phone_db";
mixFodDeps = beamPackages.fetchMixDeps {
TOP_SRC = src;
pname = "${pname}-mix-deps";
inherit src version;
hash = "sha256-SO9eHTS+Xs7QnuZv5LaGrF8ErRhJuU6iv8WjnbtQ4+U=";
# hash = pkgs.lib.fakeHash;
};
nodejs = pkgs.nodejs;
nodePackages = pkgs.buildNpmPackage {
name = "phone_db_assets";
src = ./assets;
npmDepsHash = builtins.readFile ./npm-deps-hash;
dontNpmBuild = true;
inherit nodejs;
nativeBuildInputs = [
(pkgs.python3.withPackages (ps: [ps.setuptools])) # Used by gyp
];
installPhase = ''
mkdir $out
cp -r node_modules $out
ln -s $out/node_modules/.bin $out/bin
'';
};
pkg = beamPackages.mixRelease {
TOP_SRC = src;
inherit pname version elixir src mixFodDeps;
postBuild = ''
ln -sf ${mixFodDeps}/deps deps
ln -sf ${nodePackages}/node_modules assets/node_modules
export PATH="${nodePackages}/bin:$PATH"
${nodejs}/bin/npm run deploy --prefix ./assets
# for external task you need a workaround for the no deps check flag
# https://github.com/phoenixframework/phoenix/issues/2690
mix do deps.loadpaths --no-deps-check, phx.digest
mix phx.digest --no-deps-check
'';
};
psql = pkgs.writeShellScriptBin "pd_psql" ''
exec "${pkgs.postgresql}/bin/psql" "$DATABASE_URL" "$@"
'';
port = 4000;
postgres_port = 6101;
ldap_port = 6102;
ldap_dir = ".devenv/state/ldap";
ldap_url = "ldap://localhost:${toString ldap_port}/";
phone_username = "phone";
phone_password = "password";
ldap_schemas = pkgs.stdenv.mkDerivation {
name = "ldap_schemas";
src = ./ldap_schemas;
installPhase = ''
mkdir $out
cp -a * $out
'';
};
dn_suffix = "dc=python-ldap,dc=org";
root_dn = "cn=root,${dn_suffix}";
slapd_config = pkgs.writeTextFile {
name = "slapd.conf";
text = ''
include ${ldap_schemas}/00-core.schema
include ${ldap_schemas}/10-cosine.schema
include ${ldap_schemas}/50-inetorgperson.schema
include ${ldap_schemas}/70-eduperson.schema
include ${ldap_schemas}/90-aueduperson.schema
include ${ldap_schemas}/90-schac.schema
include ${ldap_schemas}/nis.schema
allow bind_v2
# Database
moduleload back_mdb
moduleload ppolicy
database mdb
directory ${ldap_dir}
suffix ${dn_suffix}
overlay ppolicy
ppolicy_default cn=default,${dn_suffix}
access to dn.sub=${dn_suffix} attrs=userPassword
by anonymous auth
access to dn.sub=${dn_suffix}
by dn.exact=${root_dn} write
'';
};
admin_ldif = pkgs.writeTextFile {
name = "admin.ldif";
text = ''
dn: ${dn_suffix}
o: Test Org
objectClass: dcObject
objectClass: organization
dn: ${root_dn}
cn: ${root_dn}
objectClass: simpleSecurityObject
objectClass: organizationalRole
userPassword: your_secure_password_here
dn: cn=default,${dn_suffix}
objectClass: top
objectClass: device
objectClass: pwdPolicy
pwdAttribute: userPassword
pwdLockout: TRUE
dn: ou=People,${dn_suffix}
objectClass: top
objectClass: OrganizationalUnit
ou: People
dn: ou=Groups,${dn_suffix}
objectClass: top
objectClass: OrganizationalUnit
'';
};
pd_ldapsearch = pkgs.writeShellScriptBin "pd_ldapsearch" ''
exec ldapsearch -H "${ldap_url}" -D "${root_dn}" -b "${dn_suffix}" -w your_secure_password_here
'';
start_ldap = pkgs.writeShellScriptBin "start_ldap" ''
set -e
set -x
if ! test -d "${ldap_dir}"; then
mkdir "${ldap_dir}"
cat "${admin_ldif}"
"${pkgs.openldap}/bin/slapadd" -n 1 -f "${slapd_config}" -l "${admin_ldif}"
fi
"${pkgs.openldap}/libexec/slapd" -f "${slapd_config}" -h "${ldap_url}" -d 1
'';
test_phone_call = pkgs.writeShellScriptBin "test_phone_call" ''
curl --json '{"phone_number":"1", "destination_number":"2"}' --user "${phone_username}:${phone_password}" "http://localhost:${
toString port
}/api/incoming_call/"
'';
devShell = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [
{
enterShell = ''
export HTTP_URL="http://localhost:${toString port}"
export PORT="${toString port}"
export RELEASE_TMP=/tmp
export DATABASE_URL_TEST="postgres://phone_db:your_secure_password_here@localhost:${
toString postgres_port
}/phone_db_test"
export DATABASE_URL="postgres://phone_db:your_secure_password_here@localhost:${
toString postgres_port
}/phone_db"
export LDAP_SERVER="localhost"
export LDAP_PORT="${toString ldap_port}"
export LDAP_BASE_DN="${dn_suffix}"
export LDAP_USERNAME="root"
export LDAP_USER_PASSWORD="your_secure_password_here"
export PHONE_USERNAME="${phone_username}"
export PHONE_PASSWORD="${phone_password}"
'';
packages = with pkgs;
[
psql
openldap
start_ldap
pd_ldapsearch
test_phone_call
elixir
elixir_ls
glibcLocales
node2nix
nodejs
pkgs.prefetch-npm-deps
]
++ optional stdenv.isLinux inotify-tools
++ optional stdenv.isDarwin terminal-notifier
++ optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [
CoreFoundation
CoreServices
]);
processes.slapd = {
exec = "${start_ldap}/bin/start_ldap";
process-compose = {
readiness_probe = {
exec.command = "${pd_ldapsearch}/bin/pd_ldapsearch";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 1;
success_threshold = 1;
failure_threshold = 3;
};
};
};
services.postgres = {
enable = true;
package = pkgs.postgresql_15;
listen_addresses = "127.0.0.1";
port = postgres_port;
initialDatabases = [{name = "phone_db";}];
initialScript = ''
\c phone_db;
CREATE USER phone_db with encrypted password 'your_secure_password_here';
GRANT ALL PRIVILEGES ON DATABASE phone_db TO phone_db;
'';
};
}
];
};
test = pkgs.nixosTest {
name = "phone_db";
nodes.machine = {...}: {
imports = [
self.nixosModules.default
];
services.phone_db = {
enable = true;
http_url = "http://localhost:4000";
port = 4000;
secrets = pkgs.writeText "secrets.txt" ''
export RELEASE_COOKIE="12345678901234567890123456789012345678901234567890123456"
export DATABASE_URL="postgres://phone_db:your_secure_password_here@localhost/phone_db"
export GUARDIAN_SECRET="1234567890123456789012345678901234567890123456789012345678901234"
export SECRET_KEY_BASE="1234567890123456789012345678901234567890123456789012345678901234"
export SIGNING_SALT="12345678901234567890123456789012"
export OIDC_DISCOVERY_URL="http://localhost"
export OIDC_CLIENT_ID="photos"
export OIDC_CLIENT_SECRET="12345678901234567890123456789012"
export OIDC_AUTH_SCOPE="openid profile groups"
export DATABASE_URL_TEST="postgres://phone_db:your_secure_password_here@localhost:${
toString postgres_port
}/phone_db_test"
export DATABASE_URL="postgres://phone_db:your_secure_password_here@localhost:${
toString postgres_port
}/phone_db"
export LDAP_SERVER="localhost"
export LDAP_PORT="${toString ldap_port}"
export LDAP_BASE_DN="${dn_suffix}"
export LDAP_USERNAME="root"
export LDAP_USER_PASSWORD="your_secure_password_here"
export PHONE_USERNAME="${phone_username}"
export PHONE_PASSWORD="${phone_password}"
'';
};
system.stateVersion = "24.05";
services.postgresql = {
enable = true;
package = pkgs.postgresql_15;
settings.port = postgres_port;
initialScript = pkgs.writeText "init.psql" ''
CREATE DATABASE phone_db;
CREATE USER phone_db with encrypted password 'your_secure_password_here';
ALTER DATABASE phone_db OWNER TO phone_db;
'';
};
services.openldap = {
enable = true;
/*
enable plain and secure connections
*/
urlList = [ldap_url];
settings = {
attrs = {
olcLogLevel = "conns config";
};
children = {
"cn=schema".includes = [
"${pkgs.openldap}/etc/schema/core.ldif"
"${pkgs.openldap}/etc/schema/cosine.ldif"
"${pkgs.openldap}/etc/schema/inetorgperson.ldif"
];
"olcDatabase={1}mdb" = {
attrs = {
objectClass = ["olcDatabaseConfig" "olcMdbConfig"];
olcDatabase = "{1}mdb";
olcDbDirectory = "/var/lib/openldap/test";
olcSuffix = dn_suffix;
/*
your admin account, do not use writeText on a production system
*/
olcRootDN = root_dn;
olcRootPW.path = pkgs.writeText "olcRootPW" "your_secure_password_here";
olcAccess = [
/*
custom access rules for userPassword attributes
*/
''
{0}to attrs=userPassword
by self write
by anonymous auth
by * none
''
/*
allow read on anything else
*/
''
{1}to *
by * read
''
];
};
children = {
"olcOverlay={2}ppolicy".attrs = {
objectClass = ["olcOverlayConfig" "olcPPolicyConfig" "top"];
olcOverlay = "{2}ppolicy";
olcPPolicyHashCleartext = "TRUE";
};
"olcOverlay={3}memberof".attrs = {
objectClass = ["olcOverlayConfig" "olcMemberOf" "top"];
olcOverlay = "{3}memberof";
olcMemberOfRefInt = "TRUE";
olcMemberOfDangling = "ignore";
olcMemberOfGroupOC = "groupOfNames";
olcMemberOfMemberAD = "member";
olcMemberOfMemberOfAD = "memberOf";
};
"olcOverlay={4}refint".attrs = {
objectClass = ["olcOverlayConfig" "olcRefintConfig" "top"];
olcOverlay = "{4}refint";
olcRefintAttribute = ["memberof" "member" "manager" "owner"];
};
};
};
};
};
};
};
testScript = ''
machine.wait_for_unit("phone_db.service")
machine.wait_for_open_port(4000)
machine.succeed("${pkgs.curl}/bin/curl --fail -v http://localhost:4000/_health")
machine.succeed("${test_phone_call}/bin/test_phone_call")
'';
};
in {
checks.nixosModules = test;
packages = {
devenv-up = devShell.config.procfileScript;
default = pkg;
};
inherit devShell;
})
// {
nixosModules.default = import ./module.nix {inherit self;};
};
}