Skip to content

Bazel build inside nix flake fails: 'python3: No such file or directory #1601

@SpamDoodler

Description

@SpamDoodler

I trying to compile cpp to webassambly using bazel.
bazel build //:add_wasm
fails with:

...
env: 'python3': No such file or directory
...

flake.nix

{
  description = "Nix flake";
  
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };
  
  outputs = { self, nixpkgs, flake-utils }: 
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
      in
      {
        devShells.default = pkgs.mkShell {
          name = "wasm-dev";
          EM_CONFIG = pkgs.writeText ".emscripten" ''
            EMSCRIPTEN_ROOT = '${pkgs.emscripten}/share/emscripten'
            LLVM_ROOT = '${pkgs.emscripten.llvmEnv}/bin'
            BINARYEN_ROOT = '${pkgs.binaryen}'
            NODE_JS = '${pkgs.nodejs}/bin/node'
            CACHE = '${toString ./.cache}'
          '';
          buildInputs = with pkgs; [
            clang_21          
            lld_21             
            emscripten
            nodejs
            yarn
            bazel
            python313Full
            python313Packages.pip
          ];
          
          shellHook = ''
            export PATH=${pkgs.python3Full}/bin:$PATH
            export PYTHON=python3 
            export PYTHON_BIN_PATH=${pkgs.python3Full}/bin/python3

            # Initialize emscripten config if needed (first run)
            if [ ! -f "$HOME/.emscripten" ]; then
              echo "Initializing Emscripten configuration..."
              emcc --version > /dev/null 2>&1
            fi
          '';
        };
      }
    );

MODULE.bazel

bazel_version = "7.6.0"

module(
    name = "polyglot_wasm",
    version = "0.1.0",
)

bazel_dep(name = "emsdk", version = "4.0.13")
bazel_dep(name = "rules_rust", version = "0.64.0")
bazel_dep(name = "rules_python", version = "0.31.0")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    python_version = "3.13",
)
use_repo(python, "python_3_13")

git_override(
    module_name = "emsdk",
    remote = "https://github.com/emscripten-core/emsdk.git",
    strip_prefix = "bazel",
    commit = "eff90ca04a3785f571a8095b3a42b63799cf384a",
)

emscripten_deps = use_extension(
    "@emsdk//:emscripten_deps.bzl",
    "emscripten_deps",
)

BUILD

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

cc_binary(
    name = "add",
    srcs = ["hello.c"],
    linkopts = [
        "-s", "EXPORTED_FUNCTIONS=['_main']",
        "-s", "EXPORTED_RUNTIME_METHODS=['ccall','cwrap']",
    ],  
)

wasm_cc_binary(
    name = "add_wasm",
    cc_target = ":add",
)

.bazelrc

build --incompatible_enable_cc_toolchain_resolution
build --platforms=@emsdk//:platform_wasm
build --extra_toolchains=@emsdk//:all
$> which python3
/nix/store/0n3p6v273rh7g1ypbfc0vwlw1sw79y1p-python3-3.13.6/bin/python3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions