Skip to content

Instantly share code, notes, and snippets.

@ggemre
Created October 11, 2025 08:18
Show Gist options
  • Select an option

  • Save ggemre/029c258795353fda98e4c17d8e09a56c to your computer and use it in GitHub Desktop.

Select an option

Save ggemre/029c258795353fda98e4c17d8e09a56c to your computer and use it in GitHub Desktop.
Nix flake for building cubyz, (https://github.com/PixelGuys/Cubyz), an open source 3D voxel sandbox game.
# A nix flake for building cubyz, (https://github.com/PixelGuys/Cubyz),
# an open source 3D voxel sandbox game.
# SERIOUS LIMITATION: cubyz uses their own custom patched zig compiler ୧(๑•̀ᗝ•́)૭
# to build, you will need to do nix build --option sandbox false.
# This is because the custom zig compiler looks for dynamically linked libraries
# in an FHS environment. There are ways around this but I'm too lazy to solve for now.
# IMPORTANT NOTE: use `zon2nix` to generate a local deps.nix file with dependencies.
# It may contain local files that don't exist on your computer, so just delete that
# that dependency from the zon file.
{
description = "A Nix flake to build Cubyz with the PixelGuys Zig compiler and offline deps";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
outputs = { self, nixpkgs }:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system:
let pkgs = import nixpkgs { inherit system; };
in f system pkgs
);
in
{
packages = forAllSystems (system: pkgs:
let
arch = if system == "x86_64-linux" then "x86_64" else "aarch64";
OS = "linux";
zigVersion = "0.15.0-dev.1034+bd97b6618";
renderZig = pkgs.fetchurl {
url = "https://github.com/PixelGuys/Cubyz-std-lib/releases/download/${zigVersion}/render.zig";
hash = "sha256-55bpDPUOzfDIxliEnLorwtcw/DwqR0fu19a3w+VD7eE=";
};
# Zig compiler from PixelGuys releases
zigCompiler = pkgs.stdenv.mkDerivation {
pname = "cubyz-zig";
version = zigVersion;
src = pkgs.fetchurl {
url = "https://github.com/PixelGuys/Cubyz-zig-versions/releases/download/${zigVersion}/zig-${arch}-${OS}-${zigVersion}.tar.xz";
hash = "sha256-MmiDkByXDxbDOlh7ykSyYnHITSjRZuGPFBHvSjf+i1M=";
};
nativeBuildInputs = [ pkgs.gnutar pkgs.wget ];
buildPhase = ''
mkdir -p $out
tar --xz -xf $src --strip-components 1 -C $out
# patch render.zig
cp ${renderZig} $out/lib/std/zig/render.zig
'';
installPhase = ''
mkdir -p $out
cp -r * $out/
'';
};
zigDeps = pkgs.callPackage ./deps.nix { };
in pkgs.stdenv.mkDerivation rec {
pname = "cubyz";
version = "0.0.0";
src = pkgs.fetchFromGitHub {
owner = "PixelGuys";
repo = "Cubyz";
rev = "d1872a26e8e3a4618f5a51876b530085aead3f9e";
hash = "sha256-MYzgYaBbIyqSqkn9/2JquoruBV5Ayr7OaEn3oG5iUMc=";
};
nativeBuildInputs = [
zigCompiler
pkgs.gcc
pkgs.glibc.dev
pkgs.makeWrapper
];
buildPhase = ''
export ZIG_GLOBAL_CACHE_DIR="$PWD/zig-cache"
mkdir -p "$ZIG_GLOBAL_CACHE_DIR"
ln -s ${zigDeps} "$ZIG_GLOBAL_CACHE_DIR/p"
export C_INCLUDE_PATH=${pkgs.glibc.dev}/include
export LIBRARY_PATH=${pkgs.glibc}/lib
export PATH=${zigCompiler}/bin:$PATH
${zigCompiler}/zig build --prominent-compile-errors
'';
installPhase = ''
mkdir -p $out/bin
cp zig-out/bin/Cubyz $out/bin/Cubyz
makeWrapper $out/bin/Cubyz $out/bin/cubyz \
--chdir $out/share/cubyz \
--set LD_LIBRARY_PATH ${pkgs.lib.makeLibraryPath [
pkgs.libGL pkgs.glfw pkgs.openal pkgs.vulkan-loader
pkgs.xorg.libX11 pkgs.xorg.libXi pkgs.xorg.libXcursor
pkgs.xorg.libXrandr pkgs.xorg.libXxf86vm
]}
mkdir -p $out/share/cubyz
cp -r assets $out/share/cubyz/
'';
meta = with pkgs.lib; {
description = "Cubyz — a 3D voxel game written in Zig";
homepage = "https://github.com/PixelGuys/Cubyz";
license = licenses.gpl3Plus;
platforms = [ "x86_64-linux" "aarch64-linux" ];
};
}
);
defaultPackage = forAllSystems (system: pkgs: self.packages.${system});
apps = forAllSystems (system: pkgs: {
cubyz = {
type = "app";
program = "${self.packages.${system}.cubyz}/bin/cubyz";
};
});
devShells = forAllSystems (system: pkgs: {
default = pkgs.mkShell {
nativeBuildInputs = [ pkgs.zig pkgs.glfw pkgs.openal pkgs.vulkan-loader ];
shellHook = ''
export ZIG_GLOBAL_CACHE_DIR="$PWD/zig-cache"
mkdir -p "$ZIG_GLOBAL_CACHE_DIR"
ln -s ${pkgs.callPackage ./deps.nix { }} "$ZIG_GLOBAL_CACHE_DIR/p"
'';
};
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment