1 00:00:02,170 --> 00:00:05,230 Right, take two. 2 00:00:05,230 --> 00:00:06,950 Good evening, NixVegas. 3 00:00:07,190 --> 00:00:11,030 I will be talking about the Nix Pipe Operator today. 4 00:00:11,150 --> 00:00:14,410 But before we get started, can I get a show of hands? 5 00:00:14,410 --> 00:00:17,550 How many of you here don't use Nix? 6 00:00:18,730 --> 00:00:20,550 Okay, that's nobody. 7 00:00:20,550 --> 00:00:21,330 That's good. 8 00:00:21,330 --> 00:00:26,450 And how many of you have had experience with some other functional language before Nix? 9 00:00:26,930 --> 00:00:29,730 I assume, like, most of you. 10 00:00:29,730 --> 00:00:31,510 It's not me, though. 11 00:00:31,510 --> 00:00:34,590 My first proper functional language experience was with Nix. 12 00:00:34,770 --> 00:00:38,730 And as you can imagine, all the parentheses, they are very scary. 13 00:00:43,260 --> 00:00:45,400 Right, so who am I? 14 00:00:45,500 --> 00:00:48,640 You might know me on GitHub as illustris. 15 00:00:48,640 --> 00:00:50,620 I'm an Nix user since 2020. 16 00:00:50,620 --> 00:00:53,780 I'm one of the shepherds of the RFC 0148. 17 00:00:53,780 --> 00:00:56,160 That's trying to introduce the Pipe Operator. 18 00:00:57,020 --> 00: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. 19 00:01:07,280 --> 00: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? 20 00:01:15,800 --> 00:01:20,080 If I'm trying to figure out what this snippet of code does, I have to look all over the place. 21 00:01:20,080 --> 00:01:23,080 First I have to find the object that it's operating on, which is my list. 22 00:01:23,080 --> 00:01:24,420 Right, that's somewhere in the middle. 23 00:01:24,420 --> 00:01:26,140 It's in the middle to the right. 24 00:01:26,140 --> 00: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. 25 00:01:31,740 --> 00: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. 26 00:01:40,020 --> 00:01:43,520 Now, I used to write a lot of Bash. 27 00:01:43,600 --> 00: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. 28 00:01:53,460 --> 00:01:58,660 So, this is kind of what the pipeline operator will let you achieve. 29 00:01:58,660 --> 00:02:01,540 You've got your value, my list. 30 00:02:01,700 --> 00:02:03,000 It doesn't have to be a value. 31 00:02:03,000 --> 00:02:05,960 I mean, a function is also a value in mix, I guess. 32 00:02:06,300 --> 00:02:09,840 But yeah, that value gets piped from one function to another. 33 00:02:09,840 --> 00: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. 34 00:02:17,180 --> 00:02:20,140 Right, same thing, much cleaner, much easier to follow. 35 00:02:20,140 --> 00:02:23,200 You can follow the flow of data from one function to another. 36 00:02:26,640 --> 00:02:34,320 Right, so how it works, the pipe operator, the way it is implemented right now, is vertical pipe, right angle bracket. 37 00:02:34,320 --> 00:02:39,500 There is also a converse left pipe operator, which is left angle bracket, vertical pipe. 38 00:02:39,860 --> 00:02:43,860 And what that does is, it just makes the thing on the right flow to the thing on the left. 39 00:02:46,860 --> 00:02:50,660 Right, so let me show you a less trivial example. 40 00:02:50,740 --> 00: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. 41 00:02:57,600 --> 00: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. 42 00:03:05,300 --> 00:03:09,760 So, implementation is this, and again, you kind of have to look all over the place. 43 00:03:10,080 --> 00: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? 44 00:03:22,380 --> 00:03:26,900 So, this is what that would look like implemented with pipes. 45 00:03:27,420 --> 00:03:32,220 Much easier to read, I've got the same comments, it's just so much easier to just follow along. 46 00:03:33,860 --> 00: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? 47 00:03:41,800 --> 00:03:44,960 So, this is what the lib dot pipe implementation would look like. 48 00:03:45,240 --> 00:03:51,980 Lib dot pipe lets you do pipe, value, and then a list of functions to apply. 49 00:03:53,400 --> 00:03:56,180 But there are some limitations to that, such as... 50 00:04:01,940 --> 00:04:10,660 Right, so, there are still external macros, there are error messages which are a bit harder to deal with. 51 00:04:10,660 --> 00: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. 52 00:04:17,020 --> 00:04:21,880 And once I saw it, I started using it whenever I needed compatibility with existing Nix functions. 53 00:04:22,380 --> 00:04:26,080 But, the biggest problem I see with it is that it's not lazy. 54 00:04:26,260 --> 00:04:27,440 What do I mean by that? 55 00:04:27,440 --> 00:04:29,260 Let me show an example. 56 00:04:30,860 --> 00: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. 57 00:04:45,080 --> 00:04:50,840 Right, so, typical Nix style, if I do f false, g null, f is just selecting. 58 00:04:50,840 --> 00:04:54,180 Like, it's an optional call. 59 00:04:54,680 --> 00: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. 60 00:05:06,740 --> 00:05:08,740 Right, and you can see that it just prints f. 61 00:05:08,740 --> 00: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. 62 00:05:19,140 --> 00: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. 63 00:05:24,780 --> 00: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, 64 00:05:42,100 --> 00:05:45,520 it will have to evaluate the entire set of machines for no reason. 65 00:05:47,260 --> 00:05:52,560 The pipe operator, the way it's implemented right now, behaves exactly the same as just regular function composition. 66 00:05:54,400 --> 00:05:59,300 Right, so, more reasons for why a native implementation is needed. 67 00:06:00,980 --> 00:06:03,380 Availability everywhere, you don't need to import Nix PPGs. 68 00:06:03,740 --> 00: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. 69 00:06:10,800 --> 00: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. 70 00:06:23,280 --> 00:06:28,180 Now, what do we need from the community? 71 00:06:28,180 --> 00: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. 72 00:06:38,960 --> 00:06:42,040 And we need community consensus on various design decisions. 73 00:06:42,040 --> 00: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. 74 00:06:51,640 --> 00:06:54,420 I'll just briefly go over some of the design decisions as well. 75 00:06:54,860 --> 00:06:57,660 It has no residence and function application. 76 00:06:59,040 --> 00:07:02,080 So if you're doing something like... 77 00:07:02,080 --> 00: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. 78 00:07:10,400 --> 00:07:14,680 It allows natural chaining, and there are two versions, left and right. 79 00:07:14,880 --> 00:07:20,320 Personally, I've only used the right apply operator, but there are some people arguing for left as well. 80 00:07:20,320 --> 00:07:23,000 Join in on the discussion if you have any opinions on that. 81 00:07:24,540 --> 00:07:26,140 Right, so yeah. 82 00:07:34,380 --> 00: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. 83 00:07:49,240 --> 00: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. 84 00:08:01,060 --> 00:08:07,680 So for power users, again, better composition patterns, more maintainable code, reduces parenthesis fatigue. 85 00:08:07,780 --> 00:08:11,360 Although, again, that last one might be a me problem more than anything else. 86 00:08:12,980 --> 00:08:14,380 Comparison with other languages. 87 00:08:14,380 --> 00:08:16,600 Lots of other languages have pipes. 88 00:08:16,600 --> 00:08:18,260 Bash, of course, we all know. 89 00:08:18,380 --> 00:08:21,400 Elixir, Fsharp, OCaml, Elm... 90 00:08:21,400 --> 00:08:23,940 I believe Haskell also has a dot operator. 91 00:08:27,360 --> 00:08:28,640 Extra-experimental features. 92 00:08:28,640 --> 00:08:29,420 Pipe operator. 93 00:08:29,420 --> 00:08:31,660 You can grapple it right now on your Nix machines. 94 00:08:31,660 --> 00:08:34,780 As long as your Nix is relatively new, it should work. 95 00:08:36,220 --> 00:08:37,380 And here's a link. 96 00:08:37,480 --> 00:08:38,880 That's a link to the RFC. 97 00:08:38,960 --> 00:08:39,880 So try it out. 98 00:08:39,880 --> 00:08:42,400 And if you have any thoughts, please join in on the discussion. 99 00:08:51,230 --> 00:08:52,490 That's the end of that. 100 00:08:52,490 --> 00:08:53,430 Thanks everyone. 101 00:08:53,430 --> 00:08:54,510 Any questions? 102 00:08:55,650 --> 00:08:56,270 Thank you.