-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflake.nix
368 lines (336 loc) · 10.9 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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils = {
url = "github:numtide/flake-utils/v1.0.0";
};
devenv = {
url = "github:cachix/devenv";
inputs.nixpkgs.follows = "nixpkgs";
};
treefmt-nix.url = "github:numtide/treefmt-nix";
zig2nix.url = "github:Cloudef/zig2nix";
};
outputs =
{
self,
nixpkgs,
devenv,
flake-utils,
zig2nix,
treefmt-nix,
...
}@inputs:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
};
};
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
getErlangLibs =
erlangPkg:
let
erlangPath = "${erlangPkg}/lib/erlang/lib/";
dirs = builtins.attrNames (builtins.readDir erlangPath);
interfaceVersion = builtins.head (
builtins.filter (s: builtins.substring 0 13 s == "erl_interface") dirs
);
interfacePath = erlangPath + interfaceVersion;
in
{
path = erlangPath;
dirs = dirs;
interface = {
version = interfaceVersion;
path = interfacePath;
};
};
# Environment-specific packages
linuxPkgs = with pkgs; [
inotify-tools
glfw
libGL
libpulseaudio
libxkbcommon
pkg-config
xorg.libxcb
xorg.libXft
xorg.libX11
xorg.libX11.dev
xorg.libXrandr
xorg.libXinerama
xorg.libXcursor
xorg.libXi
# Wayland stuff
glfw-wayland
wayland
wayland-protocols
];
linuxLibs =
with pkgs;
lib.makeLibraryPath [
libGL
libxkbcommon
raylib
xorg.libxcb
xorg.libXft
xorg.libX11
xorg.libX11.dev
xorg.libXrandr
xorg.libXinerama
xorg.libXcursor
xorg.libXi
# Wayland stuff
glfw-wayland
wayland
wayland-protocols
];
darwinPkgs = with pkgs.darwin.apple_sdk.frameworks; [
CoreFoundation
CoreServices
];
# App config
app_version = "0.1.0";
# Erlang
erlangLatest = pkgs.erlang_27;
erlangLibs = getErlangLibs erlangLatest;
erl_app = "server";
# Zig shit (Incomplete)
zig_app = "lyceum-client";
zigLatest = pkgs.zig;
raylib = pkgs.raylib;
env = zig2nix.outputs.zig-env.${system} {
#zig = zig2nix.outputs.packages.${system}.zig.master.bin;
customRuntimeLibs =
[
pkgs.pkg-config
erlangLibs
raylib
]
++ pkgs.lib.optionals pkgs.stdenv.isLinux linuxPkgs
++ pkgs.lib.optionals pkgs.stdenv.isLinux darwinPkgs;
customRuntimeDeps =
[
erlangLibs
raylib
]
++ pkgs.lib.optionals pkgs.stdenv.isLinux linuxPkgs
++ pkgs.lib.optionals pkgs.stdenv.isLinux darwinPkgs;
enableOpenGL = true;
enableWayland = true;
enableX11 = true;
};
system-triple = env.lib.zigTripleFromString system;
mkEnvVars = pkgs: erlangLatest: erlangLibs: raylib: {
LOCALE_ARCHIVE = pkgs.lib.optionalString pkgs.stdenv.isLinux "${pkgs.glibcLocales}/lib/locale/locale-archive";
LANG = "en_US.UTF-8";
# https://www.erlang.org/doc/man/kernel_app.html
ERL_AFLAGS = "-kernel shell_history enabled";
ERL_INCLUDE_PATH = "${erlangLatest}/lib/erlang/usr/include";
# Setup path for non-NixOS users
#LD_LIBRARY_PATH = "$LD_LIBRARY_PATH:${linuxLibs}";
# Devenv sets this to something else
# https://www.postgresql.org/docs/7.0/libpq-envars.htm
PGHOST = "127.0.0.1";
};
in
{
# nix build
packages = rec {
devenv-up = self.devShells.${system}.default.config.procfileScript;
# Leverages nix to build the erlang backend release
# nix build .#server
server =
let
deps = import ./server/rebar-deps.nix {
inherit (pkgs) fetchHex fetchFromGitHub fetchgit;
builder = pkgs.beamPackages.buildRebar3;
};
in
pkgs.beamPackages.rebar3Relx {
pname = erl_app;
version = app_version;
root = ./server;
src = pkgs.lib.cleanSource ./server;
releaseType = "release";
profile = "prod";
include = [
"rebar.config"
];
beamDeps = builtins.attrValues deps;
buildPhase = ''
runHook preBuild
HOME=. DEBUG=1 rebar3 as prod release --relname server
runHook postBuild
'';
};
# nix build .#dockerImage
dockerImage = pkgs.dockerTools.buildLayeredImage {
name = erl_app;
tag = "latest";
created = "now";
# This will copy the compiled erlang release to the image
contents = [
server
pkgs.coreutils
pkgs.gawk
pkgs.gnugrep
pkgs.openssl
];
config = {
Volumes = {
"/opt/${erl_app}/etc" = { };
"/opt/${erl_app}/data" = { };
"/opt/${erl_app}/log" = { };
};
WorkingDir = "/opt/${erl_app}";
Cmd = [
"${server}/bin/${erl_app}"
"foreground"
];
Env = [
"ERL_DIST_PORT=8080"
"ERL_AFLAGS=\"-kernel shell_history enabled\""
"NODE_NAME=${erl_app}"
];
ExposedPorts = {
"4369/tcp" = { };
"4369/ucp" = { };
"8080/tcp" = { };
"8080/udp" = { };
};
};
};
# TODO: Still can't make this crapola build with zig2nix
# nix build .#client
client = pkgs.stdenv.mkDerivation {
pname = zig_app;
version = app_version;
src = pkgs.lib.cleanSource ./client;
zigBuildFlags = [
"-fsys=raylib"
"--release=fast"
"-Dassets=${builtins.toString ./client}/assets"
];
nativeBuildInputs = [
zigLatest.hook
pkgs.makeWrapper
];
buildInputs =
with pkgs; [ raylib zigLatest erlangLatest ]
++ lib.optionals stdenv.isLinux (linuxPkgs)
++ lib.optionals stdenv.isDarwin darwinPkgs;
postPatch = ''
ln -s ${pkgs.callPackage ./client/zon-deps.nix { }} $ZIG_GLOBAL_CACHE_DIR/p
'';
postInstall = ''
wrapProgram "$out/bin/${zig_app}" --prefix LD_LIBRARY_PATH ":" "${linuxLibs}"
'';
};
};
# nix run
apps = {
packages.default = env.lib.packages.target.${system-triple}.override {
# Prefer nix friendly settings.
zigPreferMusl = false;
zigDisableWrap = false;
};
# nix run .#build
apps.build = env.app [ ] "zig build -- \"$@\"";
# nix run .#test
apps.test = env.app [ ] "zig build test -- \"$@\"";
};
devShells = {
# `nix develop .#ci`
# reduce the number of packages to the bare minimum needed for CI
ci = pkgs.mkShell {
env = mkEnvVars pkgs erlangLatest erlangLibs raylib;
buildInputs = with pkgs; [
erlangLatest
just
rebar3
rsync
zigLatest
raylib
];
};
# `nix develop`
default = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [
(
{ pkgs, lib, ... }:
{
packages =
with pkgs;
[
just
raylib
sqls
]
++ lib.optionals stdenv.isLinux (linuxPkgs)
++ lib.optionals stdenv.isDarwin darwinPkgs;
languages.erlang = {
enable = true;
package = erlangLatest;
};
languages.zig = {
enable = true;
package = zigLatest;
};
env = mkEnvVars pkgs erlangLatest erlangLibs raylib;
scripts = {
build.exec = "just build";
server.exec = "just server";
client.exec = "just client";
client-release.exec = "just client-release";
db-up.exec = "just db-up";
db-down.exec = "just db-down";
db-reset.exec = "just db-reset";
};
enterShell = ''
echo "Starting Development Environment..."
'';
services.postgres = {
enable = true;
package = pkgs.postgresql_17;
extensions = ext: [
ext.periods
];
initdbArgs = [
"--locale=C"
"--encoding=UTF8"
];
settings = {
shared_preload_libraries = "pg_stat_statements";
# pg_stat_statements config, nested attr sets need to be
# converted to strings, otherwise postgresql.conf fails
# to be generated.
compute_query_id = "on";
"pg_stat_statements.max" = 10000;
"pg_stat_statements.track" = "all";
};
initialDatabases = [ { name = "mmo"; } ];
port = 5432;
listen_addresses = "127.0.0.1";
initialScript = ''
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE USER admin SUPERUSER;
ALTER USER admin PASSWORD 'admin';
GRANT ALL PRIVILEGES ON DATABASE mmo to admin;
'';
};
}
)
];
};
};
# nix fmt
formatter = treefmtEval.config.build.wrapper;
}
);
}