Add post about tangling org files

This commit is contained in:
Oystein Kristoffer Tveit 2022-10-16 08:41:53 +02:00
parent b8c1bf5a77
commit e4ffa89a4a
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
1 changed files with 57 additions and 0 deletions

View File

@ -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