Skip to content

Conversation

@CupOfTeaJay
Copy link

@CupOfTeaJay CupOfTeaJay commented Aug 2, 2025

I'm definitely not the best with Nix and am especially new to writing flakes of my own, but I gave a shot at writing one for the Bevy CLI as I use NixOS as a daily driver. I was able to get my Bevy app running in my browser by adding the following to the Nix flake in Bevy's docs:

{
  description = "bevy flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
    flake-utils.url = "github:numtide/flake-utils";
+   bevy_cli.url = "github:TheBevyFlock/bevy_cli";
  };

  outputs =
    {
      nixpkgs,
      rust-overlay,
      flake-utils,
+     bevy_cli,
      ...
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        overlays = [ (import rust-overlay) ];
        pkgs = import nixpkgs {
          inherit system overlays;
        };
      in
      {
        devShells.default =
          with pkgs;
          mkShell {
            buildInputs =
              [
                # Rust dependencies
-                 (rust-bin.stable.latest.default.override { extensions = [ "rust-src" ]; })
+                (
+                  rust-bin.stable.latest.default.override { 
+                    extensions = [ "rust-src" ]; 
+                    targets = [ "wasm32-unknown-unknown" ];
+                  }
+                )
                pkg-config
              ]
              ++ lib.optionals (lib.strings.hasInfix "linux" system) [
                # for Linux
                # Audio (Linux only)
                alsa-lib
                # Cross Platform 3D Graphics API
                vulkan-loader
                # For debugging around vulkan
                vulkan-tools
                # Other dependencies
                libudev-zero
                xorg.libX11
                xorg.libXcursor
                xorg.libXi
                xorg.libXrandr
                libxkbcommon
+               wasm-bindgen-cli
              ];
            RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
            LD_LIBRARY_PATH = lib.makeLibraryPath [
              vulkan-loader
              xorg.libX11
              xorg.libXi
              xorg.libXcursor
              libxkbcommon
            ];
          };
      }
    );
}

I wasn't able to get the linter to work. I'm not quite sure how to deal with it in a declarative way since it requires rustup at runtime. Was hoping someone more experienced with Nix might be able to provide a solution here. Regardless I think this flake should be a good starting point.

@BD103 BD103 added A-CLI Related to the main CLI and not a more specific subcommand C-Usability An improvement that makes the API more pleasant S-Needs-Review The PR needs to be reviewed before it can be merged labels Aug 6, 2025
@BD103
Copy link
Member

BD103 commented Aug 7, 2025

Hi, thanks for the PR! I'm not comfortable reviewing this PR yet, as I still am too unfamiliar with Nix (and none of the other maintainers use it I believe). At some point I do want to support this, but I don't want to merge it without understanding the technology behind it.

In the meantime, feel free to use your fork as an unofficial Nix Flake! You may also be interested in #390, which tried to do this same thing in the past. I no longer agree with what I said in #390 (comment), and now believe that Nix Flakes are the way to go, but it may be a while before we can merge this. Sorry!

@CupOfTeaJay
Copy link
Author

@BD103 I understand, thanks for the response.

Would you consider pulling in these changes on a feature branch, as opposed to main? That way others might benefit from the flake while main is kept intact, until you are comfortable with the feature and opt to merge it in. Or, if you ultimately decide against it, just drop the branch altogether.

I would also be interested in making this a nixpkgs package in addition to the flake. I have some prior experience with that and would be happy to add that as another way for those who are anti-flake to still use the CLI in a declarative manner.

@BD103
Copy link
Member

BD103 commented Aug 11, 2025

Would you consider pulling in these changes on a feature branch, as opposed to main? That way others might benefit from the flake while main is kept intact, until you are comfortable with the feature and opt to merge it in. Or, if you ultimately decide against it, just drop the branch altogether.

I gave this some thought, but I don't think I'm comfortable doing this yet. Anything on the main repository has some level of "officialness", even if it's on a separate branch. I don't want to give code that I don't understand any sort of (arguably small) endorsement like that.

Given, I'm just one maintainer. If one of the others tests and approves this PR, then I have no complaints! I don't have anything against NixOS, it's just not at the top of my todo list yet :)

@TimJentzsch
Copy link
Collaborator

I also have no nix experience unfortunately, but it seems that there is enough interest in this to warrant pulling this in.
@CupOfTeaJay would you mind finding other nix users in the Bevy community (e.g. on Discord) to review this PR?
That would make me more comfortable merging this in.

I'd also like instructions on how to use this in the README, along with a warning that this is supported on a best-effort basis and might break at any time :)

@TimJentzsch TimJentzsch added the X-Controversial There is active debate or serious implications around merging this PR label Aug 12, 2025
@DaAlbrecht
Copy link
Collaborator

I would love an addition to nixpkgs I think that would help nix users a lot already!

@CupOfTeaJay
Copy link
Author

@TimJentzsch Absolutely, I'll get in touch with some folks in the Discord.
@DaAlbrecht I'll give this a shot too!

@BD103 BD103 moved this to Backburner in BD103 Work Planning Sep 8, 2025
@OleStrohm
Copy link
Contributor

OleStrohm commented Oct 10, 2025

I just cherry-picked this to my local fork so that I could test a change, so this both works, and is valuable for contributors using Nix.

The benefit of being able to use it as a direct flake input until it's accepted in nixpkgs is also valuable, as that PR has gone stale.

As long as there aren't test that are affected by it there should be no way this can affect other users, and since nix projects depending on the repo will use a specific sha there's no issue if it does go out of date.

I therefore propose we merge this.

I only had one issue when using it, and I've added a comment for that

@DaAlbrecht
Copy link
Collaborator

DaAlbrecht commented Oct 12, 2025

I'm open to merge this after resolving the outstanding issues.

I'd also like instructions on how to use this in the README, along with a warning that this is supported on a best-effort basis and might break at any time :)

I like the disclaimer (and a good description of what the flake provides, you mentioned it does not support the linter as of now), but I wonder if the README is the best place for this. Is the flake for contributors using nix or for nix users who want to use the CLI / linter?. If it's the first, I think I would like a section under the contributor guide more, if it's the latter, then yea I think the README is fine.

@DaAlbrecht DaAlbrecht added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review The PR needs to be reviewed before it can be merged labels Oct 12, 2025
Copy link
Collaborator

@DaAlbrecht DaAlbrecht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, could you also add the required user documentation with the disclaimer (what @TimJentzsch ) mentioned.

Copy link

@musjj musjj Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I've tested out your PR and you just need to make a few changes to make bevy lint work:

diff --git a/tmp/.psub.oO4pntvWFW b/flake.nix
index 980c6ba..cadee16 100644
--- a/tmp/.psub.oO4pntvWFW
+++ b/flake.nix
@@ -4,13 +4,17 @@
   inputs = {
     flake-utils.url = "github:numtide/flake-utils";
     nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
+    rust-overlay = {
+      url = "github:oxalica/rust-overlay";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
   };
 
-  outputs = { self, flake-utils, nixpkgs }:
+  outputs = { self, flake-utils, nixpkgs, rust-overlay }:
     flake-utils.lib.eachDefaultSystem (
       system:
       let
-        pkgs = nixpkgs.legacyPackages.${system};
+        pkgs = nixpkgs.legacyPackages.${system}.extend rust-overlay.overlays.default;
 
         nativeBuildInputs = with pkgs; [
           pkg-config
@@ -19,17 +23,34 @@
         buildInputs = with pkgs; [
           openssl
         ];
+
+        toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
+
+        rustPlatform = pkgs.makeRustPlatform {
+          cargo = toolchain;
+          rustc = toolchain;
+        };
       in
       {
-        packages.default = pkgs.rustPlatform.buildRustPackage {
+        packages.default = rustPlatform.buildRustPackage {
           pname = "bevy";
           version = "0.1.0-dev";
           src = ./.;
-          cargoLock = {
-            lockFile = ./Cargo.lock;
-          };
+          cargoLock.lockFile = ./Cargo.lock;
+          cargoBuildFlags = [ "--all" ];
           doCheck = false;
+
+          nativeBuildInputs = nativeBuildInputs ++ (with pkgs; [ makeBinaryWrapper ]);
-          inherit buildInputs nativeBuildInputs;
+          inherit buildInputs;
+
+          postInstall = ''
+            for bin in $out/bin/bevy{,_lint}; do
+              wrapProgram $bin --set BEVY_LINT_SYSROOT ${toolchain}
+            done
+          '';
         };
 
         devShells.default = pkgs.mkShell {

A few important points:

  1. Nightly toolchain
    bevy_lint requires the nightly compiler, so we need to pull in a community-maintained overlay since nixpkgs doesn't provide one. The rust-toolchain.toml from the repo is used here, so we'll never go out-of-sync. See the official manual for details.
  2. Sysroot location
    We can't expect rustup to be available on NixOS systems, so we need to point BEVY_LINT_SYSROOT to the same nightly toolchain used to build the linter.
  3. cargoBuildFlags = [ "--all" ];
    This just builds all the packages in the workspace so that we get both bevy and bevy_lint. Is it worth it to be more specific?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does setting BEVY_LINT_SYSROOT for the bevy cli (not the bevy_lint cli) do anything, or should it be set for only bevy_cli?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does setting BEVY_LINT_SYSROOT for the bevy cli (not the bevy_lint cli) do anything, or should it be set for only bevy_cli?

The Bevy CLI doesn't read BEVY_LINT_SYSROOT, but if you run the linter through the CLI (such as with bevy lint web, for example), you'll still want BEVY_LINT_SYSROOT to be set. Check out the docs on environmental variables for more details. :)

];
in
{
packages.default = pkgs.rustPlatform.buildRustPackage {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but packages are usually structured something like this:

packages = {
  default = self.outputs.packages.${system}.bevy;
  bevy = ...;
}

@BD103 BD103 added X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers and removed X-Controversial There is active debate or serious implications around merging this PR labels Oct 17, 2025
@BD103
Copy link
Member

BD103 commented Oct 17, 2025

I'm marking this as blessed! It was originally controversial, but I think this is the direction we want to take in regard to the Nix ecosystem.

@CupOfTeaJay
Copy link
Author

Thank you, could you also add the required user documentation with the disclaimer (what @TimJentzsch ) mentioned.

Absolutely, just returned from travel. I will work on it this weekend!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CLI Related to the main CLI and not a more specific subcommand C-Usability An improvement that makes the API more pleasant S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add nix flake to allow Nix users to easily use bevy_cli

6 participants