diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..29ab271 --- /dev/null +++ b/default.nix @@ -0,0 +1,24 @@ +{ pkgs ? import { }, nixpkgs ? +, home-manager ? , ... }: rec { + # Applications + home-manager-search = pkgs.callPackage ./home-manager-search.nix { + inherit home-manager; + inherit (internals) json2nix; + }; + + nix-option-search = + pkgs.callPackage ./nix-option-search.nix { inherit nixpkgs; }; + + nix-package-search = pkgs.callPackage ./nix-package-search.nix { }; + + internals = { + # Data sources + home-manager-json = home-manager.packages.${system}.docs-json; + nix-options-json = + (import "${nixpkgs}/nixos/release.nix" { inherit nixpkgs; }).options; + nix-packages-json = pkgs.emptyFile; + + # Internal Tools + json2nix = pkgs.callPackage ./internals/json2nix { }; + }; +} diff --git a/flake.nix b/flake.nix index c154a24..c8c968c 100644 --- a/flake.nix +++ b/flake.nix @@ -17,22 +17,50 @@ type = "app"; program = toString pkg; }; - in builtins.mapAttrs toApp self.packages.${system}; + in builtins.mapAttrs toApp { + inherit (self.packages.${system}) + home-manager-search nix-option-search nix-package-search nix2json; + }; + + checks.${system} = let + + in { + # hlint = pkgs.callPackage ./utils/hlint.nix {}; + # format = pkgs.callPackage ./utils/format.nix {}; + }; hydraJobs = with pkgs.lib; mapAttrs' (name: value: nameValuePair name { ${system} = value; }) self.packages.${system}; packages.${system} = { + # Applications home-manager-search = - pkgs.callPackage ./home-manager-search.nix { inherit home-manager; }; - nix-option-search = pkgs.callPackage ./nix-option-search.nix { }; - nix-package-search = pkgs.callPackage ./nix-package-search.nix { }; + pkgs.callPackage ./searchers/home-manager-search.nix { + inherit home-manager; + inherit (self.packages.${system}) json2nix; + }; + nix-option-search = pkgs.callPackage ./searchers/nix-option-search.nix { + inherit nixpkgs; + }; + nix-package-search = + pkgs.callPackage ./searchers/nix-package-search.nix { }; + nix-lib-search = pkgs.callPackage ./searchers/nix-lib-search.nix { }; + nur-package-search = + pkgs.callPackage ./searchers/nur-package-search.nix { }; + + # Data sources + home-manager-json = home-manager.packages.${system}.docs-json; + nix-options-json = + (import "${nixpkgs}/nixos/release.nix" { inherit nixpkgs; }).options; + # nix-packages-json = pkgs.emptyFile; + + # Internal Tools + json2nix = pkgs.callPackage ./internals/json2nix { }; }; - overlays.default = final: prev: prev // self.packages.${system}; + overlays.default = _: prev: prev // self.packages.${system}; - devShells.${system}.default = - pkgs.mkShell { packages = with pkgs; [ nixfmt ]; }; + devShells.${system}.default = pkgs.callPackage ./shell.nix { }; }; } diff --git a/home-manager-search.nix b/home-manager-search.nix deleted file mode 100644 index 35a1479..0000000 --- a/home-manager-search.nix +++ /dev/null @@ -1,159 +0,0 @@ -{ pkgs, lib, home-manager, system, ... }: -let - - # TODO: Preprocess XML tags in description. - - # TODO: Make non-literal examples have better syntax highlighting - - # TODO: Colorize preview - - usage = pkgs.writeText "home-manager-search-usage" '' - Usage: - home-manager-search [ -j | -n ] [-f= [-r=]] - - Options: - -h | --help Display this message. - -j | --json Display raw data in json format. - -n | --no-preview Don't display a preview. - -f=* | --flake=* Use a flake url to an instance (e.g. a fork) of home-manager, - generate its options, and use those instead of the global ones. - -r=* | --ref=* If a flake url is specified, this option might be used to choose - a specific reference (branch, tag, commit, etc.) to use for - generating the docs. - - Example usage: - home-manager-search - home-manager-search -j - home-manager-search --flake="github:nix-community/home-manager" - home-manager-search --flake="github:nix-community/home-manager" --ref="release-22.05" - ''; - - inherit (pkgs.callPackage ./shared.nix { }) - jq fzf gomplate blinkred clear flatten; - - template = pkgs.writeText "preview-home-manager-attrs-template" (flatten '' - {{ with (ds "opt")}} - {{- $title := join .loc "." }} - {{- $title }} - {{ strings.Repeat (strings.RuneCount $title) "=" }} - - {{ if .readOnly }} - ${blinkred}READONLY${clear} - {{ end }} - - Type: {{ .type }} - - {{ if has . "description" }} - DESCRIPTION - {{ strings.Repeat 20 "=" }} - {{ .description }} - {{ end }} - - {{- if has . "example" }} - EXAMPLE - {{ strings.Repeat 20 "=" }} - {{ if and (has .example "_type") }} - {{ else }} - {{ data.ToJSONPretty " " .example }} - {{ end }} - {{ end }} - - {{- if has . "default" }} - DEFAULT - {{ strings.Repeat 20 "=" }} - {{ .default }} - {{ end }} - - CONFIGURED IN: - {{ strings.Repeat 20 "=" }} - {{ range .declarations }} - {{- .path }} - {{ end }} - - {{ else }} - ERROR: Could not find datasource - {{ end }} - ''); - - previewJson = pkgs.writers.writeBash "preview-home-manager-attrs-json" '' - OPTION_KEY=$1 - JSON_MANUAL_PATH=$2 - - ${jq} -C ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH - ''; - - previewGomplate = - pkgs.writers.writeBash "preview-home-manager-attrs-gomplate" '' - OPTION_KEY=$1 - JSON_MANUAL_PATH=$2 - - ${jq} ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH | ${gomplate} --datasource opt=stdin:?type=application/json --file ${template} - ''; - - defaultManualPath = "${ - home-manager.packages.${system}.docs-json - }/share/doc/home-manager/options.json"; - -in pkgs.writers.writeBash "search-home-manager-attrs" '' - JSON_MANUAL_PATH="${defaultManualPath}" - - for i in "$@"; do - case $i in - -h|--help) - cat ${usage} - exit 0 - ;; - -j|--json) - PRINT_JSON=1 - shift - ;; - -n|--no-preview) - NO_PREVIEW=1 - shift - ;; - -f=*|--flake=*) - FLAKE="''${i#*=}" - shift - ;; - -r=*|--ref=*) - REF="''${i#*=}" - shift - ;; - *|-*|--*) - echo "Unknown option $i" - cat ${usage} - exit 1 - ;; - esac - done - - if [ -v PRINT_JSON ] && [ -v NO_PREVIEW ]; then - echo "Cannot preview as json with no-preview enabled" - cat ${usage} - exit 1 - fi - - if [ -v FLAKE ]; then - FLAKE_URL="''${FLAKE}" - - if [ -v REF ]; then - FLAKE_URL="''${FLAKE_URL}?ref=$REF" - fi - - FLAKE_URL="''${FLAKE_URL}#docs-json" - echo "Building docs from $FLAKE_URL" - - OUT_PATH=$(${pkgs.nix}/bin/nix build "$FLAKE_URL" --no-link --print-out-paths --no-write-lock-file) - JSON_MANUAL_PATH="$OUT_PATH/share/doc/home-manager/options.json" - echo "Using docs located at $JSON_MANUAL_PATH" - fi - - if [ -v NO_PREVIEW ]; then - ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} - elif [ -v PRINT_JSON ]; then - ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} --preview "${previewJson} {} $JSON_MANUAL_PATH" - else - ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} --preview "${previewGomplate} {} $JSON_MANUAL_PATH" - fi -'' - diff --git a/internals/json2nix/default.nix b/internals/json2nix/default.nix new file mode 100644 index 0000000..3bd8e9d --- /dev/null +++ b/internals/json2nix/default.nix @@ -0,0 +1,4 @@ +{ pkgs, ... }: +pkgs.writers.writeHaskellBin "json2nix" { + libraries = with pkgs.haskellPackages; [ aeson ]; +} (builtins.readFile ./json2nix.hs) diff --git a/internals/json2nix/json2nix.hs b/internals/json2nix/json2nix.hs new file mode 100644 index 0000000..6beb6ee --- /dev/null +++ b/internals/json2nix/json2nix.hs @@ -0,0 +1,35 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Data.Aeson (Value(..), decode) +import Data.Aeson.Key (Key, toString) +import Data.Aeson.KeyMap (foldMapWithKey) +import Data.Aeson.Parser (json') +import Data.Aeson.Types (parse) +import Data.String (fromString) +import Data.Text (Text(..), unpack, replace, isInfixOf) +import Data.Vector (Vector) +import Data.Maybe (fromMaybe) + +main :: IO () +main = interact f + where + f input = case decode $ fromString input of + Nothing -> "json2nix error - could not parse input\n" ++ show input + Just jsonValue -> json2Nix jsonValue + +keyValToString :: Key -> Value -> String +keyValToString key value = toString key ++ " = " ++ json2Nix value ++ ";" + +-- escapeDollar :: Text -> Text +-- escapeDollar = replace "''${" "\\''${" + +json2Nix :: Value -> String +json2Nix (Object object) = "{" ++ foldMapWithKey keyValToString object ++ "}" +json2Nix (Array array) = "[" ++ foldr (\x y -> x ++ " " ++ y) "" (fmap json2Nix array) ++ "]" +json2Nix (Number n) = show n +json2Nix (Bool False) = "false" +json2Nix (Bool True) = "true" +json2Nix Null = "null" +json2Nix (String text) = sep ++ unpack text ++ sep + where + sep = if "\"" `isInfixOf` text then "\'\'" else "\"" diff --git a/internals/lib.nix b/internals/lib.nix new file mode 100644 index 0000000..9dadb87 --- /dev/null +++ b/internals/lib.nix @@ -0,0 +1,59 @@ +{ pkgs, lib, ... }: { + # Executables + jq = "${pkgs.jq}/bin/jq"; + fzf = "${pkgs.fzf}/bin/fzf"; + gomplate = "${pkgs.gomplate}/bin/gomplate"; + nixfmt = "${pkgs.nixfmt}/bin/nixfmt"; + bat = "${pkgs.bat}/bin/bat"; + perl = "${pkgs.perl}/bin/perl"; + + # ANSI Colors + bold = "\\x1b[1m"; + blinkred = "\\x1b[31;5m"; + red = "\\x1b[31m"; + green = "\\x1b[32m"; + yellow = "\\x1b[33m"; + blue = "\\x1b[34m"; + magenta = "\\x1b[35m"; + clear = "\\x1b[0m"; + + # Misc functionality + + # flatten takes a string that might be formatted like this: + # + # '' + # A string + # with some random indents + # + # and + # + # newlines + # '' + # + # And turns it into this: + # + # '' + # A string + # with some random indents + # and + # newlines + # '' + # + # Useful for keeping track of gomplates logic + # while keeping the template flat + # + # flatten :: String -> String + flatten = with lib; + let + stripPrefixSpace = s: + if hasPrefix " " s then + stripPrefixSpace ((substring 1 (stringLength s)) s) + else + s; + in (flip pipe) [ + (splitString "\n") + (remove "") + (map stripPrefixSpace) + (concatStringsSep "\n") + ]; +} diff --git a/nix-option-search.nix b/nix-option-search.nix deleted file mode 100644 index 9fdaa7f..0000000 --- a/nix-option-search.nix +++ /dev/null @@ -1 +0,0 @@ -{ pkgs, ... }: pkgs.emptyFile diff --git a/nix-package-search.nix b/nix-package-search.nix deleted file mode 100644 index 9fdaa7f..0000000 --- a/nix-package-search.nix +++ /dev/null @@ -1 +0,0 @@ -{ pkgs, ... }: pkgs.emptyFile diff --git a/searchers/home-manager-search.nix b/searchers/home-manager-search.nix new file mode 100644 index 0000000..d97919d --- /dev/null +++ b/searchers/home-manager-search.nix @@ -0,0 +1,175 @@ +{ pkgs, lib, home-manager, system, json2nix, ... }: +let + usage = pkgs.writeText "home-manager-search-usage" '' + Usage: + home-manager-search [ -j | -n ] [-f= [-r=]] + + Options: + -h | --help Display this message. + -j | --json Display raw data in json format. + -np | --no-preview Don't display a preview. + -nc | --no-color Turn off ANSI colors. + -f=* | --flake=* Use a flake url to an instance (e.g. a fork) of home-manager, + generate its options, and use those instead of the global ones. + -r=* | --ref=* If a flake url is specified, this option might be used to choose + a specific reference (branch, tag, commit, etc.) to use for + generating the docs. + + Example usage: + home-manager-search + home-manager-search -j + home-manager-search --flake="github:nix-community/home-manager" + home-manager-search --flake="github:nix-community/home-manager" --ref="release-22.05" + ''; + + inherit (pkgs.callPackage ../internals/lib.nix { }) + jq fzf gomplate nixfmt bat perl blinkred bold red green yellow blue magenta + clear flatten; + + # TODO: Preprocess all XML tags in description. + substitutionsColor = let s = "\\s*"; + in { + "([^>]*)" = "${bold}`$1`${clear}"; + "([^>]*)" = "${bold}`$1`${clear}"; + "([^>]*)" = "${yellow}$1${clear}"; + "([^>]*)" = "${bold}$1${clear}"; + "([^>]*)" = "${red}$1${clear}"; + "([^>]*)" = "${red}$1${clear}"; + "" = "\\n"; + "]*)\"${s}/>" = "${blue}$1${clear}"; + "]*)\"${s}>([^<]*)" = "${bold}$2 ${clear}(${blue}$1${clear})"; + "]*)\"${s}/>" = "${blue}$1${clear}"; + }; + + substitutions = let s = "\\s*"; + in { + "([^>]*)" = "`$1`"; + "([^>]*)" = "`$1`"; + "([^>]*)" = "`$1`"; + "([^>]*)" = "`$1`"; + "([^>]*)" = "`$1`"; + "([^>]*)" = "`$1`"; + "" = "\\n"; + "]*)\"${s}/>" = "`$1`"; + "]*)\"${s}>([^<]*)" = "`$2 ($1)`"; + "]*)\"${s}/>" = "`$1`"; + }; + + perlArgsColor = with lib; + pipe substitutionsColor [ + (mapAttrsToList (name: value: "s|${name}|${value}|gm")) + (concatStringsSep ";") + (x: "-pe '${x}'") + ]; + + perlArgs = with lib; + pipe substitutions [ + (mapAttrsToList (name: value: "s|${name}|${value}|gm")) + (concatStringsSep ";") + (x: "-pe '${x}'") + ]; + + optionTemplateColor = + pkgs.callPackage ../templates/option-preview-template-color.nix { }; + + optionTemplate = + pkgs.callPackage ../templates/option-preview-template.nix { }; + + # TODO: This preview does not respect the color option... + previewJson = pkgs.writers.writeBash "preview-home-manager-attrs-json" '' + OPTION_KEY=$1 + JSON_MANUAL_PATH=$2 + + ${jq} -C ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH + ''; + + previewGomplate = isColorized: let + # TODO: Color management here needs a refactoring badly... + pArgs = if isColorized then perlArgsColor else perlArgs; + colorSuffix = if isColorized then "-color" else ""; + batColorArg = if isColorized then "--color=always " else ""; + template = if isColorized then optionTemplateColor else optionTemplate; + in pkgs.writers.writeBash "preview-home-manager-attrs-gomplate${colorSuffix}" '' + OPTION_KEY=$1 + JSON_MANUAL_PATH=$2 + + JSON_DATA=$(${jq} ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH) + export DESCRIPTION=$(echo $JSON_DATA | ${jq} -r ".description" | ${perl} ${pArgs}) + export EXAMPLE=$(echo $JSON_DATA | ${jq} -r ".example | try(.text) // ." | ${bat} ${batColorArg}--style=numbers) + export DEFAULT=$(echo $JSON_DATA | ${jq} -r ".default" | ${json2nix}/bin/json2nix | ${nixfmt} | ${bat} ${batColorArg}--style=numbers) + echo $JSON_DATA | ${gomplate} --datasource opt=stdin:?type=application/json --file ${template} + ''; + + defaultManualPath = "${ + home-manager.packages.${system}.docs-json + }/share/doc/home-manager/options.json"; + +in pkgs.writers.writeBash "search-home-manager-attrs" '' + JSON_MANUAL_PATH="${defaultManualPath}" + + for i in "$@"; do + case $i in + -h|--help) + cat ${usage} + exit 0 + ;; + -j|--json) + PRINT_JSON=1 + shift + ;; + -np|--no-preview) + NO_PREVIEW=1 + shift + ;; + -nc|--no-color) + NO_COLOR=1 + shift + ;; + -f=*|--flake=*) + FLAKE="''${i#*=}" + shift + ;; + -r=*|--ref=*) + REF="''${i#*=}" + shift + ;; + *|-*|--*) + echo "Unknown option $i" + cat ${usage} + exit 1 + ;; + esac + done + + if [ -v PRINT_JSON ] && [ -v NO_PREVIEW ]; then + echo "Cannot preview as json with no-preview enabled" + cat ${usage} + exit 1 + fi + + if [ -v FLAKE ]; then + FLAKE_URL="''${FLAKE}" + + if [ -v REF ]; then + FLAKE_URL="''${FLAKE_URL}?ref=$REF" + fi + + FLAKE_URL="''${FLAKE_URL}#docs-json" + echo "Building docs from $FLAKE_URL" + + OUT_PATH=$(${pkgs.nix}/bin/nix build "$FLAKE_URL" --no-link --print-out-paths --no-write-lock-file) + JSON_MANUAL_PATH="$OUT_PATH/share/doc/home-manager/options.json" + echo "Using docs located at $JSON_MANUAL_PATH" + fi + + if [ -v NO_PREVIEW ]; then + ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} + elif [ -v PRINT_JSON ]; then + ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} --preview "${previewJson} {} $JSON_MANUAL_PATH" + elif [ -v NO_COLOR ]; then + ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} --preview "${previewGomplate false} {} $JSON_MANUAL_PATH" + else + ${jq} -r 'keys | .[] | .' $JSON_MANUAL_PATH | ${fzf} --preview "${previewGomplate true} {} $JSON_MANUAL_PATH" + fi +'' + diff --git a/searchers/nix-lib-search.nix b/searchers/nix-lib-search.nix new file mode 100644 index 0000000..81b1588 --- /dev/null +++ b/searchers/nix-lib-search.nix @@ -0,0 +1,3 @@ +# TODO: +{ pkgs, ... }: +pkgs.emptyFile diff --git a/searchers/nix-option-search.nix b/searchers/nix-option-search.nix new file mode 100644 index 0000000..01856f4 --- /dev/null +++ b/searchers/nix-option-search.nix @@ -0,0 +1,2 @@ +# TODO: +{ pkgs, ... }: pkgs.emptyFile \ No newline at end of file diff --git a/searchers/nix-package-search.nix b/searchers/nix-package-search.nix new file mode 100644 index 0000000..81b1588 --- /dev/null +++ b/searchers/nix-package-search.nix @@ -0,0 +1,3 @@ +# TODO: +{ pkgs, ... }: +pkgs.emptyFile diff --git a/searchers/nur-package-search.nix b/searchers/nur-package-search.nix new file mode 100644 index 0000000..81b1588 --- /dev/null +++ b/searchers/nur-package-search.nix @@ -0,0 +1,3 @@ +# TODO: +{ pkgs, ... }: +pkgs.emptyFile diff --git a/shared.nix b/shared.nix deleted file mode 100644 index fc4e318..0000000 --- a/shared.nix +++ /dev/null @@ -1,28 +0,0 @@ -{ pkgs, lib, ... }: { - - # Executables - jq = "${pkgs.jq}/bin/jq"; - fzf = "${pkgs.fzf}/bin/fzf"; - gomplate = "${pkgs.gomplate}/bin/gomplate"; - - # ANSI Colors - blinkred = "\\033[31;5m"; - clear = "\\033[0m"; - - # Misc functionality - - # All empty lines, and all prefixed whitespace is removed. - flatten = with lib; - let - stripPrefixSpace = s: - if hasPrefix " " s then - stripPrefixSpace ((substring 1 (stringLength s)) s) - else - s; - in (flip pipe) [ - (splitString "\n") - (remove "") - (map stripPrefixSpace) - (concatStringsSep "\n") - ]; -} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..4117474 --- /dev/null +++ b/shell.nix @@ -0,0 +1,12 @@ +{ pkgs }: +pkgs.mkShell { + packages = with pkgs; [ nixfmt hlint jq bat gomplate ]; + + shellHook = let + format = pkgs.callPackage ./utils/format.nix { }; + hlint = pkgs.callPackage ./utils/hlint.nix { }; + in '' + alias nasf=${format}/bin/nix-attrs-search-format + alias nashl=${hlint}/bin/nix-attrs-search-hlint + ''; +} diff --git a/templates/option-preview-template-color.nix b/templates/option-preview-template-color.nix new file mode 100644 index 0000000..8c99250 --- /dev/null +++ b/templates/option-preview-template-color.nix @@ -0,0 +1,71 @@ +{ pkgs }: +let + inherit (pkgs.callPackage ../internals/lib.nix { }) + flatten red bold blinkred green yellow magenta blue clear; +in pkgs.writeText "nix-attrs-search-internal-option-template" (flatten '' + {{ define "colorize" -}} + {{ strings.ShellQuote . | strings.Trim "'" }} + {{- end -}} + {{ with (ds "opt")}} + {{- $origTitle := join .loc "." }} + {{- $title := join .loc "." | regexp.Replace "()" "${red}$1${clear}${bold}" }} + {{- template "colorize" "${bold}" -}} + {{- template "colorize" $title }} + {{ strings.Repeat (strings.RuneCount $origTitle) "=" }} + {{- template "colorize" "${clear}" }} + + {{ if .readOnly }} + {{ template "colorize" "${blinkred}READONLY${clear}" }} + {{ end }} + + {{ template "colorize" "${green}" -}} + Type: {{ .type }} + {{- template "colorize" "${clear}" }} + + {{ if has . "description" }} + {{ template "colorize" "${yellow}" -}} + DESCRIPTION + {{ strings.Repeat 20 "=" }} + {{- template "colorize" "${clear}" }} + {{ getenv "DESCRIPTION" "Gomplate error: could not fetch description" }} + {{ end }} + + {{- if has . "example" }} + {{ template "colorize" "${magenta}" -}} + EXAMPLE + {{ strings.Repeat 20 "=" }} + {{- template "colorize" "${clear}" }} + {{ getenv "EXAMPLE" "Gomplate error: could not fetch example" }} + {{ end }} + + {{- if has . "default" }} + {{ template "colorize" "${blue}" -}} + DEFAULT + {{ strings.Repeat 20 "=" }} + {{- template "colorize" "${clear}" }} + {{ getenv "DEFAULT" "Gomplate error: could not fetch default value" }} + {{ end }} + + {{- if has . "declarations" }} + {{ template "colorize" "${red}" -}} + CONFIGURED IN + {{ strings.Repeat 20 "=" }} + {{- template "colorize" "${clear}" }} + {{ range .declarations }} + {{- .path }} + {{ end }} + {{ end }} + + {{- if has . "relatedPackages" }} + RELATED PACKAGES: + {{ strings.Repeat 20 "=" }} + {{ range .relatedPackages }} + {{- .attrName }} + {{- .description }} + {{ end }} + {{ end }} + + {{ else }} + ERROR: Could not find datasource + {{ end }} +'') diff --git a/templates/option-preview-template.nix b/templates/option-preview-template.nix new file mode 100644 index 0000000..ecaacf1 --- /dev/null +++ b/templates/option-preview-template.nix @@ -0,0 +1,54 @@ +{ pkgs }: +let inherit (pkgs.callPackage ../internals/lib.nix { }) flatten; +in pkgs.writeText "nix-attrs-search-internal-option-template" (flatten '' + {{ with (ds "opt")}} + {{- $origTitle := join .loc "." }} + {{- $title := join .loc "." }} + {{- $title }} + {{ strings.Repeat (strings.RuneCount $origTitle) "=" }} + + {{ if .readOnly }} + READONLY + {{ end }} + + Type: {{ .type }} + + {{ if has . "description" }} + DESCRIPTION + {{ strings.Repeat 20 "=" }} + {{ getenv "DESCRIPTION" "Gomplate error: could not fetch description" }} + {{ end }} + + {{- if has . "example" }} + EXAMPLE + {{ strings.Repeat 20 "=" }} + {{ getenv "EXAMPLE" "Gomplate error: could not fetch example" }} + {{ end }} + + {{- if has . "default" }} + DEFAULT + {{ strings.Repeat 20 "=" }} + {{ getenv "DEFAULT" "Gomplate error: could not fetch default value" }} + {{ end }} + + {{- if has . "declarations" }} + CONFIGURED IN + {{ strings.Repeat 20 "=" }} + {{ range .declarations }} + {{- .path }} + {{ end }} + {{ end }} + + {{- if has . "relatedPackages" }} + RELATED PACKAGES: + {{ strings.Repeat 20 "=" }} + {{ range .relatedPackages }} + {{- .attrName }} + {{- .description }} + {{ end }} + {{ end }} + + {{ else }} + ERROR: Could not find datasource + {{ end }} +'') diff --git a/templates/package-preview-template-color.nix b/templates/package-preview-template-color.nix new file mode 100644 index 0000000..d29b982 --- /dev/null +++ b/templates/package-preview-template-color.nix @@ -0,0 +1,3 @@ +# TODO: +{ pkgs, ... }: +pkgs.emptyFile \ No newline at end of file diff --git a/templates/package-preview-template.nix b/templates/package-preview-template.nix new file mode 100644 index 0000000..d29b982 --- /dev/null +++ b/templates/package-preview-template.nix @@ -0,0 +1,3 @@ +# TODO: +{ pkgs, ... }: +pkgs.emptyFile \ No newline at end of file diff --git a/utils/format.nix b/utils/format.nix new file mode 100644 index 0000000..8608120 --- /dev/null +++ b/utils/format.nix @@ -0,0 +1,4 @@ +{ pkgs, ... }: +pkgs.writeScriptBin "nix-attrs-search-format" '' + find -name '*.nix' -exec '${pkgs.nixfmt}/bin/nixfmt' {} \; +'' diff --git a/utils/hlint.nix b/utils/hlint.nix new file mode 100644 index 0000000..c3e77dc --- /dev/null +++ b/utils/hlint.nix @@ -0,0 +1,4 @@ +{ pkgs, ... }: +pkgs.writeScriptBin "nix-attrs-search-hlint" '' + find -name '*.hs' -exec '${pkgs.hlint}/bin/hlint' {} \; +'' diff --git a/utils/test-json2nix.nix b/utils/test-json2nix.nix new file mode 100644 index 0000000..7728dc9 --- /dev/null +++ b/utils/test-json2nix.nix @@ -0,0 +1,17 @@ +{ pkgs, home-manager-options, json2nix }: +pkgs.writers.writeBash "nix-attrs-search-test-json2nix" '' + ELEMENT_COUNT=$(${jq} 'keys | length' ${home-manager-options}/share/doc/home-manager/options.json) + echo "Trying $(($ELEMENT_COUNT - 1)) elements" + for i in $(seq 0 $(($ELEMENT_COUNT - 1))); do + if ! (($i % 100)); then + echo "$i..." + fi + DEFAULT_VAL=$(${jq} "[.[]][$i].default" ${home-manager-options}/share/doc/home-manager/options.json) + ERROR_MESSAGE=$( { echo $DEFAULT_VAL | ${json2nix} | ${nixfmt} } 2>&1 ) + if [ $? != 0 ]; then + echo "[$i] ERROR:" + echo $DEFAULT_VAL + echo $ERROR_MESSAGE + fi + done +''