• Stars
    star
    152
  • Rank 244,685 (Top 5 %)
  • Language
    Rust
  • License
    GNU Lesser Genera...
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An interactive Nix documentation tool providing a CLI for function search, a Nix plugin for docs in the REPL, and a ctags implementation for Nix script

nix-doc

A Nix developer tool leveraging the rnix Nix parser for intelligent documentation search and tags generation.

Features

  • Nix plugin that adds a builtin that can display the signature and documentation for a lambda object in a friendly format
  • Command line tool that searches Nix files in a directory for functions and shows documentation for matching ones
  • Tags generator similar to ctags that can generate a vim compatible tags file for Nix source
  • High performance, threaded implementation in Rust

Setup

# nix-doc is in nixpkgs
$ nix-env -i nix-doc

# you can alternatively get it from git
$ nix-env -i -f https://github.com/lf-/nix-doc/archive/main.tar.gz

# or if you don't want to use nix (only includes the command line tool for
# search and tags)
$ cargo install --locked nix-doc

Nix Plugin

To install the Nix plugin on a single-user installation of Nix, add this to your Nix config at ~/.config/nix/nix.conf after installing nix-doc with nix-env:

plugin-files = /home/YOURUSERNAMEHERE/.nix-profile/lib/libnix_doc_plugin.so

For a multi-user installation, you will need to do something like this:

$ sudo ln -s $(nix-build '<nixpkgs>' -A nix-doc) /opt/nix-doc

and then put this into your /etc/nix/nix.conf:

plugin-files = /opt/nix-doc/lib/libnix_doc_plugin.so

NixOS Installation

Link the plugin file using nix.extraOptions:

{ pkgs, ... }:

{
  nix.extraOptions = ''
    plugin-files = ${pkgs.nix-doc}/lib/libnix_doc_plugin.so
  '';

  environment.systemPackages = with pkgs; [
    nix-doc
  ];
}

Usage

CLI

nix-doc <command>

nix-doc tags [dir]

Generates a vim-compatible tags file in the current directory, for all nix script files below the directory dir.

Example:

nixpkgs$ nix-doc tags

nixpkgs$ file tags
tags: Exuberant Ctags tag file, ASCII text, with very long lines (502)

# opens vim to the function callCabal2nix
nixpkgs$ vim -t callCabal2nix

nix-doc search <regex> [dir]

Example output:

nixpkgs$ nix-doc search callPackage
   Call the package function in the file `fn' with the required
   arguments automatically.  The function is called with the
   arguments `args', but any missing arguments are obtained from
   `autoArgs'.  This function is intended to be partially
   parameterised, e.g.,

   callPackage = callPackageWith pkgs;
   pkgs = {
   libfoo = callPackage ./foo.nix { };
   libbar = callPackage ./bar.nix { };
   };

   If the `libbar' function expects an argument named `libfoo', it is
   automatically passed as an argument.  Overrides or missing
   arguments can be supplied in `args', e.g.

   libbar = callPackage ./bar.nix {
   libfoo = null;
   enableX11 = true;
   };
callPackageWith = autoArgs: fn: args: ...
# ./lib/customisation.nix:117
─────────────────────────────────────────────
   Like callPackage, but for a function that returns an attribute
   set of derivations. The override function is added to the
   individual attributes.
callPackagesWith = autoArgs: fn: args: ...
# ./lib/customisation.nix:127
─────────────────────────────────────────────
   Similar to callPackageWith/callPackage, but without makeOverridable
callPackageWith = autoArgs: fn: args: ...
# ./pkgs/development/beam-modules/lib.nix:7

Nix plugin

The Nix plugin provides three builtins:

builtins.doc f

Prints the documentation of the function f to the screen. Returns null.

builtins.getDoc f

Returns the documentation message for the function f as a string (exactly the same output as builtins.doc, just as a string).

builtins.unsafeGetLambdaPos

A backport of NixOS/Nix#3912. Returns the position of a lambda, in a similar fashion to unsafeGetAttrPos for attributes.

Sample usage:

Β» nix repl
Welcome to Nix version 2.3.7. Type :? for help.

nix-repl> n=import <nixpkgs> {}

nix-repl> builtins.doc n.lib.callPackageWith
   Call the package function in the file `fn' with the required
   arguments automatically.  The function is called with the
   arguments `args', but any missing arguments are obtained from
   `autoArgs'.  This function is intended to be partially
   parameterised, e.g.,

     callPackage = callPackageWith pkgs;
     pkgs = {
       libfoo = callPackage ./foo.nix { };
       libbar = callPackage ./bar.nix { };
     };

   If the `libbar' function expects an argument named `libfoo', it is
   automatically passed as an argument.  Overrides or missing
   arguments can be supplied in `args', e.g.

     libbar = callPackage ./bar.nix {
       libfoo = null;
       enableX11 = true;
     };
func = autoArgs: fn: args: ...
# /nix/store/frpij1x0ihnyc4r5f7v0zxwpslkq6s27-nixpkgs-20.09pre237807.0dc87c6e54f/nixpkgs/lib/customisation.nix:117
null

Development

This repository is set up as a Cargo workspace with the plugin and the command line tool/library as parts.

It is not really possible to build the plugin outside a Nix shell since Nix does not provide libraries outside the shell environment. As such, it is suggested to use a nix shell while developing the plugin as follows:

$ nix-shell
[nix-shell]$ cargo build
[nix-shell]$ cargo check
[nix-shell]$ cargo test
# etc

TODO

  • Tech: should update rnix to the latest major.

Related work

  • NixOS/nix#1652: A PR implementing basically the same thing as this tool's plugin in Nix itself, which has been deferred indefinitely due to disagreements about what syntax to use in documentation comments.
  • https://github.com/tazjin/nixdoc: A Rust tool producing DocBook documentation for Nix library functions.
  • https://github.com/mlvzk/manix: An early fork of this tool with a stronger focus on CLI usage, with support for indexing and faster search. By comparison, their CLI is better, but they don't do tags or a Nix plugin.

Project information

Everyone is expected to follow the code of conduct while participating in this project.

More Repositories

1

clipper

TLS key escrow/interception for debugging
Rust
244
star
2

nix-otel

Nix OpenTelemetry sender plugin
Rust
56
star
3

flakey-profile

Declarative profiles with nix flakes
Nix
54
star
4

gridlock

Nix compatible lockfile manager, without Nix
Rust
47
star
5

pwintln

pwintln uwu
Rust
34
star
6

dotfiles

Dotfiles, a monorepo, who is to know!!
Haskell
24
star
7

affinity-crimes

Make affinity designer work on linux, supposedly
Nix
23
star
8

mu

[in-progress] toy rust microkernel and operating system for riscv64
Rust
23
star
9

aiopanel

An asyncio text-based panel
Python
18
star
10

ShortcutItPy

[UNMAINTAINED but probably works] Fusion 360 add-in to make it possible to add shortcuts for things that previously could not be shortcut
Python
15
star
11

typst-algorithmic

Algorithm pseudocode typesetting library for Typst
Typst
14
star
12

influx_nut

[unmaintained but still works] A small interface between NUT and influxdb
Python
14
star
13

nix-lib

Jade's Nix libraries
Nix
10
star
14

de1-soc-nixos

NixOS for the DE1-SoC Cyclone V dev board
Nix
9
star
15

RenameThisDimension

Ever wanted to rename a sketch dimension from the sketch editor in Fusion 360? Me too. Now you can.
C++
7
star
16

mediapad

A *tiny* piece of code for implementing a media keypad/macropad with an arduino and a cheap keypad
C++
7
star
17

necktangler

hydra branch crimer
Rust
7
star
18

splice_graft

GitHub API multitool
Python
6
star
19

hsfixit

WIP: Automatic warning fixer for GHC 9.4+
Haskell
6
star
20

switchstring

Rust
3
star
21

lti13

A Haskell LTI 1.3 library
Haskell
3
star
22

blog

The code behind jade.fyi
JavaScript
3
star
23

aiobspwm

An asyncio library for bspwm
Python
2
star
24

flake-templates

Flake templates using flake-utils
Nix
2
star
25

netlogin

[unmaintained, probably non working] Easy login for captive portal networks
Python
2
star
26

PSFactorio

[unmaintained fun project] A minimal Factorio mod management PowerShell module
PowerShell
2
star
27

cpen400p-llvm-nix

C++
1
star
28

ctf

PHP
1
star
29

.vim

[ARCHIVED] this has been merged into lf-/dotfiles as a subtree
Vim Script
1
star
30

notion-entry

Improved Notion data entry, at the command line
Rust
1
star
31

ssc-tz

Chrome extension that adds timezone info to the UBC course selection site.
TypeScript
1
star
32

ServoExtender

A digispark project to extend a servo to its full range given limited range input
C++
1
star
33

reality

Documentation for physical projects
HTML
1
star
34

decsession

debug tool: decrypt yesod sessions from the command line
Nix
1
star
35

readmem

Verilog $readmemb/$readmemh for Rust
Rust
1
star
36

armasm-test-framework

Assembly
1
star
37

polkadots

Personal dotfile manager
Python
1
star