WEBVTT captioned by George Huebner NOTE Introduction 00:00:00.480 --> 00:00:02.439 Alright, we have a lot to get through and not a 00:00:02.440 --> 00:00:03.899 whole lot of time, so I'm just going to preface 00:00:03.900 --> 00:00:05.399 the talk by saying all the materials I use will 00:00:05.400 --> 00:00:07.740 be available on the talk page at emacsconf.org. 00:00:07.940 --> 00:00:09.999 I also encourage you to stop and pause the talk 00:00:10.000 --> 00:00:11.959 so you can try the demos at home, and of course 00:00:11.960 --> 00:00:13.220 you can ask questions after the talk. 00:00:14.320 --> 00:00:15.820 Alright, let's get into it! 00:00:17.420 --> 00:00:19.839 Alright, so Org babel is a very powerful system for 00:00:19.840 --> 00:00:20.580 literate programming. 00:00:21.280 --> 00:00:22.780 There is extensive language support, 00:00:23.980 --> 00:00:25.439 we can make our code more flexible through the 00:00:25.440 --> 00:00:27.180 usage of header arguments and variables, 00:00:27.780 --> 00:00:30.279 we can use noweb syntax to dovetail functions, 00:00:30.280 --> 00:00:31.960 even those written in different languages, 00:00:32.860 --> 00:00:35.079 and we can also use TRAMP to execute source 00:00:35.080 --> 00:00:36.240 blocks on remote machines. NOTE What happens when we want to use Org babel with a language that's not supported by default? 00:00:37.540 --> 00:00:39.159 What happens when we want to use Org babel with a 00:00:39.160 --> 00:00:40.480 language that's not supported by default? 00:00:41.380 --> 00:00:43.780 Hmm, no org-babel-execute function for Zig. 00:00:44.380 --> 00:00:46.599 Well, we could write our own ob-zig.el, but that 00:00:46.600 --> 00:00:48.319 would bring our .emacs.d 100 lines closer to 00:00:48.320 --> 00:00:49.540 "Emacs bankruptcy", 00:00:49.840 --> 00:00:51.919 and it would also add a dependency to this 00:00:51.920 --> 00:00:53.640 Org mode document: if you don't have ob-zig, 00:00:53.740 --> 00:00:55.260 you can't execute the source block. 00:00:55.560 --> 00:00:57.319 So, in today's talk, I'm going to show you how to 00:00:57.320 --> 00:00:58.920 define an arbitrary "runnable" 00:00:59.020 --> 00:01:01.279 that not only allows you to recover the 00:01:01.280 --> 00:01:03.440 functionality of org-babel-execute-src-block, 00:01:03.540 --> 00:01:05.679 but do so much more by having an arbitrary 00:01:05.680 --> 00:01:06.959 function on some source code— 00:01:06.960 --> 00:01:08.359 that could be compiling and running, but it could 00:01:08.360 --> 00:01:09.580 also be other stuff. NOTE Version information 00:01:10.280 --> 00:01:12.479 So, before we get into things, I do just want to 00:01:12.480 --> 00:01:13.919 clarify that I'm using a pretty tricked out 00:01:13.920 --> 00:01:16.599 personal build of Emacs and also tecosaur's fork of 00:01:16.600 --> 00:01:17.319 Org mode. 00:01:17.320 --> 00:01:19.359 But everything I talk about in today's talk you 00:01:19.360 --> 00:01:21.919 can use in a version of Emacs from probably the 00:01:21.920 --> 00:01:23.240 past 5 years or so. 00:01:23.440 --> 00:01:25.639 So not to worry, you can run everything at home. 00:01:25.640 --> 00:01:28.180 (You will need some external packages like, for example, Zig.) NOTE The technique 00:01:29.420 --> 00:01:32.199 Ok, so here's the technique: it's literally just 00:01:32.200 --> 00:01:33.759 one line of ELisp. 00:01:33.760 --> 00:01:35.199 I like to call this "block-code". 00:01:35.200 --> 00:01:36.599 So, what's going on here? 00:01:36.600 --> 00:01:39.159 org-babel-expand-noweb-references, and 00:01:39.160 --> 00:01:40.600 then org-babel-lob--src-info... 00:01:40.700 --> 00:01:43.119 all this is doing is taking the name of an 00:01:43.120 --> 00:01:46.119 Org babel block, and it's returning the content of 00:01:46.120 --> 00:01:48.239 the source code of that block. 00:01:48.240 --> 00:01:50.780 We will use this using Org's CALL keyword: 00:01:51.080 --> 00:01:53.199 we have some runnable code block, we use 00:01:53.200 --> 00:01:54.679 "block-code", and then we pass in the name 00:01:54.680 --> 00:01:56.600 of the code block we want to execute. 00:01:57.100 --> 00:01:58.879 That's all a little bit abstract, so I think it's 00:01:58.880 --> 00:02:00.080 easier to show than tell. 00:02:00.480 --> 00:02:02.919 Here I have some ELisp test block, just counts 00:02:02.920 --> 00:02:05.280 the number of lines in my narrowed buffer. 00:02:06.280 --> 00:02:08.479 Obviously I can press C-c C-c like I just did, 00:02:08.480 --> 00:02:10.079 but for the sake of demonstration, let's use 00:02:10.080 --> 00:02:10.679 a runnable. 00:02:10.680 --> 00:02:13.239 So here I have "eval", that just evaluates a string 00:02:13.240 --> 00:02:16.119 as an S-expression, and here I have "eval", 00:02:16.120 --> 00:02:17.759 "block-code", "elisp-test". 00:02:17.760 --> 00:02:20.900 So I put my point over this line, I press C-c C-c, 00:02:21.200 --> 00:02:22.820 and as you can see I get the same thing. 00:02:23.220 --> 00:02:25.039 So that might be a little underwhelming, we just 00:02:25.040 --> 00:02:27.400 accomplished the same thing as ob-elisp.el 00:02:27.500 --> 00:02:28.640 but with extra steps. 00:02:28.740 --> 00:02:30.799 But in the coming examples I'm going to show, I think 00:02:30.800 --> 00:02:32.919 you will find that "block-code" is small 00:02:32.920 --> 00:02:34.080 but mighty. NOTE Budget ob-LANG 00:02:34.780 --> 00:02:37.200 Ok, so let's return to the example of Zig from 00:02:37.220 --> 00:02:37.719 the beginning. 00:02:37.720 --> 00:02:39.439 As a reminder, we cannot use 00:02:39.440 --> 00:02:40.640 org-babel-execute-src-block on this. 00:02:41.440 --> 00:02:44.040 Down here I define a runnable which is only 00:02:44.140 --> 00:02:45.040 4 lines of code 00:02:45.240 --> 00:02:47.340 (in fairness, Zig is pulling a lot of weight for us here), 00:02:47.440 --> 00:02:48.740 and I'm defining this in Bash. 00:02:48.940 --> 00:02:52.039 You can use things other than Bash and ELisp, 00:02:52.040 --> 00:02:53.960 anything supported by Org babel by default, but I 00:02:54.060 --> 00:02:56.300 would encourage you to use ELisp or Bash 00:02:56.400 --> 00:02:58.039 because it's the most portable. 00:02:58.040 --> 00:03:00.530 So we call this down here, and... 00:03:00.730 --> 00:03:02.000 here we go, we get "Hello, world!" 00:03:02.400 --> 00:03:03.799 One thing I want to mention is that we're 00:03:03.800 --> 00:03:06.020 actually redirecting stderr to stdout, and that's 00:03:06.320 --> 00:03:08.519 not a generally desirable thing to do, that might 00:03:08.520 --> 00:03:10.399 actually be kind of annoying to do with a 00:03:10.400 --> 00:03:11.980 normal Org babel flow, 00:03:12.280 --> 00:03:14.959 but because this is an ad-hoc runnable— 00:03:14.960 --> 00:03:16.479 it doesn't need to be the most general, it just 00:03:16.480 --> 00:03:18.050 needs to fit our use case— 00:03:18.150 --> 00:03:19.080 we can just add this in. 00:03:19.180 --> 00:03:20.999 Again, that's kind of a recurring theme for this 00:03:21.000 --> 00:03:24.519 talk, molding Org babel to fit your use case, 00:03:24.520 --> 00:03:26.330 even if it's not the most general thing in the world. NOTE Library of Babel 00:03:27.330 --> 00:03:28.410 Alright, so the next thing I want to talk about 00:03:28.420 --> 00:03:30.999 is Library of Babel, and to do that I have some 00:03:31.000 --> 00:03:32.719 code blocks, these are actually from the Org 00:03:32.720 --> 00:03:34.779 document I use for job applications/cover letters 00:03:34.780 --> 00:03:36.039 and stuff like that, 00:03:36.040 --> 00:03:38.039 and this is a little more involved than 00:03:38.040 --> 00:03:40.100 "block-code", but it's doing the same thing in spirit— 00:03:40.200 --> 00:03:42.900 you can see right here I've got the same thing in spirit. 00:03:44.100 --> 00:03:46.239 But I'm doing some additional introspection using 00:03:46.240 --> 00:03:50.360 ELisp to pass variables to this inner code block, 00:03:51.060 --> 00:03:52.960 and what I want to show here is 00:03:52.980 --> 00:03:54.580 —let me tangle this real quick— 00:03:55.380 --> 00:03:56.839 I have some file example.org, 00:03:56.840 --> 00:03:59.839 which has a block named "lorem-letter", and here 00:03:59.840 --> 00:04:02.879 in my other file, org_babel_everywhere.org, I can 00:04:02.880 --> 00:04:05.920 actually use "lorem-letter", I can reference it 00:04:06.020 --> 00:04:07.199 by prepending the file name to it, 00:04:07.200 --> 00:04:09.220 "lorem-letter:example.org". 00:04:09.420 --> 00:04:13.860 So I press C-c C-c here... and I open this up, and 00:04:13.960 --> 00:04:16.620 we have some lorem ipsum stuff in the file. 00:04:17.120 --> 00:04:19.079 So this is a pretty cool technique, that I can 00:04:19.080 --> 00:04:21.720 use Org babel's Library of Babel functionality. 00:04:21.820 --> 00:04:24.399 I will say, there is a feature where you can add 00:04:24.400 --> 00:04:25.959 Org files to your personal "Library of Babel" 00:04:25.960 --> 00:04:28.320 and then reference them without prepending the filename. 00:04:28.520 --> 00:04:30.639 I'm personally not such a big fan of this because 00:04:30.640 --> 00:04:32.199 it makes it harder for someone else to run the 00:04:32.200 --> 00:04:34.320 same Org document on their machine. 00:04:34.420 --> 00:04:36.599 But maybe it could be useful if you have some 00:04:36.600 --> 00:04:39.439 company-wide Emacs config, that everyone has the 00:04:39.440 --> 00:04:40.620 same setup everywhere. NOTE Examples 00:04:42.120 --> 00:04:43.399 We'll get onto the examples. 00:04:43.400 --> 00:04:45.039 I just want to emphasize that all of these examples 00:04:45.040 --> 00:04:47.119 are real use cases I've actually used, 00:04:47.120 --> 00:04:48.680 and none of them are contrived. 00:04:49.280 --> 00:04:51.860 The first one is micro VMs, this is an excerpt 00:04:51.960 --> 00:04:54.560 from a kernel pwn writeup I made about a year ago. 00:04:54.860 --> 00:04:57.360 (I'm just switching the version of Zig that's in my $PATH.) 00:04:57.960 --> 00:04:59.940 So, this is some kernel exploit 00:05:00.640 --> 00:05:02.680 (the code is not important, just the context is), 00:05:02.780 --> 00:05:06.660 it's a vulnerable x86 Linux kernel module. 00:05:07.160 --> 00:05:09.260 So I'm going to call this runnable 00:05:09.360 --> 00:05:10.260 "pawny-run-exploit". 00:05:10.360 --> 00:05:13.560 Ok, we have uid=1337, some stuff, 00:05:13.660 --> 00:05:14.919 then we get root, that's generally a good 00:05:14.920 --> 00:05:15.980 sign for kernel pwn. 00:05:16.980 --> 00:05:19.759 Just to prove there's no movie magic, let's add a 00:05:19.760 --> 00:05:20.720 compile error here. 00:05:21.520 --> 00:05:23.600 Ok, so we get the error in our *Org-Babel Error Output* 00:05:23.700 --> 00:05:25.300 buffer, just as we'd expect. 00:05:25.700 --> 00:05:27.610 And also, we can also add 00:05:27.710 --> 00:05:31.420 "kernel base are belong to us". 00:05:32.020 --> 00:05:33.330 Run this again— 00:05:33.830 --> 00:05:35.380 sorry, still have "foobar" here— 00:05:38.280 --> 00:05:41.740 and we will see output reflected here. 00:05:41.940 --> 00:05:43.060 So what's actually happening? 00:05:43.160 --> 00:05:44.980 For the sake of time, I'm going to give a very 00:05:45.080 --> 00:05:47.020 high level play-by-play. 00:05:47.320 --> 00:05:50.599 So "pawny-compile-exploit", it's cross-compiling 00:05:50.600 --> 00:05:53.650 our block of code to x86_64-linux 00:05:53.750 --> 00:05:55.560 (I'm currently on aarch64-darwin), 00:05:56.360 --> 00:05:57.719 then "pawny-run-command": 00:05:57.720 --> 00:06:00.960 ok, so this is going to regenerate an initramfs, 00:06:01.160 --> 00:06:03.980 it's going to include the exploit we just compiled, 00:06:04.080 --> 00:06:06.959 it also, based on some Org babel variable, 00:06:06.960 --> 00:06:09.839 it will change the init shell to be run 00:06:09.840 --> 00:06:11.599 as root or an unprivileged user, which is really 00:06:11.600 --> 00:06:13.140 useful for debugging a kernel exploit. 00:06:13.740 --> 00:06:17.200 Then we're going to run a QEMU micro VM over here, 00:06:17.900 --> 00:06:20.759 and we're actually communicating bidirectionally with 00:06:20.760 --> 00:06:23.039 the shell in the VM and the Bash process running 00:06:23.040 --> 00:06:25.220 this Org babel code block with the power of 00:06:25.320 --> 00:06:26.719 Unix sockets and socat. 00:06:26.720 --> 00:06:28.580 And then we're sending some $shellcmd. 00:06:29.480 --> 00:06:31.719 And then down here, this is an ELisp block I'm using to 00:06:31.720 --> 00:06:33.520 clean up the output using Org babel's 00:06:33.620 --> 00:06:34.620 :post header argument. 00:06:34.820 --> 00:06:36.799 That's just to get rid of ANSI control codes and 00:06:36.800 --> 00:06:37.780 boot logs and stuff like that. 00:06:38.380 --> 00:06:40.860 So, this is a pretty nice workflow! 00:06:40.960 --> 00:06:43.700 I actually used this to solve this challenge, and 00:06:43.800 --> 00:06:45.620 we also get all the trimmings of Org mode: 00:06:45.820 --> 00:06:49.659 I can use noweb syntax, I can reverse tangle and then I 00:06:49.660 --> 00:06:52.180 can use LSP in a proper zig-mode buffer, 00:06:52.380 --> 00:06:56.100 I can also use Org export and I can export this code 00:06:56.200 --> 00:07:00.380 and the results to a PDF or a website. 00:07:00.580 --> 00:07:02.940 I used ox-hugo to export this exact writeup 00:07:03.040 --> 00:07:03.960 to my website. 00:07:04.560 --> 00:07:06.359 Overall, just a really cool technique. 00:07:06.360 --> 00:07:08.999 And again, to remind you: I can send this Org 00:07:09.000 --> 00:07:10.520 document to you, and you can run the exact same 00:07:10.620 --> 00:07:12.180 thing on base Emacs 00:07:12.380 --> 00:07:13.839 (with no extra ELisp packages). 00:07:13.840 --> 00:07:15.999 You will obviously need QEMU and Zig to run this 00:07:16.000 --> 00:07:19.000 stuff, but yeah, that's pretty cool. 00:07:20.800 --> 00:07:22.959 For the next example, I thought if I can talk to 00:07:22.960 --> 00:07:25.839 a VM over socat, why not do the same thing using 00:07:25.840 --> 00:07:27.460 the rich functionality of Emacs core? NOTE Serial 00:07:27.560 --> 00:07:30.180 So I made this little interactive serial REPL, 00:07:30.380 --> 00:07:32.339 which again, doesn't require any external functionality, 00:07:32.340 --> 00:07:36.100 it's all in ELisp—30-odd lines of ELisp— 00:07:36.150 --> 00:07:37.940 which is pretty impressive for something like this. 00:07:39.060 --> 00:07:40.679 I actually have a board on my desk, I'm doing 00:07:40.680 --> 00:07:41.999 bring-up on this RISC-V board. 00:07:42.000 --> 00:07:46.359 So I'm going to CALL this runnable, and it will 00:07:46.360 --> 00:07:48.720 wait until it hits a U-Boot prompt... 00:07:49.420 --> 00:07:50.800 Let me turn on the board... 00:07:52.500 --> 00:07:53.239 and there we go, we 00:07:53.240 --> 00:07:55.560 have in our buffer "Pretty cool, huh?". 00:07:56.260 --> 00:07:58.260 I also added a feature 00:07:58.860 --> 00:08:00.900 to give me an interactive terminal— 00:08:01.200 --> 00:08:03.800 it will open the serial device in a term-mode buffer 00:08:05.000 --> 00:08:08.160 So, that's pretty neat. 00:08:08.860 --> 00:08:12.360 (I'll turn this off so the fan doesn't annoy me.) 00:08:14.060 --> 00:08:16.799 Again, I just want to emphasize, this doesn't 00:08:16.800 --> 00:08:18.479 have to be the most general runnable of all time, 00:08:18.480 --> 00:08:19.759 it really just has to fit my use case. 00:08:19.760 --> 00:08:22.039 I actually did use this to bring up this RISC-V 00:08:22.040 --> 00:08:23.480 board, it was really useful because 00:08:23.780 --> 00:08:25.439 when you're debugging a kernel panic, you don't have a 00:08:25.440 --> 00:08:26.800 userspace and therefore no TRAMP. 00:08:27.800 --> 00:08:29.559 Ok, the last example I want to talk about is 00:08:29.560 --> 00:08:30.599 about higher order functions. 00:08:30.600 --> 00:08:32.479 If we're passing around code blocks as values, 00:08:32.480 --> 00:08:34.719 naturally you might think of something like map 00:08:34.720 --> 00:08:35.540 as a higher order function. 00:08:36.440 --> 00:08:40.279 The details I'm going to gloss over because we're 00:08:40.280 --> 00:08:43.679 short on time, but down here I have this "map". 00:08:43.680 --> 00:08:45.679 I'm mapping the above code block over my 3 00:08:45.680 --> 00:08:48.780 most recent TRAMP machines. 00:08:49.280 --> 00:08:51.420 So it's going to open this stuff... 00:08:51.520 --> 00:08:53.320 it's taking a little bit to think... 00:08:53.920 --> 00:08:56.479 I will emphasize, the things you pass in, it's 00:08:56.480 --> 00:08:58.439 just a list-of-lists where each child list is the 00:08:58.440 --> 00:08:59.719 list of header arguments you pass to the 00:08:59.720 --> 00:09:00.340 source block. 00:09:00.740 --> 00:09:03.280 So here I'm mapping over :dir, but you could just as 00:09:03.380 --> 00:09:07.960 easily map over :results, :var, whatever. 00:09:08.060 --> 00:09:10.539 And then I have some :post header argument to 00:09:10.540 --> 00:09:11.480 make the table look nice. NOTE Parallel map 00:09:12.880 --> 00:09:14.519 I want to briefly talk about "parallel map", 00:09:14.520 --> 00:09:16.519 a little experiment I did to try and use Emacs's 00:09:16.520 --> 00:09:18.280 multithreading feature to speed up map. 00:09:18.980 --> 00:09:20.520 Unfortunately, it only works with ELisp, 00:09:20.620 --> 00:09:22.200 it doesn't work with any external processes. 00:09:22.400 --> 00:09:24.279 Probably not too useful, but maybe someone out 00:09:24.280 --> 00:09:25.700 there will find it interesting. NOTE Tagle map 00:09:26.700 --> 00:09:28.900 The last thing I want to talk about is "tangle map". 00:09:29.720 --> 00:09:33.239 This is essentially mapping a cat runnable over a 00:09:33.240 --> 00:09:35.739 file, but you can use this in conjunction with 00:09:35.740 --> 00:09:39.060 "map" to get a kind of devops workflow. 00:09:42.360 --> 00:09:45.760 (I have to type in my auth-source password for my password-store password.) 00:09:47.060 --> 00:09:49.260 I'm tangling this to /var/run, so that requires root, 00:09:49.360 --> 00:09:50.660 which is why I need to add my password. 00:09:51.060 --> 00:09:54.120 Just to prove that's actually there... 00:10:04.420 --> 00:10:06.079 So yeah, that's another pretty interesting 00:10:06.080 --> 00:10:06.640 thing you can do. 00:10:07.340 --> 00:10:09.920 And then if we're talking about map, of course we have fold. 00:10:13.140 --> 00:10:15.999 I guess I did not load ob-python, but that's not 00:10:16.000 --> 00:10:16.479 too important. 00:10:16.480 --> 00:10:20.400 But fold works the same way as map, pretty much. 00:10:20.500 --> 00:10:23.239 Maybe you could fold a code block multiple times 00:10:23.240 --> 00:10:24.740 to take some benchmarking statistics. NOTE Conclusion 00:10:26.040 --> 00:10:27.780 So that brings me to the end of my talk. 00:10:27.880 --> 00:10:29.440 If you take away one thing, I just want to 00:10:29.540 --> 00:10:31.240 emphasize that you can use a tiny bit of ELisp 00:10:31.540 --> 00:10:33.959 to augment the already very powerful capabilities of 00:10:33.960 --> 00:10:34.560 Org babel. 00:10:34.760 --> 00:10:36.940 Whether your document is a research paper, blog post, 00:10:37.040 --> 00:10:38.879 homework assignment, whatever: I encourage 00:10:38.880 --> 00:10:40.880 you to get creative and bend Emacs to your will. 00:10:41.880 --> 00:10:42.720 Happy hacking!