Run nixos interactive tests on aarch64-darwin

Hi!

I’m trying to write a nixos test for a custom nixos home-manager module.

Here is my nixos module test:

{
  pkgs,
  home-manager,
  nixos,
  ...
}: let
  nixos-lib = import (pkgs.path + "/nixos/lib") {};
in
  # runTest: https://github.com/NixOS/nixpkgs/blob/a9c0720daf7f166918002e6b2fcc272615c95ae0/nixos/lib/testing/default.nix
  # derivation: https://github.com/NixOS/nixpkgs/blob/a9c0720daf7f166918002e6b2fcc272615c95ae0/nixos/lib/testing/run.nix
  nixos-lib.runTest {
    name = "homemanager-module_proton-bridge";

    hostPkgs = pkgs;
    node = nixos.lib.optionalAttrs pkgs.stdenv.isDarwin {
      pkgs = import nixos {
        system = builtins.replaceStrings ["darwin"] ["linux"] pkgs.stdenv.hostPlatform.system;
      };
    };

    nodes.machine = {...}: {
      imports = [home-manager.nixosModules.home-manager];

      virtualisation.host = nixos.lib.optionalAttrs pkgs.stdenv.isDarwin {inherit pkgs;};

      users.users.alice = {
        isNormalUser = true;
      };

      home-manager.users.alice = {
        imports = [../default.nix];
        home = {
          stateVersion = "23.11";
        };
        services.protonmail-bridge.enable = true;
      };
    };

    testScript = _: ''
      machine.wait_for_unit("default.target")
      machine.succeed("su -- alice -c 'which protonmail-bridge'")
      machine.fail("su -- root -c 'which protonmail-bridge'")
    '';
  }

to avoid the error: assertion '(stdenv).isLinux' failed issue, I needed to override the node pkgs from aarch64-darwin to aarch64-linux, as seen on line 15

Here is the flake’s output declaration

    packages.aarch64-darwin.foo = let
      pkgs = import nixos {system = "aarch64-darwin";};
    in import ./nix/home-manager/modules/proton-bridge/tests (self.inputs // { inherit pkgs; });

running nix build .#foo work as expected and I get my test success without issues. Cool.

When I try to build nix build .#foo.driver (or the driverInteractive version) it produce the expect output ./result/bin/nixos-test-driver which can be executed but is failing with this output:

a lot of errors like

machine # [    1.729347] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-bless-boot-generator failed with exit status 127.
machine # [    1.729888] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-fstab-generator failed with exit status 127.
machine # [    1.730311] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-hibernate-resume-generator failed with exit status 127.
machine # [    1.730858] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-debug-generator failed with exit status 127.
machine # [    1.731336] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-system-update-generator failed with exit status 127.
machine # [    1.731805] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-getty-generator failed with exit status 127.
machine # [    1.732353] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-integritysetup-generator failed with exit status 127.
machine # [    1.732853] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-run-generator failed with exit status 127.
machine # [    1.733347] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-veritysetup-generator failed with exit status 127.
machine # [    1.733816] (sd-execu[476]: /nix/store/kq7xjfgy6186gw0v9qacmxm5dhp7vyrx-systemd-254.3/lib/systemd/system-generators/systemd-gpt-auto-generator failed with exit status 127.

or errors like

machine # [    2.330438] systemd[1]: Failed to read symlink /etc/systemd/system/<path>: Too many open files

I’ve tried to find example of producing the interactive test version on darwin (which is why I try to build the .driver version so I can have faster iterations when I’m writing the test assertions), I’ve found multiple links like:

but Im still not able to understand what is the problem in my example.

Thank you by advance for your time and help

1 Like

Hi,

I invested some time to make this work on macOS natively. Please try nixpkgs from this pull request:

https://github.com/NixOS/nixpkgs/pull/261694

This should work out of the box, assuming that you have a linux builder configured elsewhere.
But it only works with pkgs.testers.runNixOSTest.

2 Likes

Thanks for your answer, it pointed me with the issue which was the missing

useNixStoreImage = true;
writableStore = true;

configurations in nodes.machine.virtualisation

out of curiosity, what are the differences between / in which context should we use:

  • pkgs.nixosTest
  • nixos-lib.runTest
  • pkgs.testers.runNixOSTest
    ?
2 Likes

pkgs.testers.runNixOSTest calls nixos-lib.runTest for you, but without you having to obtain nixos-lib from somewhere. I’d regard this as the latest official way to run nixos tests.

pkgs.nixosTest is still used in all the existing tests, but it’s missing the nice new NixOS-module based configurability that the new function (see nixos docs) has. Write new tests using pkgs.testers.runNixOSTest.

3 Likes