gunk-dev/the-valley

knowledge.nix

source60ad80d5a5a5main's published tip history
5.1 KiB
1# The knowledge lint and the prose: the lint in the reusable form a consuming
2# project instantiates, the graphs it must reject, and the format every
3# markdown file is held to. the-valley's own source holds no knowledge graph:
4# its plan graph is project storage, at refs/the-valley/knots, and the push
5# hook holds it to the knot module's rules. The nodes under
6# archive/.the-valley/ are history, and nothing lints them.
7{
8 pkgs,
9 lib,
10 self,
11 system,
12 packages,
13 partOf,
14 ...
15}:
16let
17 root = ../..;
18
19 # What prettier reads: every markdown file, and the files it takes its
20 # configuration and its list of files to ignore from.
21 prettierReads = lib.fileset.fileFilter (
22 f:
23 f.hasExt "md"
24 || builtins.elem f.name [
25 ".editorconfig"
26 ".gitignore"
27 ".prettierignore"
28 "package.json"
29 "package.yaml"
30 ]
31 || lib.hasPrefix ".prettierrc" f.name
32 || lib.hasPrefix "prettier.config." f.name
33 ) root;
34
35 # The documents whose links prose-format.sh resolves, and their text.
36 linking =
37 lib.filter builtins.pathExists [
38 (root + "/README.md")
39 (root + "/AGENTS.md")
40 ]
41 ++ map (n: root + "/docs/${n}") (
42 lib.filter (lib.hasSuffix ".md") (builtins.attrNames (builtins.readDir (root + "/docs")))
43 );
44 linkingText = lib.concatMapStrings builtins.readFile linking;
45
46 # Every directory and file of the tree, as a path relative to its root,
47 # with a slash after each directory. A checkout's .git is no part of the
48 # source, and is skipped where a path flake shows it.
49 entries =
50 dir: prefix:
51 lib.concatLists (
52 lib.mapAttrsToList (
53 name: type:
54 if type == "directory" then
55 [ "${prefix}${name}/" ] ++ entries (dir + "/${name}") "${prefix}${name}/"
56 else
57 [ "${prefix}${name}" ]
58 ) (removeAttrs (builtins.readDir dir) [ ".git" ])
59 );
60
61 # The link check asks only whether a link's target exists, and a link
62 # names its target's path, so the target's name is in the linking
63 # documents' text. The tree's other files and directories whose names are
64 # in that text are given by path alone, and stand empty in the tree the
65 # check reads. Every link resolves as it does in the source, and an edit
66 # to one of those files changes nothing here.
67 #
68 # The text is split once into words, at the characters a link's path
69 # cannot hold (whitespace, brackets, parentheses, quotes, #, :, comma and so on) and
70 # at slashes, so each path component of a link is a word. An entry is a
71 # target when its name is one of the words, which is a set lookup.
72 # Searching the text for each entry's name instead costs a minute.
73 linkWords = builtins.listToAttrs (
74 map (word: lib.nameValuePair word true) (
75 lib.filter (w: w != "" && lib.isString w) (
76 builtins.split "[][:space:]()<>\"'`#:,/*|]+" linkingText
77 )
78 )
79 );
80 linkTargets = lib.filter (e: !(lib.hasSuffix ".md" e) && linkWords ? ${baseNameOf e}) (
81 entries root ""
82 );
83in
84{
85 # The reusable form, exercised over a foreign tree: a project that
86 # is a README, a docs directory and seven nodes, and a project with no
87 # graph at all. Both must pass, and both are checked by depending on
88 # the instantiated derivations rather than by re-running the lint.
89 knowledge-lint-consumer = pkgs.runCommand "valley-knowledge-lint-consumer" {
90 consumer = self.lib.knowledgeLint {
91 inherit system;
92 src = ../../examples/graphs/consumer;
93 };
94 graphless = self.lib.knowledgeLint {
95 inherit system;
96 src = ../../examples/graphs/graphless;
97 };
98 } (builtins.readFile ./knowledge-lint-consumer.sh);
99
100 # The lint must fail on a broken graph, and its failure must name
101 # the file and the defect โ€” a check that only says "no" leaves the
102 # reader to find the breakage themselves. The script and the schema
103 # here are the same store paths lib.knowledgeLint uses, so the
104 # rejections cannot drift from what a consumer runs.
105 knowledge-lint-rejects = pkgs.runCommand "valley-knowledge-lint-rejects" {
106 nativeBuildInputs = [
107 pkgs.cue
108 pkgs.python3
109 ];
110 lint = ../knowledge-lint.py;
111 nodeSchema = ../../schema/node.cue;
112 graphs = partOf ../../examples/graphs [ ../../examples/graphs/rejected ];
113 rejectedCases = ./knowledge-lint-rejected.txt;
114 } (builtins.readFile ./knowledge-lint-rejects.sh);
115
116 # Markdown prose format (ida-1ec03b1): formatter idempotence โ€”
117 # formatting the tree must change nothing โ€” and, across README.md,
118 # AGENTS.md and docs/*.md, relative link and heading-anchor integrity
119 # and the style rules of AGENTS.md that a pattern can catch.
120 # On formatting failure, fix with `nix run .#fmt`.
121 #
122 # It reads every markdown file in the tree, archives and fixtures
123 # included, and prettier's configuration. Other files are link targets
124 # only, given by path.
125 prose-format = pkgs.runCommand "valley-prose-format" {
126 nativeBuildInputs = [
127 pkgs.gawk
128 pkgs.prettier
129 pkgs.python3
130 ];
131 markdown = lib.fileset.toSource {
132 inherit root;
133 fileset = prettierReads;
134 };
135 linkTargets = lib.concatLines linkTargets;
136 passAsFile = [ "linkTargets" ];
137 inherit (packages) proseFmtArgs;
138 } (builtins.readFile ./prose-format.sh);
139}