-
Notifications
You must be signed in to change notification settings - Fork 3
/
flake.nix
262 lines (238 loc) · 11.4 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
{
description = "topos-cli";
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
services-flake.url = "github:juspay/services-flake";
};
outputs = { self, nixpkgs, flake-parts, poetry2nix, process-compose-flake, services-flake }@inputs:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ inputs.process-compose-flake.flakeModule ];
systems = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
perSystem = { self', pkgs, system, lib, ... }:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
inputs.poetry2nix.overlays.default
(final: prev: {
toposPoetryEnv = final.callPackage toposPoetryEnv { };
pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [
(python-final: python-prev: {
pystray = python-final.callPackage ./nix/overlays/pystray/default.nix { };
})
];
})
];
};
# see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples.
#TODO: Figure out how to add setuptools to all the packages which need it, this is currently not working as expected.
overrides = pkgs.poetry2nix.overrides.withDefaults (final: super:
pkgs.lib.mapAttrs
(attr: systems: super.${attr}.overridePythonAttrs
(old: {
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ map (a: final.${a}) systems;
}))
{
# https://github.com/nix-community/poetry2nix/blob/master/docs/edgecases.md#modulenotfounderror-no-module-named-packagename
package = [ "setuptools" ];
}
);
toposPoetryEnv = pkgs.poetry2nix.mkPoetryEnv {
python = pkgs.python39; # set python version https://stackoverflow.com/questions/68625627/nix-flake-get-a-specific-python-version
projectDir = self;
preferWheels = true;
inherit overrides;
};
envFile = pkgs.writeText "env_dev" (builtins.readFile ./.env_dev);
parseEnvFile = envFile:
let
content = builtins.readFile envFile;
lines = lib.filter (l: l != "" && !lib.hasPrefix "#" l) (lib.splitString "\n" content);
parseLine = l:
let
parts = lib.splitString "=" l;
in
{ name = lib.head parts; value = lib.concatStringsSep "=" (lib.tail parts); };
in
builtins.listToAttrs (map parseLine lines);
envVars = parseEnvFile ./.env_dev;
configFile = pkgs.copyPathToStore ./config.yaml;
yq = pkgs.yq-go;
kafkaPreStartup = ''
echo "Kafka is ready. Creating topic..."
${pkgs.apacheKafka}/bin/kafka-topics.sh --create --topic chat_topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 --if-not-exists
'';
# Note: This only loads the settings from the repos config file
# if one is not already set in the user's .config directory.
toposSetupHook = ''
export $(cat ${envFile} | xargs)
export TOPOS_CONFIG_PATH="$HOME/.topos/config.yaml"
mkdir -p "$(dirname "$TOPOS_CONFIG_PATH")"
if [ ! -f "$TOPOS_CONFIG_PATH" ]; then
echo "Creating new config file at $TOPOS_CONFIG_PATH"
echo "# Topos Configuration" > "$TOPOS_CONFIG_PATH"
${yq}/bin/yq eval ${configFile} | while IFS= read -r line; do
echo "$line" >> "$TOPOS_CONFIG_PATH"
done
echo "Config file created at $TOPOS_CONFIG_PATH"
else
echo "Config file already exists at $TOPOS_CONFIG_PATH"
fi
${kafkaPreStartup}
'';
in
{
process-compose."services-flake-topos" = { config, ... }: {
imports = [
inputs.services-flake.processComposeModules.default
(import ./nix/services/topos-service.nix { inherit pkgs lib config; topos = self'.packages.topos; })
];
services = let dataDirBase = "$HOME/.topos"; in {
# Backend service to perform inference on LLM models
ollama."ollama" = {
enable = true;
# The models are usually huge, downloading them in every project
# directory can lead to a lot of duplication. Change here to a
# directory where the Ollama models can be stored and shared across
# projects.
# dataDir = "${dataDirBase}/ollama";
# Define the models to download when our app starts
#
# You can also initialize this to empty list, and download the
# models manually in the UI.
#
# Search for the models here: https://ollama.com/library
models = [ "dolphin-llama3" ];
};
postgres."pg" = {
# data options https://search.nixos.org/options?query=services.postgresql
enable = true;
package = pkgs.postgresql_16.withPackages (p: [ p.pgvector ]);
port = 5432;
listen_addresses = "127.0.0.1";
# dataDir = "${dataDirBase}/pg";
initialDatabases = [
{ name = "${envVars.POSTGRES_DB}"; }
];
initialScript = {
before = ''
CREATE EXTENSION IF NOT EXISTS vector;
CREATE USER ${envVars.POSTGRES_USER} WITH SUPERUSER PASSWORD '${envVars.POSTGRES_PASSWORD}';
'';
after = ''
-- THESE CREATE TABLE STATEMENTS HERE DO NOT WORK
-- THE WAY THE TABLES GET BUILT RN IS THROUGH THE PYTHON CODE _ensure_table_exists
CREATE TABLE IF NOT EXISTS conversation (
message_id VARCHAR PRIMARY KEY,
conv_id VARCHAR NOT NULL,
userid VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
name VARCHAR,
role VARCHAR NOT NULL,
message TEXT NOT NULL
);
-- Create the utterance_token_info table
CREATE TABLE IF NOT EXISTS utterance_token_info (
message_id VARCHAR PRIMARY KEY,
conv_id VARCHAR NOT NULL,
userid VARCHAR NOT NULL,
name VARCHAR,
role VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
ents JSONB
);
-- Create the utterance_text_info table
CREATE TABLE IF NOT EXISTS utterance_text_info (
message_id VARCHAR PRIMARY KEY,
conv_id VARCHAR NOT NULL,
userid VARCHAR NOT NULL,
name VARCHAR,
role VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
moderator JSONB,
mod_label VARCHAR,
tern_sent JSONB,
tern_label VARCHAR,
emo_27 JSONB,
emo_27_label VARCHAR
);
GRANT ALL PRIVILEGES ON DATABASE ${envVars.POSTGRES_DB} TO ${envVars.POSTGRES_USER};
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ${envVars.POSTGRES_USER};
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ${envVars.POSTGRES_USER};
GRANT ALL PRIVILEGES ON SCHEMA public TO ${envVars.POSTGRES_USER};
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ${envVars.POSTGRES_USER};
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO ${envVars.POSTGRES_USER};
'';
};
};
zookeeper."zookeeper".enable = true;
apache-kafka."kafka" = {
enable = true;
port = 9092;
# dataDir = "${dataDirBase}/kafka";
settings = {
"offsets.topic.replication.factor" = 1;
"zookeeper.connect" = [ "localhost:2181" ];
};
formatLogDirs = true;
formatLogDirsIgnoreFormatted = true;
jvmOptions = [
"-Xmx512M"
"-Xms512M"
];
};
topos.enable = true;
topos.args = [ "run" ];
};
settings.processes = {
kafka.depends_on."zookeeper".condition = "process_healthy";
kafka.depends_on.pg.condition = "process_healthy";
topos.depends_on.pg.condition = "process_healthy";
topos.depends_on.kafka.condition = "process_healthy";
};
};
packages = rec {
toposPoetry = pkgs.poetry2nix.mkPoetryApplication {
projectDir = self;
preferWheels = true;
inherit overrides;
};
topos = pkgs.writeShellScriptBin "topos" ''
${toposSetupHook}
${toposPoetry}/bin/topos "$@"
'';
default = self'.packages."services-flake-topos";
};
devShells = {
# Shell for app dependencies.
#
# nix develop
#
# Use this shell for developing your app.
default = pkgs.mkShell {
inputsFrom = [ toposPoetryEnv ];
packages = [ ];
shellHook = ''
export PATH="${toposPoetryEnv}/bin:$PATH"
${toposSetupHook}
'';
};
# Shell for poetry.
#
# nix develop .#poetry
#
# Use this shell for changes to pyproject.toml and poetry.lock.
poetry = pkgs.mkShell {
packages = [ pkgs.poetry ];
};
};
legacyPackages = pkgs;
};
};
}