Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
163 lines (146 loc) · 5.47 KB
/
Copy pathflake.nix
File metadata and controls
163 lines (146 loc) · 5.47 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
{
description = "ContextMine development environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
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";
};
};
outputs =
inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ inputs.treefmt-nix.flakeModule ];
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
perSystem =
{
config,
pkgs,
lib,
system,
...
}:
let
# The Python environment is built by Nix from uv.lock.
workspace = inputs.uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; };
# Packages built from source that don't declare their build backend.
buildSystemOverrides = final: prev: {
multilspy = prev.multilspy.overrideAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs ++ final.resolveBuildSystem { flit-core = [ ]; };
});
};
# Workspace members and their directories, installed editable in the dev shell.
workspaceMembers = {
contextmine-api = "apps/api";
contextmine-core = "packages/core";
contextmine-worker = "apps/worker";
};
# hatchling needs `editables` to build editable installs.
editableOverrides =
final: prev:
lib.genAttrs (lib.attrNames workspaceMembers) (
name:
prev.${name}.overrideAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs ++ final.resolveBuildSystem { editables = [ ]; };
})
);
pythonSet =
(pkgs.callPackage inputs.pyproject-nix.build.packages { python = pkgs.python314; }).overrideScope
(
lib.composeManyExtensions [
inputs.pyproject-build-systems.overlays.wheel
(workspace.mkPyprojectOverlay { sourcePreference = "wheel"; })
buildSystemOverrides
# Workspace members are installed editable from the checkout.
(workspace.mkEditablePyprojectOverlay { root = "$REPO_ROOT"; })
editableOverrides
]
);
virtualenv = pythonSet.mkVirtualEnv "contextmine-dev-env" workspace.deps.all;
# Same toolchain as rust/spider_md/rust-toolchain.toml and the worker image.
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust/spider_md/rust-toolchain.toml;
in
{
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ inputs.rust-overlay.overlays.default ];
};
# `nix fmt` / `treefmt`; also exposed as a `nix flake check` check.
treefmt = {
projectRootFile = "flake.nix";
programs.nixfmt.enable = true;
programs.ruff-format = {
enable = true;
# Same ruff as uv.lock / pre-commit, so formatting doesn't drift.
package = lib.addMetaAttrs { mainProgram = "ruff"; } pythonSet.ruff;
# Generated protobuf code, also excluded in [tool.ruff].
excludes = [
"*_pb2.py"
"*_pb2.pyi"
];
};
programs.rustfmt = {
enable = true;
package = rustToolchain;
};
};
devShells.default = pkgs.mkShell {
packages = [
# Python workspace incl. dev group (ruff, ty, pytest, pre-commit)
virtualenv
pkgs.uv
# Frontend (apps/web)
pkgs.nodejs_24
# Crawler (rust/spider_md)
rustToolchain
pkgs.pkg-config
pkgs.openssl
# Local database / tooling
pkgs.postgresql_16
pkgs.git
config.treefmt.build.wrapper
];
env = {
# uv must not manage the environment; Nix does. `uv run` uses it as is.
UV_NO_SYNC = "1";
UV_PYTHON = "${virtualenv}/bin/python";
UV_PROJECT_ENVIRONMENT = "${virtualenv}";
UV_PYTHON_DOWNLOADS = "never";
};
shellHook = ''
export REPO_ROOT=$(git rev-parse --show-toplevel)
# The editable .pth files expand $REPO_ROOT at runtime, which static tools
# like ty can't evaluate. PYTHONPATH makes the members visible to them too.
export PYTHONPATH=${
lib.concatMapStringsSep ":" (dir: "$REPO_ROOT/${dir}") (lib.attrValues workspaceMembers)
}
'';
};
};
};
}