Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
239 lines (222 loc) · 8.92 KB
/
Copy pathflake.nix
File metadata and controls
239 lines (222 loc) · 8.92 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
{
description = "idx: Indonesian stock market (IDX / IHSG) CLI and MCP server for people and AI agents";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
crane,
rust-overlay,
}:
let
inherit (nixpkgs) lib;
# No x86_64-darwin: nixpkgs 26.05 is the last release that builds it.
systems = [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
];
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
inherit (cargoToml.package) version;
# Cargo.toml's rust-version ("1.89") as a toolchain release ("1.89.0").
msrvRelease = lib.versions.pad 3 cargoToml.package.rust-version;
perSystem = lib.genAttrs systems (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
# rust-toolchain.toml (stable, rustfmt, clippy) is the one toolchain source.
toolchainFor = p: p.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain toolchainFor;
# The declared minimum (Cargo.toml rust-version), for checks.msrv.
msrvCraneLib = (crane.mkLib pkgs).overrideToolchain (
p: p.rust-bin.stable.${msrvRelease}.minimal
);
# What the crate compiles and its tests read (Cargo.toml `include`
# without most docs): sources, the bundled CSVs, the recorded
# fixtures, and the JSON reference whose -o jsonl table a unit test
# checks against the code. Add a path here if the crate starts
# embedding files from elsewhere.
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./rustfmt.toml
./src
./data
./tests
./docs/json-output.md
];
};
commonArgs = {
inherit src version;
pname = cargoToml.package.name;
strictDeps = true;
# rusqlite's bundled SQLite and ring build their C with the stdenv
# compiler; rustls runs on ring with webpki-roots, so no OpenSSL,
# pkg-config or system CA bundle.
buildInputs = lib.optionals pkgs.stdenv.hostPlatform.isDarwin [ pkgs.libiconv ];
};
# Dependencies are built once per profile: release for the package,
# dev for clippy and the tests (the profile CI uses). The release
# set only feeds `cargo build`, so it skips buildDepsOnly's default
# `cargo check` pass, which only clippy would reuse.
cargoArtifacts = craneLib.buildDepsOnly (
commonArgs
// {
doCheck = false;
buildPhaseCargoCommand = "cargoWithProfile build --locked";
}
);
devArgs = commonArgs // {
CARGO_PROFILE = "dev";
};
devArtifacts = craneLib.buildDepsOnly devArgs;
idx = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
doCheck = false; # checks.test runs the suite
nativeBuildInputs = [ pkgs.installShellFiles ];
postInstall = lib.optionalString (pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform) ''
export HOME="$TMPDIR"
installShellCompletion --cmd idx \
--bash <($out/bin/idx completions bash) \
--fish <($out/bin/idx completions fish) \
--zsh <($out/bin/idx completions zsh)
'';
meta = {
description = "Indonesian stock market (IDX / IHSG) CLI and MCP server";
homepage = "https://ihsg.midoriresearch.com";
license = lib.licenses.mit;
mainProgram = "idx";
platforms = systems;
};
}
);
# `nix run github:fzn0x/idx#mcp`: the MCP server over stdio. Agent
# configs should point at an installed idx instead (README, Install):
# a cold `nix run` builds from source first.
idx-mcp = pkgs.writeShellApplication {
name = "idx-mcp";
text = ''exec ${lib.getExe idx} mcp "$@"'';
};
checks = {
inherit idx;
fmt = craneLib.cargoFmt { inherit src; };
clippy = craneLib.cargoClippy (
devArgs
// {
cargoArtifacts = devArtifacts;
cargoClippyExtraArgs = "--all-targets -- -D warnings";
doInstallCargoArtifacts = false; # nothing builds on top; keep the store small
}
);
test = craneLib.cargoTest (
devArgs
// {
cargoArtifacts = devArtifacts;
cargoTestExtraArgs = "--all-targets";
doInstallCargoArtifacts = false;
# The tests point config and cache at temp dirs themselves;
# code that still falls back to ~ gets a writable temp home, as
# on a developer machine, instead of the sandbox's unwritable
# /homeless-shelter.
preBuild = ''export HOME="$TMPDIR"'';
}
);
# A `cargo check` of every target, tests included, on the locked
# dependency tree with the declared minimum Rust (Cargo.toml
# rust-version). It type-checks the targets; it does not build
# the crate's binaries or run the tests.
msrv = msrvCraneLib.mkCargoDerivation (
commonArgs
// {
pnameSuffix = "-msrv";
cargoArtifacts = null;
doInstallCargoArtifacts = false;
buildPhaseCargoCommand = "cargo check --locked --all-targets";
}
);
# The packaged binary and the MCP app start, report their version,
# keep the offline exit-code contract (release.yml smokes the same
# way) and answer an MCP handshake over stdio.
smoke = pkgs.runCommand "idx-smoke" { } ''
export HOME="$TMPDIR"
${lib.getExe idx} version
${lib.getExe idx} -o json version > version.json
grep -q '"version": "${version}"' version.json
status=0
${lib.getExe idx} --offline -o json stocks quote BBCA 2>err.json || status=$?
test "$status" -eq 5
grep -q '"code":"offline_miss"' err.json
{
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"nix-smoke","version":"0"}}}'
echo '{"jsonrpc":"2.0","method":"notifications/initialized"}'
echo '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'
sleep 2
} | IDX_OFFLINE=1 timeout 60 ${lib.getExe idx-mcp} > mcp.jsonl
# Name and version only, so a field rmcp or get_info adds later
# (title, description, icons, websiteUrl) does not fail it.
grep -q '"serverInfo":{"name":"idx",[^}]*"version":"${version}"' mcp.jsonl
grep -q '"name":"idx_quote"' mcp.jsonl
touch "$out"
'';
};
# The same toolchain plus what an editor needs.
devCraneLib = (crane.mkLib pkgs).overrideToolchain (
p:
(toolchainFor p).override {
extensions = [
"rustfmt"
"clippy"
"rust-src"
"rust-analyzer"
];
}
);
in
{
inherit idx idx-mcp checks;
devShell = devCraneLib.devShell {
inherit (commonArgs) buildInputs;
# web/ (node --test web/test/*.mjs, npx wrangler) and npm/.
packages = [ pkgs.nodejs_22 ];
};
}
);
in
{
packages = lib.mapAttrs (_: s: {
default = s.idx;
inherit (s) idx idx-mcp;
}) perSystem;
apps = lib.mapAttrs (_: s: {
default = {
type = "app";
program = lib.getExe s.idx;
meta.description = "idx CLI";
};
mcp = {
type = "app";
program = lib.getExe s.idx-mcp;
meta.description = "idx MCP server over stdio";
};
}) perSystem;
checks = lib.mapAttrs (_: s: s.checks) perSystem;
devShells = lib.mapAttrs (_: s: { default = s.devShell; }) perSystem;
overlays.default = final: _prev: {
idx = self.packages.${final.stdenv.hostPlatform.system}.idx;
};
};
}