Skip to content

Commit 0570682

Browse files
authored
Merge pull request #127 from bbarker/nix_flake
add flake.nix
2 parents e7c1abe + 95da252 commit 0570682

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ There is a [post about how to set up the iOS release workflow][workflow_bevy_ios
7676
If you don't want to target Android or iOS, you can just delete the `/mobile`, `/build/android`, and `/build/ios` directories.
7777
Then delete the `[workspace]` section from `Cargo.toml`.
7878

79+
# Development environments
80+
81+
## Nix Support
82+
83+
nixgl is only used on non-NixOS Linux systems;
84+
when running there we need to use the `--impure` flag:
85+
86+
```
87+
nix develop --impure
88+
```
89+
90+
If using nixgl, then .e.g. `gl cargo run`, other use
91+
`cargo` as usual.
92+
7993
# Getting started with Bevy
8094

8195
You should check out the Bevy website for [links to resources][bevy-learn] and the [Bevy Cheat Book] for a bunch of helpful documentation and examples. I can also recommend the [official Bevy Discord server][bevy-discord] for keeping up to date with the development and getting help from other Bevy users.

flake.lock

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
}

nix/rust-toolchain.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "1.87.0"
3+
components = ["rustfmt", "clippy", "rust-analyzer"]

0 commit comments

Comments
 (0)