Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mods: add a mods module #6339

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ipsavitsky
Copy link

@ipsavitsky ipsavitsky commented Jan 19, 2025

Description

This change adds a module that configures charmbracelet/mods

Checklist

  • Change is backwards compatible.

  • Code formatted with ./format.

  • Code tested through nix-shell --pure tests -A run.all or nix develop --ignore-environment .#all using Flakes.

  • Test cases updated/added. See example.

  • Commit messages are formatted like

    {component}: {description}
    
    {long description}
    

    See CONTRIBUTING for more information and recent commit messages for examples.

  • If this PR adds a new module

    • Added myself as module maintainer. See example.

Copy link
Contributor

@trueNAHO trueNAHO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider integrating a LocalAI service to run models locally.

The following patchset from my Home Manager setup should work and can be used as reference:

commit 27aa16d58d9bf3c63c2093975c73955ce5ed8b32
Author: NAHO <[email protected]>
Date:   2025-01-17 17:21:02 +0100

    local-ai: add complementary service module to integrate with pkgs.mods

    Add a complementary service module to integrate with the future
    pkgs.mods module.

diff --git a/home_configurations/private/full/default.nix b/home_configurations/private/full/default.nix
index 3ad98081..ae496c08 100644
--- a/home_configurations/private/full/default.nix
+++ b/home_configurations/private/full/default.nix
@@ -226,6 +226,7 @@ lib.dotfiles.homeManagerConfiguration.homeManagerConfiguration
           easyeffects.enable = true;
           gammastep.enable = true;
           gpg-agent.enable = true;
+          local-ai.enable = true;
           psd.enable = true;
           swayidle.enable = true;
         };
diff --git a/home_configurations/private/full/imports.nix b/home_configurations/private/full/imports.nix
index 5230621d..2fbf0fd5 100644
--- a/home_configurations/private/full/imports.nix
+++ b/home_configurations/private/full/imports.nix
@@ -185,6 +185,7 @@
     ../../../modules/inputs/home-manager/services/easyeffects
     ../../../modules/inputs/home-manager/services/gammastep
     ../../../modules/inputs/home-manager/services/gpg-agent
+    ../../../modules/inputs/home-manager/services/local-ai
     ../../../modules/inputs/home-manager/services/psd
     ../../../modules/inputs/home-manager/services/swayidle
     ../../../modules/inputs/home-manager/systemd/user/tmpfiles/rules
diff --git a/modules/inputs/home-manager/home/packages/local-ai/default.nix b/modules/inputs/home-manager/home/packages/local-ai/default.nix
index 1762f75e..64feb970 100644
--- a/modules/inputs/home-manager/home/packages/local-ai/default.nix
+++ b/modules/inputs/home-manager/home/packages/local-ai/default.nix
@@ -1,14 +1,19 @@
 {
   config,
   lib,
-  pkgs,
   ...
 }: {
+  imports = [../../../services/local-ai];
+
   options.dotfiles.inputs.home-manager.home.packages.local-ai.enable =
     lib.dotfiles.mkEnableOption;

   config =
     lib.mkIf
     config.dotfiles.inputs.home-manager.home.packages.local-ai.enable
-    {home.packages = [pkgs.local-ai];};
+    {
+      home.packages = [
+        config.dotfiles.inputs.home-manager.services.local-ai.package
+      ];
+    };
 }
diff --git a/modules/inputs/home-manager/services/local-ai/default.nix b/modules/inputs/home-manager/services/local-ai/default.nix
new file mode 100644
index 00000000..d8096b77
--- /dev/null
+++ b/modules/inputs/home-manager/services/local-ai/default.nix
@@ -0,0 +1,94 @@
+# TODO: Upstream this module to Home Manager.
+{
+  pkgs,
+  config,
+  lib,
+  ...
+}: let
+  cfg = config.dotfiles.inputs.home-manager.services.local-ai;
+  settingsPath = "local/config.yml";
+in {
+  options.dotfiles.inputs.home-manager.services.local-ai = {
+    enable = lib.dotfiles.mkEnableOption;
+
+    extraArgs = lib.mkOption {
+      type = lib.types.listOf lib.types.str;
+      default = [];
+    };
+
+    modelsPath = lib.mkOption {
+      default = "${config.xdg.dataHome}/local-ai/models";
+      example = "${config.home.homeDirectory}/models";
+      type = lib.types.str;
+    };
+
+    openPackage =
+      lib.dotfiles.mkEnableOption
+      // {
+        default = true;
+        example = false;
+      };
+
+    package = lib.mkPackageOption pkgs "local-ai" {};
+
+    port = lib.mkOption {
+      default = 8080;
+      type = lib.types.port;
+    };
+
+    settings = lib.mkOption {
+      inherit (pkgs.formats.yaml {}) type;
+
+      default.models-path = cfg.modelsPath;
+
+      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"
+                "--models-path"
+                "${cfg.modelsPath}"
+              ]
+              ++ cfg.extraArgs
+            );
+
+            RuntimeDirectory = "local-ai";
+            WorkingDirectory = "%t/local-ai";
+          };
+        };
+
+        xdg.configFile.${settingsPath}.text = builtins.toJSON cfg.settings;
+      }
+
+      {
+        dotfiles.inputs.home-manager.services.local-ai.port = 1111;
+      }
+    ]
+  );
+}

commit 19965c8b06a9b6cf0c518ece92024b3bff9d3f03
Author: NAHO <[email protected]>
Date:   2025-01-17 19:23:46 +0100

    mods: init

diff --git a/home_configurations/private/full/default.nix b/home_configurations/private/full/default.nix
index ae496c08..ef4a25e3 100644
--- a/home_configurations/private/full/default.nix
+++ b/home_configurations/private/full/default.nix
@@ -99,6 +99,7 @@ lib.dotfiles.homeManagerConfiguration.homeManagerConfiguration
             man-pages-posix.enable = true;
             man-pages.enable = true;
             mediainfo.enable = true;
+            mods.enable = true;
             moreutils.enable = true;
             mtr.enable = true;
             netcat.enable = true;
diff --git a/home_configurations/private/full/imports.nix b/home_configurations/private/full/imports.nix
index 2fbf0fd5..4b7d46dc 100644
--- a/home_configurations/private/full/imports.nix
+++ b/home_configurations/private/full/imports.nix
@@ -81,6 +81,7 @@
     ../../../modules/inputs/home-manager/home/packages/man-pages
     ../../../modules/inputs/home-manager/home/packages/man-pages-posix
     ../../../modules/inputs/home-manager/home/packages/mediainfo
+    ../../../modules/inputs/home-manager/home/packages/mods
     ../../../modules/inputs/home-manager/home/packages/moreutils
     ../../../modules/inputs/home-manager/home/packages/mtr
     ../../../modules/inputs/home-manager/home/packages/netcat
diff --git a/modules/inputs/home-manager/home/packages/mods/default.nix b/modules/inputs/home-manager/home/packages/mods/default.nix
new file mode 100644
index 00000000..b97e38d5
--- /dev/null
+++ b/modules/inputs/home-manager/home/packages/mods/default.nix
@@ -0,0 +1,58 @@
+# TODO: Upstream this module to Home Manager.
+{
+  config,
+  lib,
+  pkgs,
+  ...
+}: let
+  settingsPath = "mods/mods.yml";
+in {
+  imports = [../../../services/local-ai];
+
+  options.dotfiles.inputs.home-manager.home.packages.mods = {
+    enable = lib.dotfiles.mkEnableOption;
+    package = lib.mkPackageOption pkgs "mods" {};
+
+    settings = lib.mkOption {
+      inherit (pkgs.formats.yaml {}) type;
+
+      default = [];
+
+      description = ''
+        Configuration written to {file}`$XDG_CONFIG_HOME/${settingsPath}`.
+      '';
+    };
+  };
+
+  config = let
+    cfg = config.dotfiles.inputs.home-manager.home.packages.mods;
+  in
+    lib.mkIf cfg.enable (
+      lib.mkMerge [
+        {
+          home.packages = [cfg.package];
+          xdg.configFile.${settingsPath}.text = builtins.toJSON cfg.settings;
+        }
+
+        {
+          dotfiles.inputs.home-manager = {
+            home.packages.mods.settings = {
+              apis.localai = {
+                base-url = "http://localhost:${
+                  toString
+                  config.dotfiles.inputs.home-manager.services.local-ai.port
+                }";
+
+                models.ggml-gpt4all-j.aliases = ["local" "4all"];
+              };
+
+              default-model = "ggml-gpt4all-j";
+              max-input-chars = -1;
+            };
+
+            services.local-ai.enable = true;
+          };
+        }
+      ]
+    );
+}

Additionally, it provides a convenient services.local-ai.openPackage option to xdg-open the running service.

While the LocalAI service works, I am currently unable to use mods due to the upstream issue #316 ("LocalAI not working - OPENAI_API_KEY required error").

@ipsavitsky ipsavitsky changed the title Add a mods module mods: add a mods module Jan 19, 2025
@ipsavitsky ipsavitsky marked this pull request as ready for review January 19, 2025 18:41
@ipsavitsky
Copy link
Author

@trueNAHO good idea, this is my first contribution, maybe it makes sense to add this in the next patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants