Skip to content

Rust: import functionality #1946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Rust: import functionality #1946

wants to merge 6 commits into from

Conversation

domenkozar
Copy link
Member

@domenkozar domenkozar commented Jun 23, 2025

Allow packaging Rust projects using cargo2nix.

{ pkgs, lib, config, ... }: {

  languages.rust.enable = true;
  languages.rust.import = {
    mypackage = {
      root = ./.;
    };
  };

  packages = languages.rust.import.mypackage.packages;

  outputs.mypackage = config.languages.rust.import.mypackage.packages;
}

Depends on #1500

Copy link
Contributor

claude bot commented Jun 23, 2025

Claude finished @domenkozar's task —— View job


Code Review Progress

Reviewing PR #1946: Rust cargo2nix import functionality

Tasks:

  • Read and analyze core implementation in src/modules/languages/rust.nix
  • Review test coverage and test files in tests/rust/
  • Check documentation updates
  • Evaluate code quality and best practices
  • Look for potential bugs and issues
  • Consider performance implications
  • Check for security concerns
  • Provide comprehensive review feedback

Review Feedback

This PR adds cargo2nix integration to allow importing Rust projects for granular builds. Overall, this is a well-implemented feature with good test coverage. Here's my detailed analysis:

Strengths

  1. Well-structured API Design: The languages.rust.import configuration is intuitive and flexible, supporting both full workspaces and individual workspace members.

  2. Comprehensive Test Coverage: The test setup in tests/rust/ is excellent - it includes a complete Rust project with generated Cargo.nix, proper integration tests, and verification that both traditional cargo builds and cargo2nix imports work.

  3. Error Handling: Good assertions and error messages, particularly:

    • rust.nix:172-173: Clear assertion for missing Cargo.toml
    • rust.nix:174-175: Helpful error directing users to run the cargo2nix task
  4. Task Integration: Smart approach to generate cargo2nix:${name} tasks automatically for each import (lines 309-319)

⚠️ Issues & Concerns

  1. Documentation Gap: The documentation in docs/supported-languages/rust.md doesn't include the new import functionality. Users won't know this feature exists.

  2. Rust Version Consistency Logic: In rust.nix:162-168, the version mapping logic is potentially confusing:

    rustVersion = if cfg.channel != "nixpkgs" then cfg.version else "latest";
    rustChannel = cfg.channel;
    # ...
    rustVersion = if rustChannel == "nixpkgs" then "latest" else rustVersion;
    rustChannel = if rustChannel == "nixpkgs" then "stable" else rustChannel;

    This double-transformation could be simplified and made more explicit.

  3. Build-time Assertions: The assert on line 172 uses builtins.pathExists which is evaluated at build time. This is correct, but the error could be more user-friendly by suggesting the exact command to fix it.

  4. Overlay Application: Line 306 applies the cargo2nix overlay unconditionally when imports are used. Consider if this could conflict with user-defined overlays.

🔍 Minor Issues

  1. Unused Variables: In rust.nix:160, cargoTomlExists is calculated but only used in the assertion. Consider removing if not needed elsewhere.

  2. Code Organization: The import logic (lines 133-206) is quite dense. Consider extracting the package derivation logic into a separate function.

  3. Test Specificity: The test in devenv.nix:18-23 checks that RUSTFLAGS is unset when mold is disabled, but this seems unrelated to the cargo2nix functionality being added.

🛡️ Security Considerations

  1. Path Handling: The code correctly uses toString for path interpolation in error messages, preventing injection issues.

  2. Input Validation: Good use of Nix's type system to validate configuration inputs.

  3. Dependency Management: cargo2nix input is properly constrained with follows = [ "nixpkgs" ].

🚀 Performance Considerations

  1. Lazy Evaluation: Good use of readOnly option and lazy evaluation for the package derivation.

  2. Build Efficiency: cargo2nix approach allows for better incremental builds compared to standard Rust builds.

📋 Recommendations

  1. Add Documentation: Update docs/supported-languages/rust.md to document the new import option with examples.

  2. Simplify Version Logic: Refactor the version/channel mapping logic for clarity.

  3. Consider Edge Cases:

    • What happens if Cargo.lock is missing?
    • How does this interact with existing RUSTFLAGS from mold or user settings?
  4. Test Enhancement: Add a test case that verifies the task generation works correctly.

🏁 Conclusion

This is a solid implementation that adds valuable functionality. The main concern is the missing documentation, which should be addressed before merging. The code quality is good with appropriate error handling and test coverage.

Recommendation: Approve with minor changes (primarily documentation update).


This adds support for importing Rust projects using cargo2nix, enabling granular
builds and better caching. Users can now import Rust projects and their
dependencies as Nix packages using the new `languages.rust.import` option.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

Deploying devenv with  Cloudflare Pages  Cloudflare Pages

Latest commit: ccf572a
Status: ✅  Deploy successful!
Preview URL: https://279ac28a.devenv.pages.dev
Branch Preview URL: https://cargo2nix.devenv.pages.dev

View logs

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.

2 participants