-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
309 lines (258 loc) · 8.29 KB
/
flake.nix
File metadata and controls
309 lines (258 loc) · 8.29 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{
description = "ML Ops";
outputs =
{
self,
flake-utils,
nixpkgs,
llmpkgs,
naersk,
pyproject-nix,
uv2nix,
pyproject-build-systems,
...
}:
let
kernelName = "ml_ops";
jupyterDir = "jupyter";
kernelsDir = "${jupyterDir}/kernels";
workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; };
overlay = workspace.mkPyprojectOverlay {
sourcePreference = "wheel";
};
inherit (nixpkgs) lib;
forAllSystems = lib.genAttrs lib.systems.flakeExposed;
pythonSets = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
python = pkgs.python3;
in
(pkgs.callPackage pyproject-nix.build.packages {
inherit python;
}).overrideScope
(
lib.composeManyExtensions [
pyproject-build-systems.overlays.wheel
overlay
]
)
);
in
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
# config.cudaSupport = true;
};
lib = pkgs.lib;
llms = llmpkgs.packages.${system};
naerskLib = pkgs.callPackage naersk { };
cargoBuild = with pkgs; [
rustc
cargo
];
alliumCliSrc = pkgs.runCommand "allium-cli-3.2.3-src" { } ''
mkdir -p "$out"
tar -xzf ${
pkgs.fetchurl {
url = "https://static.crates.io/crates/allium-cli/allium-cli-3.2.3.crate";
hash = "sha256-Hf74VsRGSyFsyODyBnMVFIGHwQt3Nebjhgz6LJmmZ9k=";
}
} -C "$out" --strip-components=1
'';
alliumCli = naerskLib.buildPackage {
pname = "allium-cli";
version = "3.2.3";
src = alliumCliSrc;
singleStep = true;
};
pythonPkgs =
(with pkgs; [
python313
uv
sphinx
pyright
# jupyter-all
])
++ (with pkgs.python313Packages; [
ipykernel
jupyter
jupyterlab
]);
devPkgs = with pkgs; [
bash
bun
curl
gawk
gh
git
# Python
basePython
alliumCli
startJupyter
# Infrastructure as Code
opentofu
];
docsPkgs = with pkgs; [
pandoc
quarto
d2
# LaTeX for Sphinx PDF export
texlive.combined.scheme-full
];
typstPkgs =
(with pkgs; [
typst
typst-live
typstyle
poppler-utils
])
++ (with pkgs.typstPackages; [
ori
ilm
tbl
]);
llmPkgs = with llms; [
claude-code
copilot-cli
kilocode-cli
opencode
openskills
openspec
pi
spec-kit
rtk
];
startJupyterPy = pkgs.writeShellScriptBin "start-jupyter.py" ''
#!/usr/bin/env python3
import os
import subprocess
import sys
from pathlib import Path
KERNEL_NAME = ${builtins.toJSON kernelName}
JUPYTER_DIR = ${builtins.toJSON jupyterDir}
KERNELS_DIR = ${builtins.toJSON kernelsDir}
def ensure_kernel(project_root: Path) -> None:
jupyter_path = os.environ.get("JUPYTER_PATH")
project_jupyter_dir = str(project_root / JUPYTER_DIR)
os.environ["JUPYTER_PATH"] = f"{project_jupyter_dir}:{jupyter_path}" if jupyter_path else project_jupyter_dir
kernel_dir = project_root / KERNELS_DIR / KERNEL_NAME
kernel_dir.mkdir(parents=True, exist_ok=True)
subprocess.run(
[sys.executable, "-m", "ipykernel", "install", "--user", "--name", KERNEL_NAME, "--display-name", KERNEL_NAME],
check=True,
)
cfg_dir = Path.home() / ".jupyter"
cfg_dir.mkdir(parents=True, exist_ok=True)
(cfg_dir / "jupyter_server_config.py").write_text("c.ServerApp.disable_check_xsrf = True\n")
(cfg_dir / "jupyter_notebook_config.py").write_text("c.NotebookApp.disable_check_xsrf = True\n")
def main() -> None:
project_root = Path(os.environ.get("PROJECT_ROOT", os.getcwd())).resolve()
os.environ["PROJECT_ROOT"] = str(project_root)
ensure_only = False
passthrough_args = []
for arg in sys.argv[1:]:
if arg == "--ensure-only":
ensure_only = True
else:
passthrough_args.append(arg)
ensure_kernel(project_root)
if ensure_only:
return
cmd = [
"jupyter-lab",
"--no-browser",
'--ip=*',
"--NotebookApp.token=",
"--NotebookApp.password=",
"--ServerApp.disable_check_xsrf=True",
*passthrough_args,
]
os.execvp(cmd[0], cmd)
if __name__ == "__main__":
main()
'';
shellEntryHook = ''
export PROJECT_ROOT="$PWD"
if [ -f .env ]; then
set -a
. ./.env
set +a
fi
export REPO_ROOT=$(git rev-parse --show-toplevel)
unset PYTHONPATH
export JUPYTER_PATH="$PROJECT_ROOT/${jupyterDir}''${JUPYTER_PATH:+:$JUPYTER_PATH}"
export JUPYTER_CONFIG_DIR="$HOME/.config/jupyter"
export UV_PYTHON_DOWNLOADS=never
export UV_PROJECT_ENVIRONMENT="$PROJECT_ROOT/.venv"
# export LD_LIBRARY_PATH=${pkgs.cudaPackages.cuda_cudart}/lib64:${pkgs.cudaPackages.cuda_cudart}/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=${pkgs.stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH
if [ -f pyproject.toml ]; then
if [ ! -x "$UV_PROJECT_ENVIRONMENT/bin/python" ]; then
echo "Creating project virtualenv in $UV_PROJECT_ENVIRONMENT"
uv venv --python ${pkgs.python3}/bin/python "$UV_PROJECT_ENVIRONMENT" >/dev/null
fi
. "$UV_PROJECT_ENVIRONMENT/bin/activate"
NEED_UV_SYNC=0
if [ "''${UV_SYNC_REQUESTED:-0}" = "1" ]; then
NEED_UV_SYNC=1
elif ! uv sync --check >/dev/null 2>&1; then
NEED_UV_SYNC=1
fi
if [ "$NEED_UV_SYNC" -eq 1 ]; then
uv sync >/dev/null
fi
fi
python --version
${startJupyterPy} --ensure-only >/dev/null
echo
'';
in
{
devShells.default = pkgs.mkShell {
buildInputs = cargoBuild;
packages = pythonPkgs ++ devPkgs ++ docsPkgs ++ typstPkgs ++ llmPkgs;
shellHook = shellEntryHook;
env = {
UV_NO_SYNC = "1";
UV_PYTHON = "${pkgs.python3}/bin/python";
UV_PYTHON_DOWNLOADS = "never";
};
};
packages = forAllSystems (system: {
default = pythonSets.${system}.mkVirtualEnv "ml_deploy" workspace.deps.default;
});
formatter = pkgs.nixfmt;
}
);
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
# LLM Agents packaged for Nix
llmpkgs.url = "github:numtide/llm-agents.nix";
# Rust / Cargo packaging
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
# Python packaging integration with Nix
pyproject-nix = {
url = "github:pyproject-nix/pyproject.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
uv2nix = {
url = "github:pyproject-nix/uv2nix";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
pyproject-build-systems = {
url = "github:pyproject-nix/build-system-pkgs";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.uv2nix.follows = "uv2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
}