Last active
April 17, 2024 03:02
-
-
Save Qyriad/01ae9433db26e78c4867cc275c7401f7 to your computer and use it in GitHub Desktop.
Proof of concept Linux -> macOS cross compilation in Nixpkgs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nodes": { | |
| "flake-utils": { | |
| "inputs": { | |
| "systems": "systems" | |
| }, | |
| "locked": { | |
| "lastModified": 1710146030, | |
| "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", | |
| "owner": "numtide", | |
| "repo": "flake-utils", | |
| "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", | |
| "type": "github" | |
| }, | |
| "original": { | |
| "owner": "numtide", | |
| "repo": "flake-utils", | |
| "type": "github" | |
| } | |
| }, | |
| "nixpkgs": { | |
| "locked": { | |
| "lastModified": 1712849433, | |
| "narHash": "sha256-flQtf/ZPJgkLY/So3Fd+dGilw2DKIsiwgMEn7BbBHL0=", | |
| "owner": "NixOS", | |
| "repo": "nixpkgs", | |
| "rev": "f173d0881eff3b21ebb29a2ef8bedbc106c86ea5", | |
| "type": "github" | |
| }, | |
| "original": { | |
| "owner": "NixOS", | |
| "ref": "nixpkgs-unstable", | |
| "repo": "nixpkgs", | |
| "type": "github" | |
| } | |
| }, | |
| "root": { | |
| "inputs": { | |
| "flake-utils": "flake-utils", | |
| "nixpkgs": "nixpkgs" | |
| } | |
| }, | |
| "systems": { | |
| "locked": { | |
| "lastModified": 1681028828, | |
| "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", | |
| "owner": "nix-systems", | |
| "repo": "default", | |
| "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", | |
| "type": "github" | |
| }, | |
| "original": { | |
| "owner": "nix-systems", | |
| "repo": "default", | |
| "type": "github" | |
| } | |
| } | |
| }, | |
| "root": "root", | |
| "version": 7 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| inputs = { | |
| nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; | |
| flake-utils.url = "github:numtide/flake-utils"; | |
| }; | |
| outputs = { self, nixpkgs, flake-utils }: | |
| flake-utils.lib.eachDefaultSystem (system: let | |
| pkgs = import nixpkgs { | |
| inherit system; | |
| config.allowUnsupportedSystems = true; | |
| }; | |
| package = pkgs.callPackage ./package.nix { }; | |
| in { | |
| packages.default = package; | |
| packages.hello = package; | |
| devShells.default = pkgs.mkShell { | |
| inputsFrom = [ | |
| package | |
| ]; | |
| }; | |
| }) # eachDefaultSystem | |
| ; # outputs | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| path, | |
| lib, | |
| stdenvNoCC, | |
| darwin, | |
| llvmPackages_15, | |
| wrapCCWith, | |
| hello, | |
| coreutils, | |
| python3, | |
| boost, | |
| }: let | |
| darwinPkgs = import path { | |
| localSystem.system = "x86_64-darwin"; | |
| config.allowUnsupportedSystem = true; | |
| }; | |
| libunwind = darwinPkgs.llvmPackages.libunwind; | |
| libcxx = darwinPkgs.llvmPackages.libcxx; | |
| llvmPackages = llvmPackages_15; | |
| darwinSdk = darwin.apple_sdk_11_0.MacOSX-SDK; | |
| libc = darwin.apple_sdk_11_0.Libsystem; | |
| inherit (llvmPackages) clang-unwrapped; | |
| extraBuildCommands = { | |
| libcxx-cxxflags = let | |
| list = [ | |
| #"-nostdlib++" | |
| #"-nostdinc++" | |
| #"-nostartfiles" | |
| "-isystem ${lib.getDev libcxx}/include/c++/v1" | |
| "-isystem ${darwinSdk}/usr/include" | |
| "-idirafter ${darwinSdk}/usr/include" | |
| "-iframework${darwinSdk}/System/Library/Frameworks" | |
| "-L${darwinSdk}/usr/lib" | |
| "-L${lib.getLib libunwind}" | |
| "-lc++" | |
| "-lc++abi" | |
| ]; | |
| op = acc: item: acc + "\necho ${lib.strings.escapeShellArg item} >> \"$out/nix-support/libcxx-cxxflags\""; | |
| in lib.foldl' op "" list; | |
| }; | |
| cc = wrapCCWith { | |
| cc = clang-unwrapped; | |
| #echo "-isystem ${darwinSdk}/usr/include" >> "$out/nix-support/libc-cflags" | |
| #echo "-idirafter ${darwinSdk}/usr/include" >> "$out/nix-support/libc-cflags" | |
| #echo "-iframework${darwinSdk}/System/Library/Frameworks" >> "$out/nix-support/libc-cflags" | |
| #echo "-isystem ${darwinSdk}/System/Library/Frameworks/Kernel.framework/Versions/A/Headers" >> "$out/nix-support/libc-cflags" | |
| # We need to remove the -dynamic-linker arguments lol | |
| extraBuildCommands = '' | |
| echo "making wrapper stuff in $out" | |
| cat ${./osxcross-cc-wrapper-hook.bash} >> "$out/nix-support/cc-wrapper-hook" | |
| '' + extraBuildCommands.libcxx-cxxflags; | |
| #echo "-isystem ${lib.getDev libcxx}/include/c++/v1" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-isystem ${darwinSdk}/usr/include" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-lc++" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-lc++abi" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-L${darwinSdk}/usr/lib" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-L${libunwind}/lib" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-idirafter ${darwinSdk}/usr/include" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-iframework${darwinSdk}/System/Library/Frameworks" >> "$out/nix-support/libcxx-cxxflags" | |
| #echo "-idirafter ${darwinSdk}/System/Library/Frameworks/Kernel.framework/Versions/A/Headers" >> "$out/nix-support/libc-cflags" | |
| inherit libc; | |
| bintools = llvmPackages.bintools.override { | |
| inherit libc; | |
| }; | |
| #inherit (llvmPackages) libcxx; | |
| gccForLibs = null; | |
| extraTools = lib.singleton llvmPackages.lld; | |
| isClang = true; | |
| useCcForLibs = false; | |
| nixSupport.cc-cflags = [ | |
| #"-nostdlib" | |
| #"-isystem" "${darwinSdk}/usr/include" | |
| "-idirafter" "${darwinSdk}/System/Library/Frameworks/Kernel.framework/Versions/A/Headers" | |
| "--target=x86_64-apple-macosx11.0" | |
| "-fuse-ld=lld" | |
| "-DTARGET_OS_MACOSX" | |
| ]; | |
| nixSupport.cc-ldflags = [ | |
| #"-fuse-ld=lld" | |
| "-lc" | |
| "-L${darwinSdk}/usr/lib" | |
| ]; | |
| }; | |
| #stdenv = overrideCC stdenvNoCC cc; | |
| stdenv = stdenvNoCC.override { | |
| hostPlatform = lib.systems.elaborate "x86_64-darwin"; | |
| inherit (stdenvNoCC) buildPlatform; | |
| targetPlatform = lib.systems.elaborate "x86_64-darwin"; | |
| inherit cc; | |
| hasCC = true; | |
| allowedRequisites = null; | |
| }; | |
| trivial-c-cxx = stdenv.mkDerivation { | |
| name = "trivial-c-c++"; | |
| #passthru = { | |
| # inherit toCCArgs cc; | |
| #}; | |
| src = ./.; | |
| nativeBuildInputs = [ | |
| #cc | |
| #llvmPackages.lld | |
| ]; | |
| buildPhase = '' | |
| runHook preBuild | |
| mkdir -p "$out" | |
| (set -x && cc -o $out/main-c ./main.c) | |
| (set -x && c++ -o $out/main-cpp ./main.cpp) | |
| runHook postBuild | |
| ''; | |
| }; | |
| hello-darwin = (hello.override { inherit stdenv; }).overrideAttrs (prev: { | |
| buildInputs = prev.buildInputs or [ ] ++ [ | |
| darwinPkgs.libiconv | |
| ]; | |
| }); | |
| python3-darwin = (python3.override { | |
| inherit stdenv; | |
| enableLTO = false; | |
| configd = darwinPkgs.darwin.apple_sdk.frameworks.SystemConfiguration; | |
| }).overrideAttrs (prev: { | |
| configureFlags = prev.configureFlags or [ ] ++ [ | |
| "MACHDEP=darwin" | |
| ]; | |
| }); | |
| in hello-darwin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| declare -a _extraBefore | |
| for arg in "${extraBefore[@]}"; do | |
| if [[ "$arg" != *"-dynamic-linker"* ]]; then | |
| _extraBefore+=("$arg") | |
| fi | |
| done | |
| extraBefore=("${_extraBefore[@]}") | |
| unset _extraBefore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment