about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-05 21:31:40 +0200
committerAlan Pearce2024-05-05 21:31:40 +0200
commit71de5479bc625aa101f72a24b056b687b0630a9a (patch)
tree8501b1f9c3b9cf27b79e651277c3612f30ed6ec3
parent1fdd1f839782339f39a16ba3100dec129cd40a0c (diff)
downloadsearchix-71de5479bc625aa101f72a24b056b687b0630a9a.tar.lz
searchix-71de5479bc625aa101f72a24b056b687b0630a9a.tar.zst
searchix-71de5479bc625aa101f72a24b056b687b0630a9a.zip
feat: link to nixpkgs on github for declarations
-rw-r--r--frontend/templates/blocks/options.gotmpl2
-rw-r--r--importers/nixos-options.nix32
-rw-r--r--internal/options/option.go7
3 files changed, 37 insertions, 4 deletions
diff --git a/frontend/templates/blocks/options.gotmpl b/frontend/templates/blocks/options.gotmpl
index 7c98450..89c6f33 100644
--- a/frontend/templates/blocks/options.gotmpl
+++ b/frontend/templates/blocks/options.gotmpl
@@ -34,7 +34,7 @@
           <dt>Declared</dt>
           {{- range . }}
             <dd>
-              <a href="file://{{ . }}">{{ . }}</a>
+              <a href="{{ .URL }}">{{ .Name }}</a>
             </dd>
           {{- end }}
         {{- end }}
diff --git a/importers/nixos-options.nix b/importers/nixos-options.nix
index 894f05e..3c0a18e 100644
--- a/importers/nixos-options.nix
+++ b/importers/nixos-options.nix
@@ -1,17 +1,45 @@
-{ pkgs ? import <nixpkgs> { }
+{ nixpkgs ? <nixpkgs>
+, pkgs ? import nixpkgs { }
 , system ? builtins.currentSystem
 , stateVersion ? pkgs.lib.version
 , ...
 }:
 let
   inherit (pkgs) lib;
-  nixos = pkgs.nixos ({ lib, pkgs, config, ... }: {
+  inherit (lib) hasPrefix removePrefix;
+
+  nixos = pkgs.nixos ({ lib, ... }: {
     nixpkgs.hostPlatform = system;
     system.stateVersion = lib.versions.majorMinor stateVersion;
   });
 
+  inherit (nixos.config.system.nixos) revision;
+
+  gitHubDeclaration = user: repo: ref: subpath:
+    # Default to `master` if we don't know what revision the system
+    # configuration is using (custom nixpkgs, etc.).
+    let urlRef = if ref != null then ref else "master";
+    in {
+      url = "https://github.com/${user}/${repo}/blob/${urlRef}/${subpath}";
+      name = "<${repo}/${subpath}>";
+    };
+
   doc = pkgs.nixosOptionsDoc {
     inherit (nixos) options;
+    transformOptions = opt: opt // {
+      declarations =
+        map
+          (decl:
+            if hasPrefix (toString nixpkgs) (toString decl)
+            then
+              gitHubDeclaration "NixOS" "nixpkgs" revision
+                (removePrefix "/"
+                  (removePrefix (toString nixpkgs) (toString decl)))
+            else if decl == "lib/modules.nix" then
+              gitHubDeclaration "NixOS" "nixpkgs" revision decl
+            else decl)
+          opt.declarations;
+    };
   };
 in
 doc.optionsJSON
diff --git a/internal/options/option.go b/internal/options/option.go
index d714ad2..b8b838a 100644
--- a/internal/options/option.go
+++ b/internal/options/option.go
@@ -47,9 +47,14 @@ func (html *HTML) UnmarshalJSON(raw []byte) error {
 	return nil
 }
 
+type Link struct {
+	Name string
+	URL  string `json:"url"`
+}
+
 type NixOption struct {
 	Option          string
-	Declarations    []string
+	Declarations    []Link
 	Default         NixValue
 	Description     HTML
 	Example         NixValue