yusdacra / nix-cargo-integration

Library to easily and effortlessly integrate Cargo projects with Nix.
https://flake.parts/options/nix-cargo-integration.html
MIT License
182 stars 19 forks source link

Help running `cargo test` in checks. #145

Closed nejucomo closed 9 months ago

nejucomo commented 9 months ago

Hi, I'm fairly new to nixos and learning how to wrap my rust projects in flakes, so I'm trying to understand basic flakes, flake-parts, and nci.

When I run nix flake check --print-build-logs I see no output from cargo tests.

If I add checks.default = outputs."${default-crate}".check; then I do see the tests for that single crate execute with flake check. How can I ensure all tests for all crates in the cargo workspace are executed with nix flake check?

From this comment on #136, it sounds like the check output should be automatically added, but it doesn't seem so in my case. What am I missing?

I have this flake.nix:

{
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  inputs.nci.url = "github:yusdacra/nix-cargo-integration";
  inputs.nci.inputs.nixpkgs.follows = "nixpkgs";
  inputs.parts.url = "github:hercules-ci/flake-parts";
  inputs.parts.inputs.nixpkgs-lib.follows = "nixpkgs";

  outputs = inputs @ {
    parts,
    nci,
    ...
  }:
    let
      project = "my-project";
      default-crate = "my-main-crate";
    in
      parts.lib.mkFlake { inherit inputs; } {
        systems = ["x86_64-linux"];
        imports = [nci.flakeModule];
        perSystem = { config, pkgs, ... }:
          let
            pglib = import ./nix-pglib { nixpkgs = pkgs; };
            outputs = config.nci.outputs;
          in {
            nci.projects."${project}" = {
              path = ./.;
              export = true;
            };
            nci.crates = pglib.find-crate-configs ./.;

            devShells.default = outputs."${project}".devShell;
            packages.default = outputs."${default-crate}".packages.release;
          };
}

Tangential question: I wrote the function find-crate-configs parses the workspace Cargo.toml to populate nci.crates with "${crate-name} = {}; for each crate. Is there functionality already present in nci to automate this?

When I run nix show I see:

───devShells
│   └───x86_64-linux
│       ├───default: development environment 'my-project-devshell'
│       ├───my-project: development environment 'my-project-devshell'
│       ├───my-project-foo: development environment 'my-project-foo-devshell'
…
───packages
    └───x86_64-linux
        ├───default: package 'my-project-host'
        ├───my-project-foo-dev: package 'my-project-foo'
        ├───my-project-foo-release: package 'my-project-foo'
        ├───my-project-bar-dev: package 'my-project-bar'
        ├───my-project-bar-release: package 'my-project-bar'
…

It doesn't show checks, only devShells and packages.

yusdacra commented 9 months ago

From https://github.com/yusdacra/nix-cargo-integration/issues/136#issuecomment-1744735909 on https://github.com/yusdacra/nix-cargo-integration/issues/136, it sounds like the check output should be automatically added, but it doesn't seem so in my case. What am I missing?

The check output is not automatically exported to the flake outputs. I don't remember why I didn't do this before but I will make them be exported if export is set to true. Thanks for reporting.

Tangential question: I wrote the function find-crate-configs parses the workspace Cargo.toml to populate nci.crates with "${crate-name} = {}; for each crate. Is there functionality already present in nci to automate this?

I didn't implement anything like this because I didn't find it useful. Mainly because you'll likely want to configure the crates so you have to specify the crate name and configuration anyway (and most of the times you don't want everything to be exported, in my experience). But I can add an option for this, if it's proving useful.