[00:00.500 --> 00:01.680] Okay, great. [00:01.680 --> 00:02.120] Cool. [00:02.120 --> 00:03.260] Thanks for coming, everyone. [00:03.900 --> 00:05.200] This is my talk. [00:05.200 --> 00:06.800] I want to talk about auto formatting. [00:07.520 --> 00:09.520] And I'm excited to see everyone here. [00:09.720 --> 00:11.720] So a little bit about me. [00:11.720 --> 00:13.260] My name is Jeremy Fleischman. [00:13.260 --> 00:15.340] You'll see me on GitHub as JFly. [00:15.400 --> 00:18.040] On the left there is my profile picture. [00:18.100 --> 00:20.540] The reason I have so much hair is because it's from 2010. [00:21.200 --> 00:22.900] I was very into Rubik's cubes. [00:22.900 --> 00:27.060] This is right after I won US Nationals forever ago. [00:27.460 --> 00:30.220] And I've been using Nix since 2021. [00:30.840 --> 00:36.440] And I quit my job last year to learn more about Nix and try to give back to the community. [00:36.520 --> 00:41.120] So I've become a maintainer of TreeFMT, which I want to talk about today. [00:41.200 --> 00:43.140] I'm also on the infra team. [00:43.260 --> 00:46.980] And I recently became the leader of the Nix formatting team. [00:47.360 --> 00:56.580] So my goals for this talk are if you don't already use TreeFMT, I'd love to convince you that it's a useful tool and for have you check it out. [00:56.880 --> 01:02.220] If you're a NeoVim user, I'd love for you to check out this built in that I added to NunLS. [01:02.400 --> 01:04.900] It's called Nix Flake FMT . [01:05.100 --> 01:10.560] And if you use something other than NeoVim, I'd like to persuade you to build something like that for your own editor. [01:11.000 --> 01:12.980] And lastly, contribute to Nix. [01:12.980 --> 01:15.260] It's fun and not scary. [01:16.060 --> 01:19.460] So I'm going to get on my TreeFMT soapbox first. [01:19.560 --> 01:23.340] I realized while prepping for this talk, I didn't actually know how to pronounce the name of this tool. [01:23.340 --> 01:24.700] I'd been saying TreeFmt. [01:24.860 --> 01:27.360] So here's my little mnemonic to help you remember. [01:27.360 --> 01:28.000] Tree. [01:28.100 --> 01:28.820] FM. [01:29.060 --> 01:29.560] T. [01:30.880 --> 01:33.160] Okay, so why would you want to use this? [01:33.160 --> 01:34.260] Why does this exist? [01:34.400 --> 01:37.700] So here's some real code I ran across in Image. [01:37.700 --> 01:40.320] I was contributing to their mobile app the other day. [01:40.320 --> 01:41.780] And they've got this make file. [01:41.780 --> 01:46.540] And I think this is like a perfect example of the sort of thing you see in make files around the world. [01:46.540 --> 01:48.920] Which is, I want to start formatting. [01:48.920 --> 01:50.660] I add a make format target. [01:51.160 --> 01:53.200] And then I like invoke my formatter. [01:53.320 --> 01:55.920] And this one's a great example of like Dart format. [01:56.120 --> 01:59.080] Tries to recurse through your file system and find all your Dart files. [01:59.080 --> 02:03.780] But there's this like bug where it can't ignore, you can't tell it to ignore some files . [02:03.780 --> 02:07.600] And the image has some auto-generated files that they don't want to auto-format. [02:07.600 --> 02:15.720] So they have to like do this shell hackery with find to like find all the Dart files except for these auto -generated Dart files. [02:15.880 --> 02:16.800] And this works for them. [02:16.800 --> 02:24.800] But you can imagine if you were going to add another formatter, then you've got to write another incantation like this and you're going to be recursing through the file system multiple times. [02:25.140 --> 02:29.160] Maybe it doesn't matter in small projects, but that's why TreeFMD exists. [02:29.160 --> 02:31.020] So let me show you what it looks like. [02:31.420 --> 02:34.840] Uh, so it basically defines this thing called the formatter specification. [02:34.840 --> 02:36.240] It's very simple. [02:36.460 --> 02:41.780] The very short version of it is, if you implement a formatter, try to make it follow this CLI. [02:41.780 --> 02:48.000] So it's like command, some options, and then a list of one or more files to format. [02:48.000 --> 02:55.760] So NixFMT, which is a Haskell tool that the Nix formatting team maintains, it obeys the specification. [02:55.760 --> 03:02.200] If you want to format a Nix file, you say NixFMT, you feed it a list of Nix files and it formats them. [03:02.960 --> 03:10.900] Uh, so what TreeFMD will do is if your formatter implements this specification, we will walk the file system for you. [03:10.900 --> 03:16.660] And we'll do it fast, and we'll understand Go, we'll under, er, understand Git, we'll understand JJ, whatever. [03:16.800 --> 03:23.100] You don't need to worry about those nuances in your formatter, so make it simpler for you and for anybody who wants to format a codebase . [03:23.440 --> 03:26.020] We'll walk the file system consistently. [03:27.200 --> 03:31.200] Uh, so to take a look at that earlier example, we have this code. [03:31.560 --> 03:33.480] In TreeFMD, you'd write something like this. [03:33.480 --> 03:34.880] It's a simple TOML file. [03:35.040 --> 03:37.440] You specify all your formatters for all your files. [03:37.440 --> 03:45.600] You say, this is the command, these are the options, format these files, and if you want to exclude some files for whatever reason, you can. [03:46.240 --> 03:47.560] Uh, and here you go. [03:47.560 --> 03:53.100] So this is TreeFMD, we use it in Nix packages to enforce formatting across the entire codebase. [03:53.140 --> 03:57.760] Uh, and you can see, this is me running TreeFMD in a Nix packages checkout. [03:57.760 --> 04:07.400] It took 40 seconds to chug through 49,000 files, and then if I run it again without making any changes, it chugs through everything in 400 milliseconds. [04:07.400 --> 04:08.420] So we also do caching. [04:08.420 --> 04:14.860] We, we understand if a file hasn't changed, there's no reason to invoke the formatter again, it's, it doesn't need to be formatted. [04:16.160 --> 04:20.500] Okay, but we're Nixers, we, we see a config file like this and we ask some questions. [04:20.500 --> 04:22.580] We're like, where does Dart come from? [04:22.580 --> 04:24.720] Which version of TreeFMD are you going to use? [04:24.760 --> 04:25.520] Etcetera. [04:25.520 --> 04:32.220] So, for basically any tool out there, some, somebody has written a Nix abstraction on top of it to let you configure it. [04:32.220 --> 04:40.140] So, there's a tool called TreeFMD Nix, and if you're a Nixer attending this talk, I'd suggest that this be your gateway into TreeFMD. [04:40.260 --> 04:45.000] So, this is just some flake boilerplate because I wanted to show people a complete example. [04:45.000 --> 04:48.580] The interesting part is on the right here, this formatting.nix file. [04:48.580 --> 04:50.920] So, I'll just throw away the boilerplate. [04:50.920 --> 04:53.380] So, this is the, the TreeFMD Nix stuff. [04:53.380 --> 04:57.420] You basically say, okay, I want to enable some TreeFMD programs. [04:57.820 --> 04:59.520] There's one called Dart format. [04:59.520 --> 05:08.780] This is all configured with the Nix module system, so this is strongly typed, like, if you type in the name of some program that doesn't exist, you're going to get a clear error. [05:08.860 --> 05:11.080] And you'll notice there's some stuff missing here. [05:11.080 --> 05:17.620] I've, I've specified the excludes, I've turned on the Dart format program, but I haven't said where the Dart package is. [05:17.620 --> 05:25.580] It is packaged in Nix packages, but somebody else wrote that code, figured out which exact attribute in Nix packages provides the formatter. [05:25.680 --> 05:34.640] And also the, the list of the, of the Dart, that's not here, because that's just intrinsic to the formatter, is what files it tries to format. [05:34.640 --> 05:36.960] So, you know, you get to delete a little bit of stuff. [05:36.960 --> 05:38.260] So, okay. [05:38.420 --> 05:41.520] And then if you do this, you get a couple of flake outputs. [05:41.540 --> 05:50.720] So, you get it enforced automatically in CI, we've got this check right here, that'll fail if you have files that are not correctly formatted. [05:50.720 --> 05:52.960] And then you also have a formatter output. [05:53.480 --> 06:00.240] And so there are a couple of CLI utilities that the, the Nix CLI provides that understands these flake outputs. [06:00.240 --> 06:03.560] So, this is the Nix flake check command, which you could use in CI. [06:03.840 --> 06:09.300] Uh, and then Nix space FMT, which will just go and, uh oh. [06:09.500 --> 06:10.500] Was that me? [06:12.680 --> 06:14.140] Uh oh. [06:16.160 --> 06:17.500] That's not me Morgan? [06:18.000 --> 06:19.120] Okay. [06:19.140 --> 06:20.240] It overheated? [06:21.040 --> 06:21.840] Okay. [06:21.840 --> 06:22.620] Wow. [06:22.620 --> 06:24.700] I didn't know projectors could crash. [06:26.840 --> 06:27.720] Okay. [06:30.700 --> 06:31.580] Hello. [06:31.740 --> 06:32.240] Okay. [06:32.240 --> 06:34.400] So, anyways, I'll just keep talking until it comes back. [06:34.400 --> 06:35.600] Oh, and we're back. [06:35.600 --> 06:36.040] Great. [06:36.040 --> 06:41.320] So, these are the two commands that you just get support for if you use TreeFMT Nix. [06:41.320 --> 06:41.940] Okay. [06:42.300 --> 06:45.620] So, to summarize, we started here and we ended here. [06:45.620 --> 06:49.340] So, and this isn't like really an apples to apples comparison. [06:49.340 --> 06:52.460] We benefit from all the, the power of Nix. [06:52.960 --> 06:58.000] We're, we're being very specific about the version of TreeFMT and the version of Dart that we're using here. [06:58.460 --> 07:00.520] And you get the benefits of TreeFMT too. [07:00.520 --> 07:01.920] We're walking the file system. [07:01.940 --> 07:02.820] We're doing it quickly. [07:02.820 --> 07:04.400] We understand your git ignores. [07:04.400 --> 07:09.020] If you use git submodules or something else wonky, we'll do something same. [07:09.880 --> 07:10.320] Okay. [07:10.320 --> 07:13.200] But, you know, it's nice to have it enforced in CI. [07:13.200 --> 07:16.220] It's nice to have a command you can run to format all the code. [07:16.420 --> 07:22.320] But when you're writing code, you're not thinking to run, like I'm going to drop into the shell and I'm going to like go format all the code. [07:22.320 --> 07:23.320] You're just writing code. [07:23.320 --> 07:25.740] So people try to push this further left. [07:25.760 --> 07:29.980] And so as a TreeFMT maintainer, people usually ask for features that we haven't implemented. [07:30.060 --> 07:32.260] One of which is pre-commit hooks. [07:32.320 --> 07:33.280] People build them. [07:33.280 --> 07:35.240] We don't have support for it in TreeFMT. [07:36.140 --> 07:44.160] Some people try to run things in the background that just watch the file system for changes and automatically invoke the formatters on the files that changed. [07:44.160 --> 07:46.760] That's like a little better because it's even further left. [07:46.760 --> 07:49.340] But then it starts to fight with your IDE a bit. [07:49.340 --> 07:57.320] Like if you save a change in your IDE and then some other tool goes and edits the file, your IDE will probably say, hey, whoa, somebody changed that file. [07:57.620 --> 07:58.820] What's going on here? [07:59.220 --> 08:02.380] So I think editor integration is the right way to go. [08:02.380 --> 08:04.240] That's what I want to talk about in the rest of the talk. [08:04.240 --> 08:07.080] And hopefully I don't need to convince you that that's the way to do it. [08:07.520 --> 08:15.080] So again, couldn't talk about NeoVim here, but if you're following along, I, you know, I really want people to build similar integrations for their own editors. [08:15.080 --> 08:18.700] So hopefully you can, you know, take inspiration from this. [08:19.680 --> 08:20.040] Okay. [08:20.040 --> 08:27.940] So what I wanted to do is I wanted to have a tool in NeoVim that would automatically run Nix space FMT on whatever files I changed. [08:27.940 --> 08:29.080] Super straightforward . [08:30.020 --> 08:34.740] But performance matters and Nix FMT, it turns out, is quite slow. [08:34.740 --> 08:41.880] So for example , in Nix packages, it takes 14 and a half seconds on my laptop to just figure out what the formatter is and build it. [08:42.320 --> 08:45.100] So you only need to do that once. [08:45.100 --> 08:46.160] I want to cache it. [08:46.240 --> 08:53.300] Uh, and so that's, uh, what I opted to do basically is like, let's figure out what the formatter is and let's cache it. [08:53.300 --> 08:55.040] And that ended up being a little tricky. [08:55.040 --> 08:56.860] So I'm going to breeze through the next slide. [08:56.960 --> 09:02.380] Cause a lot of this is just like NeoVim details that nobody cares about, except for me when I was building this. [09:02.880 --> 09:08.200] But I'm just going to like explain some of the stuff I did, which is like none LS is a natural place to put this. [09:08.200 --> 09:18.020] If you haven't heard of it, it's a plugin in the NeoVim ecosystem that basically lets you wrap existing shell commands and just hook them into NeoVim's LSP infrastructure. [09:18.800 --> 09:26.680] Unfortunately, it had this dynamic command thing, which was great, but it assumed that the command, discovering the command was quick. [09:26.680 --> 09:34.680] It was basically like used for finding binaries and node modules, which is like just search the file system to see if you've got something in the node modules directory. [09:34.740 --> 09:38.600] But this is like, oh wait, we've got to like spend 14 seconds figuring something out. [09:38.960 --> 09:45.380] I would like to be able to cache that and I needed to make some changes to dynamic command to make that possible. [09:45.380 --> 09:46.720] So whatever, doesn't matter. [09:46.720 --> 09:53.340] Um, there was another detail with TreeFMT where it wouldn't actually format the files because it like understands Git. [09:53.340 --> 09:55.640] It didn't want to format the temp files that none LS created. [09:55.640 --> 09:56.680] I fixed that. [09:56.680 --> 09:58.640] I ran into a bunch of other TreeFMT bugs. [09:58.640 --> 10:00.600] That's how I ended up becoming a maintainer. [10:00.880 --> 10:09.360] Uh, because Nix space FMT can run arbitrary code, I didn't want to just do that when you open a random project and hit save. [10:09.500 --> 10:12.420] So I hooked into NeoVim's trust infrastructure. [10:12.520 --> 10:15.480] Turned out the support for trusting directories was buggy. [10:15.480 --> 10:17.540] So that was another, that was a PR to NeoVim. [10:17.840 --> 10:24.160] And then this is the one I want to talk about, which is previously it was very hard in Nix to actually discover the formatter. [10:24.160 --> 10:30.740] You could invoke the formatter, you could even build it, but the actual like path to the formatter was not exposed by the Nix CLI. [10:31.020 --> 10:36.300] So at first I opted to just like work around this by writing a bunch of code in Lua. [10:37.160 --> 10:38.420] That was a mistake. [10:38.420 --> 10:39.660] I ended up writing a lot of code. [10:39.660 --> 10:41.180] I didn't get it right the first time. [10:41.200 --> 10:47.120] So I want to talk about what I should have done instead and you guys can benefit from the work I did. [10:47.120 --> 10:50.020] So anyways, it turned into a bunch of PRs to a bunch of places. [10:50.220 --> 10:51.440] Details don't really matter. [10:51.620 --> 10:52.020] Okay. [10:52.020 --> 10:54.120] So it landed in none LS. [10:54.120 --> 10:55.460] Somebody actually accepted my PR. [10:55.460 --> 10:56.200] So that's great. [10:56.200 --> 10:57.000] It's there. [10:57.820 --> 11:02.060] Uh, one of my fellow maintainers on none LS noticed that it was like 300 lines of Lua. [11:02.060 --> 11:03.980] It was like, this probably doesn't belong here. [11:03.980 --> 11:05.360] I totally agreed with him. [11:05.360 --> 11:09.240] So then I was like, can I, can I make this easier to do? [11:09.240 --> 11:15.220] So this is the implementation of Nix space FMT in CPP Nix. [11:15.220 --> 11:19.100] The, the, the thing we call Nix, Nixlang. [11:19.100 --> 11:28.580] Uh, and I wanted to show this to people cause I'm like not a C++ guy, but I found this code like not that intimidating, which was just like a wonderful surprise. [11:28.580 --> 11:30.020] I was really afraid to look at this stuff. [11:30.020 --> 11:32.160] It's like, I don't totally understand everything here. [11:32.160 --> 11:34.440] Like I don't know what an installable is. [11:34.600 --> 11:42.520] I, I kind of had heard the word app before in like the context of flakes, but like this, okay, yeah, we're exacting something and oh, app dot program. [11:42.520 --> 11:43.440] That's the thing I need. [11:43.440 --> 11:45.000] How can I get my hands on that? [11:45.000 --> 11:55.200] So that was like pretty straightforward to like, I, I put together a proof of concept PR to, to CPP Nix and said like, Hey, I'd really like to get, get this path. [11:55.400 --> 12:04.140] Um, so I put together a PR and then I got a little bit of feedback, but people didn't totally understand why I was trying to do this or whatever. [12:04.380 --> 12:07.540] Uh, and I really like real-time communication with people. [12:07.540 --> 12:10.060] And this is something I just want to encourage everyone here to do. [12:10.140 --> 12:19.040] If you're involved in the Nix community, so many of the Nix teams have these like weekly meetings or bi, bi-monthly meetings where you can just like show up and chat with people. [12:19.040 --> 12:25.600] And it's just like a great way to like figure out if the thing you're trying to do is in scope for the team and they'll like tell you what to do. [12:25.600 --> 12:26.560] They want to help you out. [12:26.560 --> 12:29.800] I found it to be like so unintimidating. [12:29.800 --> 12:31.680] I was like so afraid to attend. [12:31.740 --> 12:42.460] Uh, so I encourage you if there's something you want to do, there's a lot of like low hanging sort of, uh, cosmetic fruit like that you can pick in, in Nix itself or whatever project. [12:42.460 --> 12:44.620] So it was very helpful. [12:44.920 --> 12:46.440] Uh, feedback on my PR. [12:46.440 --> 12:53.540] I learned a little bit about C++ and now we have Nix, uh, 2.29, uh, which has this new Nix formatter subcommand. [12:53.580 --> 12:55.280] So I'll show you what that does. [12:55.440 --> 12:57.500] You've got this Nix formatter build command. [12:57.500 --> 13:03.220] So what this will do is evaluate and build the formatter and prints out the absolute path to the formatter. [13:03.220 --> 13:08.000] So this is a thing that your editor can run once and hold on to it. [13:08.000 --> 13:11.880] And then every time you'd make a change to a file after that, it'll just exact this thing. [13:11.880 --> 13:14.800] It'll be super fast and it does exactly what you want. [13:15.560 --> 13:22.420] Uh, and then for completeness, we added this Nix formatter run command, which is just equivalent to the Nix space FMT command. [13:22.420 --> 13:28.580] We're not going to get, I don't think we're going to get rid of Nix space FMT, but this is just sort of here for parallelism, I guess. [13:28.920 --> 13:31.520] So, uh, yeah, that's it. [13:31.520 --> 13:34.600] Um, if you use NaveM, check out the thing I built. [13:34.680 --> 13:35.940] Uh, you can try it out. [13:36.420 --> 13:39.760] I seem to be doing fine on time, so I'll give a brief demo of it, I guess. [13:39.760 --> 13:42.960] Um, so, yeah. [13:43.080 --> 13:44.120] Folks can see stuff? [13:44.120 --> 13:44.720] Okay, great. [13:44.720 --> 13:46.940] So, uh, you can run this demo yourself. [13:46.940 --> 13:49.960] It's fully self-contained output of my, my Flake here. [13:50.200 --> 13:54.720] Uh, and if I open it, if I run it, you know, this is some Nix code right here. [13:55.040 --> 14:10.340] Uh, you know, say, uh, this is not plugins or something, and then I hit save, and I learn about, like, a quirk of our Nix formatter, which is, like, these things get moved to the next line, and I'm like, oh, okay, actually, probably that should, like, [14:10.340 --> 14:11.480] go up here. [14:11.740 --> 14:18.860] And, you know, I think this is why having it hooked into the editor is the right way to do it, because auto-formatters sometimes do surprising things. [14:18.860 --> 14:22.000] So, anyways, uh, yeah. [14:22.000 --> 14:25.720] So, if you want to build something like this for your own IDE, do it. [14:25.720 --> 14:26.600] Feel free to reach out. [14:26.600 --> 14:27.400] Here's my email. [14:27.400 --> 14:28.500] Here's my matrix. [14:28.700 --> 14:30.640] Uh, and that's all I had. [14:30.640 --> 14:33.300] If folks have questions, I'm happy to answer. [14:39.210 --> 14:40.010] Harit? [14:56.520 --> 14:57.300] Yeah, okay. [14:57.300 --> 15:00.480] So, for Harit's asking, wait, why was this so hard? [15:00.480 --> 15:04.540] I ran this Nixie val command here, and it printed out the derivation. [15:04.540 --> 15:05.580] And the answer is yeah. [15:05.580 --> 15:14.600] So, that's what the original implementation I built did, is it took advantage of this to evaluate and build the formatter. [15:14.600 --> 15:27.640] But all that gives you when you build something in Nix is you get a store path, which has a bin directory inside of it, and then some executable underneath it, but you don't know what the, like, program, which, which script in the bin directory to execute. [15:27.760 --> 15:32.040] So, if you were going to assume, oh, yeah, it's treefmt, you could, like, hard code that string. [15:32.120 --> 15:40.140] But what you really need to do is the equivalent of, like, lib dot get exe from Nix packages, the thing that looks at meta dot main program. [15:41.140 --> 15:52.620] And I wrote all that code, and then I got it wrong, because, like, it turns out Nix packages also provides a formatter package, like a package called formatter, and it screws up this thing that I'm showing here . [15:52.620 --> 15:55.620] So, it was a lot I was able to delete. [15:55.620 --> 15:59.300] It, it turned into, like, 200 or so lines of Lua to get it, to get it right. [15:59.300 --> 16:03.940] And I figured, I want to convince other people to build this for their IDs. [16:03.940 --> 16:08.100] I didn't want them to, like, port all that Lua code to whatever. [16:08.460 --> 16:10.300] So, yeah. [16:14.200 --> 16:18.460] Uh, so the question is, are there any other subcommands for the formatter that would make sense? [16:19.000 --> 16:22.400] I don't have any ideas, but let me know if you do. [16:28.090 --> 16:28.890] Alright. [16:29.370 --> 16:30.570] Thanks a lot, everyone.