nixos/users/modules/aichat.nix

76 lines
2.3 KiB
Nix

{
pkgs,
lib,
config,
...
}: let
cfg = config.programs.aichat;
yamlFormat = pkgs.formats.yaml {};
aichatWrapper = pkgs.writeShellApplication {
name = "aichat";
runtimeInputs = [cfg.package];
text = ''
OPENAI_API_KEY="$(cat ${cfg.openaiApiKeyFile})"; export OPENAI_API_KEY
aichat "$@"
'';
};
rolesTransformed = lib.mapAttrsToList (name: config: {inherit name;} // config) cfg.roles;
in {
options.programs.aichat = {
enable = lib.mkEnableOption "aichat, the terminal ai interface";
package = lib.mkPackageOption pkgs "aichat" {};
openaiApiKeyFile = lib.mkOption {
type = lib.types.str;
description = "API key file for OpenAI";
};
settings = lib.mkOption {
type = lib.types.submodule {freeformType = yamlFormat.type;};
default = {};
description = ''
aichat configuration options.
See <https://github.com/sigoden/aichat?tab=readme-ov-file#configuration> for details.
'';
};
roles = lib.mkOption {
type = with lib.types;
attrsOf (submodule {
options = {
prompt = lib.mkOption {
type = lines;
description = "Instructions and context provided to the LLM.";
};
temperature = lib.mkOption {
type = nullOr number;
description = "(Optional) Controls the creativity and randomness of the LLM's response.";
default = null;
};
top_p = lib.mkOption {
type = nullOr number;
description = "(Optional) Alternative way to control LLM's output diversity, affecting the probability distribution of tokens.";
default = null;
};
};
});
default = {};
description = ''
aichat role configuration.
See <https://github.com/sigoden/aichat?tab=readme-ov-file#defining-roles> for details.
'';
};
};
config = lib.mkIf cfg.enable {
home.packages = [aichatWrapper];
programs.fish.shellAbbrs = {
g = "aichat";
c = "aichat -e";
};
xdg.configFile."aichat/config.yaml".source = yamlFormat.generate "config.yaml" cfg.settings;
xdg.configFile."aichat/roles.yaml".source = yamlFormat.generate "roles.yaml" rolesTransformed;
};
}