Skip to content

add flake.nix #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ There is a [post about how to set up the iOS release workflow][workflow_bevy_ios
If you don't want to target Android or iOS, you can just delete the `/mobile`, `/build/android`, and `/build/ios` directories.
Then delete the `[workspace]` section from `Cargo.toml`.

# Development environments

## Nix Support

nixgl is only used on non-NixOS Linux systems;
when running there we need to use the `--impure` flag:

```
nix develop --impure
```

If using nixgl, then .e.g. `gl cargo run`, other use
`cargo` as usual.

# Getting started with Bevy

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.
Expand Down
133 changes: 133 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
description = "Bevy Game development environment";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
# nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
nixgl = {
url = "github:guibou/nixGL";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, nixgl }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgsOrig = import nixpkgs {inherit system; };
forced = builtins.trace "Current system is: ${system}" null;
isLinux = (builtins.match ".*linux.*" system) != null;
isDarwin = (builtins.match ".*darwin.*" system) != null;
isImpure = builtins.hasAttr "currentTime" builtins;

pkgs = import nixpkgs {
inherit system;
overlays = if (isLinux && isImpure)
then [ nixgl.overlays.default ]
else [ ];
};

linuxPackages = with pkgs; [
alsa-lib
alsa-plugins
udev
];
linuxImpurePackages = with pkgs; [
pkgs.nixgl.auto.nixGLDefault
];

sdkVersion = "15";
appleSDK = pkgs."apple-sdk_${sdkVersion}";
appleLibs = if isDarwin then ([
appleSDK
]) else [];

# Platform-specific packages
platformPackages =
(if (isLinux && !isImpure) then
linuxPackages
else if isLinux then
linuxPackages ++ linuxImpurePackages
else if isDarwin then
appleLibs
else
[ ]);

# Common bindgen configuration
commonBindgenIncludes = with pkgs; [
''-I"${llvmPackages_latest.libclang.lib}/lib/clang/${llvmPackages_latest.libclang.version}/include"''
];

# Platform-specific bindgen configuration
platformBindgenInputs = if isLinux then
with pkgs; [
glibc.dev
]
else [];

platformBindgenIncludes = if isLinux then
with pkgs; [
''-I"${glib.dev}/include/glib-2.0"''
''-I${glib.out}/lib/glib-2.0/include/''
]
else [];

overrides = (builtins.fromTOML (builtins.readFile (self + "/rust-toolchain.toml")));

# Platform-specific shell hook
platformShellHook = if isLinux then ''
alias gl='nixGL'
export ALSA_PLUGIN_DIR=${pkgs.alsa-plugins}/lib/alsa-lib
''
else
"";

libPath = with pkgs; lib.makeLibraryPath [
# load external libraries that you need in your rust project here
];
in
{
devShells.default = pkgs.mkShell rec {
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = with pkgs; [
clang
llvmPackages.bintools
rustup

vulkan-loader
xorg.libX11
xorg.libXcursor
xorg.libXi
xorg.libXrandr
libxkbcommon
] ++ platformPackages;

RUSTC_VERSION = overrides.toolchain.channel;

LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
FORCED = forced;
shellHook = ''
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
${platformShellHook}
'';

# Add precompiled library to rustc search path
RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') [
# add libraries here (e.g. pkgs.libvmi)
]);

LD_LIBRARY_PATH =
(pkgs.lib.makeLibraryPath (buildInputs ++ nativeBuildInputs)) +
# packages with non-standard lib paths:
(if isLinux then ":${pkgs.alsa-plugins}/lib/alsa-lib" else "");

# Add clang and other headers to bindgen search path
BINDGEN_EXTRA_CLANG_ARGS =
(builtins.map (a: ''-I"${a}/include"'') platformBindgenInputs)
++ commonBindgenIncludes
++ platformBindgenIncludes;
};
}
);
}