From 662dde1cccf9c46680b02fb9517626ae16b186c1 Mon Sep 17 00:00:00 2001 From: Allan Douglas Date: Fri, 9 Jan 2026 22:08:59 +0000 Subject: [PATCH] chore: add flake.nix --- flake.lock | 48 ++++++++++++++++++++++++++++++ flake.nix | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..ed37e096 --- /dev/null +++ b/flake.lock @@ -0,0 +1,48 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1767892417, + "narHash": "sha256-dhhvQY67aboBk8b0/u0XB6vwHdgbROZT3fJAjyNh5Ww=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3497aa5c9457a9d88d71fa93a4a8368816fbeeba", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1767926800, + "narHash": "sha256-x0n73J6ufD/EhDlVdcoAmF0OQHZ+b0a2cKDc8RZyt+o=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "499e9eed88ff9494b6604205b42847e847dfeb91", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..dd5efbb9 --- /dev/null +++ b/flake.nix @@ -0,0 +1,87 @@ +{ + description = "ROS 2 Rust client library (rclrs)"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, nixpkgs, rust-overlay, ... }: + let + supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + + pkgsFor = system: import nixpkgs { + inherit system; + overlays = [ rust-overlay.overlays.default ]; + }; + in + { + devShells = forAllSystems (system: + let + pkgs = pkgsFor system; + + rustToolchain = pkgs.rust-bin.stable.latest.default.override { + extensions = [ "rust-src" "rust-analyzer" "clippy" ]; + }; + in + { + default = let + # Wrapper script that adds --features use_ros_shim to cargo commands + cargoWrapper = pkgs.writeShellScriptBin "cargo" '' + args=("$@") + cmd="''${args[0]:-}" + + case "$cmd" in + build|test|doc|check|clippy|run) + exec ${rustToolchain}/bin/cargo "$@" --features use_ros_shim + ;; + *) + exec ${rustToolchain}/bin/cargo "$@" + ;; + esac + ''; + in pkgs.mkShell { + name = "ros2-rust"; + + nativeBuildInputs = [ + cargoWrapper + rustToolchain + pkgs.pkg-config + pkgs.llvmPackages.libclang + ]; + + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + RUSTFLAGS = "--cfg ros_distro=\"humble\""; + + shellHook = '' + echo "═══════════════════════════════════════════════════════" + echo " ROS 2 Rust Development Environment" + echo "═══════════════════════════════════════════════════════" + echo " Rust: $(${rustToolchain}/bin/rustc --version)" + echo "" + echo " cargo build/test/doc automatically use --features use_ros_shim" + echo "═══════════════════════════════════════════════════════" + ''; + }; + }); + + checks = forAllSystems (system: + let + pkgs = pkgsFor system; + in + { + fmt = pkgs.runCommand "check-fmt" { + nativeBuildInputs = [ pkgs.rust-bin.stable.latest.default ]; + src = self; + } '' + cd $src + cargo fmt -- --check + touch $out + ''; + }); + }; +}