Skip to content

Commit

Permalink
nixos/doc: add ctags support for NixOS options
Browse files Browse the repository at this point in the history
This is rather a strawman of a PR to try to figure out the best spot to
put this code and the documentation of how to use it. I don't really
know.

Sample content:
```
!_TAG_FILE_SORTED	1	1
!_TAG_FILE_ENCODING	utf-8	1
_module.args	/home/jade/dev/nixpkgs/lib/modules.nix	122
_module.check	/home/jade/dev/nixpkgs/lib/modules.nix	186
_module.freeformType	/home/jade/dev/nixpkgs/lib/modules.nix	193
_module.specialArgs	/home/jade/dev/nixpkgs/lib/modules.nix	209
appstream.enable	/home/jade/dev/nixpkgs/nixos/modules/config/appstream.nix	6
assertions	/home/jade/dev/nixpkgs/nixos/modules/misc/assertions.nix	9
boot.binfmt.emulatedSystems	/home/jade/dev/nixpkgs/nixos/modules/system/boot/binfmt.nix	272
```

Sample usage (can be used in parallel with `nix-doc tags`):
```
nix build -f nixos/release.nix optionsCtags -o opts-tags
(in vim): :set tags+=opts-tags
          :tj boot.binfmt.emulatedSystems
```

This PR depends on and contains NixOS#249243. Marked as draft till that's
worked out. This PR pair replaces NixOS#208173.
  • Loading branch information
lf- committed Aug 30, 2023
1 parent 26a5174 commit 796e168
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
28 changes: 28 additions & 0 deletions nixos/lib/ctags.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Implementation of ctags(1) compatible tags file for NixOS options
{ lib, options }:
with lib;
let
locationToTagLine = name: loc:
let line = if loc ? line && loc.line != null then loc.line else 1;
in
if loc != null then
"${name}\t${loc.file}\t${toString line}"
else "";

toTags = acc0: struct: (foldl'
(acc: name:
let item = struct.${name}; in
if item ? _type && item._type == "option" then
acc ++ map (locationToTagLine (toString item)) item.declarationPositions
else toTags acc item
)
acc0
(attrNames struct));

# see vim help tags-file-format for details
header = ''
!_TAG_FILE_SORTED''\t1''\t1
!_TAG_FILE_ENCODING''\tutf-8''\t1
'';
in
header + concatStringsSep "\n" (sort lessThan (toTags [ ] options)) + "\n"
5 changes: 5 additions & 0 deletions nixos/modules/misc/documentation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ let
];
};

options-ctags = import ../../lib/ctags.nix {
inherit lib options;
};

in

{
Expand Down Expand Up @@ -344,6 +348,7 @@ in

(mkIf cfg.nixos.enable {
system.build.manual = manual;
system.build.optionsCtags = pkgs.writeTextFile { name = "options-tags"; text = options-ctags; };

environment.systemPackages = []
++ optional cfg.man.enable manual.manpages
Expand Down
1 change: 1 addition & 0 deletions nixos/release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ in rec {
manualEpub = (buildFromConfig ({ ... }: { }) (config: config.system.build.manual.manualEpub));
manpages = buildFromConfig ({ ... }: { }) (config: config.system.build.manual.manpages);
options = (buildFromConfig ({ ... }: { }) (config: config.system.build.manual.optionsJSON)).x86_64-linux;
optionsCtags = (buildFromConfig ({ ... }: { }) (config: config.system.build.optionsCtags)).x86_64-linux;


# Build the initial ramdisk so Hydra can keep track of its size over time.
Expand Down

0 comments on commit 796e168

Please sign in to comment.