Skip to content

Cannot run nix-unit in derivation build #183

@fricklerhandwerk

Description

@fricklerhandwerk

Problem

I want to run unit tests in CI, which is why I tried to wrap the invocation of nix-unit in runCommand. This doesn't work though, presumably because nix-unit tries to initialise its own Nix shenanigans but fails.

Reproducer

default.nix
{ sources ? import ./npins
, system ? builtins.currentSystem
, pkgs ? import sources.nixpkgs {
    inherit system;
    config = { };
    overlays = [ ];
  }
}:
rec {
  run-tests = pkgs.writeShellApplication {
    name = "run-tests";
    text = with pkgs; with lib; ''
      ${getExe nix-unit} ${toString ./tests.nix} "$@"
    '';
  };

  shell =
    pkgs.mkShellNoCC {
      packages = [
        pkgs.npins
        run-tests
      ];
    };

  tests = with pkgs; with lib; runCommand "run-tests" { } ''
    touch $out
    ${getExe nix-unit} ${./tests.nix} "$@"
  '';
}
tests.nix
{
  testTest = {
    expr = 1 + 1;
    expected = 2;
  };
}
$ npins init
$ echo '(import ./. { }).shell' > shell.nix
$ echo use_nix >> .envrc
$ direnv allow
$ run-tests
warning: unknown setting 'allowed-users'
warning: unknown setting 'trusted-users'
warning: `--gc-roots-dir' not specified
✅ testTest

🎉 1/1 successful
$ nix-build -A tests
this derivation will be built:
  /nix/store/ai9r71fk66a29f6h23aqqqxl516w59yq-run-tests.drv
building '/nix/store/ai9r71fk66a29f6h23aqqqxl516w59yq-run-tests.drv'...
warning: `--gc-roots-dir' not specified
error: creating directory '/nix/var/nix/profiles': Permission denied
error: builder for '/nix/store/ai9r71fk66a29f6h23aqqqxl516w59yq-run-tests.drv' failed with exit code 1;
       last 2 log lines:
       > warning: `--gc-roots-dir' not specified
       > error: creating directory '/nix/var/nix/profiles': Permission denied
       For full logs, run 'nix log /nix/store/ai9r71fk66a29f6h23aqqqxl516w59yq-run-tests.drv'.

Ruminations

Maybe this can be fixed somewhat easily by setting the relevant Nix configuration options on the nix-unit side such that it doesn't even try to get outside of the derivation sandbox, e.g. by pointing profiles to /tmp.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions