3
How to do cross-compiling dev envs?
(programming.dev)
Hey guys, I have a project which I want to cross-compile to windows (because I don't want to install windows on my machine, nor do I plan on developing on windows) and eventually MacOS.
All I really need is to know that it will compile for & run on windows.
This is what I tried, but I'm not sure what the best approach is here. Searching online didn't yield any conclusive results either.
{
description = "cross-compile dev env";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { nixpkgs, ... }:
let
supportedSystems = ["x86_64-linux"];
eachSystem = fn: nixpkgs.lib.genAttrs supportedSystems (system:
fn nixpkgs.legacyPackages.${system}
);
in
{
#1 this is what I tried at first,
# but it created conflicts in the environment (obviously)
devShells = eachSystem (pkgs: {
default = pkgs.mkShell.override { stdenv = pkgs.gcc15.stdenv; } {
packages = with pkgs; [
...
pkgsCross.mingwW64.buildPackages.gcc15
];
};
});
#2 this is probably a better solution?
devShells = eachSystem (pkgs: let packages = with pkgs; [
...
]; in {
default = pkgs.mkShell.override { stdenv = pkgs.gcc15.stdenv; } {
inherit packages;
};
windows = pkgs.pkgsCross.mingwW64.mkShell {
inherit packages;
};
});
};
}
The project is just a C program which compiles using a Makefile. I stripped out dependencies etc. from the flake.
~~Multiple options:~~
~~Use flake-utils: https://github.com/numtide/flake-utils#example~~
~~Create a helper function: https://ayats.org/blog/no-flake-utils (what I do)~~
~~flake-parts: https://ayats.org/blog/no-flake-utils#option-a-flake-just-for-yourself , https://github.com/hercules-ci/flake-parts~~
~~But this is one of the things I find annoying af about nix flakes. Reproducibility is a spectrum, and I think focusing on "purity" over being able to easily ship it to multiple architectures, or not being able to use the GPU is a step too far on that spectrum for the vast majority of usecases. I can understand why scientific computing or anything that needs absolute precision might want it, but most people only just want the same versions of packages installed in their development environment.~~
EDIT: whoops forget everything I said, you are asking for cross compiliation of a dev env to Windows. Nix doesn't support Windows. It only supports linux. You can't make a nix dev shell that works on windows.
My goal is to cross-compile from nix to windows. I need to have this program running on windows (or at least provide binaries for it haha), but I really don't want to dual-boot again (I just got rid of windows a couple of months ago, and I'm not too keen on looking at it again in the near future). Maybe I phrased my question the wrong way.
So I don't need the environment to run on windows, it just needs to be able to compile for windows.
Ah, I see.
A quick search leads me here: https://discourse.nixos.org/t/nix-on-windows/1113/105
which you might have seen before. It is a very long post, but some people mention having achieved what you want.