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.

  • moonpiedumplings@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    2 days ago

    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.

    • x74sys@programming.devOP
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      1 day ago

      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.