From bc24e08f0ff01cff93dfba169c7f18228d549ae6 Mon Sep 17 00:00:00 2001 From: Marcos Benevides Date: Mon, 6 Jan 2025 21:05:23 -0300 Subject: [PATCH] 82: Add a pure client build that leverages Nix (#83) * 82: attempt to make a pure client build + update instructions --- .github/workflows/client_build.yml | 3 +- README.org | 65 +++++++++++++++++++++++------- client/build.zig | 14 +++++++ client/src/assets.zig | 3 +- flake.nix | 20 ++------- 5 files changed, 72 insertions(+), 33 deletions(-) diff --git a/.github/workflows/client_build.yml b/.github/workflows/client_build.yml index 6bb699f..39af7d8 100644 --- a/.github/workflows/client_build.yml +++ b/.github/workflows/client_build.yml @@ -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 diff --git a/README.org b/README.org index 646809f..f0a2499 100644 --- a/README.org +++ b/README.org @@ -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 @@ -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 diff --git a/client/build.zig b/client/build.zig index cb638f4..87fc248 100644 --- a/client/build.zig +++ b/client/build.zig @@ -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); diff --git a/client/src/assets.zig b/client/src/assets.zig index e1d6114..1e34db2 100644 --- a/client/src/assets.zig +++ b/client/src/assets.zig @@ -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, diff --git a/flake.nix b/flake.nix index b8e0167..833474d 100644 --- a/flake.nix +++ b/flake.nix @@ -229,6 +229,7 @@ zigBuildFlags = [ "-fsys=raylib" "--release=fast" + "-Dassets=${builtins.toString ./client}/assets" ]; nativeBuildInputs = [ @@ -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 @@ -287,14 +283,6 @@ ]; }; - # `nix develop .#server` - server = pkgs.mkShell { - buildInputs = with pkgs; [ - erlangLatest - rebar3 - ]; - }; - # `nix develop` default = devenv.lib.mkShell { inherit inputs pkgs;