this post was submitted on 26 May 2026
2 points (100.0% liked)

Nix / NixOS

2744 readers
3 users here now

Main links

Videos

founded 2 years ago
MODERATORS
 

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.

you are viewing a single comment's thread
view the rest of the comments
[–] moonpiedumplings@programming.dev 2 points 8 hours ago* (last edited 8 hours ago) (1 children)

~~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.dev 1 points 8 hours ago* (last edited 8 hours ago) (1 children)

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.