Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
}
28 changes: 26 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 = 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: `multiService` lib should be devoid of process-compose specific options
dataDir = lib.mkOption {
type = lib.types.str;
default = "./data/${name}";
description = "The directory where all data for `${service}.<name>` is stored";
};
# FIXME: `multiService` lib should be devoid of process-compose specific options
namespace = lib.mkOption {
description = ''
Namespace for the ${service} service
Expand All @@ -29,6 +31,7 @@
type = lib.types.str;
};
outputs = {
# FIXME: `multiService` lib should be devoid of process-compose specific options
defaultProcessSettings = lib.mkOption {
type = lib.types.deferredModule;
internal = true;
Expand All @@ -40,6 +43,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 +56,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 +86,17 @@
});
};
};
config = {
config = lib.optionalAttrs (name == "homeModules")
{
systemd.user.services = lib.pipe config.services.${service} [
(lib.filterAttrs (_: cfg: cfg.enable))
(lib.concatMapAttrs (_: cfg: cfg.outputs.systemd))
];
launchd.agents = lib.pipe config.services.${service} [
(lib.concatMapAttrs (_: cfg: lib.mapAttrs (_: cf: { ... }: { imports = [ cf ]; }) cfg.outputs.launchd))
];
}
// lib.optionalAttrs (name == "processComposeModules") {
settings = {
imports =
lib.pipe config.services.${service} [
Expand Down
19 changes: 14 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,19 @@ in
./weaviate.nix
./searxng.nix
./tika.nix
]) ++ [
./devshell.nix
];
in
{
processComposeModules = {
imports = (builtins.map (multiService "processComposeModules") services) ++ [ ./devShell.nix ];
};

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

config = {
outputs = {
# systemd and launchd config based on https://github.com/nix-community/home-manager/blob/5031c6d2978109336637977c165f82aa49fa16a7/modules/services/ollama.nix#L78-L108
# TODO: refactor to reuse between all three outputs and also set sane defaults for `systemd` and `launchd`, just like `defaultProcessSettings`.
systemd."${name}" = lib.mkIf pkgs.stdenv.isLinux {
Unit = {
After = [ "network.target" ];
};
Service = {
ExecStart = "${lib.getExe ollamaPackage} serve";
Environment =
(lib.mapAttrsToList (n: v: "${n}=${v}") config.environment)
++ [ "OLLAMA_HOST=${config.host}:${toString config.port}" ];
};
Install = { WantedBy = [ "default.target" ]; };
};
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