Skip to content

Commit

Permalink
feat(multi-backend-services): init, demo with Ollama
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh committed Feb 14, 2025
1 parent 03de4ef commit d421ffe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
20 changes: 11 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
description = "declarative, composable, and reproducible services for Nix development environment";
outputs = _: {
processComposeModules.default = ./nix/services;
outputs = _:
let modules = import ./nix/services; in {
processComposeModules.default = modules.processComposeModules;
homeModules.default = modules.homeModules;

templates.default = {
description = "Example flake using process-compose-flake";
path = builtins.path { path = ./example/simple; filter = path: _: baseNameOf path == "flake.nix"; };
};
templates.default = {
description = "Example flake using process-compose-flake";
path = builtins.path { path = ./example/simple; filter = path: _: baseNameOf path == "flake.nix"; };
};

lib = import ./nix/lib.nix;
lib = import ./nix/lib.nix;

om = import ./nix/omnix.nix;
};
om = import ./nix/omnix.nix;
};
}
31 changes: 29 additions & 2 deletions nix/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# where module filename is of form `${name}.nix`. The submodule takes this
# 'name' parameter, and is expected to set the final process-compose config in
# its `outputs.settings` option.
multiService = mod:
multiService = process-manager-name: mod:
{ config, pkgs, lib, ... }:
let
# Derive name from filename
Expand All @@ -16,11 +16,13 @@
serviceModule = { config, name, ... }: {
options = {
enable = lib.mkEnableOption "Enable the ${service}.<name> service";
# FIXME: process-compose option
dataDir = lib.mkOption {
type = lib.types.str;
default = "./data/${name}";
description = "The directory where all data for `${service}.<name>` is stored";
};
# FIXME: process-compose option
namespace = lib.mkOption {
description = ''
Namespace for the ${service} service
Expand All @@ -29,6 +31,8 @@
type = lib.types.str;
};
outputs = {
# FIXME: Need better distinction for process-manager specific options.
# I also don't think this option should be under `outputs`.
defaultProcessSettings = lib.mkOption {
type = lib.types.deferredModule;
internal = true;
Expand All @@ -40,6 +44,7 @@
namespace = lib.mkDefault config.namespace;
};
};
# FIXME: rename to `process-compose`
settings = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
internal = true;
Expand All @@ -52,6 +57,16 @@
);
};
};
systemd = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.deferredModule;
internal = true;
default = { };
};
launchd = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.deferredModule;
internal = true;
default = { };
};
};
};
};
Expand All @@ -72,7 +87,19 @@
});
};
};
config = {
config = lib.optionalAttrs (process-manager-name == "launchd")
{
launchd.agents = lib.pipe config.services.${service} [
(lib.concatMapAttrs (_: cfg: lib.mapAttrs (_: cf: { ... }: { imports = [ cf ]; }) cfg.outputs.launchd))
];
}
// lib.optionalAttrs (process-manager-name == "systemd") {
systemd.user.services = lib.pipe config.services.${service} [
(lib.filterAttrs (_: cfg: cfg.enable))
(lib.concatMapAttrs (_: cfg: cfg.outputs.systemd))
];
}
// lib.optionalAttrs (process-manager-name == "process-compose") {
settings = {
imports =
lib.pipe config.services.${service} [
Expand Down
14 changes: 9 additions & 5 deletions nix/services/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let
inherit (import ../lib.nix) multiService;
in
{
imports = (builtins.map multiService [
services = [
./apache-kafka.nix
./clickhouse
./elasticsearch.nix
Expand All @@ -26,8 +24,14 @@ in
./weaviate.nix
./searxng.nix
./tika.nix
]) ++ [
./devshell.nix
];
in
{
processComposeModules = {
imports = (builtins.map (multiService "process-compose") services) ++ [ ./devShell.nix ];
};

homeModules = {
imports = (builtins.map (multiService "systemd") services) ++ (builtins.map (multiService "launchd") services);
};
}
18 changes: 18 additions & 0 deletions nix/services/ollama.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ in

config = {
outputs = {
launchd = {
"${name}" = lib.mkIf pkgs.stdenv.isDarwin {
config = {
enable = config.enable;
config = {
ProgramArguments = [ (lib.getExe config.package) "serve" ];
EnvironmentVariables = config.environment // {
OLLAMA_HOST = "${config.host}:${toString config.port}";
};
KeepAlive = {
Crashed = true;
SuccessfulExit = false;
};
ProcessType = "Background";
};
};
};
};
settings = {
processes = {
"${name}" =
Expand Down

0 comments on commit d421ffe

Please sign in to comment.