Initial commit
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
{
|
||||
description = "siege-deck — a from-scratch SDL3 tower defense in C++26";
|
||||
|
||||
# Same channel the NixOS config tracks, so the shell's SDL3/mesa match the
|
||||
# system's GL/Wayland drivers instead of drifting a release apart.
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
systems = [ "x86_64-linux" ];
|
||||
forAllSystems = f:
|
||||
nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
|
||||
|
||||
mkGameShell = pkgs: stdenv:
|
||||
let
|
||||
lib = pkgs.lib;
|
||||
|
||||
# Dear ImGui in nixpkgs builds ONLY the GLFW + OpenGL3 backends,
|
||||
# which are dead weight here — this game draws through SDL_Renderer,
|
||||
# not GL, so the stock package ships no usable impl at all. The
|
||||
# override swaps them for imgui_impl_sdl3 + imgui_impl_sdlrenderer3
|
||||
# and drops the glfw dependency. Headers land flat in include/, so
|
||||
# it's `#include <imgui_impl_sdl3.h>`, NOT `<backends/...>` — see
|
||||
# cmake/ImGui.cmake, which papers over that difference for the
|
||||
# CPM-from-source path.
|
||||
imgui = pkgs.imgui.override {
|
||||
IMGUI_BUILD_GLFW_BINDING = false;
|
||||
IMGUI_BUILD_OPENGL3_BINDING = false;
|
||||
IMGUI_BUILD_SDL3_BINDING = true;
|
||||
IMGUI_BUILD_SDL3_RENDERER_BINDING = true;
|
||||
};
|
||||
|
||||
# Everything CPM would otherwise download and rebuild from source.
|
||||
# CPM_USE_LOCAL_PACKAGES (exported below) makes CPMAddPackage try
|
||||
# find_package first, so inside this shell these are used as-is and
|
||||
# only what nixpkgs lacks gets fetched.
|
||||
deps = (with pkgs; [
|
||||
sdl3
|
||||
sdl3-image
|
||||
sdl3-ttf
|
||||
sdl3-mixer
|
||||
spdlog
|
||||
glm
|
||||
entt
|
||||
boost
|
||||
]) ++ [ imgui ];
|
||||
|
||||
tools = with pkgs; [
|
||||
cmake
|
||||
ninja
|
||||
pkg-config
|
||||
|
||||
# CPM.cmake itself, so a fresh configure needs no network at all.
|
||||
cpm-cmake
|
||||
|
||||
# clangd / clang-format / clang-tidy. Kept separate from the
|
||||
# compiler so the gcc shell below gets the same tooling.
|
||||
clang-tools
|
||||
|
||||
gdb
|
||||
ccache
|
||||
];
|
||||
in
|
||||
(pkgs.mkShell.override { inherit stdenv; }) {
|
||||
packages = tools;
|
||||
buildInputs = deps;
|
||||
|
||||
# CMake's own find_package search path. The cmake setup hook already
|
||||
# populates this from buildInputs, but spelling it out keeps the
|
||||
# shell honest if that hook ever changes, and makes it greppable when
|
||||
# a find_package unexpectedly misses.
|
||||
CMAKE_PREFIX_PATH = lib.makeSearchPathOutput "dev" "" deps;
|
||||
|
||||
shellHook = ''
|
||||
# --- CPM: prefer nixpkgs, fall back to downloading -------------
|
||||
# The CMakeLists keeps game-from-scratch's CPMAddPackage calls
|
||||
# verbatim so the tree still configures on a machine without nix
|
||||
# (Windows, a fresh checkout, CI). These two variables are the only
|
||||
# thing that changes inside the shell: CPM resolves through
|
||||
# find_package first, and anything it does have to fetch is cached
|
||||
# outside the build dir so `rm -rf cmake-build-*` stays cheap.
|
||||
export CPM_USE_LOCAL_PACKAGES=ON
|
||||
export CPM_SOURCE_CACHE="''${XDG_CACHE_HOME:-$HOME/.cache}/CPM"
|
||||
export CPM_CMAKE_PATH="${pkgs.cpm-cmake}/share/cpm/CPM.cmake"
|
||||
|
||||
export CMAKE_GENERATOR=Ninja
|
||||
|
||||
# CMAKE_PREFIX_PATH above lists only direct deps. nixpkgs' own cmake also
|
||||
# reads NIXPKGS_CMAKE_PREFIX_PATH, which carries propagated ones (fmt for
|
||||
# spdlog); any other cmake, such as CLion's bundled one, does not, so
|
||||
# spdlog's find_dependency(fmt) failed there and CPM quietly downloaded
|
||||
# spdlog instead. Appending it gives every cmake the same search path.
|
||||
if [ -n "$NIXPKGS_CMAKE_PREFIX_PATH" ]; then
|
||||
export CMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH:$NIXPKGS_CMAKE_PREFIX_PATH"
|
||||
fi
|
||||
|
||||
# --- clangd: teach it where the standard library lives ---------
|
||||
# On NixOS the C++ system headers are injected by the cc-wrapper
|
||||
# through NIX_CFLAGS_COMPILE at *compile* time. clangd never sees
|
||||
# that variable — it only reads compile_commands.json, where the
|
||||
# flags are absent — so every std include goes red while the build
|
||||
# itself succeeds. Asking the real compiler for its search list and
|
||||
# pinning it into .clangd fixes every editor at once (CLion's
|
||||
# bundled clangd, VS Code, nvim) rather than per-IDE
|
||||
# --query-driver settings, which CLion in particular makes awkward.
|
||||
_sd_clangd=$(mktemp)
|
||||
{
|
||||
echo "# Generated by the nix devShell on entry. Do not edit; see flake.nix."
|
||||
echo "CompileFlags:"
|
||||
echo " Add:"
|
||||
$CXX -E -x c++ - -v </dev/null 2>&1 \
|
||||
| sed -n '/^#include <\.\.\.>/,/^End of search list/p' \
|
||||
| grep '^ ' | sed 's/^ *//' \
|
||||
| while read -r dir; do echo " - -isystem$dir"; done
|
||||
echo " Remove:"
|
||||
# CLion/CMake hand clangd flags its parser rejects outright.
|
||||
echo " - -fno-keep-inline-dllexport"
|
||||
} > "$_sd_clangd"
|
||||
if ! cmp -s "$_sd_clangd" .clangd; then
|
||||
mv "$_sd_clangd" .clangd
|
||||
else
|
||||
rm -f "$_sd_clangd"
|
||||
fi
|
||||
unset _sd_clangd
|
||||
|
||||
echo "siege-deck devshell — $($CXX --version | head -1)"
|
||||
echo " cmake --preset debug configure (Ninja, compile_commands.json)"
|
||||
echo " cmake --build --preset debug"
|
||||
echo " ./cmake-build-debug/bin/siege_deck"
|
||||
echo
|
||||
echo " CLion: toolchain environment file cmake/clion-env.sh (README)"
|
||||
echo " nix develop .#gcc same shell, gcc instead of clang"
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
devShells = forAllSystems (pkgs: {
|
||||
# clang by default: CLion's bundled clangd parses with clang
|
||||
# semantics, so building with the same compiler removes the whole
|
||||
# class of "red in the editor, green in the build" mismatches — worse
|
||||
# than usual on NixOS, where gcc's include paths are non-standard.
|
||||
# nixpkgs' clang links libstdc++, so this stays ABI-compatible with
|
||||
# the nixpkgs-built SDL3/spdlog/imgui above.
|
||||
default = mkGameShell pkgs pkgs.clangStdenv;
|
||||
|
||||
# Second opinion on diagnostics, and a check that the code isn't
|
||||
# accidentally leaning on a clang extension.
|
||||
gcc = mkGameShell pkgs pkgs.stdenv;
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user