Skip to content

Commit

Permalink
82: Add a pure client build that leverages Nix (#83)
Browse files Browse the repository at this point in the history
* 82: attempt to make a pure client build + update instructions
  • Loading branch information
schonfinkel authored Jan 7, 2025
1 parent 43e61f5 commit bc24e08
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/client_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ jobs:

- name: "[Client] Build"
run: |
nix develop .#ci --impure -c just client-build-ci
nix build .#client
- name: "[Client] Tests"
run: |
nix develop .#ci --impure -c just client-build-ci
nix develop .#ci --impure -c just client-test-ci
65 changes: 50 additions & 15 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,14 @@ There are several ways to run this project, but they all leverage [[https://nixo
single [[https://devenv.sh/][devenv]] configured, that has all the tooling required to run this project,
it also includes postgres.

** Testing the Game (DEMO)

1. ~nix develop --impure~
2. ~devenv up~ to launch a local ~postgres~
3. ~just db-reset~ or ~db-reset~, to initialize ~postgres~ for local development
5. ~server~ (or ~./result/bin/server foreground~ if you want to run the pure nix build)
6. followed by ~client-release~
7. Once you get the client to launch, connect to ~localhost~ and you can try
playing with any of the local credentials [[https://github.com/Dr-Nekoma/lyceum/blob/master/server/database/main.input.sql#L3][here]].

** Development Shell

To enter a development shell we assume you have at least [[https://nixos.org/][Nix]] installed:

#+BEGIN_SRC shell
# To enter the developmet shell
nix develop --impure
# To spawn postgres
# To spawn postgres (for instance)
devenv up -d
#+END_SRC

Expand All @@ -53,16 +45,59 @@ to migrate the local dababase, you can use ~just db-up~ or ~db-up~.
You can also build the server and related OCI images with Nix.

#+BEGIN_SRC shell
nix build .#server
nix build .#server
#+END_SRC

if you prefer running a container for the server:

#+BEGIN_SRC shell
nix build .#buildImage
docker load < ./result
docker container run --network=host lyceum:latest
nix build .#buildImage
docker load < ./result
docker container run --network=host lyceum:latest
#+END_SRC

or if you want to build the ~zig~ client, run ~nix build .#client~.

** Testing the Game

To run our DEMO you need to follow the steps in this particular order:

1. ~nix develop --impure~ (to enter a development shell)
2. ~devenv up~ to launch a local ~postgres~
3. ~just db-reset~ or ~db-reset~, to initialize ~postgres~ for local development
4. Run the ~erlang~ backend in a separate shell with either:

#+begin_src shell
server
# or
just server
# or if you want a pure build
nix build .#server
./result/bin/server foreground
#+end_src

5. Followed by running the ~zig~ client in a third shell with either:

#+begin_src shell
client-release
# or
just client-release
# or if you prefer a pure build
nix build .#client
./result/bin/lyceum-client
#+end_src

*Addendum*: We got [[https://github.com/Dr-Nekoma/lyceum/pull/83#issuecomment-2550476288][reports]] that (sometimes) non-NixOS users will have to rely on[[https://github.com/nix-community/nixGL][ nixGL]].

#+begin_src shell
nix run --impure github:nix-community/nixGL -- ./result/bin/lyceum-client
#+end_src

6. Once you get the client to launch, connect to ~localhost~ and you can try
playing with any of the local credentials [[https://github.com/Dr-Nekoma/lyceum/blob/master/server/database/main.input.sql#L3][here]].

** Deployment

This game is deployed in our [[https://github.com/Dr-Nekoma/trashcan][NixOS server]].

* Assets
Expand Down
14 changes: 14 additions & 0 deletions client/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ pub fn build(b: *std.Build) !void {
exe.root_module.addImport("zerl", zerl.module("zerl"));
}

const assets = b.addInstallDirectory(.{
.source_dir = b.path("assets"),
.install_dir = .prefix,
.install_subdir = "assets",
});

exe.step.dependOn(&assets.step);

// https://ziglang.org/learn/build-system/#conditional-compilation
const assets_opt = b.option([]const u8, "assets", "custom path for the assets directory") orelse "./assets";
const options = b.addOptions();
options.addOption([]const u8, "assets", assets_opt);
exe.root_module.addOptions("build_options", options);

if (b.lazyImport(@This(), "zerl")) |zerl_build| {
if (std.posix.getenv("LIBRARY_PATH")) |lib_path| {
try zerl_build.add_erlang_paths(b, lib_path);
Expand Down
3 changes: 2 additions & 1 deletion client/src/assets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const rl = @import("raylib");
const rm = rl.math;
const std = @import("std");
const GameState = @import("game/state.zig");
const build_options = @import("build_options");

const base_filepath = "./assets/";
const base_filepath = build_options.assets;

fn load(
comptime T: type,
Expand Down
20 changes: 4 additions & 16 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
zigBuildFlags = [
"-fsys=raylib"
"--release=fast"
"-Dassets=${builtins.toString ./client}/assets"
];

nativeBuildInputs = [
Expand All @@ -237,14 +238,9 @@
];

buildInputs =
with pkgs;
[
raylib
zigLatest
erlangLatest
]
++ lib.optionals stdenv.isLinux (linuxPkgs)
++ lib.optionals stdenv.isDarwin darwinPkgs;
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
Expand Down Expand Up @@ -287,14 +283,6 @@
];
};

# `nix develop .#server`
server = pkgs.mkShell {
buildInputs = with pkgs; [
erlangLatest
rebar3
];
};

# `nix develop`
default = devenv.lib.mkShell {
inherit inputs pkgs;
Expand Down

0 comments on commit bc24e08

Please sign in to comment.