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
|
{
lib,
runCommand,
callPackage,
buildPythonPackage,
fetchFromGitHub,
fetchpatch,
pytestCheckHook,
replaceVars,
setuptools,
click-default-group,
condense-json,
numpy,
openai,
pip,
pluggy,
puremagic,
pydantic,
python,
python-ulid,
pyyaml,
sqlite-migrate,
cogapp,
pytest-asyncio,
pytest-httpx,
pytest-recording,
sqlite,
sqlite-utils,
syrupy,
llm-echo,
}:
let
/**
Make a derivation for `llm` that contains `llm` plus the relevant plugins.
The function signature of `withPlugins` is the list of all the plugins `llm` knows about.
Adding a parameter here requires that it be in `python3Packages` attrset.
# Type
```
withPlugins ::
{
llm-anthropic :: bool,
llm-gemini :: bool,
...
}
-> derivation
```
See `lib.attrNames (lib.functionArgs llm.withPlugins)` for the total list of plugins supported.
# Examples
:::{.example}
## `llm.withPlugins` usage example
```nix
llm.withPlugins { llm-gemini = true; llm-groq = true; }
=> «derivation /nix/store/<hash>-python3-3.12.10-llm-with-llm-gemini-llm-groq.drv»
```
:::
*/
withPlugins =
# Keep this list up to date with the plugins in python3Packages!
{
llm-anthropic ? false,
llm-cmd ? false,
llm-command-r ? false,
llm-deepseek ? false,
llm-docs ? false,
llm-echo ? false,
llm-fragments-github ? false,
llm-fragments-pypi ? false,
llm-fragments-reader ? false,
llm-fragments-symbex ? false,
llm-gemini ? false,
llm-gguf ? false,
llm-git ? false,
llm-github-copilot ? false,
llm-grok ? false,
llm-groq ? false,
llm-hacker-news ? false,
llm-jq ? false,
llm-llama-server ? false,
llm-lmstudio ? false,
llm-mistral ? false,
llm-ollama ? false,
llm-openai-plugin ? false,
llm-openrouter ? false,
llm-pdf-to-images ? false,
llm-perplexity ? false,
llm-sentence-transformers ? false,
llm-templates-fabric ? false,
llm-templates-github ? false,
llm-tools-datasette ? false,
llm-tools-quickjs ? false,
llm-tools-simpleeval ? false,
llm-tools-sqlite ? false,
llm-venice ? false,
llm-video-frames ? false,
...
}@args:
let
# Filter to just the attributes which are set to a true value.
setArgs = lib.filterAttrs (name: lib.id) args;
# Make the derivation name reflect what's inside it, up to a certain limit.
setArgNames = lib.attrNames setArgs;
drvName =
let
len = builtins.length setArgNames;
in
if len == 0 then
"llm-${llm.version}"
else if len > 20 then
"llm-${llm.version}-with-${toString len}-plugins"
else
# Make a string with those names separated with a dash.
"llm-${llm.version}-with-${lib.concatStringsSep "-" setArgNames}";
# Make a python environment with just those plugins.
python-environment = python.withPackages (
ps:
let
# Throw a diagnostic if this list gets out of sync with the names in python3Packages
allPluginsPresent = pluginNames == withPluginsArgNames;
pluginNames = lib.attrNames (lib.intersectAttrs ps withPluginsArgs);
missingNamesList = lib.attrNames (lib.removeAttrs withPluginsArgs pluginNames);
missingNames = lib.concatStringsSep ", " missingNamesList;
# The relevant plugins are the ones the user asked for.
plugins = lib.intersectAttrs setArgs ps;
in
assert lib.assertMsg allPluginsPresent "Missing these plugins: ${missingNames}";
([ ps.llm ] ++ lib.attrValues plugins)
);
in
# That Python environment produced above contains too many irrelevant binaries, due to how
# Python needs to use propagatedBuildInputs. Let's make one with just what's needed: `llm`.
# Since we include the `passthru` and `meta` information, it's as good as the original
# derivation.
runCommand "${python.name}-${drvName}" { inherit (llm) passthru meta; } ''
mkdir -p $out/bin
ln -s ${python-environment}/bin/llm $out/bin/llm
'';
# Uses the `withPlugins` names to make a Python environment with everything.
withAllPlugins = withPlugins (lib.genAttrs withPluginsArgNames (name: true));
# The function signature of `withPlugins` is the list of all the plugins `llm` knows about.
# The plugin directory is at <https://llm.datasette.io/en/stable/plugins/directory.html>
withPluginsArgs = lib.functionArgs withPlugins;
withPluginsArgNames = lib.attrNames withPluginsArgs;
# In order to help with usability, we patch `llm install` and `llm uninstall` to tell users how to
# customize `llm` with plugins in Nix, including the name of the plugin, its description, and
# where it's coming from.
listOfPackagedPlugins = builtins.toFile "plugins.txt" (
lib.concatStringsSep "\n " (
map (name: ''
# ${python.pkgs.${name}.meta.description} <${python.pkgs.${name}.meta.homepage}>
${name} = true;
'') withPluginsArgNames
)
);
llm = buildPythonPackage rec {
pname = "llm";
version = "0.28";
pyproject = true;
build-system = [ setuptools ];
src = fetchFromGitHub {
owner = "simonw";
repo = "llm";
tag = version;
hash = "sha256-PMQGyBwP6UCIz7p94atWgepbw9IwW6ym60sfP/PBrCA=";
};
patches = [
./001-disable-install-uninstall-commands.patch
]
# See https://github.com/NixOS/nixpkgs/issues/476258 and https://github.com/simonw/llm/pull/1334
# TODO: Remove when sqlite 3.52.x is released.
++ lib.optionals (sqlite.version == "3.51.1") [
(fetchpatch {
url = "https://github.com/simonw/llm/commit/6e24b883c3e3c4ddd2ec9006714d0a9ec17b59da.patch";
hash = "sha256-4AKQdZCr6qxuWnjWoSW6I44hPL5e7tnvREx2Ns0WwNc=";
})
];
postPatch = ''
substituteInPlace llm/cli.py \
--replace-fail "@listOfPackagedPlugins@" "$(< ${listOfPackagedPlugins})"
'';
dependencies = [
click-default-group
condense-json
numpy
openai
pip
pluggy
puremagic
pydantic
python-ulid
pyyaml
setuptools # for pkg_resources
sqlite-migrate
sqlite-utils
];
nativeCheckInputs = [
cogapp
numpy
pytest-asyncio
pytest-httpx
pytest-recording
syrupy
pytestCheckHook
];
doCheck = true;
# The tests make use of `llm_echo` but that would be a circular dependency.
# So we make a local copy in this derivation, as it's a super-simple package of one file.
preCheck = ''
cp ${llm-echo.src}/llm_echo.py llm_echo.py
'';
pytestFlags = [
"-svv"
];
enabledTestPaths = [
"tests/"
];
disabledTests = [
# AssertionError: The following responses are mocked but not requested:
# - Match POST request on https://api.openai.com/v1/chat/completions
# https://github.com/simonw/llm/issues/1292
"test_gpt4o_mini_sync_and_async"
# TypeError: CliRunner.__init__() got an unexpected keyword argument 'mix_stderr
# https://github.com/simonw/llm/issues/1293
"test_embed_multi_files_encoding"
];
pythonImportsCheck = [ "llm" ];
passthru = {
inherit withPlugins withAllPlugins;
mkPluginTest = plugin: {
${plugin.pname} = callPackage ./mk-plugin-test.nix { inherit llm plugin; };
};
# include tests for all the plugins
tests = lib.mergeAttrsList (map (name: python.pkgs.${name}.tests or { }) withPluginsArgNames);
};
meta = {
homepage = "https://github.com/simonw/llm";
description = "Access large language models from the command-line";
changelog = "https://github.com/simonw/llm/releases/tag/${src.tag}";
license = lib.licenses.asl20;
mainProgram = "llm";
maintainers = with lib.maintainers; [
aldoborrero
mccartykim
philiptaron
];
};
};
in
llm
|