callPackage workaround for cross-compilation. In this case, targeting windows using mingw from nix-darwin.
This isn't how you'd build a package, but might be useful for shellcoding.
shell.nix
{
lib ? import ,
pkgs ? import {
localSystem = "aarch64-darwin";
# https://github.com/NixOS/nixpkgs/blob/master/lib/systems/examples.nix
crossSystem = lib.systems.examples.ucrt64;
},
}:
pkgs.callPackage (
{
# add packages here as well as the correct list below
mkShell,
pkg-config,
}:
mkShell {
strictDeps = true;
depsBuildBuild = [];
nativeBuildInputs = [
pkg-config
];
buildInputs = [];
}
) { }
main.c
#include
void main() {
WinExec("calc.exe", 0);
ExitProcess(0);
}
$CC -o main src/main.c
file main
[nix-shell:~/Code/shelc]$ $CC main.c
[nix-shell:~/Code/shelc]$ file a.exe
a.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows, 9 sections
[nix-shell:~/Code/shelc]$ xxd *.exe | head -10
00000000: 4d5a 9000 0300 0000 0400 0000 ffff 0000 MZ..............
00000010: b800 0000 0000 0000 4000 0000 0000 0000 ........@.......
00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000030: 0000 0000 0000 0000 0000 0000 8000 0000 ................
00000040: 0e1f ba0e 00b4 09cd 21b8 014c cd21 5468 ........!..L.!Th
00000050: 6973 2070 726f 6772 616d 2063 616e 6e6f is program canno
00000060: 7420 6265 2072 756e 2069 6e20 444f 5320 t be run in DOS
00000070: 6d6f 6465 2e0d 0d0a 2400 0000 0000 0000 mode....$.......
00000080: 5045 0000 6486 0900 00a6 ce12 003e 0000 PE..d........>..
00000090: c804 0000 f000 2602 0b02 022c 001c 0000 ......&....,....