WEBVTT

00:02.170 --> 00:05.230
Right, take two.

00:05.230 --> 00:06.950
Good evening, NixVegas.

00:07.190 --> 00:11.030
I will be talking about the Nix Pipe Operator today.

00:11.150 --> 00:14.410
But before we get started, can I get a show of hands?

00:14.410 --> 00:17.550
How many of you here don't use Nix?

00:18.730 --> 00:20.550
Okay, that's nobody.

00:20.550 --> 00:21.330
That's good.

00:21.330 --> 00:26.450
And how many of you have had experience with some other functional language before Nix?

00:26.930 --> 00:29.730
I assume, like, most of you.

00:29.730 --> 00:31.510
It's not me, though.

00:31.510 --> 00:34.590
My first proper functional language experience was with Nix.

00:34.770 --> 00:38.730
And as you can imagine, all the parentheses, they are very scary.

00:43.260 --> 00:45.400
Right, so who am I?

00:45.500 --> 00:48.640
You might know me on GitHub as illustris.

00:48.640 --> 00:50.620
I'm an Nix user since 2020.

00:50.620 --> 00:53.780
I'm one of the shepherds of the RFC 0148.

00:53.780 --> 00:56.160
That's trying to introduce the Pipe Operator.

00:57.020 --> 01:06.060
Right, so today's goal is to introduce it to anybody who is not familiar with it and to get people to engage in the RFC process and get that moving.

01:07.280 --> 01:15.800
Right, so the problem with Nix, or the problem I'm going to talk about with Nix, is, well, you can see it right here, right?

01:15.800 --> 01:20.080
If I'm trying to figure out what this snippet of code does, I have to look all over the place.

01:20.080 --> 01:23.080
First I have to find the object that it's operating on, which is my list.

01:23.080 --> 01:24.420
Right, that's somewhere in the middle.

01:24.420 --> 01:26.140
It's in the middle to the right.

01:26.140 --> 01:31.680
Then I have to see, okay, it's applying built-ins on map on that, and then it's applying a filter, then it's applying built-ins on 2G site.

01:31.740 --> 01:38.100
Right, so you kind of have to look all over the place to try and figure out what this snippet is doing.

01:40.020 --> 01:43.520
Now, I used to write a lot of Bash.

01:43.600 --> 01:49.380
I still do to a great extent, and I'm very much used to thinking left to right, you know, pipe this to that to that.

01:53.460 --> 01:58.660
So, this is kind of what the pipeline operator will let you achieve.

01:58.660 --> 02:01.540
You've got your value, my list.

02:01.700 --> 02:03.000
It doesn't have to be a value.

02:03.000 --> 02:05.960
I mean, a function is also a value in mix, I guess.

02:06.300 --> 02:09.840
But yeah, that value gets piped from one function to another.

02:09.840 --> 02:17.180
So you take my list, you do built-ins on map, X, X into 2, multiply it, then you do a filter, greater than 5, to JSON.

02:17.180 --> 02:20.140
Right, same thing, much cleaner, much easier to follow.

02:20.140 --> 02:23.200
You can follow the flow of data from one function to another.

02:26.640 --> 02:34.320
Right, so how it works, the pipe operator, the way it is implemented right now, is vertical pipe, right angle bracket.

02:34.320 --> 02:39.500
There is also a converse left pipe operator, which is left angle bracket, vertical pipe.

02:39.860 --> 02:43.860
And what that does is, it just makes the thing on the right flow to the thing on the left.

02:46.860 --> 02:50.660
Right, so let me show you a less trivial example.

02:50.740 --> 02:57.600
This is an actual function from one of my flicks, where I need to generate a list of subdirectories, given a directory.

02:57.600 --> 03:04.920
So you do get subdir dot slash dot, it should give me a list of absolute next door paths to all the subdirectories in a given directory.

03:05.300 --> 03:09.760
So, implementation is this, and again, you kind of have to look all over the place.

03:10.080 --> 03:21.160
P is the value that is being passed in, so now I have to look again to the center and to the right to see, okay, P is there, read dot P, then filter attos, then atto names, then map, right?

03:22.380 --> 03:26.900
So, this is what that would look like implemented with pipes.

03:27.420 --> 03:32.220
Much easier to read, I've got the same comments, it's just so much easier to just follow along.

03:33.860 --> 03:41.180
Now, some of you who really know Nix and NixPKGs might say, but lib dot pipe already exists, why are you not just using that?

03:41.800 --> 03:44.960
So, this is what the lib dot pipe implementation would look like.

03:45.240 --> 03:51.980
Lib dot pipe lets you do pipe, value, and then a list of functions to apply.

03:53.400 --> 03:56.180
But there are some limitations to that, such as...

04:01.940 --> 04:10.660
Right, so, there are still external macros, there are error messages which are a bit harder to deal with.

04:10.660 --> 04:16.980
It's not very discoverable, like, I didn't know that lib dot pipe existed until the RSE for the pipe operator came out.

04:17.020 --> 04:21.880
And once I saw it, I started using it whenever I needed compatibility with existing Nix functions.

04:22.380 --> 04:26.080
But, the biggest problem I see with it is that it's not lazy.

04:26.260 --> 04:27.440
What do I mean by that?

04:27.440 --> 04:29.260
Let me show an example.

04:30.860 --> 04:44.960
So, I'm defining a couple of functions here, f and g, and you can see that whenever they're evaluated, they will print f and g, and when their insides are evaluated, you'll see these things printed as well, g body and f body.

04:45.080 --> 04:50.840
Right, so, typical Nix style, if I do f false, g null, f is just selecting.

04:50.840 --> 04:54.180
Like, it's an optional call.

04:54.680 --> 05:06.440
If the condition is true, then it will evaluate g, it will print the second argument, which in this case is an application of g on null.

05:06.740 --> 05:08.740
Right, and you can see that it just prints f.

05:08.740 --> 05:18.120
Like, here, it short-circuits, it just sees that there's a false, the option evaluates to false, so it just prints an empty list.

05:19.140 --> 05:24.640
However, if we try to do the same thing with the pipe operator, you can see that it's doing so much more.

05:24.780 --> 05:42.100
And this, the biggest place I saw this, in fact, was, there are some lakes where I've got lots of Nix OS machines, and they are kind of generated programmatically by iterating over a list of things, and in those situations, if I have to go into a machine and evaluate a single attribute from that,

05:42.100 --> 05:45.520
it will have to evaluate the entire set of machines for no reason.

05:47.260 --> 05:52.560
The pipe operator, the way it's implemented right now, behaves exactly the same as just regular function composition.

05:54.400 --> 05:59.300
Right, so, more reasons for why a native implementation is needed.

06:00.980 --> 06:03.380
Availability everywhere, you don't need to import Nix PPGs.

06:03.740 --> 06:10.220
It's more discoverable because it's a part of the language, it performs better, and the syntax, in my opinion, looks cleaner.

06:10.800 --> 06:22.420
The current status of this is that it's implemented in Nix PPGs as an experimental feature, you can enable it just like Lakes with a flag, and you can use it today in your configs.

06:23.280 --> 06:28.180
Now, what do we need from the community?

06:28.180 --> 06:38.960
We need real-world usage feedback, we need more people to try it out, to discover edge cases, anything that's not accounted for so far, and to put it back in the RFC thread.

06:38.960 --> 06:42.040
And we need community consensus on various design decisions.

06:42.040 --> 06:50.840
A bunch of people have just gotten together and made some decisions, including me, and we probably made some mistakes along the way, so feedback is appreciated.

06:51.640 --> 06:54.420
I'll just briefly go over some of the design decisions as well.

06:54.860 --> 06:57.660
It has no residence and function application.

06:59.040 --> 07:02.080
So if you're doing something like...

07:02.080 --> 07:09.760
I guess the most significant impact of that is you can't just have in the middle of a pipe x colon x plus one or something like that without brackets.

07:10.400 --> 07:14.680
It allows natural chaining, and there are two versions, left and right.

07:14.880 --> 07:20.320
Personally, I've only used the right apply operator, but there are some people arguing for left as well.

07:20.320 --> 07:23.000
Join in on the discussion if you have any opinions on that.

07:24.540 --> 07:26.140
Right, so yeah.

07:34.380 --> 07:49.240
One very important design pattern that I've started to rely on with the introduction of pipes is if I've got a long pipe where multiple transformations are being applied, and something's breaking in between, I can just stick a trace file in between.

07:49.240 --> 07:56.660
So pipe fx, pipe fy, I can just put a pipe trace file in between to see what's being sent from function x to function y.

08:01.060 --> 08:07.680
So for power users, again, better composition patterns, more maintainable code, reduces parenthesis fatigue.

08:07.780 --> 08:11.360
Although, again, that last one might be a me problem more than anything else.

08:12.980 --> 08:14.380
Comparison with other languages.

08:14.380 --> 08:16.600
Lots of other languages have pipes.

08:16.600 --> 08:18.260
Bash, of course, we all know.

08:18.380 --> 08:21.400
Elixir, Fsharp, OCaml, Elm...

08:21.400 --> 08:23.940
I believe Haskell also has a dot operator.

08:27.360 --> 08:28.640
Extra-experimental features.

08:28.640 --> 08:29.420
Pipe operator.

08:29.420 --> 08:31.660
You can grapple it right now on your Nix machines.

08:31.660 --> 08:34.780
As long as your Nix is relatively new, it should work.

08:36.220 --> 08:37.380
And here's a link.

08:37.480 --> 08:38.880
That's a link to the RFC.

08:38.960 --> 08:39.880
So try it out.

08:39.880 --> 08:42.400
And if you have any thoughts, please join in on the discussion.

08:51.230 --> 08:52.490
That's the end of that.

08:52.490 --> 08:53.430
Thanks everyone.

08:53.430 --> 08:54.510
Any questions?

08:55.650 --> 08:56.270
Thank you.

