Skip to content

Commit d7df583

Browse files
committed
docs: eval modules without access to pkgs
Replace the `package-options` test with a stricter implementation. When evaluating modules for use in the docs, provide them with a stubbed `pkgs` instance that throws an error whenever a package is evaluated. This ensures we don't accidentally use any packages in defaults or examples.
1 parent 0b4a4e8 commit d7df583

File tree

3 files changed

+51
-96
lines changed

3 files changed

+51
-96
lines changed

docs/default.nix

+51-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,56 @@ let
99
pkgs = import ./pkgs.nix { inherit system nixpkgs; };
1010
inherit (pkgs) lib;
1111

12+
# A stub pkgs instance used while evaluating the nixvim modules for the docs
13+
# If any non-meta attr is accessed, the eval will throw
14+
noPkgs =
15+
let
16+
# Known suffixes for package sets
17+
suffixes = [
18+
"Plugins"
19+
"Packages"
20+
];
21+
22+
# Predicate for whether an attr name looks like a package set
23+
# Determines whether stubPackage should recurse
24+
isPackageSet = name: builtins.any (lib.flip lib.strings.hasSuffix name) suffixes;
25+
26+
# Need to retain `meta.homepage` if present
27+
stubPackage =
28+
prefix: name: package:
29+
let
30+
loc = prefix ++ [ name ];
31+
in
32+
if isPackageSet name then
33+
lib.mapAttrs (stubPackage loc) package
34+
else
35+
lib.mapAttrs (_: throwAccessError loc) package
36+
// lib.optionalAttrs (package ? meta) { inherit (package) meta; };
37+
38+
throwAccessError =
39+
loc:
40+
throw "Attempted to access `${
41+
lib.concatStringsSep "." ([ "pkgs" ] ++ loc)
42+
}` while rendering the docs.";
43+
in
44+
lib.fix (
45+
self:
46+
lib.mapAttrs (stubPackage [ ]) pkgs
47+
// {
48+
pkgs = self;
49+
# The following pkgs attrs are required to eval nixvim, even for the docs:
50+
inherit (pkgs)
51+
_type
52+
stdenv
53+
stdenvNoCC
54+
symlinkJoin
55+
runCommand
56+
runCommandLocal
57+
writeShellApplication
58+
;
59+
}
60+
);
61+
1262
nixvimPath = toString ./..;
1363

1464
gitHubDeclaration = user: repo: branch: subpath: {
@@ -37,7 +87,7 @@ let
3787
modules = [
3888
{
3989
isDocs = true;
40-
nixpkgs.pkgs = pkgs;
90+
_module.args.pkgs = lib.mkForce noPkgs;
4191
}
4292
];
4393
};

tests/default.nix

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ in
4444
nixpkgs-module = callTest ./nixpkgs-module.nix { };
4545
plugins-by-name = callTest ./plugins-by-name.nix { };
4646
generated = callTest ./generated.nix { };
47-
package-options = callTest ./package-options.nix { };
4847
lsp-all-servers = callTest ./lsp-servers.nix { };
4948
}
5049
# Expose some tests from the docs as flake-checks too

tests/package-options.nix

-94
This file was deleted.

0 commit comments

Comments
 (0)