← Pablo Murad

Note X

Tonight I finally got my act together and pushed the code of Nyx, a wiki I wrote in 2020 during the pandemic boredom. I updated it last week and released it today. You can clone it from https://code.pablomurad.com/nyx-wiki.git.

The fun part is that a single binary does both jobs. nyx servir serves the wiki on the web, and the same nyx is the terminal client, which talks to the server through its HTTP API. Here is one page seen from both sides:

Two windows side by side showing the same wiki page. On the left, a dark terminal after the command nyx ver about: the title About Nyx in bold and underlined, a paragraph saying every page is a .wir file in a folder that is also a Git repository and that the same binary serves the wiki on the web and shows the pages in the terminal, a small table with Where and Command, and a footnote at the end. On the right, a browser showing the same page in a light theme with a large italic title, the same paragraph and table, and the footnote at the bottom.

The commands are in Portuguese, and so is the rest of the interface.1

How it's built #

It's written in Go and released under the MIT license. There's no database and no JavaScript: the pages are plain HTML and CSS, and the server's Content Security Policy wouldn't run a script even if one showed up.

Each page is a .wir file in a folder that is also a Git repository. Every save becomes a commit under the name of the account that made it, and the wiki can push to a remote on its own, so the history of a page is the history Git already keeps.

Saving is careful about other people. Each page carries a version, the first 8 bytes of the SHA-256 of its file. If the page changed after you opened it, the save is refused instead of overwriting someone else's work. The new text goes to a temporary file first and is then renamed over the old one, so a crash in the middle never leaves half a page behind.

When it starts, Nyx builds an index in memory with titles, tags, links, embeds and backlinks. Search ignores accents, so "pagina" finds "página". Because links follow one strict form, nyx mover can rename a page and fix every link that pointed to it in the other pages.

In the terminal, nyx with no arguments opens a fuzzy finder over all the pages. nyx editar opens a page in your $VISUAL or $EDITOR and sends it back when you close the editor. nyx ver draws the page with ANSI codes: bold titles, underlined links with the target in brackets, dim footnotes and code in cyan. The width follows the window up to 100 columns, and when the output goes into a pipe it comes out as plain text. On Windows it switches on the console's terminal mode so the colors work there too. nyx entrar creates one token per terminal, and you can keep more than one profile.

On the web there are five themes built into the binary with go:embed: folha, terminal, herbario, rede and 1994. Folha puts footnotes in the right margin, the way this blog does.

$ go build -trimpath -ldflags="-s -w" -o nyx ./cmd/nyx
$ ./nyx versao
Nyx Wiki 0.1
Source
$ go build -trimpath -ldflags="-s -w" -o nyx ./cmd/nyx
$ ./nyx versao
Nyx Wiki 0.1

That build comes out at about 11 MB, themes and all.

Why .wir and not Markdown #

The page format is my own, and it beats Markdown in three places.

It's safe by construction. Markdown accepts HTML in the middle of the text, so every Markdown wiki has to filter it afterwards and risks letting a <script> through. .wir has no HTML at all, which removes a whole class of bugs from Nyx.

It's made for a wiki. [[page]] is always a link and {{page}} is always an embed, and both take a section (#Plot) and a label (| text). In Markdown, wiki links are an extension and every tool writes them its own way. This regularity is what lets Nyx rewrite links in other pages safely when you move one.

It's predictable. What matters is how the line starts, and there are no hidden rules. Markdown has dozens of ambiguous cases, like when a list begins or when a * turns into italics. The writer gets fewer surprises, and the server gets a small, fast parser that no text can choke.

There are smaller comforts too. A footnote sits in the middle of the sentence, ((like this)), instead of somewhere far below. Metadata are simple @titulo lines instead of YAML. A table header is |= instead of a row of dashes. And the emoji shortcuts are in Portuguese.

This is the page from the screenshot, as it sits in the repository:

@titulo About Nyx
@tags nyx, wiki

= About Nyx

Every page is a .wir file in a folder that is also a Git repository((Every save becomes a commit.)).
The same binary serves the wiki on the web and shows the pages in the terminal.

|= Where |= Command |
| web | nyx servir |
| terminal | nyx ver about |

Go back to the [[inicio | front page]].

P.S. Version 1.1, if it ever exists, will come with compression. The poor thing has been eating pandemic snacks since 2020 and could use a diet.

P.P.S. In case you're wondering, .wir is "wiki" and "runv" (as in runv.club) glued together. No deep reason, I just mashed the two words.

Notes

  1. That's my one mistake with Nyx, but back in 2020 that's just how it was. ↩