Tech News
← Back to articles

Custom Cross Compiler with Nix

read original related products more articles

This post is about trying to do something seemingly simple. You have an unusual system that you want to write code for. You even have a cross compiler! But how can you get this working with nix? Although the answer turned out to be simple, the journey to getting there was super long and undocumented. I hope this post saves you a lot of time!

My Motivations Link to heading

I have a deep love for an operating system called Risc Os. It is a really old operating system made by Acorn Computers Ltd. for their Archimedes range of computers and you can still run it on Raspberry Pi! I love it for a few reasons:

It’s so simple It was one of the first things I nerded out over after seeing at the national computing museum in Bletchley Park Programming it in C helped is where pretty much all of my C knowledge comes from

But programming it is not easy. It predates every programming convention we have. There are no good text editors on it, and compiling is a pain!

But there is a cross compiler! The only issue is that when you try to build it, it becomes obvious that you need a seriously old version of ubuntu on X86_64 linux (it’s a patched GCC 4.7.4) and these days, I sport an Arm Mac which won’t even build a compiler that old!

Can we nix it? Link to heading

Well of course we can. I cannot think of a better use of a flake! And then we can use something like the rosetta builder from Mac OS to cross build software! I thought that would be easy, but my god it was not! Nix was a dream, but building old versions of GCC is a task I would not wish upon my worst enemies! But it is at this point we fast forward because this is the point I assume you are reading this. You have a cross toolchain built in nix (maybe because you have followed a great tutorial like this one). And that’s all well and good…. but it’s a bit annoying.

Every time you want to build something, you have to manually invoke the correct compiler (e.g. arm-unknown-riscos-gcc ), or you have to manually pass the correct --target to ./configure or whatever. What you want is something more like a pkgsCross."your platform".callPackage ... that does all of that for you! You want a custom cross-stdenv!

How C compilers work in Nix Link to heading

... continue reading