Unverified Commit b8698cd8 authored by Gabriella Gonzalez's avatar Gabriella Gonzalez Committed by GitHub
Browse files

macOS support for NixOS tests (#282401)

Closes #193336
Closes #261694
Related to #108984

The goal here was to get the following flake to build and run on
`aarch64-darwin`:

```nix
{ inputs.nixpkgs.url = <this branch>;

  outputs = { nixpkgs, ... }: {
    checks.aarch64-darwin.default =
      nixpkgs.legacyPackages.aarch64-darwin.nixosTest {
        name = "test";

        nodes.machine = { };

        testScript = "";
      };
  };
}
```

… and after this change it does.  There's no longer a need for the
user to set `nodes.*.nixpkgs.pkgs` or
`nodes.*.virtualisation.host.pkgs` as the correct values are inferred
from the host system.
parent 458b097d
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -14,6 +14,25 @@ let
    types
    ;

  inherit (hostPkgs) hostPlatform;

  guestSystem =
    if hostPlatform.isLinux
    then hostPlatform.system
    else
      let
        hostToGuest = {
          "x86_64-darwin" = "x86_64-linux";
          "aarch64-darwin" = "aarch64-linux";
        };

        supportedHosts = lib.concatStringsSep ", " (lib.attrNames hostToGuest);

        message =
          "NixOS Test: don't know which VM guest system to pair with VM host system: ${hostPlatform.system}. Perhaps you intended to run the tests on a Linux host, or one of the following systems that may run NixOS tests: ${supportedHosts}";
      in
        hostToGuest.${hostPlatform.system} or (throw message);

  baseOS =
    import ../eval-config.nix {
      inherit lib;
@@ -27,13 +46,14 @@ let
          ({ config, ... }:
            {
              virtualisation.qemu.package = testModuleArgs.config.qemu.package;
              virtualisation.host.pkgs = hostPkgs;
            })
          ({ options, ... }: {
            key = "nodes.nix-pkgs";
            config = optionalAttrs (!config.node.pkgsReadOnly) (
              mkIf (!options.nixpkgs.pkgs.isDefined) {
                # TODO: switch to nixpkgs.hostPlatform and make sure containers-imperative test still evaluates.
                nixpkgs.system = hostPkgs.stdenv.hostPlatform.system;
                nixpkgs.system = guestSystem;
              }
            );
          })
+5 −1
Original line number Diff line number Diff line
@@ -2,7 +2,11 @@
{
  config = {
    # default pkgs for use in VMs
    _module.args.pkgs = hostPkgs;
    _module.args.pkgs =
      # TODO: deprecate it everywhere; not just on darwin. Throw on darwin?
      lib.warnIf hostPkgs.stdenv.hostPlatform.isDarwin
        "Do not use the `pkgs` module argument in tests you want to run on darwin. It is ambiguous, and many tests are broken because of it. If you need to use a package on the VM host, use `hostPkgs`. Otherwise, use `config.node.pkgs`, or `config.nodes.<name>.nixpkgs.pkgs`."
        hostPkgs;

    defaults = {
      # TODO: a module to set a shared pkgs, if options.nixpkgs.* is untouched by user (highestPrio) */
+3 −1
Original line number Diff line number Diff line
@@ -41,7 +41,9 @@ in
    rawTestDerivation = hostPkgs.stdenv.mkDerivation {
      name = "vm-test-run-${config.name}";

      requiredSystemFeatures = [ "kvm" "nixos-test" ];
      requiredSystemFeatures = [ "nixos-test" ]
        ++ lib.optionals hostPkgs.stdenv.hostPlatform.isLinux [ "kvm" ]
        ++ lib.optionals hostPkgs.stdenv.hostPlatform.isDarwin [ "apple-virt" ];

      buildCommand = ''
        mkdir -p $out
+4 −1
Original line number Diff line number Diff line
{ pkgs, lib, ... }: let
{ config, lib, ... }: let

  pkgs = config.node.pkgs;

  commonConfig = ./common/acme/client;

  dnsServerIP = nodes: nodes.dnsserver.networking.primaryIPAddress;
+2 −1
Original line number Diff line number Diff line
@@ -78,8 +78,9 @@ let
    #       it with `allowAliases = false`?
    # warnIf pkgs.config.allowAliases "nixosTests: pkgs includes aliases."
    {
      _file = "${__curPos.file} readOnlyPkgs";
      _class = "nixosTest";
      node.pkgs = pkgs;
      node.pkgs = pkgs.pkgsLinux;
    };

in {
Loading