1{
2 config,
3 pkgs,
4 lib,
5 ...
6}:
7let
8 inherit (lib)
9 getExe
10 isBool
11 listToAttrs
12 literalExpression
13 maintainers
14 mkEnableOption
15 mkIf
16 mkOption
17 mkPackageOption
18 optionalString
19 replaceChars
20 substring
21 toLower
22 types
23 ;
24 inherit (types)
25 bool
26 either
27 listOf
28 str
29 submodule
30 ;
31
32 cfg = config.programs.pay-respects;
33
34 settingsFormat = pkgs.formats.toml { };
35 inherit (settingsFormat) generate type;
36
37 finalPackage =
38 if cfg.aiIntegration != true then
39 (pkgs.runCommand "pay-respects-wrapper"
40 {
41 nativeBuildInputs = [ pkgs.makeBinaryWrapper ];
42 inherit (cfg.package) meta;
43 }
44 ''
45 mkdir -p $out/bin
46 makeWrapper ${getExe cfg.package} $out/bin/${cfg.package.meta.mainProgram} \
47 ${optionalString (cfg.aiIntegration == false) "--set _PR_AI_DISABLE true"}
48 ${optionalString (cfg.aiIntegration != false) ''
49 --set _PR_AI_URL ${cfg.aiIntegration.url} \
50 --set _PR_AI_MODEL ${cfg.aiIntegration.model} \
51 --set _PR_AI_LOCALE ${cfg.aiIntegration.locale}
52 ''}
53 ''
54 )
55 else
56 cfg.package;
57
58 initScript =
59 shell:
60 if (shell != "fish") then
61 ''
62 eval "$(${getExe finalPackage} ${shell} --alias ${cfg.alias})"
63 ''
64 else
65 ''
66 ${getExe finalPackage} ${shell} --alias ${cfg.alias} | source
67 '';
68in
69{
70 options = {
71 programs.pay-respects = {
72 enable = mkEnableOption "pay-respects, an app which corrects your previous console command";
73
74 package = mkPackageOption pkgs "pay-respects" { };
75
76 alias = mkOption {
77 default = "f";
78 type = str;
79 description = ''
80 `pay-respects` needs an alias to be configured.
81 The default value is `f`, but you can use anything else as well.
82 '';
83 };
84 runtimeRules = mkOption {
85 type = listOf type;
86 default = [ ];
87 example = literalExpression ''
88 [
89 {
90 command = "xl";
91 match_err = [
92 {
93 pattern = [
94 "Permission denied"
95 ];
96 suggest = [
97 '''
98 #[executable(sudo), !cmd_contains(sudo), err_contains(libxl: error:)]
99 sudo {{command}}
100 '''
101 ];
102 }
103 ];
104 }
105 ];
106 '';
107 description = ''
108 List of rules to be added to `/etc/xdg/pay-respects/rules`.
109 `pay-respects` will read the contents of these generated rules to recommend command corrections.
110 Each rule module should start with the `command` attribute that specifies the command name. See the [upstream documentation](https://codeberg.org/iff/pay-respects/src/branch/main/rules.md) for more information.
111 '';
112 };
113 aiIntegration = mkOption {
114 default = false;
115 example = {
116 url = "http://127.0.0.1:11434/v1/chat/completions";
117 model = "llama3";
118 locale = "nl-be";
119 };
120 description = ''
121 Whether to enable `pay-respects`' LLM integration. When there is no rule for a given error, `pay-respects` can query an OpenAI-compatible API endpoint for command corrections.
122
123 - If this is set to `false`, all LLM-related features are disabled.
124 - If this is set to `true`, the default OpenAI endpoint will be used, using upstream's API key. This default API key may be rate-limited.
125 - You can also set a custom API endpoint, large language model and locale for command corrections. Simply access the `aiIntegration.url`, `aiIntegration.model` and `aiIntegration.locale` options, as described in the example.
126 - Take a look at the [services.ollama](#opt-services.ollama.enable) NixOS module if you wish to host a local large language model for `pay-respects`.
127
128 For all of these methods, you can set a custom secret API key by using the `_PR_AI_API_KEY` environment variable.
129 '';
130 type = either bool (submodule {
131 options = {
132 url = mkOption {
133 default = "";
134 example = "https://api.openai.com/v1/chat/completions";
135 type = str;
136 description = "The OpenAI-compatible API endpoint that `pay-respects` will query for command corrections.";
137 };
138 model = mkOption {
139 default = "";
140 example = "llama3";
141 type = str;
142 description = "The model used by `pay-respects` to generate command corrections.";
143 };
144 locale = mkOption {
145 default = toLower (replaceChars [ "_" ] [ "-" ] (substring 0 5 config.i18n.defaultLocale));
146 example = "nl-be";
147 type = str;
148 description = ''
149 The locale to be used for LLM responses.
150 The accepted format is a lowercase [`ISO 639-1` language code](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes), followed by a dash '-', followed by a lowercase [`ISO 3166-1 alpha-2` country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
151 '';
152 };
153 };
154 });
155 };
156 };
157 };
158
159 config = mkIf cfg.enable {
160 assertions =
161 map
162 (attr: {
163 assertion = (!isBool cfg.aiIntegration) -> (cfg.aiIntegration.${attr} != "");
164 message = ''
165 programs.pay-respects.aiIntegration is configured as a submodule, but you have not configured a value for programs.pay-respects.aiIntegration.${attr}!
166 '';
167 })
168 [
169 "url"
170 "model"
171 ];
172
173 environment = {
174 etc = listToAttrs (
175 map (rule: {
176 name = "xdg/pay-respects/rules/${rule.command}.toml";
177 value = {
178 source = generate "${rule.command}.toml" rule;
179 };
180 }) cfg.runtimeRules
181 );
182
183 systemPackages = [ finalPackage ];
184 };
185
186 programs = {
187 bash.interactiveShellInit = initScript "bash";
188 fish.interactiveShellInit = optionalString config.programs.fish.enable (initScript "fish");
189 zsh.interactiveShellInit = optionalString config.programs.zsh.enable (initScript "zsh");
190 };
191 };
192 meta.maintainers = with maintainers; [ sigmasquadron ];
193}