How do I cross-compile a flake?

I didn’t realize it was already there, I can probably drop it from the nix-gaming PR. Thanks for adding it :slight_smile:

It wasn’t in nixpkgs when the branch my PR is on was started (Add basic DXVK builder and installer by MagicRB · Pull Request #7 · fufexan/nix-gaming · GitHub is from december last year) and I didn’t think to check if someone had added it.

Still applies for vkd3d-proton of course, and the complaints about not having a good way to do this in flakes in general are not solved just because one package is in nixpkgs and sidesteps the issue.

1 Like

Ah, okay. Sorry if I was a bit terse. I use DXVK on Darwin, and my Linux machines are not suitable for gaming, so my ability to test on Linux is pretty limited. I wasn’t certain whether there might have been something wrong with the derivation in nixpkgs. I see now that it was a timing issue. Thanks for the clarification.

It’s possible to cross-build in a flake without using an overlay by manually setting crossSystem to the target and localSystem to the host system. It’s not exactly pretty or obvious though.

Unless I misunderstand, and the issue is cross-building another flake’s package and not just cross-building within a flake. Flake can be written to support it, but that may not be ideal.

(Edit: Revised the example to do implement that.)

{
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11";

  outputs = inputs@{ self, nixpkgs, ... }:
    let
      lib = nixpkgs.lib;

      darwin = [ "x86_64-darwin" "aarch64-darwin" ];
      linux = [ "x86_64-linux" "aarch64-linux" ];
      allSystems = darwin ++ linux;

      forEachSystem = systems: f: lib.genAttrs systems (system: f system);
      forAllSystems = forEachSystem allSystems;
    in
    {
      packages = forAllSystems (system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          inherit (pkgs) hello;
          cross = forEachSystem (lib.filter (sys: sys != system) allSystems) (targetSystem:
            let
              crossPkgs = import nixpkgs { localSystem = system; crossSystem = targetSystem; };
            in
            {
              inherit (crossPkgs) hello;
            }
          );
        }
      );
    };
}
2 Likes

I think it’ll be helpful to link here another solution from this forum I found simpler and helpful for my purpose:

Basically, it instructs to use pkgs.pkgsCross.<targetSystem>.callPackage which gives you the power of nixpkgs’ cross compilation override system, along with the familiar and comfortable also for flakes interface of an expression file to which you callPackage.

3 Likes