start	end	text
500	1680	Okay, great.
1680	2120	Cool.
2120	3260	Thanks for coming, everyone.
3900	5200	This is my talk.
5200	6800	I want to talk about auto formatting.
7520	9520	And I'm excited to see everyone here.
9720	11720	So a little bit about me.
11720	13260	My name is Jeremy Fleischman.
13260	15340	You'll see me on GitHub as JFly.
15400	18040	On the left there is my profile picture.
18100	20540	The reason I have so much hair is because it's from 2010.
21200	22900	I was very into Rubik's cubes.
22900	27060	This is right after I won US Nationals forever ago.
27460	30220	And I've been using Nix since 2021.
30840	36440	And I quit my job last year to learn more about Nix and try to give back to the community.
36520	41120	So I've become a maintainer of TreeFMT, which I want to talk about today.
41200	43140	I'm also on the infra team.
43260	46980	And I recently became the leader of the Nix formatting team.
47360	56580	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.
56880	62220	If you're a NeoVim user, I'd love for you to check out this built in that I added to NunLS.
62400	64900	It's called Nix Flake FMT .
65100	70560	And if you use something other than NeoVim, I'd like to persuade you to build something like that for your own editor.
71000	72980	And lastly, contribute to Nix.
72980	75260	It's fun and not scary.
76060	79460	So I'm going to get on my TreeFMT soapbox first.
79560	83340	I realized while prepping for this talk, I didn't actually know how to pronounce the name of this tool.
83340	84700	I'd been saying TreeFmt.
84860	87360	So here's my little mnemonic to help you remember.
87360	88000	Tree.
88100	88820	FM.
89060	89560	T.
90880	93160	Okay, so why would you want to use this?
93160	94260	Why does this exist?
94400	97700	So here's some real code I ran across in Image.
97700	100320	I was contributing to their mobile app the other day.
100320	101780	And they've got this make file.
101780	106540	And I think this is like a perfect example of the sort of thing you see in make files around the world.
106540	108920	Which is, I want to start formatting.
108920	110660	I add a make format target.
111160	113200	And then I like invoke my formatter.
113320	115920	And this one's a great example of like Dart format.
116120	119080	Tries to recurse through your file system and find all your Dart files.
119080	123780	But there's this like bug where it can't ignore, you can't tell it to ignore some files .
123780	127600	And the image has some auto-generated files that they don't want to auto-format.
127600	135720	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.
135880	136800	And this works for them.
136800	144800	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.
145140	149160	Maybe it doesn't matter in small projects, but that's why TreeFMD exists.
149160	151020	So let me show you what it looks like.
151420	154840	Uh, so it basically defines this thing called the formatter specification.
154840	156240	It's very simple.
156460	161780	The very short version of it is, if you implement a formatter, try to make it follow this CLI.
161780	168000	So it's like command, some options, and then a list of one or more files to format.
168000	175760	So NixFMT, which is a Haskell tool that the Nix formatting team maintains, it obeys the specification.
175760	182200	If you want to format a Nix file, you say NixFMT, you feed it a list of Nix files and it formats them.
182960	190900	Uh, so what TreeFMD will do is if your formatter implements this specification, we will walk the file system for you.
190900	196660	And we'll do it fast, and we'll understand Go, we'll under, er, understand Git, we'll understand JJ, whatever.
196800	203100	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 .
203440	206020	We'll walk the file system consistently.
207200	211200	Uh, so to take a look at that earlier example, we have this code.
211560	213480	In TreeFMD, you'd write something like this.
213480	214880	It's a simple TOML file.
215040	217440	You specify all your formatters for all your files.
217440	225600	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.
226240	227560	Uh, and here you go.
227560	233100	So this is TreeFMD, we use it in Nix packages to enforce formatting across the entire codebase.
233140	237760	Uh, and you can see, this is me running TreeFMD in a Nix packages checkout.
237760	247400	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.
247400	248420	So we also do caching.
248420	254860	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.
256160	260500	Okay, but we're Nixers, we, we see a config file like this and we ask some questions.
260500	262580	We're like, where does Dart come from?
262580	264720	Which version of TreeFMD are you going to use?
264760	265520	Etcetera.
265520	272220	So, for basically any tool out there, some, somebody has written a Nix abstraction on top of it to let you configure it.
272220	280140	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.
280260	285000	So, this is just some flake boilerplate because I wanted to show people a complete example.
285000	288580	The interesting part is on the right here, this formatting.nix file.
288580	290920	So, I'll just throw away the boilerplate.
290920	293380	So, this is the, the TreeFMD Nix stuff.
293380	297420	You basically say, okay, I want to enable some TreeFMD programs.
297820	299520	There's one called Dart format.
299520	308780	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.
308860	311080	And you'll notice there's some stuff missing here.
311080	317620	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.
317620	325580	It is packaged in Nix packages, but somebody else wrote that code, figured out which exact attribute in Nix packages provides the formatter.
325680	334640	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.
334640	336960	So, you know, you get to delete a little bit of stuff.
336960	338260	So, okay.
338420	341520	And then if you do this, you get a couple of flake outputs.
341540	350720	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.
350720	352960	And then you also have a formatter output.
353480	360240	And so there are a couple of CLI utilities that the, the Nix CLI provides that understands these flake outputs.
360240	363560	So, this is the Nix flake check command, which you could use in CI.
363840	369300	Uh, and then Nix space FMT, which will just go and, uh oh.
369500	370500	Was that me?
372680	374140	Uh oh.
376160	377500	That's not me Morgan?
378000	379120	Okay.
379140	380240	It overheated?
381040	381840	Okay.
381840	382620	Wow.
382620	384700	I didn't know projectors could crash.
386840	387720	Okay.
390700	391580	Hello.
391740	392240	Okay.
392240	394400	So, anyways, I'll just keep talking until it comes back.
394400	395600	Oh, and we're back.
395600	396040	Great.
396040	401320	So, these are the two commands that you just get support for if you use TreeFMT Nix.
401320	401940	Okay.
402300	405620	So, to summarize, we started here and we ended here.
405620	409340	So, and this isn't like really an apples to apples comparison.
409340	412460	We benefit from all the, the power of Nix.
412960	418000	We're, we're being very specific about the version of TreeFMT and the version of Dart that we're using here.
418460	420520	And you get the benefits of TreeFMT too.
420520	421920	We're walking the file system.
421940	422820	We're doing it quickly.
422820	424400	We understand your git ignores.
424400	429020	If you use git submodules or something else wonky, we'll do something same.
429880	430320	Okay.
430320	433200	But, you know, it's nice to have it enforced in CI.
433200	436220	It's nice to have a command you can run to format all the code.
436420	442320	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.
442320	443320	You're just writing code.
443320	445740	So people try to push this further left.
445760	449980	And so as a TreeFMT maintainer, people usually ask for features that we haven't implemented.
450060	452260	One of which is pre-commit hooks.
452320	453280	People build them.
453280	455240	We don't have support for it in TreeFMT.
456140	464160	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.
464160	466760	That's like a little better because it's even further left.
466760	469340	But then it starts to fight with your IDE a bit.
469340	477320	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.
477620	478820	What's going on here?
479220	482380	So I think editor integration is the right way to go.
482380	484240	That's what I want to talk about in the rest of the talk.
484240	487080	And hopefully I don't need to convince you that that's the way to do it.
487520	495080	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.
495080	498700	So hopefully you can, you know, take inspiration from this.
499680	500040	Okay.
500040	507940	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.
507940	509080	Super straightforward .
510020	514740	But performance matters and Nix FMT, it turns out, is quite slow.
514740	521880	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.
522320	525100	So you only need to do that once.
525100	526160	I want to cache it.
526240	533300	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.
533300	535040	And that ended up being a little tricky.
535040	536860	So I'm going to breeze through the next slide.
536960	542380	Cause a lot of this is just like NeoVim details that nobody cares about, except for me when I was building this.
542880	548200	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.
548200	558020	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.
558800	566680	Unfortunately, it had this dynamic command thing, which was great, but it assumed that the command, discovering the command was quick.
566680	574680	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.
574740	578600	But this is like, oh wait, we've got to like spend 14 seconds figuring something out.
578960	585380	I would like to be able to cache that and I needed to make some changes to dynamic command to make that possible.
585380	586720	So whatever, doesn't matter.
586720	593340	Um, there was another detail with TreeFMT where it wouldn't actually format the files because it like understands Git.
593340	595640	It didn't want to format the temp files that none LS created.
595640	596680	I fixed that.
596680	598640	I ran into a bunch of other TreeFMT bugs.
598640	600600	That's how I ended up becoming a maintainer.
600880	609360	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.
609500	612420	So I hooked into NeoVim's trust infrastructure.
612520	615480	Turned out the support for trusting directories was buggy.
615480	617540	So that was another, that was a PR to NeoVim.
617840	624160	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.
624160	630740	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.
631020	636300	So at first I opted to just like work around this by writing a bunch of code in Lua.
637160	638420	That was a mistake.
638420	639660	I ended up writing a lot of code.
639660	641180	I didn't get it right the first time.
641200	647120	So I want to talk about what I should have done instead and you guys can benefit from the work I did.
647120	650020	So anyways, it turned into a bunch of PRs to a bunch of places.
650220	651440	Details don't really matter.
651620	652020	Okay.
652020	654120	So it landed in none LS.
654120	655460	Somebody actually accepted my PR.
655460	656200	So that's great.
656200	657000	It's there.
657820	662060	Uh, one of my fellow maintainers on none LS noticed that it was like 300 lines of Lua.
662060	663980	It was like, this probably doesn't belong here.
663980	665360	I totally agreed with him.
665360	669240	So then I was like, can I, can I make this easier to do?
669240	675220	So this is the implementation of Nix space FMT in CPP Nix.
675220	679100	The, the, the thing we call Nix, Nixlang.
679100	688580	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.
688580	690020	I was really afraid to look at this stuff.
690020	692160	It's like, I don't totally understand everything here.
692160	694440	Like I don't know what an installable is.
694600	702520	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.
702520	703440	That's the thing I need.
703440	705000	How can I get my hands on that?
705000	715200	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.
715400	724140	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.
724380	727540	Uh, and I really like real-time communication with people.
727540	730060	And this is something I just want to encourage everyone here to do.
730140	739040	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.
739040	745600	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.
745600	746560	They want to help you out.
746560	749800	I found it to be like so unintimidating.
749800	751680	I was like so afraid to attend.
751740	762460	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.
762460	764620	So it was very helpful.
764920	766440	Uh, feedback on my PR.
766440	773540	I learned a little bit about C++ and now we have Nix, uh, 2.29, uh, which has this new Nix formatter subcommand.
773580	775280	So I'll show you what that does.
775440	777500	You've got this Nix formatter build command.
777500	783220	So what this will do is evaluate and build the formatter and prints out the absolute path to the formatter.
783220	788000	So this is a thing that your editor can run once and hold on to it.
788000	791880	And then every time you'd make a change to a file after that, it'll just exact this thing.
791880	794800	It'll be super fast and it does exactly what you want.
795560	802420	Uh, and then for completeness, we added this Nix formatter run command, which is just equivalent to the Nix space FMT command.
802420	808580	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.
808920	811520	So, uh, yeah, that's it.
811520	814600	Um, if you use NaveM, check out the thing I built.
814680	815940	Uh, you can try it out.
816420	819760	I seem to be doing fine on time, so I'll give a brief demo of it, I guess.
819760	822960	Um, so, yeah.
823080	824120	Folks can see stuff?
824120	824720	Okay, great.
824720	826940	So, uh, you can run this demo yourself.
826940	829960	It's fully self-contained output of my, my Flake here.
830200	834720	Uh, and if I open it, if I run it, you know, this is some Nix code right here.
835040	850340	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,
850340	851480	go up here.
851740	858860	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.
858860	862000	So, anyways, uh, yeah.
862000	865720	So, if you want to build something like this for your own IDE, do it.
865720	866600	Feel free to reach out.
866600	867400	Here's my email.
867400	868500	Here's my matrix.
868700	870640	Uh, and that's all I had.
870640	873300	If folks have questions, I'm happy to answer.
879210	880010	Harit?
896520	897300	Yeah, okay.
897300	900480	So, for Harit's asking, wait, why was this so hard?
900480	904540	I ran this Nixie val command here, and it printed out the derivation.
904540	905580	And the answer is yeah.
905580	914600	So, that's what the original implementation I built did, is it took advantage of this to evaluate and build the formatter.
914600	927640	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.
927760	932040	So, if you were going to assume, oh, yeah, it's treefmt, you could, like, hard code that string.
932120	940140	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.
941140	952620	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 .
952620	955620	So, it was a lot I was able to delete.
955620	959300	It, it turned into, like, 200 or so lines of Lua to get it, to get it right.
959300	963940	And I figured, I want to convince other people to build this for their IDs.
963940	968100	I didn't want them to, like, port all that Lua code to whatever.
968460	970300	So, yeah.
974200	978460	Uh, so the question is, are there any other subcommands for the formatter that would make sense?
979000	982400	I don't have any ideas, but let me know if you do.
988090	988890	Alright.
989370	990570	Thanks a lot, everyone.
