start	end	text
0	7860	So my talk is on doing toolchains declaratively, which is very powerful with Nix.
11980	15000	So the main thing is...
23100	31420	Toolchains essentially allow for being able to target different platforms that are compiling code between systems.
43920	54220	One of the problems with toolchains has been that on a lot of distributions they live in random places like opt or just in US or local.
54720	58520	With Nix we are able to put them in a very specific location.
58520	59980	It's very powerful.
61680	68500	And one of the other problems is toolchains can only target one platform until you have to compile it for another platform like GCC.
68500	78900	So if you want to target RISC-V and you're on X86, you have to recompile GCC entirely, which can be very difficult if you don't have the hardware for it.
79100	87860	With Nix we can easily do this and distribute it between multiple systems using remote builders and various other features.
94340	100560	So the more of Nix way of doing things is splitting toolchains into their base components.
100560	114900	So this would be having a derivation specifically for your C compiler, runtime libraries, your C++ implementation, pin tools and all that.
114900	121500	Currently LLVM and Nix packages does this, but we're working on this with GCC right now.
121500	125460	Another powerful thing is being able to have multiple versions of a compiler.
126040	137160	Because some code might like only certain versions of a compiler and being able to change versions of a compiler dynamically is very useful.
142080	148600	So one of the current problems we have with compilers in Nix packages right now is GCC is very monolithic.
148600	158260	Essentially what that means is GCC will build all components inside of one derivation, which takes a lot of time.
158460	161860	So you're not able to parallelize it that well.
162440	164400	It's also not multi-target.
164400	169540	What this essentially means is GCC only targets your current target.
169700	177760	Like if you have GCC targeting ARM, you can't specify to do x86 or you can't specify to do RISC-V.
177760	186200	You need to do an entirely new build, which could be another 30 minutes or on slower hardware a few hours of just compiling GCC.
189220	194780	And one of the problems we also have in Nix packages is cross-compilations are heavy.
195100	201600	One of the ways this is a problem is we essentially instantiate an entirely new instance of Nix packages.
201600	205700	So let's say Nix packages take about 4 gigs of RAM to evaluate.
206000	214200	You essentially create a new instance of that, which is another 4 gigs of RAM being allocated just to be able to cross-compile that.
215260	225280	There is another way of doing cross-compilation, which is emulated, but that is not real cross-compilation and there's a lot of issues with that.
228400	236720	So pretty much the plan is to streamline how we handle cross-compilation right now in Nix packages.
239200	249280	So part of that plan is to redo how the C compiler is introduced in or loaded in Nix packages.
249280	256360	Currently there is a CC attribute, which contains the C compiler, which is essentially the entire toolchain for C.
256360	268300	Inside the standard environment, there is an SCM no-libc function, or no-cc set, which is the SCM without a C compiler.
268560	278740	So the idea is to make that essentially the default, and for more packages to use that by default without a C compiler.
278740	285440	Instead of explicitly inheriting a C compiler without implicitly having one.
287500	297320	One of the things we are doing is also adding new toolchain attributes, so this is part of the splitting of GCC.
298000	300560	So now it's all going to be in their own components.
303540	305600	Why cross-compiling is important?
305920	314400	If you want to target an AVR, you can't really compile your code on an AVR device, or you can't compile on an ESP32.
314400	321820	You have to have a toolchain which can cross-compile from your host system to the target system.
322420	327200	It's also useful for finding bugs in builds without having the actual hardware.
327200	338440	So if you want to support RISC-V, but you just don't have any hardware for it, you then can use a cross-compilation build.
338440	351320	And then you can also integrate that with QMU and NixOS VMs, and have an automated test suite essentially to be able to build and test for any device that you don't really have.
354000	358960	There's two kinds of methodologies into cross-compilation in Nix packages.
358960	375840	There is packages-across, which gives you a set of various platforms like targeting PowerPC, x86, 32-bit and 64-bit, RISC-V, 32-bit, 64-bit, all that.
376580	381060	They create in a new Nix packages instance, which we call a variant now.
381060	385300	It's pretty simple to use, but it's harder to chain up.
385300	392440	Let's say if you want to do a statically linked LLVM build for RISC-V, you can't really chain that.
392440	397640	A lot of the attributes just collide and you lose a lot of that inheriting that's going on.
397640	407460	You can do this by manually importing Nix packages and specifying the attributes, but that is not really recommended currently in Nix packages.
408300	410880	There is the override way.
410900	418180	Essentially, the SEM is an attribute that everyone pulls in to create a package.
419160	430540	That's actually powerful because if you use an override, an overlay onto a package, you can actually override that package and then have a new standard environment.
431440	441720	This will change how we have disjoining CC and SEM, but essentially it is the same principle going on.
445660	450640	A new way of doing CROSS we have being worked on is called CROSS-SEM.
450640	454060	It is a chainable attribute set.
454060	465200	As you can tell by the example, we are able to define a CROSS-compiling standard environment for ARM that is statically linked.
466280	476100	That's a lot of a more streamlined way of doing it rather than being able to create a new Nix packages instance.
477000	482220	The idea is to also do one stage of bootstrapping.
482220	487380	With CROSS-compiling currently, it goes through three to four stages.
487680	493340	It builds from your host to target, then build to target.
493340	501340	It does this weird set of chaining that takes a lot of resources, a lot of disk space, increases your closure size.
501580	506580	The new way is to be CROSS-compiling as minimal as possible.
506580	516780	Essentially, if you only need to have this specific library for that architecture or whatever you are targeting, it is only going to be building that.
518760	523420	It is easier to manipulate and mutate.
524140	528020	It is also explicit CROSS versus implicit.
528020	537120	With doing packages across, it depends on what you put into the native build inputs or build inputs.
537140	552120	But being able to override and specify that you want this specific package as a dependency for the package you are CROSS-compiling and have that one compiled instead rather than having it implicitly done is pretty powerful.
552800	556320	This is going to work a lot better with multi-target compilers.
556320	567920	This will make it easier to compile your compiler once and then target any platform.
568820	572780	There is a PR fork right now that has been slow to work on.
573700	576640	There is something called GCCNG.
576780	581660	John Erickson and I have been working on this for about two months now.
581660	589980	It is a very LLVM-brained way of doing GCC where everything is on their own components.
589980	596780	We actually splice it better so we are able to handle CROSS-compiling a lot better.
600980	606680	This version of GCC is also easier to work with vendor forks.
606680	616060	Some vendors like Tenstore or Expressive like to fork and change GCC and different compilers around.
616060	630600	Being able to point GCCNG to whatever that source is and tell it to compile and get the compiler is a lot easier to work with than trying to manually do it or do some weird overrides.
630600	633520	We are weirdly importing it in a way.
633920	644350	There is a function called buildGCCPackageSet which spits out an entire GCC package set for whatever version of GCC you want.
645510	666110	LLVM is getting the same sort of treatment where I have been for pretty much a year now refactoring a lot of how LLVM works to make it work better with splicing and overrides and being able to be a lot more flexible and easier to work with but more maintainable.
666410	671510	The entire idea is to streamline how we are doing toolchains.
671510	677830	I actually do have a bit of a demo of how we can do this currently.
692510	697190	As you can tell, these are three different kinds of ways of doing cross compiling.
697550	700710	This is the PackagesCross method.
700710	706190	You then have the Override method which you can see we just specify PackagesCross and pull in the same environment.
706510	713350	And then this is the sort of new way of doing it with the CrossSDN.
713350	720790	As you can tell, we are doing cross compiling for a 32-bit ARM and doing it with Hello.
721570	724930	And then if I give it a start,
730710	735110	you can see it's just evaluated everything and now it's actually cross compiling all those.
736950	741350	And there's actually a lot of logic of how this works currently.
741630	753330	But with a lot of new work, this will be very streamlined once we have a more override-friendly way of doing cross compiling and manipulating toolchains.
754210	755930	Any questions?
761390	763510	No, but...
764270	775790	We can't really use it with the badges because the Espressif IDF, which is their SDK, is very strict with how it wants to be used.
775790	781050	You technically could, but we haven't had the time to actually do that.
781050	784130	And they're using the infrastructure in these packages right now.
784190	786550	But it technically is possible.
793000	794540	Explaining splicing.
794680	799280	Essentially, the idea is that
802780	807080	a lot of how Nix Packages works is it happens in a series of...
807080	810840	You can imagine everything as an overlay in Nix Packages.
811000	815160	And let's say if you want to override something, you have to splice it.
815160	820300	Essentially, that means you cut it out at that point and inject something new.
820300	827600	We do this a lot with the three different build platforms.
828240	833360	This is a way of how things are done with compilers.
833360	836720	You have build, host, and target.
836880	840620	Those define how things are pointed to and what supports what.
840620	846220	Host is the system you're natively running on.
846220	848680	But the build is what you're actually building on.
848680	851780	And then target is whatever devices you're targeting.
851860	856240	And so we have three attribute sets which mimic that behavior.
856240	860980	We have build packages, host packages, and target packages.
861460	867100	And splicing changes based on what phase of Nix Packages you're in.
868240	875780	And so using that make function that handles splicing a lot better...
877260	886920	Yeah, that essentially controls how the attribute set is defined with overrides.
887880	893660	There's a lot of logic in actual Nix Packages of how to handle the splicing correctly.
900050	901650	Any other questions?
901650	902430	Does
906680	910860	this change anything with regards to cross-compilation in Nix?
910860	913520	Does it make it better or is it still changing?
914000	918860	I don't see it impacting flakes currently.
918940	927860	But I could see it in the future being a stepping stone to improving cross-compilation.
928740	931120	I have floated some ideas with...
932560	934460	I'm trying to think of his name...
934460	936460	It's Rob...
937500	940480	The other Rob, but he's on...
940480	943760	I forget the last name, but he's on the Nix team.
943760	958040	And we've had some discussions because one of the things I brought up was adding a definition that pulls in more information about your host into flakes.
958340	961860	And so there's been some discussions about that a few months ago.
961860	966080	But this could definitely be some sort of thing to look at.
966140	970220	Improving Nix itself to make flakes handle cross-compilation better.
