Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc/doc-support: Generalize library docs code #305847

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ let

common = import ./common.nix;

lib-docs = import ./doc-support/lib-function-docs.nix {
lib-docs = import ./doc-support/generate-function-docs.nix {
inherit pkgs nixpkgs;
library = pkgs.lib;
src = ../lib;
name = "nixpkgs-lib-docs";
prefix = "lib";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A am about to change this default in nixdoc. Can you change it to "lib." already here ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So more like a string prefix than a path prefix? That's also fine.

Copy link
Contributor

@hsjobeki hsjobeki Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that what you requested in nix-community/nixdoc#119? Or did I understand it wong?

This now depends on which one gets merged earlier. Ideally, we would merge this PR first nix-community/nixdoc#122 to support empty prefixes with the default prefix="lib.".

The problem is, that if we change the prefix delimiter in the wrong way. The manual will not build because the heading IDs on the manual change.

If we merge this one first, we are in a deadlock. Where we must implement a backwards compatible migration in nixdoc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both solutions are valid, and I don't have a preference.
Requiring the . to be specified on the command line is more of a breaking change, but the implementation is slightly simpler.

libsets = [
{ name = "asserts"; description = "assertion functions"; }
{ name = "attrsets"; description = "attribute set functions"; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
# Generates the documentation for library functions via nixdoc.

{ pkgs, nixpkgs, libsets }:
{ pkgs, nixpkgs, libsets, library, src, name, prefix }:

with pkgs;

let
locationsJSON = import ./lib-function-locations.nix { inherit pkgs nixpkgs libsets; };
locationsJSON = import ./lib-function-locations.nix { inherit pkgs nixpkgs libsets library prefix; };
in
stdenv.mkDerivation {
name = "nixpkgs-lib-docs";
src = ../../lib;
inherit src name;

buildInputs = [ nixdoc ];
env = {
# lower case $prefix is written by setup.sh for ./configure when empty
PREFIX = prefix;
PREFIXDOT = if prefix == "" then "" else "${prefix}.";
};
installPhase = ''
function docgen {
name=$1
baseName=$2
description=$3
# TODO: wrap lib.$name in <literal>, make nixdoc not escape it
# TODO: wrap ${prefix}$name in <literal>, make nixdoc not escape it
if [[ -e "../lib/$baseName.nix" ]]; then
nixdoc -c "$name" -d "lib.$name: $description" -l ${locationsJSON} -f "$baseName.nix" > "$out/$name.md"
nixdoc -c "$name" -d "$PREFIXDOT$name: $description" -l ${locationsJSON} -f "$baseName.nix" --prefix "$PREFIX" > "$out/$name.md"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require nixdoc to support that feature as you already mentioned. I could take care of that in the next couple of days.

else
nixdoc -c "$name" -d "lib.$name: $description" -l ${locationsJSON} -f "$baseName/default.nix" > "$out/$name.md"
nixdoc -c "$name" -d "$PREFIXDOT$name: $description" -l ${locationsJSON} -f "$baseName/default.nix" --prefix "$PREFIX" > "$out/$name.md"
fi
echo "$out/$name.md" >> "$out/index.md"
}
Expand Down
15 changes: 12 additions & 3 deletions doc/doc-support/lib-function-locations.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{ pkgs, nixpkgs ? { }, libsets }:
{ pkgs,
nixpkgs ? { },
# Metadata about the structure of the library in the form of a list of sub-libraries
libsets,
# An instance of the library, as one may use it. This is used for retrieving the locations with `builtins.unsafeGetAttrPos`.
library,
prefix,
}:
let
revision = pkgs.lib.trivial.revisionWithDefault (nixpkgs.rev or "master");

Expand All @@ -22,10 +29,12 @@ let

nixpkgsLib = pkgs.lib;

prefixDot = if prefix == "" then "" else prefix + ".";

flattenedLibSubset = { subsetname, functions }:
builtins.map
(fn: {
name = "lib.${subsetname}.${fn.name}";
name = "${prefixDot}${subsetname}.${fn.name}";
value = fn.location;
})
functions;
Expand All @@ -44,7 +53,7 @@ let
builtins.filter
(elem: elem.value != null)
(nixpkgsLib.lists.flatten
(locatedlibsets nixpkgsLib));
(locatedlibsets library));

fnLocationRelative = { name, value }:
{
Expand Down