|
| 1 | +{ |
| 2 | + description = "Bevy Game development environment"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; |
| 6 | + # nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; |
| 7 | + flake-utils.url = "github:numtide/flake-utils"; |
| 8 | + nixgl = { |
| 9 | + url = "github:guibou/nixGL"; |
| 10 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 11 | + }; |
| 12 | + }; |
| 13 | + |
| 14 | + outputs = { self, nixpkgs, flake-utils, nixgl }: |
| 15 | + flake-utils.lib.eachDefaultSystem (system: |
| 16 | + let |
| 17 | + pkgsOrig = import nixpkgs {inherit system; }; |
| 18 | + forced = builtins.trace "Current system is: ${system}" null; |
| 19 | + isLinux = (builtins.match ".*linux.*" system) != null; |
| 20 | + isDarwin = (builtins.match ".*darwin.*" system) != null; |
| 21 | + isImpure = builtins.hasAttr "currentTime" builtins; |
| 22 | + |
| 23 | + pkgs = import nixpkgs { |
| 24 | + inherit system; |
| 25 | + overlays = if (isLinux && isImpure) |
| 26 | + then [ nixgl.overlays.default ] |
| 27 | + else [ ]; |
| 28 | + }; |
| 29 | + |
| 30 | + linuxPackages = with pkgs; [ |
| 31 | + alsa-lib |
| 32 | + alsa-plugins |
| 33 | + udev |
| 34 | + ]; |
| 35 | + linuxImpurePackages = with pkgs; [ |
| 36 | + pkgs.nixgl.auto.nixGLDefault |
| 37 | + ]; |
| 38 | + |
| 39 | + sdkVersion = "15"; |
| 40 | + appleSDK = pkgs."apple-sdk_${sdkVersion}"; |
| 41 | + appleLibs = if isDarwin then ([ |
| 42 | + appleSDK |
| 43 | + ]) else []; |
| 44 | + |
| 45 | + # Platform-specific packages |
| 46 | + platformPackages = |
| 47 | + (if (isLinux && !isImpure) then |
| 48 | + linuxPackages |
| 49 | + else if isLinux then |
| 50 | + linuxPackages ++ linuxImpurePackages |
| 51 | + else if isDarwin then |
| 52 | + appleLibs |
| 53 | + else |
| 54 | + [ ]); |
| 55 | + |
| 56 | + # Common bindgen configuration |
| 57 | + commonBindgenIncludes = with pkgs; [ |
| 58 | + ''-I"${llvmPackages_latest.libclang.lib}/lib/clang/${llvmPackages_latest.libclang.version}/include"'' |
| 59 | + ]; |
| 60 | + |
| 61 | + # Platform-specific bindgen configuration |
| 62 | + platformBindgenInputs = if isLinux then |
| 63 | + with pkgs; [ |
| 64 | + glibc.dev |
| 65 | + ] |
| 66 | + else []; |
| 67 | + |
| 68 | + platformBindgenIncludes = if isLinux then |
| 69 | + with pkgs; [ |
| 70 | + ''-I"${glib.dev}/include/glib-2.0"'' |
| 71 | + ''-I${glib.out}/lib/glib-2.0/include/'' |
| 72 | + ] |
| 73 | + else []; |
| 74 | + |
| 75 | + # It's standard in nix to pin the versions for reproducibility; here's where we |
| 76 | + # pin the Rust toolchain. |
| 77 | + overrides = (builtins.fromTOML (builtins.readFile (self + "/nix/rust-toolchain.toml"))); |
| 78 | + |
| 79 | + # Platform-specific shell hook |
| 80 | + platformShellHook = if isLinux then '' |
| 81 | + alias gl='nixGL' |
| 82 | + export ALSA_PLUGIN_DIR=${pkgs.alsa-plugins}/lib/alsa-lib |
| 83 | + '' |
| 84 | + else |
| 85 | + ""; |
| 86 | + |
| 87 | + libPath = with pkgs; lib.makeLibraryPath [ |
| 88 | + # load external libraries that you need in your rust project here |
| 89 | + ]; |
| 90 | + in |
| 91 | + { |
| 92 | + devShells.default = pkgs.mkShell rec { |
| 93 | + nativeBuildInputs = [ pkgs.pkg-config ]; |
| 94 | + buildInputs = with pkgs; [ |
| 95 | + clang |
| 96 | + llvmPackages.bintools |
| 97 | + # shouldn't need this, but rustup (andt thus cargo), is too old for 2024 edition requirements: |
| 98 | + # https://github.com/NixOS/nixpkgs/pull/397177 |
| 99 | + cargo |
| 100 | + rustup |
| 101 | + |
| 102 | + vulkan-loader |
| 103 | + xorg.libX11 |
| 104 | + xorg.libXcursor |
| 105 | + xorg.libXi |
| 106 | + xorg.libXrandr |
| 107 | + libxkbcommon |
| 108 | + ] ++ platformPackages; |
| 109 | + |
| 110 | + RUSTC_VERSION = overrides.toolchain.channel; |
| 111 | + RUSTC_COMPONENTS = nixpkgs.lib.escapeShellArgs (nixpkgs.lib.concatMap (c: ["-c" c]) overrides.toolchain.components); |
| 112 | + LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ]; |
| 113 | + FORCED = forced; |
| 114 | + shellHook = '' |
| 115 | + rustup toolchain install $RUSTC_VERSION $RUSTC_COMPONENTS |
| 116 | + export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin |
| 117 | + export PATH=''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/:$PATH |
| 118 | + ${platformShellHook} |
| 119 | + ''; |
| 120 | + |
| 121 | + # Add precompiled library to rustc search path |
| 122 | + RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') [ |
| 123 | + # add libraries here (e.g. pkgs.libvmi) |
| 124 | + ]); |
| 125 | + |
| 126 | + LD_LIBRARY_PATH = |
| 127 | + (pkgs.lib.makeLibraryPath (buildInputs ++ nativeBuildInputs)) + |
| 128 | + # packages with non-standard lib paths: |
| 129 | + (if isLinux then ":${pkgs.alsa-plugins}/lib/alsa-lib" else ""); |
| 130 | + |
| 131 | + # Add clang and other headers to bindgen search path |
| 132 | + BINDGEN_EXTRA_CLANG_ARGS = |
| 133 | + (builtins.map (a: ''-I"${a}/include"'') platformBindgenInputs) |
| 134 | + ++ commonBindgenIncludes |
| 135 | + ++ platformBindgenIncludes; |
| 136 | + }; |
| 137 | + } |
| 138 | + ); |
| 139 | +} |
0 commit comments