nix-community / nix-unit

Unit testing for Nix code [maintainer=@adisbladis]
https://nix-community.github.io/nix-unit/
GNU General Public License v3.0
61 stars 7 forks source link

[Question] Cannot get `nix-unit` working with `flake-parts`. #113

Closed Industrial closed 2 hours ago

Industrial commented 1 month ago

Hi.

I'm trying to implement nix-unit: https://nix-community.github.io/nix-unit/examples/flakes.html

When I add it to my nix flake check then I get an error about it not being able to download things.

The page specifically says # The nix derivation must be able to find all used inputs in the nix-store because it cannot download it during buildTime. and that's happening so I don't know what I did wrong :S

This is where I'm using the check in my flake (using flake-parts): https://github.com/Industrial/nixos-dotfiles/blob/main/flake.nix#L98

This is the check: https://github.com/Industrial/nixos-dotfiles/blob/main/checks/tests.nix

Hmm copy pasting the example into a flake works. It must be my setup that's the problem.

Industrial commented 1 month ago

This is the minimal reproduction:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    flake-parts.url = "github:hercules-ci/flake-parts";
    nix-unit.inputs.nixpkgs.follows = "nixpkgs";
    nix-unit.url = "github:nix-community/nix-unit";
  };
  outputs = inputs @ {self, ...}:
    inputs.flake-parts.lib.mkFlake {inherit inputs;} {
      systems = inputs.nixpkgs.lib.systems.flakeExposed;
      flake = {
        tests.testPass = {
          expr = 3;
          expected = 4;
        };
      };
      perSystem = {
        system,
        pkgs,
        ...
      }: {
        checks = {
          tests =
            pkgs.runCommand "tests"
            {
              nativeBuildInputs = [
                inputs.nix-unit.packages.${system}.default
              ];
            } ''
              export HOME="$(realpath .)"
              # The nix derivation must be able to find all used inputs in the nix-store
              # because it cannot download it during buildTime.
              nix-unit --eval-store "$HOME" \
              --extra-experimental-features flakes \
              --override-input nixpkgs ${inputs.nixpkgs} \
              --flake ${self}#tests
              touch $out
            '';
        };
      };
    };
}
Industrial commented 1 month ago

I caved in and removed flake-parts.

Then I found that I can just run the unit tests pre-push with https://github.com/cachix/git-hooks.nix (already had that installed). Works perfectly :D