From 61f2cc59089d48c85d761c0c94388e2dc421b712 Mon Sep 17 00:00:00 2001 From: Ilya Savitsky Date: Mon, 3 Nov 2025 04:04:44 +0000 Subject: [PATCH] local-ai: init module (#6718) --- .../misc/news/2025/10/2025-10-31_12-00-00.nix | 9 ++++ modules/services/local-ai.nix | 48 +++++++++++++++++++ tests/modules/services/local-ai/default.nix | 6 +++ .../local-ai/enabled-with-environment.nix | 20 ++++++++ tests/modules/services/local-ai/enabled.nix | 9 ++++ 5 files changed, 92 insertions(+) create mode 100644 modules/misc/news/2025/10/2025-10-31_12-00-00.nix create mode 100644 modules/services/local-ai.nix create mode 100644 tests/modules/services/local-ai/default.nix create mode 100644 tests/modules/services/local-ai/enabled-with-environment.nix create mode 100644 tests/modules/services/local-ai/enabled.nix diff --git a/modules/misc/news/2025/10/2025-10-31_12-00-00.nix b/modules/misc/news/2025/10/2025-10-31_12-00-00.nix new file mode 100644 index 000000000..179586a35 --- /dev/null +++ b/modules/misc/news/2025/10/2025-10-31_12-00-00.nix @@ -0,0 +1,9 @@ +{ + time = "2025-10-31T12:00:00+00:00"; + condition = true; + message = '' + services.local-ai: new module + + Added LocalAI, a free, Open Source OpenAI alternative. + ''; +} diff --git a/modules/services/local-ai.nix b/modules/services/local-ai.nix new file mode 100644 index 000000000..94cea9c65 --- /dev/null +++ b/modules/services/local-ai.nix @@ -0,0 +1,48 @@ +{ + pkgs, + config, + lib, + ... +}: + +let + inherit (lib) types; + cfg = config.services.local-ai; +in +{ + meta.maintainers = [ lib.maintainers.ipsavitsky ]; + + options.services.local-ai = { + enable = lib.mkEnableOption "LocalAI is the free, Open Source OpenAI alternative."; + + package = lib.mkPackageOption pkgs "local-ai" { }; + + environment = lib.mkOption { + type = types.attrsOf types.string; + default = { }; + description = '' + Additional environment passed to local-ai service. Used to configure local-ai + + See for available options. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.user.services.local-ai = { + Unit = { + Description = "Server for local large language models"; + After = [ "network.target" ]; + }; + + Service = { + ExecStart = lib.getExe cfg.package; + Environment = lib.mapAttrsToList (key: val: "${key}=${val}") cfg.environment; + }; + + Install = { + WantedBy = [ "default.target" ]; + }; + }; + }; +} diff --git a/tests/modules/services/local-ai/default.nix b/tests/modules/services/local-ai/default.nix new file mode 100644 index 000000000..bc67d7482 --- /dev/null +++ b/tests/modules/services/local-ai/default.nix @@ -0,0 +1,6 @@ +{ lib, pkgs, ... }: + +lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { + local-ai-enabled = ./enabled.nix; + local-ai-enabled-with-environment = ./enabled-with-environment.nix; +} diff --git a/tests/modules/services/local-ai/enabled-with-environment.nix b/tests/modules/services/local-ai/enabled-with-environment.nix new file mode 100644 index 000000000..aaf7c8f7c --- /dev/null +++ b/tests/modules/services/local-ai/enabled-with-environment.nix @@ -0,0 +1,20 @@ +{ ... }: + +{ + services.local-ai = { + enable = true; + environment = { + MODELS_PATH = "/tmp/models"; + PRELOAD_MODELS = "[{ \"url\": \"https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf\", \"name\": \"mistral-7b-instruct-v0.2.Q4_K_M.gguf\" }]"; + }; + }; + + nmt.script = '' + assertFileContains \ + home-files/.config/systemd/user/local-ai.service \ + "Environment=MODELS_PATH=/tmp/models" + assertFileContains \ + home-files/.config/systemd/user/local-ai.service \ + "Environment=PRELOAD_MODELS=[{ \"url\": \"https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf\", \"name\": \"mistral-7b-instruct-v0.2.Q4_K_M.gguf\" }]" + ''; +} diff --git a/tests/modules/services/local-ai/enabled.nix b/tests/modules/services/local-ai/enabled.nix new file mode 100644 index 000000000..9c75319f6 --- /dev/null +++ b/tests/modules/services/local-ai/enabled.nix @@ -0,0 +1,9 @@ +{ ... }: + +{ + services.local-ai.enable = true; + + nmt.script = '' + assertFileExists home-files/.config/systemd/user/local-ai.service + ''; +}