gunk-dev/the-valley

knowledge.nix

sourcefce1c319f644main's published tip history
4.6 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 linkTargets = lib.filter (e: !(lib.hasSuffix ".md" e) && lib.hasInfix (baseNameOf e) linkingText) (
68 entries root ""
69 );
70in
71{
72 # The reusable form, exercised over a foreign tree: a project that
73 # is a README, a docs directory and seven nodes, and a project with no
74 # graph at all. Both must pass, and both are checked by depending on
75 # the instantiated derivations rather than by re-running the lint.
76 knowledge-lint-consumer = pkgs.runCommand "valley-knowledge-lint-consumer" {
77 consumer = self.lib.knowledgeLint {
78 inherit system;
79 src = ../../examples/graphs/consumer;
80 };
81 graphless = self.lib.knowledgeLint {
82 inherit system;
83 src = ../../examples/graphs/graphless;
84 };
85 } (builtins.readFile ./knowledge-lint-consumer.sh);
86
87 # The lint must fail on a broken graph, and its failure must name
88 # the file and the defect โ€” a check that only says "no" leaves the
89 # reader to find the breakage themselves. The script and the schema
90 # here are the same store paths lib.knowledgeLint uses, so the
91 # rejections cannot drift from what a consumer runs.
92 knowledge-lint-rejects = pkgs.runCommand "valley-knowledge-lint-rejects" {
93 nativeBuildInputs = [
94 pkgs.cue
95 pkgs.python3
96 ];
97 lint = ../knowledge-lint.py;
98 nodeSchema = ../../schema/node.cue;
99 graphs = partOf ../../examples/graphs [ ../../examples/graphs/rejected ];
100 rejectedCases = ./knowledge-lint-rejected.txt;
101 } (builtins.readFile ./knowledge-lint-rejects.sh);
102
103 # Markdown prose format (ida-1ec03b1): formatter idempotence โ€”
104 # formatting the tree must change nothing โ€” and, across README.md,
105 # AGENTS.md and docs/*.md, relative link and heading-anchor integrity
106 # and the style rules of AGENTS.md that a pattern can catch.
107 # On formatting failure, fix with `nix run .#fmt`.
108 #
109 # It reads every markdown file in the tree, archives and fixtures
110 # included, and prettier's configuration. Other files are link targets
111 # only, given by path.
112 prose-format = pkgs.runCommand "valley-prose-format" {
113 nativeBuildInputs = [
114 pkgs.gawk
115 pkgs.prettier
116 pkgs.python3
117 ];
118 markdown = lib.fileset.toSource {
119 inherit root;
120 fileset = prettierReads;
121 };
122 linkTargets = lib.concatLines linkTargets;
123 passAsFile = [ "linkTargets" ];
124 inherit (packages) proseFmtArgs;
125 } (builtins.readFile ./prose-format.sh);
126}