1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
use crate::{ contains_insensitive_ascii, starts_with_insensitive_ascii, Cache, DocEntry, DocSource, Errors, Lowercase, }; use colored::*; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, fs::File, io::BufReader, path::{Path, PathBuf}, process::Command, }; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct OptionDocumentation { #[serde(default)] description: String, #[serde(default, rename(serialize = "readOnly", deserialize = "readOnly"))] read_only: bool, #[serde(rename(serialize = "loc", deserialize = "loc"))] location: Vec<String>, #[serde(rename(serialize = "type", deserialize = "type"))] option_type: String, } impl OptionDocumentation { pub fn name(&self) -> String { self.location.join(".") } pub fn pretty_printed(&self) -> String { format!( "# {}\n{}\ntype: {}\n\n", self.name().blue().bold(), self.description, self.option_type ) } } #[derive(Debug, Serialize, Deserialize)] pub enum OptionsDatabaseType { NixOS, HomeManager, } #[derive(Debug, Serialize, Deserialize)] pub struct OptionsDatabase { pub typ: OptionsDatabaseType, pub options: HashMap<String, OptionDocumentation>, } impl OptionsDatabase { pub fn new(typ: OptionsDatabaseType) -> Self { Self { typ, options: HashMap::new(), } } } pub fn try_from_file(path: &PathBuf) -> Result<HashMap<String, OptionDocumentation>, Errors> { let options: HashMap<String, OptionDocumentation> = serde_json::from_slice(&std::fs::read(path)?)?; Ok(options) } impl DocSource for OptionsDatabase { fn all_keys(&self) -> Vec<&str> { self.options.keys().map(|x| x.as_ref()).collect() } fn search(&self, query: &Lowercase) -> Vec<DocEntry> { self.options .iter() .filter(|(key, _)| starts_with_insensitive_ascii(key.as_bytes(), query)) .map(|(_, d)| DocEntry::OptionDoc(d.clone())) .collect() } fn search_liberal(&self, query: &Lowercase) -> Vec<DocEntry> { self.options .iter() .filter(|(key, _)| contains_insensitive_ascii(key.as_bytes(), query)) .map(|(_, d)| DocEntry::OptionDoc(d.clone())) .collect() } fn update(&mut self) -> Result<bool, Errors> { let opts = match self.typ { OptionsDatabaseType::NixOS => try_from_file(&get_nixos_json_doc_path()?)?, OptionsDatabaseType::HomeManager => try_from_file(&get_hm_json_doc_path()?)?, }; let old = std::mem::replace(&mut self.options, opts); Ok(old.keys().eq(self.options.keys())) } } impl Cache for OptionsDatabase {} pub fn get_hm_json_doc_path() -> Result<PathBuf, std::io::Error> { let base_path_output = Command::new("nix-build") .arg("-E") .arg( r#"{ pkgs ? import <nixpkgs> {} }: let hmargs = { pkgs = pkgs; lib = import (<home-manager/modules/lib/stdlib-extended.nix>) pkgs.lib; }; docs = import (<home-manager/doc>) hmargs; in (if builtins.isFunction docs then docs hmargs else docs).options.json "#) .output() .map(|o| String::from_utf8(o.stdout).unwrap())?; Ok(PathBuf::from(base_path_output.trim_end_matches("\n")) .join("share/doc/home-manager/options.json")) } pub fn get_nixos_json_doc_path() -> Result<PathBuf, std::io::Error> { let base_path_output = Command::new("nix-build") .env("NIXPKGS_ALLOW_UNFREE", "1") .env("NIXPKGS_ALLOW_BROKEN", "1") .arg("--no-out-link") .arg("-E") .arg(r#"with import <nixpkgs> {}; let eval = import (pkgs.path + "/nixos/lib/eval-config.nix") { modules = []; }; opts = (nixosOptionsDoc { options = eval.options; }).optionsJSON; in runCommandLocal "options.json" { inherit opts; } "cp $opts/share/doc/nixos/options.json $out""#) .output() .map(|o| String::from_utf8(o.stdout).unwrap())?; Ok(PathBuf::from(base_path_output.trim_end_matches("\n"))) }