From 0c61d5d5d6199e5f5064e553a005cbc8e07aa97f Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 23 Jun 2025 15:15:09 -0700 Subject: [PATCH] Pre-cargo initial commit --- .cargo/config.toml | 28 ++++++++++++ .direnv/bin/nix-direnv-reload | 19 ++++++++ .envrc | 1 + flake.nix | 86 +++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 .cargo/config.toml create mode 100755 .direnv/bin/nix-direnv-reload create mode 100644 .envrc create mode 100644 flake.nix diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..08e581a --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,28 @@ +[unstable] +codegen-backend = true + +[profile.dev] +codegen-backend = "cranelift" + +[profile.dev.package."*"] +codegen-backend = "llvm" + +[profile.release] +lto = true +opt-level = 'z' + +# for Linux +[target.x86_64-unknown-linux-gnu] +linker = "clang" +rustflags = [ + # (Nightly) Make the current crate share its generic instantiations + "-Zshare-generics=y", + # Link with Mold + "-C", + "link-arg=-fuse-ld=mold" +] + + +# for Windows +[target.x86_64-pc-windows-msvc] +linker = "rust-lld.exe" diff --git a/.direnv/bin/nix-direnv-reload b/.direnv/bin/nix-direnv-reload new file mode 100755 index 0000000..6b426f6 --- /dev/null +++ b/.direnv/bin/nix-direnv-reload @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -e +if [[ ! -d "/home/pop/Projects/src/localhost/games" ]]; then + echo "Cannot find source directory; Did you move it?" + echo "(Looking for "/home/pop/Projects/src/localhost/games")" + echo 'Cannot force reload with this script - use "direnv reload" manually and then try again' + exit 1 +fi + +# rebuild the cache forcefully +_nix_direnv_force_reload=1 direnv exec "/home/pop/Projects/src/localhost/games" true + +# Update the mtime for .envrc. +# This will cause direnv to reload again - but without re-building. +touch "/home/pop/Projects/src/localhost/games/.envrc" + +# Also update the timestamp of whatever profile_rc we have. +# This makes sure that we know we are up to date. +touch -r "/home/pop/Projects/src/localhost/games/.envrc" "/home/pop/Projects/src/localhost/games/.direnv"/*.rc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..5f821b8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,86 @@ +{ + inputs = { + flake-utils.url = "github:numtide/flake-utils"; + naersk.url = "github:nix-community/naersk"; + rust-overlay.url = "github:oxalica/rust-overlay"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + wild.url = "github:davidlattimore/wild"; + }; + + outputs = { self, flake-utils, naersk, nixpkgs, rust-overlay, wild }: + flake-utils.lib.eachDefaultSystem (system: + let + systems = ["x86_64-linux"]; + overlays = [ + (import rust-overlay) + wild.overlays.default + ]; + wildStdenv = pkgs.useWildLinker pkgs.stdenv; + pkgs = (import nixpkgs) { + inherit system overlays; + config.allowUnfree = true; + }; + naersk' = pkgs.callPackage naersk { }; + buildInputs = with pkgs; [ + vulkan-loader + xorg.libXcursor + xorg.libXi + xorg.libXrandr + libxkbcommon + wayland + alsa-lib + udev + pkg-config + clang + lld + mold + binaryen + ]; + nativeBuildInputs = with pkgs; [ + libxkbcommon + (rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override { + extensions = [ "rust-src" "clippy" "rustc-codegen-cranelift-preview" ]; + targets = [ "x86_64-unknown-linux-gnu" "wasm32-unknown-unknown" ]; + })) + ]; + all_deps = with pkgs; [ + # cargo-expand + nixpkgs-fmt + cmake + wasm-pack + wasm-bindgen-cli + trunk + simple-http-server + zip + unzip + mdcat + rust-analyzer + ] ++ buildInputs ++ nativeBuildInputs; + in + rec { + # For `nix build` & `nix run`: + defaultPackage = packages.bevy_template { stdenv = wildStdenv; }; + packages = rec { + bevy_template = naersk'.buildPackage { + src = ./.; + nativeBuildInputs = nativeBuildInputs; + buildInputs = buildInputs; + postInstall = '' + cp -r assets $out/bin/ + ''; + # Disables dynamic linking when building with Nix + cargoBuildOptions = x: x ++ [ "--no-default-features" ]; + }; + }; + + # For `nix develop`: + devShell = pkgs.mkShell.override { stdenv = wildStdenv; } { + nativeBuildInputs = all_deps; + shellHook = '' + export CARGO_MANIFEST_DIR=$(pwd) + export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath all_deps}" + ''; + }; + } + ); +}