From e4ffa89a4a0ae303400b3c7d524f7e33627e0483 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 16 Oct 2022 08:41:53 +0200 Subject: [PATCH] Add post about tangling org files --- src/posts/2022-10-16-tangling-org-files.md | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/posts/2022-10-16-tangling-org-files.md diff --git a/src/posts/2022-10-16-tangling-org-files.md b/src/posts/2022-10-16-tangling-org-files.md new file mode 100644 index 0000000..46a9ae8 --- /dev/null +++ b/src/posts/2022-10-16-tangling-org-files.md @@ -0,0 +1,57 @@ +--- +title: "Tangling Org files with Nix" +keywords: nix, emacs, org-mode +image: './images/nix_banner.png' +series: "Nix shorts" +--- + +# Tangling Org files with Nix + +One of the great wonders about Emacs [Org Mode][org] is its builtin functionality for [literate programming][lit-prog]. This allows us to write code inbetween text as if it was a research article, or a markdown file, but still export and run the code. By using emacs, we can keep our source files as articles, and only extract the source code when we export the code to the nix store. + +## Example + +```org +# hello_world.org + +#+TITLE: How to write hello world in javascript +#+AUTHOR: h7x4 +#+PROPERTY: header-args :js :tangle yes + +Here's how: + +#+BEGIN_SRC js + console.log("Hello World"); +#+END_SRC + +Woah! +``` + +```nix +# default.nix +{ pkgs, stdenvNoCC }: +stdenvNoCC.mkDerivation { + name = "hello-world-js"; + src = ./.; + + buildInputs = with pkgs; [ emacs ]; + buildPhase = '' + emacs --batch -l org hello_world.org -f org-babel-tangle + ''; + + installPhase = '' + cp hello_world.js $out + ''; +} +``` + +## Tangling Nix Code? + +Unfortunately, this isn't as declarative as the previous part. If we were to process literate nix using a nix file, it would end up in the nix store. That would require us to use [import from derivation][ifd], which brings all sorts of whackiness. + +Instead, you should probably use emacs interactively to tangle the nix files, or just keep the nix files raw altogether. + +[org]: https://orgmode.org/ +[lit-prog]: https://en.wikipedia.org/wiki/Literate_programming +[ifd]: https://nixos.wiki/wiki/Import_From_Derivation +