From 8c08b67be3bcc7a0dd19cc43cebb58ede2e12ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sat, 23 Nov 2024 14:06:32 +0100 Subject: [PATCH] refactor cross systems structure --- .github/workflows/pre-release.yml | 6 +++--- cross-systems.nix | 31 ++++++++++++++++++++------ default.nix | 36 +++++++++++++++---------------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 814056c5..d1b38a28 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -11,15 +11,15 @@ jobs: matrix: include: - os: ubuntu-latest - target: + target: x86_64-unknown-linux-musl - os: ubuntu-latest target: aarch64-unknown-linux-musl - os: ubuntu-latest target: x86_64-pc-windows-gnu - os: macos-13 - target: + target: x86_64-apple-darwin - os: macos-14 - target: + target: aarch64-apple-darwin steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/cross-systems.nix b/cross-systems.nix index eda5a252..7416a4e3 100644 --- a/cross-systems.nix +++ b/cross-systems.nix @@ -1,11 +1,30 @@ +# The first level represents the build platform system, and the second +# level represents the build platform triple config. + { - aarch64-unknown-linux-musl = { - runner = { qemu, ... }: "${qemu}/bin/qemu-aarch64 ./himalaya"; + x86_64-linux = { + x86_64-unknown-linux-musl = { }; + + aarch64-unknown-linux-musl = { + runner = { qemu, ... }: "${qemu}/bin/qemu-aarch64 ./himalaya"; + }; + + x86_64-pc-windows-gnu = { + runner = { wine, ... }: + let wine64 = wine.override { wineBuild = "wine64"; }; + in "${wine64}/bin/wine64 ./himalaya.exe"; + }; }; - x86_64-pc-windows-gnu = { - runner = { wine, ... }: - let wine64 = wine.override { wineBuild = "wine64"; }; - in "${wine64}/bin/wine64 ./himalaya.exe"; + aarch64-linux = { + x86_64-unknown-linux-musl = { }; + }; + + x86_64-darwin = { + x86_64-apple-darwin = { }; + }; + + aarch64-darwin = { + aarch64-apple-darwin = { }; }; } diff --git a/default.nix b/default.nix index 0fe3a3ed..617c6b10 100644 --- a/default.nix +++ b/default.nix @@ -1,43 +1,43 @@ -# https://nixos.org/manual/nixpkgs/stable/#sec-cross-usage -{ crossSystem ? null }: +{ target ? null }: let - crossBuildEnabled = crossSystem != null && crossSystem != ""; + pkgs = import { }; + crossPkgs = if isNull target then pkgs else import { crossSystem.config = target; }; crossSystems = import ./cross-systems.nix; - buildPkgs = import { }; - hostPkgs = if crossBuildEnabled then import { inherit crossSystem; } else buildPkgs; + inherit (crossPkgs.stdenv) buildPlatform hostPlatform; - inherit (hostPkgs.stdenv) buildPlatform hostPlatform; + crossSystem = crossSystems.${buildPlatform.system}.${hostPlatform.config} + or throw ("Platform not supported: " + hostPlatform.config); - runner = if crossBuildEnabled then crossSystems.${hostPlatform.config}.runner buildPkgs else "./himalaya"; + runner = crossSystem.runner or (_: "./himalaya") pkgs; fenix = import (fetchTarball "https://github.com/soywod/fenix/archive/main.tar.gz") { }; mkToolchain = import ./rust-toolchain.nix fenix; rustToolchain = mkToolchain.fromTarget { - pkgs = hostPkgs; + pkgs = crossPkgs; targetSystem = buildPlatform.config; }; - rustPlatform = hostPkgs.makeRustPlatform { + rustPlatform = crossPkgs.makeRustPlatform { rustc = rustToolchain; cargo = rustToolchain; }; himalaya = import ./package.nix { inherit rustPlatform; - lib = hostPkgs.lib; - fetchFromGitHub = hostPkgs.fetchFromGitHub; - pkg-config = hostPkgs.pkg-config; - darwin = hostPkgs.darwin; + lib = crossPkgs.lib; + fetchFromGitHub = crossPkgs.fetchFromGitHub; + pkg-config = crossPkgs.pkg-config; + darwin = crossPkgs.darwin; installShellFiles = false; installShellCompletions = false; installManPages = false; - notmuch = hostPkgs.notmuch; - gpgme = hostPkgs.gpgme; - stdenv = hostPkgs.stdenv; + notmuch = crossPkgs.notmuch; + gpgme = crossPkgs.gpgme; + stdenv = crossPkgs.stdenv; }; in @@ -59,10 +59,10 @@ himalaya.overrideAttrs (drv: { tar -czf himalaya.tgz himalaya* share mv himalaya.tgz ../ - ${hostPkgs.zip}/bin/zip -r himalaya.zip himalaya* share + ${crossPkgs.zip}/bin/zip -r himalaya.zip himalaya* share mv himalaya.zip ../ ''; - src = hostPkgs.nix-gitignore.gitignoreSource [ ] ./.; + src = crossPkgs.nix-gitignore.gitignoreSource [ ] ./.; cargoDeps = rustPlatform.importCargoLock { lockFile = ./Cargo.lock; allowBuiltinFetchGit = true;