From 3ce5eabf2665f6beb146915d21e47d7b5fb24824 Mon Sep 17 00:00:00 2001 From: e1turin Date: Wed, 14 Oct 2026 02:25:37 +0300 Subject: [PATCH] add nix setup --- README.md | 54 +++++++++++++++++++++++++++++++++++++++++---- flake.lock | 24 ++++++++++++++++++++ flake.nix | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/README.md b/README.md index 0ec06d4..76665e3 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,57 @@ architecture-specific assumptions. from analysis). - `reports/` — research findings and comparative analysis. -## Prerequisites +## Development environment -Install [Bazelisk](https://github.com/bazelbuild/bazelisk). It selects the -Bazel version pinned in `.bazelversion`. Commands are run from the repository -root. Optimized mode is enabled by default in `.bazelrc`. +Nix support is optional. The Bazel project and its configuration do not depend +on Nix, so use whichever setup suits your platform and workflow. Commands are +run from the repository root. Optimized mode is enabled by default in +`.bazelrc`. + +### With Nix + +Install [Nix](https://nixos.org/download/) with flakes enabled, then enter the +pinned development shell: + +```bash +nix develop +bazel build //... +bazel test //... +``` + +The shell supports `x86_64-linux`, `x86_64-darwin` (Intel macOS), and +`aarch64-darwin` (Apple Silicon). It provides Bazelisk, Clang/LLVM, +`clangd`, `clang-format`, `clang-tidy`, Buildifier, and common build tools. +The `bazel` command in this shell is a wrapper around Bazelisk that passes +`--repo_env=CC=clang --repo_env=CXX=clang++`. This makes Bazel's local C++ +toolchain configuration select the Nix-provided Clang; exported `CC` and +`CXX` alone are not sufficient to guarantee that Bazel does so. + +To enter the shell automatically when changing into the repository, optionally +use [direnv](https://direnv.net/). After enabling direnv in your shell, create +a local `.envrc` containing `use flake` and run `direnv allow`. `.envrc` is a +local convenience file and should not be committed unless the project chooses +to standardize it. + +### Without Nix + +Install [Bazelisk](https://github.com/bazelbuild/bazelisk), which selects the +Bazel version pinned in `.bazelversion`. Also install a C++17-capable compiler +(such as Clang or GCC); install Clang/LLVM tools including `clangd`, +`clang-format`, and `clang-tidy` for editor tooling and linting; and install +[Buildifier](https://github.com/bazelbuild/buildtools) when formatting Bazel +files. Then use the same commands: + +```bash +bazel build //... +bazel test //... +``` + +Nix improves the developer experience without replacing this path: pinned tool +versions provide reproducible development environments, easier onboarding, and +controlled toolchain updates. It does not make results identical everywhere: +platform-specific toolchains, CPU architectures, and benchmark measurements +may still differ between machines. ## C++ editor tooling diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c14b99c --- /dev/null +++ b/flake.lock @@ -0,0 +1,24 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1790299684, + "narHash": "sha256-iK8TZqVFap2LJEfmSr4VcV5jZ1DWqEyJEh0QerRX6JE=", + "rev": "7d5589bbf421c7b6f4185371abe3c465b1b557e9", + "type": "tarball", + "url": "https://releases.nixos.org/nixpkgs/nixpkgs-26.11pre1079252.7d5589bbf421/nixexprs.tar.zst" + }, + "original": { + "type": "tarball", + "url": "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..cbfd392 --- /dev/null +++ b/flake.nix @@ -0,0 +1,65 @@ +{ + description = "Optional reproducible development environment for memcopy-bench"; + + inputs.nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst"; + + outputs = { nixpkgs, ... }: + let + supportedSystems = [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + forAllSystems = f: + nixpkgs.lib.genAttrs supportedSystems (system: f (import nixpkgs { inherit system; })); + in + { + devShells = forAllSystems (pkgs: + let + # Bazel's local C++ toolchain is configured by a repository rule. Pass + # the compiler through --repo_env so configuration is tracked and does + # not rely solely on CC/CXX inherited from the development shell. + bazel = pkgs.writeShellApplication { + name = "bazel"; + runtimeInputs = [ pkgs.bazelisk ]; + text = '' + args=() + compiler_env_added=false + + for arg in "$@"; do + args+=("$arg") + if ! "$compiler_env_added"; then + case "$arg" in + aquery|build|coverage|cquery|fetch|query|run|sync|test) + args+=(--repo_env=CC=clang --repo_env=CXX=clang++) + compiler_env_added=true + ;; + esac + fi + done + + exec bazelisk "''${args[@]}" + ''; + }; + in + { + default = pkgs.mkShell { + packages = [ + bazel + pkgs.buildifier + pkgs.clang-tools + pkgs.llvmPackages.clang + pkgs.git + pkgs.gnumake + ]; + + # These also make editor tools and commands run outside Bazel select + # the pinned Clang. The bazel wrapper above handles Bazel itself. + CC = "clang"; + CXX = "clang++"; + }; + }); + + formatter = forAllSystems (pkgs: pkgs.nixfmt-rfc-style); + }; +}