From 4760d75c85a464e6a4442dccfdef24cb8100212f Mon Sep 17 00:00:00 2001 From: Ilya Savitsky Date: Fri, 28 Mar 2025 12:03:44 +0000 Subject: [PATCH] local-ai: init module --- modules/modules.nix | 1 + modules/services/local-ai.nix | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 modules/services/local-ai.nix diff --git a/modules/modules.nix b/modules/modules.nix index b48ef31af..fc9576216 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -354,6 +354,7 @@ let ./services/lieer.nix ./services/linux-wallpaperengine.nix ./services/listenbrainz-mpd.nix + ./services/local-ai.nix ./services/lorri.nix ./services/lxqt-policykit-agent.nix ./services/macos-remap-keys diff --git a/modules/services/local-ai.nix b/modules/services/local-ai.nix new file mode 100644 index 000000000..588e624bc --- /dev/null +++ b/modules/services/local-ai.nix @@ -0,0 +1,59 @@ +{ pkgs, config, lib, ... }: +with lib; + +let + cfg = config.services.local-ai; + settingsPath = "local/config.yml"; +in { + options.services.local-ai = { + enable = + mkEnableOption "LocalAI is the free, Open Source OpenAI alternative."; + + extraArgs = lib.mkOption { + type = types.listOf types.str; + default = [ ]; + description = "Extra arguments to pass to local-ai"; + }; + + package = mkPackageOption pkgs "local-ai" { }; + + port = mkOption { + type = lib.types.port; + default = 8080; + description = "Port to set up local-ai with"; + }; + + settings = lib.mkOption { + inherit (pkgs.formats.yaml { }) type; + + description = '' + Configuration written to {file}`$XDG_CONFIG_HOME/${settingsPath}`. + ''; + }; + }; + + config = lib.mkIf cfg.enable (lib.mkMerge [{ + home.packages = lib.mkIf cfg.openPackage (lib.singleton + (pkgs.writeShellApplication { + name = "${cfg.package.pname}-open"; + runtimeInputs = [ pkgs.xdg-utils ]; + text = "xdg-open localhost:${toString cfg.port}"; + })); + + systemd.user.services.local-ai = { + Service = { + ExecStart = lib.escapeShellArgs ([ + (lib.getExe cfg.package) + "--address" + ":${toString cfg.port}" + "--debug" + ] ++ cfg.extraArgs); + + RuntimeDirectory = "local-ai"; + WorkingDirectory = "%t/local-ai"; + }; + }; + + xdg.configFile.${settingsPath}.text = builtins.toJSON cfg.settings; + }]); +}