WEBVTT

00:00.000 --> 00:07.860
So my talk is on doing toolchains declaratively, which is very powerful with Nix.

00:11.980 --> 00:15.000
So the main thing is...

00:23.100 --> 00:31.420
Toolchains essentially allow for being able to target different platforms that are compiling code between systems.

00:43.920 --> 00:54.220
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.

00:54.720 --> 00:58.520
With Nix we are able to put them in a very specific location.

00:58.520 --> 00:59.980
It's very powerful.

01:01.680 --> 01:08.500
And one of the other problems is toolchains can only target one platform until you have to compile it for another platform like GCC.

01:08.500 --> 01:18.900
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.

01:19.100 --> 01:27.860
With Nix we can easily do this and distribute it between multiple systems using remote builders and various other features.

01:34.340 --> 01:40.560
So the more of Nix way of doing things is splitting toolchains into their base components.

01:40.560 --> 01:54.900
So this would be having a derivation specifically for your C compiler, runtime libraries, your C++ implementation, pin tools and all that.

01:54.900 --> 02:01.500
Currently LLVM and Nix packages does this, but we're working on this with GCC right now.

02:01.500 --> 02:05.460
Another powerful thing is being able to have multiple versions of a compiler.

02:06.040 --> 02:17.160
Because some code might like only certain versions of a compiler and being able to change versions of a compiler dynamically is very useful.

02:22.080 --> 02:28.600
So one of the current problems we have with compilers in Nix packages right now is GCC is very monolithic.

02:28.600 --> 02:38.260
Essentially what that means is GCC will build all components inside of one derivation, which takes a lot of time.

02:38.460 --> 02:41.860
So you're not able to parallelize it that well.

02:42.440 --> 02:44.400
It's also not multi-target.

02:44.400 --> 02:49.540
What this essentially means is GCC only targets your current target.

02:49.700 --> 02:57.760
Like if you have GCC targeting ARM, you can't specify to do x86 or you can't specify to do RISC-V.

02:57.760 --> 03:06.200
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.

03:09.220 --> 03:14.780
And one of the problems we also have in Nix packages is cross-compilations are heavy.

03:15.100 --> 03:21.600
One of the ways this is a problem is we essentially instantiate an entirely new instance of Nix packages.

03:21.600 --> 03:25.700
So let's say Nix packages take about 4 gigs of RAM to evaluate.

03:26.000 --> 03:34.200
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.

03:35.260 --> 03:45.280
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.

03:48.400 --> 03:56.720
So pretty much the plan is to streamline how we handle cross-compilation right now in Nix packages.

03:59.200 --> 04:09.280
So part of that plan is to redo how the C compiler is introduced in or loaded in Nix packages.

04:09.280 --> 04:16.360
Currently there is a CC attribute, which contains the C compiler, which is essentially the entire toolchain for C.

04:16.360 --> 04:28.300
Inside the standard environment, there is an SCM no-libc function, or no-cc set, which is the SCM without a C compiler.

04:28.560 --> 04:38.740
So the idea is to make that essentially the default, and for more packages to use that by default without a C compiler.

04:38.740 --> 04:45.440
Instead of explicitly inheriting a C compiler without implicitly having one.

04:47.500 --> 04:57.320
One of the things we are doing is also adding new toolchain attributes, so this is part of the splitting of GCC.

04:58.000 --> 05:00.560
So now it's all going to be in their own components.

05:03.540 --> 05:05.600
Why cross-compiling is important?

05:05.920 --> 05:14.400
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.

05:14.400 --> 05:21.820
You have to have a toolchain which can cross-compile from your host system to the target system.

05:22.420 --> 05:27.200
It's also useful for finding bugs in builds without having the actual hardware.

05:27.200 --> 05:38.440
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.

05:38.440 --> 05:51.320
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.

05:54.000 --> 05:58.960
There's two kinds of methodologies into cross-compilation in Nix packages.

05:58.960 --> 06:15.840
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.

06:16.580 --> 06:21.060
They create in a new Nix packages instance, which we call a variant now.

06:21.060 --> 06:25.300
It's pretty simple to use, but it's harder to chain up.

06:25.300 --> 06:32.440
Let's say if you want to do a statically linked LLVM build for RISC-V, you can't really chain that.

06:32.440 --> 06:37.640
A lot of the attributes just collide and you lose a lot of that inheriting that's going on.

06:37.640 --> 06:47.460
You can do this by manually importing Nix packages and specifying the attributes, but that is not really recommended currently in Nix packages.

06:48.300 --> 06:50.880
There is the override way.

06:50.900 --> 06:58.180
Essentially, the SEM is an attribute that everyone pulls in to create a package.

06:59.160 --> 07:10.540
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.

07:11.440 --> 07:21.720
This will change how we have disjoining CC and SEM, but essentially it is the same principle going on.

07:25.660 --> 07:30.640
A new way of doing CROSS we have being worked on is called CROSS-SEM.

07:30.640 --> 07:34.060
It is a chainable attribute set.

07:34.060 --> 07:45.200
As you can tell by the example, we are able to define a CROSS-compiling standard environment for ARM that is statically linked.

07:46.280 --> 07:56.100
That's a lot of a more streamlined way of doing it rather than being able to create a new Nix packages instance.

07:57.000 --> 08:02.220
The idea is to also do one stage of bootstrapping.

08:02.220 --> 08:07.380
With CROSS-compiling currently, it goes through three to four stages.

08:07.680 --> 08:13.340
It builds from your host to target, then build to target.

08:13.340 --> 08:21.340
It does this weird set of chaining that takes a lot of resources, a lot of disk space, increases your closure size.

08:21.580 --> 08:26.580
The new way is to be CROSS-compiling as minimal as possible.

08:26.580 --> 08:36.780
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.

08:38.760 --> 08:43.420
It is easier to manipulate and mutate.

08:44.140 --> 08:48.020
It is also explicit CROSS versus implicit.

08:48.020 --> 08:57.120
With doing packages across, it depends on what you put into the native build inputs or build inputs.

08:57.140 --> 09:12.120
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.

09:12.800 --> 09:16.320
This is going to work a lot better with multi-target compilers.

09:16.320 --> 09:27.920
This will make it easier to compile your compiler once and then target any platform.

09:28.820 --> 09:32.780
There is a PR fork right now that has been slow to work on.

09:33.700 --> 09:36.640
There is something called GCCNG.

09:36.780 --> 09:41.660
John Erickson and I have been working on this for about two months now.

09:41.660 --> 09:49.980
It is a very LLVM-brained way of doing GCC where everything is on their own components.

09:49.980 --> 09:56.780
We actually splice it better so we are able to handle CROSS-compiling a lot better.

10:00.980 --> 10:06.680
This version of GCC is also easier to work with vendor forks.

10:06.680 --> 10:16.060
Some vendors like Tenstore or Expressive like to fork and change GCC and different compilers around.

10:16.060 --> 10:30.600
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.

10:30.600 --> 10:33.520
We are weirdly importing it in a way.

10:33.920 --> 10:44.350
There is a function called buildGCCPackageSet which spits out an entire GCC package set for whatever version of GCC you want.

10:45.510 --> 11:06.110
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.

11:06.410 --> 11:11.510
The entire idea is to streamline how we are doing toolchains.

11:11.510 --> 11:17.830
I actually do have a bit of a demo of how we can do this currently.

11:32.510 --> 11:37.190
As you can tell, these are three different kinds of ways of doing cross compiling.

11:37.550 --> 11:40.710
This is the PackagesCross method.

11:40.710 --> 11:46.190
You then have the Override method which you can see we just specify PackagesCross and pull in the same environment.

11:46.510 --> 11:53.350
And then this is the sort of new way of doing it with the CrossSDN.

11:53.350 --> 12:00.790
As you can tell, we are doing cross compiling for a 32-bit ARM and doing it with Hello.

12:01.570 --> 12:04.930
And then if I give it a start,

12:10.710 --> 12:15.110
you can see it's just evaluated everything and now it's actually cross compiling all those.

12:16.950 --> 12:21.350
And there's actually a lot of logic of how this works currently.

12:21.630 --> 12:33.330
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.

12:34.210 --> 12:35.930
Any questions?

12:41.390 --> 12:43.510
No, but...

12:44.270 --> 12:55.790
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.

12:55.790 --> 13:01.050
You technically could, but we haven't had the time to actually do that.

13:01.050 --> 13:04.130
And they're using the infrastructure in these packages right now.

13:04.190 --> 13:06.550
But it technically is possible.

13:13.000 --> 13:14.540
Explaining splicing.

13:14.680 --> 13:19.280
Essentially, the idea is that

13:22.780 --> 13:27.080
a lot of how Nix Packages works is it happens in a series of...

13:27.080 --> 13:30.840
You can imagine everything as an overlay in Nix Packages.

13:31.000 --> 13:35.160
And let's say if you want to override something, you have to splice it.

13:35.160 --> 13:40.300
Essentially, that means you cut it out at that point and inject something new.

13:40.300 --> 13:47.600
We do this a lot with the three different build platforms.

13:48.240 --> 13:53.360
This is a way of how things are done with compilers.

13:53.360 --> 13:56.720
You have build, host, and target.

13:56.880 --> 14:00.620
Those define how things are pointed to and what supports what.

14:00.620 --> 14:06.220
Host is the system you're natively running on.

14:06.220 --> 14:08.680
But the build is what you're actually building on.

14:08.680 --> 14:11.780
And then target is whatever devices you're targeting.

14:11.860 --> 14:16.240
And so we have three attribute sets which mimic that behavior.

14:16.240 --> 14:20.980
We have build packages, host packages, and target packages.

14:21.460 --> 14:27.100
And splicing changes based on what phase of Nix Packages you're in.

14:28.240 --> 14:35.780
And so using that make function that handles splicing a lot better...

14:37.260 --> 14:46.920
Yeah, that essentially controls how the attribute set is defined with overrides.

14:47.880 --> 14:53.660
There's a lot of logic in actual Nix Packages of how to handle the splicing correctly.

15:00.050 --> 15:01.650
Any other questions?

15:01.650 --> 15:02.430
Does

15:06.680 --> 15:10.860
this change anything with regards to cross-compilation in Nix?

15:10.860 --> 15:13.520
Does it make it better or is it still changing?

15:14.000 --> 15:18.860
I don't see it impacting flakes currently.

15:18.940 --> 15:27.860
But I could see it in the future being a stepping stone to improving cross-compilation.

15:28.740 --> 15:31.120
I have floated some ideas with...

15:32.560 --> 15:34.460
I'm trying to think of his name...

15:34.460 --> 15:36.460
It's Rob...

15:37.500 --> 15:40.480
The other Rob, but he's on...

15:40.480 --> 15:43.760
I forget the last name, but he's on the Nix team.

15:43.760 --> 15:58.040
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.

15:58.340 --> 16:01.860
And so there's been some discussions about that a few months ago.

16:01.860 --> 16:06.080
But this could definitely be some sort of thing to look at.

16:06.140 --> 16:10.220
Improving Nix itself to make flakes handle cross-compilation better.

