1 00:00:00,000 --> 00:00:07,860 So my talk is on doing toolchains declaratively, which is very powerful with Nix. 2 00:00:11,980 --> 00:00:15,000 So the main thing is... 3 00:00:23,100 --> 00:00:31,420 Toolchains essentially allow for being able to target different platforms that are compiling code between systems. 4 00:00:43,920 --> 00: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. 5 00:00:54,720 --> 00:00:58,520 With Nix we are able to put them in a very specific location. 6 00:00:58,520 --> 00:00:59,980 It's very powerful. 7 00:01:01,680 --> 00: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. 8 00:01:08,500 --> 00: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. 9 00:01:19,100 --> 00:01:27,860 With Nix we can easily do this and distribute it between multiple systems using remote builders and various other features. 10 00:01:34,340 --> 00:01:40,560 So the more of Nix way of doing things is splitting toolchains into their base components. 11 00:01:40,560 --> 00: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. 12 00:01:54,900 --> 00:02:01,500 Currently LLVM and Nix packages does this, but we're working on this with GCC right now. 13 00:02:01,500 --> 00:02:05,460 Another powerful thing is being able to have multiple versions of a compiler. 14 00:02:06,040 --> 00: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. 15 00:02:22,080 --> 00:02:28,600 So one of the current problems we have with compilers in Nix packages right now is GCC is very monolithic. 16 00:02:28,600 --> 00:02:38,260 Essentially what that means is GCC will build all components inside of one derivation, which takes a lot of time. 17 00:02:38,460 --> 00:02:41,860 So you're not able to parallelize it that well. 18 00:02:42,440 --> 00:02:44,400 It's also not multi-target. 19 00:02:44,400 --> 00:02:49,540 What this essentially means is GCC only targets your current target. 20 00:02:49,700 --> 00: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. 21 00:02:57,760 --> 00: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. 22 00:03:09,220 --> 00:03:14,780 And one of the problems we also have in Nix packages is cross-compilations are heavy. 23 00:03:15,100 --> 00:03:21,600 One of the ways this is a problem is we essentially instantiate an entirely new instance of Nix packages. 24 00:03:21,600 --> 00:03:25,700 So let's say Nix packages take about 4 gigs of RAM to evaluate. 25 00:03:26,000 --> 00: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. 26 00:03:35,260 --> 00: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. 27 00:03:48,400 --> 00:03:56,720 So pretty much the plan is to streamline how we handle cross-compilation right now in Nix packages. 28 00:03:59,200 --> 00:04:09,280 So part of that plan is to redo how the C compiler is introduced in or loaded in Nix packages. 29 00:04:09,280 --> 00:04:16,360 Currently there is a CC attribute, which contains the C compiler, which is essentially the entire toolchain for C. 30 00:04:16,360 --> 00: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. 31 00:04:28,560 --> 00: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. 32 00:04:38,740 --> 00:04:45,440 Instead of explicitly inheriting a C compiler without implicitly having one. 33 00:04:47,500 --> 00: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. 34 00:04:58,000 --> 00:05:00,560 So now it's all going to be in their own components. 35 00:05:03,540 --> 00:05:05,600 Why cross-compiling is important? 36 00:05:05,920 --> 00: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. 37 00:05:14,400 --> 00:05:21,820 You have to have a toolchain which can cross-compile from your host system to the target system. 38 00:05:22,420 --> 00:05:27,200 It's also useful for finding bugs in builds without having the actual hardware. 39 00:05:27,200 --> 00: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. 40 00:05:38,440 --> 00: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. 41 00:05:54,000 --> 00:05:58,960 There's two kinds of methodologies into cross-compilation in Nix packages. 42 00:05:58,960 --> 00: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. 43 00:06:16,580 --> 00:06:21,060 They create in a new Nix packages instance, which we call a variant now. 44 00:06:21,060 --> 00:06:25,300 It's pretty simple to use, but it's harder to chain up. 45 00:06:25,300 --> 00: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. 46 00:06:32,440 --> 00:06:37,640 A lot of the attributes just collide and you lose a lot of that inheriting that's going on. 47 00:06:37,640 --> 00: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. 48 00:06:48,300 --> 00:06:50,880 There is the override way. 49 00:06:50,900 --> 00:06:58,180 Essentially, the SEM is an attribute that everyone pulls in to create a package. 50 00:06:59,160 --> 00: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. 51 00:07:11,440 --> 00:07:21,720 This will change how we have disjoining CC and SEM, but essentially it is the same principle going on. 52 00:07:25,660 --> 00:07:30,640 A new way of doing CROSS we have being worked on is called CROSS-SEM. 53 00:07:30,640 --> 00:07:34,060 It is a chainable attribute set. 54 00:07:34,060 --> 00: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. 55 00:07:46,280 --> 00: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. 56 00:07:57,000 --> 00:08:02,220 The idea is to also do one stage of bootstrapping. 57 00:08:02,220 --> 00:08:07,380 With CROSS-compiling currently, it goes through three to four stages. 58 00:08:07,680 --> 00:08:13,340 It builds from your host to target, then build to target. 59 00:08:13,340 --> 00: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. 60 00:08:21,580 --> 00:08:26,580 The new way is to be CROSS-compiling as minimal as possible. 61 00:08:26,580 --> 00: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. 62 00:08:38,760 --> 00:08:43,420 It is easier to manipulate and mutate. 63 00:08:44,140 --> 00:08:48,020 It is also explicit CROSS versus implicit. 64 00:08:48,020 --> 00:08:57,120 With doing packages across, it depends on what you put into the native build inputs or build inputs. 65 00:08:57,140 --> 00: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. 66 00:09:12,800 --> 00:09:16,320 This is going to work a lot better with multi-target compilers. 67 00:09:16,320 --> 00:09:27,920 This will make it easier to compile your compiler once and then target any platform. 68 00:09:28,820 --> 00:09:32,780 There is a PR fork right now that has been slow to work on. 69 00:09:33,700 --> 00:09:36,640 There is something called GCCNG. 70 00:09:36,780 --> 00:09:41,660 John Erickson and I have been working on this for about two months now. 71 00:09:41,660 --> 00:09:49,980 It is a very LLVM-brained way of doing GCC where everything is on their own components. 72 00:09:49,980 --> 00:09:56,780 We actually splice it better so we are able to handle CROSS-compiling a lot better. 73 00:10:00,980 --> 00:10:06,680 This version of GCC is also easier to work with vendor forks. 74 00:10:06,680 --> 00:10:16,060 Some vendors like Tenstore or Expressive like to fork and change GCC and different compilers around. 75 00:10:16,060 --> 00: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. 76 00:10:30,600 --> 00:10:33,520 We are weirdly importing it in a way. 77 00:10:33,920 --> 00:10:44,350 There is a function called buildGCCPackageSet which spits out an entire GCC package set for whatever version of GCC you want. 78 00:10:45,510 --> 00: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. 79 00:11:06,410 --> 00:11:11,510 The entire idea is to streamline how we are doing toolchains. 80 00:11:11,510 --> 00:11:17,830 I actually do have a bit of a demo of how we can do this currently. 81 00:11:32,510 --> 00:11:37,190 As you can tell, these are three different kinds of ways of doing cross compiling. 82 00:11:37,550 --> 00:11:40,710 This is the PackagesCross method. 83 00:11:40,710 --> 00:11:46,190 You then have the Override method which you can see we just specify PackagesCross and pull in the same environment. 84 00:11:46,510 --> 00:11:53,350 And then this is the sort of new way of doing it with the CrossSDN. 85 00:11:53,350 --> 00:12:00,790 As you can tell, we are doing cross compiling for a 32-bit ARM and doing it with Hello. 86 00:12:01,570 --> 00:12:04,930 And then if I give it a start, 87 00:12:10,710 --> 00:12:15,110 you can see it's just evaluated everything and now it's actually cross compiling all those. 88 00:12:16,950 --> 00:12:21,350 And there's actually a lot of logic of how this works currently. 89 00:12:21,630 --> 00: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. 90 00:12:34,210 --> 00:12:35,930 Any questions? 91 00:12:41,390 --> 00:12:43,510 No, but... 92 00:12:44,270 --> 00: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. 93 00:12:55,790 --> 00:13:01,050 You technically could, but we haven't had the time to actually do that. 94 00:13:01,050 --> 00:13:04,130 And they're using the infrastructure in these packages right now. 95 00:13:04,190 --> 00:13:06,550 But it technically is possible. 96 00:13:13,000 --> 00:13:14,540 Explaining splicing. 97 00:13:14,680 --> 00:13:19,280 Essentially, the idea is that 98 00:13:22,780 --> 00:13:27,080 a lot of how Nix Packages works is it happens in a series of... 99 00:13:27,080 --> 00:13:30,840 You can imagine everything as an overlay in Nix Packages. 100 00:13:31,000 --> 00:13:35,160 And let's say if you want to override something, you have to splice it. 101 00:13:35,160 --> 00:13:40,300 Essentially, that means you cut it out at that point and inject something new. 102 00:13:40,300 --> 00:13:47,600 We do this a lot with the three different build platforms. 103 00:13:48,240 --> 00:13:53,360 This is a way of how things are done with compilers. 104 00:13:53,360 --> 00:13:56,720 You have build, host, and target. 105 00:13:56,880 --> 00:14:00,620 Those define how things are pointed to and what supports what. 106 00:14:00,620 --> 00:14:06,220 Host is the system you're natively running on. 107 00:14:06,220 --> 00:14:08,680 But the build is what you're actually building on. 108 00:14:08,680 --> 00:14:11,780 And then target is whatever devices you're targeting. 109 00:14:11,860 --> 00:14:16,240 And so we have three attribute sets which mimic that behavior. 110 00:14:16,240 --> 00:14:20,980 We have build packages, host packages, and target packages. 111 00:14:21,460 --> 00:14:27,100 And splicing changes based on what phase of Nix Packages you're in. 112 00:14:28,240 --> 00:14:35,780 And so using that make function that handles splicing a lot better... 113 00:14:37,260 --> 00:14:46,920 Yeah, that essentially controls how the attribute set is defined with overrides. 114 00:14:47,880 --> 00:14:53,660 There's a lot of logic in actual Nix Packages of how to handle the splicing correctly. 115 00:15:00,050 --> 00:15:01,650 Any other questions? 116 00:15:01,650 --> 00:15:02,430 Does 117 00:15:06,680 --> 00:15:10,860 this change anything with regards to cross-compilation in Nix? 118 00:15:10,860 --> 00:15:13,520 Does it make it better or is it still changing? 119 00:15:14,000 --> 00:15:18,860 I don't see it impacting flakes currently. 120 00:15:18,940 --> 00:15:27,860 But I could see it in the future being a stepping stone to improving cross-compilation. 121 00:15:28,740 --> 00:15:31,120 I have floated some ideas with... 122 00:15:32,560 --> 00:15:34,460 I'm trying to think of his name... 123 00:15:34,460 --> 00:15:36,460 It's Rob... 124 00:15:37,500 --> 00:15:40,480 The other Rob, but he's on... 125 00:15:40,480 --> 00:15:43,760 I forget the last name, but he's on the Nix team. 126 00:15:43,760 --> 00: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. 127 00:15:58,340 --> 00:16:01,860 And so there's been some discussions about that a few months ago. 128 00:16:01,860 --> 00:16:06,080 But this could definitely be some sort of thing to look at. 129 00:16:06,140 --> 00:16:10,220 Improving Nix itself to make flakes handle cross-compilation better.