Coder Social home page Coder Social logo

nix-literate-template's Introduction

What

This repo provides a template to help with literate programming of Nix projects.

When the source blocks are interspersed with text (as is common), the nix indentation in orgmode may get a little confused which can cause garbage formatting changes. Two provided functions help integrate nix fmt changes back into orgmode.

Features

  • Write in org -- then tangle, format and detangle with one function
  • Edit a tangled file directly -- propagate changes back
  • Basic feature tags for semantic commits

Usage

When working on a orgmode file, call custom functions local-proj-tangle-format-detangle to:

  1. Tangle all src blocks
  2. Call nix fmt to apply formatter from the flake
  3. Detangle all sources back into orgmode file to fix formatting

Alternatively, when a .nix file was edited directly -- call local-proj-detangle-all in orgmode to detangle all changes back.

Using as before-save-hook

It's possible to use similar functions as a pre-save hook (see caveats for, well, caveats). .dir-locals.el for this:

((org-mode
   (eval add-hook 'before-save-hook (lambda ()(org-babel-tangle)) t t)
   ;; Run nix fmt before save
   (eval add-hook 'before-save-hook (lambda ()(shell-command "nix fmt")) t t)
   (eval add-hook 'before-save-hook
         (lambda ()
           (let ((previous-value org-src-window-setup))
             ;; temporarily break org-src-window-setup, otherwise detangle creates unneeded frames
             (setq org-src-window-setup 'nil)

             (mapcar #'org-babel-detangle (directory-files-recursively "." ".*\.nix"))
             ;; revert org-src-window-setup
             (setq org-src-window-setup previous-value))) t t)))

Example

See project.org for the sample org mode file.

Alternatives

  • org-tanglesync.el
  • org-babel-detangle -- note, may cause extra frames with files to appear. This is worked around in local-proj-detangle-all.

Caveats

  • The current detangle behavior does not work well if multiple headlines (even on different level) have the same text -- stuff can get overwritten when detangling. Workaround: change #+NAME of code block or rename the headline. Alternatively, change logic of org-babel-tangle-comment-format-beg generation.
  • It's possible to use the function from .dir-locals.el as a before-save hook but if the number of src blocks is >40(empricial number), it will noticeably slow down the save process
  • When detangling -- the cursor loses its position and jumps to the beginning of SRC block.

nix-literate-template's People

Contributors

vtimofeenko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.