How to make my flake work on Darwin (MacOS)

I have this flake which has a few packages that can easily be built on Darwin and Linux. Then I also have a QEMU VM which is only built on Linux using nixpkgs.lib.optionalAttrs (system == "x86_64-linux"). Ultimately, what gets deployed with deploy-rs is a NixOS configuration. This all worked on my NixOS desktop, but I’m currently on the road on my Darwin machine and it would be nice to be able to deploy things as well.

The linked flake currently gives me:

$ deploy . -- --show-trace
🚀 ℹ️ [deploy] [INFO] Running checks for flake in .
warning: Git tree '/Users/foo/bar' is dirty
warning: unknown flake output 'deploy'
error: a 'x86_64-linux' with features {} is required to build '/nix/store/n5cr633pn52iqr0r9845ig5qgbz5qwlc-builder.pl.drv', but I am a 'x86_64-darwin' with features {benchmark, big-parallel, nixos-test}
🚀 ❌ [deploy] [ERROR] Failed to check deployment: Nix checking command resulted in a bad exit code: Some(1)

I thought that I could just build the NixOS configuration on my Darwin machine as well, but now I’m wondering if this is even possible.

Here’s the system I’m trying to deploy:

      serverSystem = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          sops-nix.nixosModules.sops
          {
            # TODO: This sucks, install the server systemwide or something
            config.serverWorkingDir = "${allSystems.packages.x86_64-linux.server}/";
            config.serverExe = "${allSystems.packages.x86_64-linux.server}/bin/migrate-and-serve";
          }
          {
            environment.systemPackages = [ allSystems.packages.x86_64-linux.litestream ];
          }
          ./nix/configuration.nix
          ./nix/systemd-server.nix
          ./nix/systemd-litestream.nix
          {
            imports = [
              "${nixpkgs-20-09}/nixos/modules/virtualisation/digital-ocean-image.nix"
            ];
          }

        ];
      };

Are you aware of nix-darwin?

Yes but for one thing I don’t want to install that just to be able to build a single project and additionally I’m not sure if this would help since my NixOS configuration still wouldn’t build on a Darwin machine I assume

I believe that what I’m trying to do is not trivial, given comment threads like Build on target server? · Issue #12 · serokell/deploy-rs · GitHub

I will have to try using a Docker image to build and deploy from Darwin

I’m sorry, I think I misunderstood what you were trying to do. So you want to build a NixOS configuration to deploy to a NixOS server from your Mac right? I haven’t heard of anyone doing this, but it should be technically possible, although potentially difficult, using the cross compilation interface.

One way to work around it might be to simply run a lightweight NixOS vm on your mac and deploy from there.

I’m currently trying to use Docker for it

docker run -it --rm -v nixcache2:/nix -v (pwd):/foo -w /foo nixpkgs/nix-flakes:latest

but inside the container I’m running into extremely weird errors

error: builder for '/nix/store/6csvdr458v13b3iqf6cagr4fvl8hfj9h-admin-authorized_keys.drv' failed with exit code 1;
       last 2 log lines:
       > unpacking sources
       > variable $src or $srcs should point to the source
       For full logs, run 'nix log /nix/store/6csvdr458v13b3iqf6cagr4fvl8hfj9h-admin-authorized_keys.drv'.
error: 1 dependencies of derivation '/nix/store/p33nhyfzr4yk4mrf8i7s96l0cm7kb8p2-etc.drv' failed to build
error: 1 dependencies of derivation '/nix/store/5yz7zds3ghpq6jck421rzkn9aqxhn3rz-nixos-system-lions-server-21.11.20210526.84aa237.drv' failed to build
error: 1 dependencies of derivation '/nix/store/4vfigw3q231gp24b6y4vcrx7l6gara73-activatable-nixos-system-lions-server-21.11.20210526.84aa237.drv' failed to build
error: 1 dependencies of derivation '/nix/store/6klknvj3ahjgr15rzzv9kl9msyvxzpwq-deploy-rs-check-activate.drv' failed to build
error: build of '/nix/store/6klknvj3ahjgr15rzzv9kl9msyvxzpwq-deploy-rs-check-activate.drv', '/nix/store/xrdgphqlpxji7g9mpn1iahqasbsf5k9i-jsonschema-deploy-system.drv' failed

inside the container I’m doing nix develop followed by deploy . I also tried nix develop --option sandbox false based on vague statements in IRC logs but that just resulted in a different derivation throwing this error.

It’s strange because the first derivation that failed with this error is https://github.com/NixOS/nixpkgs/blob/9e878c2a5dcd6aa5335e04b3e19bdac0fe3f1262/nixos/modules/services/web-servers/caddy.nix#L7 which just uses writeText

I have no idea what made the above error go away. I could reproduce it on my NixOS machine when trying to deploy with Docker but ultimately after randomly commenting things out and uncommenting them, and ending up with exactly the same code as before, it now works.

$ docker run -it --rm -v nixcache2:/nix -v (pwd):/foo -w /foo  -v ~/.ssh:/root/.ssh:ro nixpkgs/nix-flakes nix run github:serokell/deploy-rs .

Not ideal but whatever.