Skip to content

Add Nix flake development shell#617

Open
bresilla wants to merge 2 commits intoros2-rust:mainfrom
bresilla:nixos_overlay
Open

Add Nix flake development shell#617
bresilla wants to merge 2 commits intoros2-rust:mainfrom
bresilla:nixos_overlay

Conversation

@bresilla
Copy link
Copy Markdown

Summary

  • add a Nix flake that provides a reproducible Rust + ROS 2 development shell for this workspace
  • include the generated flake.lock so the flake inputs are pinned and repeatable
  • configure the shell around ROS 2 Jazzy packages plus the Rust toolchain needed to build rclrs

Notes

  • the flake is currently set up for ROS 2 Jazzy
  • adapting it to another ROS distro should mostly be a small change in the distro selection, for example swapping jazzy to humble and updating the corresponding package set as needed

Verification

  • nix develop --impure -c cargo build

Copilot AI review requested due to automatic review settings March 26, 2026 14:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a Nix flake to provide a reproducible Rust + ROS 2 (Jazzy) development shell for this workspace, with pinned inputs via flake.lock.

Changes:

  • Introduces flake.nix defining a devShell with Rust toolchain, ROS 2 Jazzy packages, and environment setup hooks.
  • Adds flake.lock to pin nixpkgs, nix-ros-overlay, rust-overlay, and flake-utils for repeatable environments.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 7 comments.

File Description
flake.nix Defines the Nix flake dev shell, ROS package environment, and shell hooks for building rclrs against ROS 2 Jazzy.
flake.lock Pins flake inputs to specific revisions/hashes for reproducible builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

flake.nix Outdated
Comment on lines +155 to +161
#!/usr/bin/env sh
SETUPEOF
cat > "$synthetic_prefix/local_setup.bash" <<'SETUPEOF'
#!/usr/bin/env bash
SETUPEOF
cat > "$synthetic_prefix/local_setup.zsh" <<'SETUPEOF'
#!/usr/bin/env zsh
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The heredoc body that writes local_setup.* is indented, so the #! line won’t start at byte 1 of the file. If these scripts are executed directly, they may fail with an exec format error. Write the heredoc contents without leading indentation so the shebang is valid.

Suggested change
#!/usr/bin/env sh
SETUPEOF
cat > "$synthetic_prefix/local_setup.bash" <<'SETUPEOF'
#!/usr/bin/env bash
SETUPEOF
cat > "$synthetic_prefix/local_setup.zsh" <<'SETUPEOF'
#!/usr/bin/env zsh
#!/usr/bin/env sh
SETUPEOF
cat > "$synthetic_prefix/local_setup.bash" <<'SETUPEOF'
#!/usr/bin/env bash
SETUPEOF
cat > "$synthetic_prefix/local_setup.zsh" <<'SETUPEOF'
#!/usr/bin/env zsh

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

flake.nix Outdated
Comment on lines +150 to +151

synthetic_prefix="$PWD/.nix-ament-prefix"
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

synthetic_prefix is created under $PWD (.nix-ament-prefix), which will modify the working tree and can leave untracked files in the repo. Consider placing this under a cache/temp location (e.g., $XDG_CACHE_HOME or $TMPDIR) to avoid polluting the workspace.

Suggested change
synthetic_prefix="$PWD/.nix-ament-prefix"
local base_dir
if [ -n "${XDG_CACHE_HOME:-}" ]; then
base_dir="$XDG_CACHE_HOME"
elif [ -n "${TMPDIR:-}" ]; then
base_dir="$TMPDIR"
else
base_dir="/tmp"
fi
synthetic_prefix="$base_dir/nix-ament-prefix"

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

flake.nix Outdated
description = "ros flake";

inputs = {
nix-ros-overlay.url = "github:lopsided98/nix-ros-overlay/master";
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The input URL github:lopsided98/nix-ros-overlay/master bakes the branch name into the flake. Since flake.lock already pins the exact revision, consider using github:lopsided98/nix-ros-overlay (or an explicit tag/rev) to avoid breakage if the default branch name changes.

Suggested change
nix-ros-overlay.url = "github:lopsided98/nix-ros-overlay/master";
nix-ros-overlay.url = "github:lopsided98/nix-ros-overlay";

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

flake.nix Outdated

outputs =
{ self, nixpkgs, rust-overlay, flake-utils, nix-ros-overlay, ... }:
flake-utils.lib.eachDefaultSystem (
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

flake-utils.lib.eachDefaultSystem will create outputs for macOS as well as Linux. Since the devShell is ROS 2–based (and most ROS Nix overlays are Linux-only), consider restricting to Linux systems explicitly (e.g., eachSystem [ "x86_64-linux" "aarch64-linux" ]) to avoid flake evaluation failures on unsupported platforms.

Suggested change
flake-utils.lib.eachDefaultSystem (
flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ] (

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@esteve esteve Mar 26, 2026

Choose a reason for hiding this comment

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

Should this be narrowed down to Linux and some specific architectures?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes. In eb61925 I narrowed the flake outputs to Linux only: x86_64-linux and aarch64-linux.

flake.nix Outdated
Comment on lines +58 to +60
export CC=clang
export CXX=clang++
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER is hard-coded to the x86_64 target triple. This will be incorrect on aarch64-linux (and any non-x86_64 system) and can cause Cargo to ignore the intended linker or fail builds. Consider either removing this override or setting the linker env var based on the current target triple.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

flake.nix Outdated
Comment on lines +51 to +53
];

RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

RUST_SRC_PATH is taken from pkgs.rust.packages.stable..., but this dev shell installs Rust via pkgs.rust-bin.stable.latest.... This can point to a different toolchain (or a missing src path) and break tools that rely on RUST_SRC_PATH. Prefer deriving RUST_SRC_PATH from the same rust-bin toolchain included in packages.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

flake.nix Outdated

pkgs = import nixpkgs {
inherit system overlays;
config.allowUnfree = true;
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

config.allowUnfree = true; enables unfree packages globally for this flake evaluation. If the dev shell doesn’t actually require unfree software, consider removing this or making it conditional to avoid surprising users/orgs with strict unfree policies.

Suggested change
config.allowUnfree = true;
config.allowUnfree = builtins.getEnv "ALLOW_UNFREE" == "1";

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Collaborator

@esteve esteve left a comment

Choose a reason for hiding this comment

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

@bresilla awesome work! I've been playing with Nix lately and this is super convenient.

pkgs.colcon
(with rosDistro; buildEnv {
paths = [
ros-core
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd reduce the list of packages to ros-core and ros-base, adding moveit and ackermann-msgs seems out of scope.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ups, yeah, that's just a copy form another project :/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Reduced the package set in eb61925 to the narrower ROS environment and removed the out-of-scope extras. The shell now builds around ros-core, ros-base, cyclonedds, rmw-cyclonedds-cpp, and test-msgs.

flake.nix Outdated

pkgs = import nixpkgs {
inherit system overlays;
config.allowUnfree = true;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No need to allow unfree packages.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed allowUnfree in eb61925.

flake.nix Outdated

outputs =
{ self, nixpkgs, rust-overlay, flake-utils, nix-ros-overlay, ... }:
flake-utils.lib.eachDefaultSystem (
Copy link
Copy Markdown
Collaborator

@esteve esteve Mar 26, 2026

Choose a reason for hiding this comment

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

Should this be narrowed down to Linux and some specific architectures?

@bresilla
Copy link
Copy Markdown
Author

@esteve all your comments are valid. ahah, sorry, i thought i removed all unnecessary things when copied from another project. Will update it this weekend with those changes :)

@bresilla
Copy link
Copy Markdown
Author

bresilla commented Mar 27, 2026

All addressed before the weekend could even arrive. :)

@bresilla bresilla requested a review from esteve March 27, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants