-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflake.nix
328 lines (284 loc) · 10.9 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
{
description = "Nava PBC Platform CLI";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
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";
};
uv2nix_hammer_overrides = {
url = "github:TyberiusPrime/uv2nix_hammer_overrides";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
pyproject-nix,
uv2nix,
pyproject-build-systems,
uv2nix_hammer_overrides,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
};
# Non-Python runtime dependencies go here
runtimePackages = with pkgs; [
gitMinimal
];
# uv2nix stuff --------------------------------------------------------
workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; };
# Create package overlay from workspace.
overlay = workspace.mkPyprojectOverlay {
# Prefer prebuilt binary wheels as a package source.
# Sdists are less likely to "just work" because of the metadata missing from uv.lock.
# Binary wheels are more likely to, but may still require overrides for library dependencies.
sourcePreference = "wheel"; # or sourcePreference = "sdist";
# Optionally customise PEP 508 environment
# environ = {
# platform_release = "5.10.65";
# };
};
# Extend generated overlay with build fixups
#
# Uv2nix can only work with what it has, and uv.lock is missing essential metadata to perform some builds.
# This is an additional overlay implementing build fixups.
# See:
# - https://adisbladis.github.io/uv2nix/FAQ.html
pyprojectOverrides = final: prev: {
# Implement build fixups here.
# Note that uv2nix is _not_ using Nixpkgs buildPythonPackage.
# It's using https://pyproject-nix.github.io/pyproject.nix/build.html
};
python = pkgs.python312;
# Construct package set
pythonSet =
# Use base package set from pyproject.nix builders
(pkgs.callPackage pyproject-nix.build.packages {
inherit python;
}).overrideScope
(
pkgs.lib.composeManyExtensions [
pyproject-build-systems.overlays.default
overlay
(uv2nix_hammer_overrides.overrides pkgs)
pyprojectOverrides
]
);
# see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples.
nava-platform-cli-env = pythonSet.mkVirtualEnv "nava-platform-cli-env" workspace.deps.default;
# TODO: inject runtimePackages here?
nava-platform-cli = (pkgs.callPackages pyproject-nix.build.util { }).mkApplication {
venv = nava-platform-cli-env;
package = pythonSet.nava-platform-cli;
};
# TODO: could add docker-client here?
generalDevPackages = with pkgs; [
# dev tooling
gnumake
# linting
shellcheck
nixfmt-rfc-style
actionlint
# container things
skopeo
];
dockerEntryPkg =
let
scriptDeps = [
pkgs.coreutils # for id, stat
pkgs.util-linux # for setpriv
];
in
pkgs.stdenv.mkDerivation {
name = "docker-entry";
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = ./bin/docker-entry;
};
nativeBuildInputs = [ pkgs.makeWrapper ];
installPhase = ''
mkdir -p $out/bin
install $src/bin/docker-entry $out/bin/docker-entry
wrapProgram $out/bin/docker-entry --prefix PATH : ${pkgs.lib.makeBinPath scriptDeps}
'';
};
dockerBuildArgs = {
name = "nava-platform-cli";
tag = "latest";
contents = [
dockerEntryPkg
nava-platform-cli
] ++ runtimePackages;
config = {
Entrypoint = "docker-entry";
WorkingDir = "/project-dir";
};
};
nava-platform-cli-docs-env = pythonSet.mkVirtualEnv "nava-platform-cli-docs-env" workspace.deps.groups;
cli-docs-site = pkgs.stdenv.mkDerivation {
name = "nava-platform-cli-docs";
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions ([
./docs
./nava
./Makefile
./mkdocs.yml
./README.md
]);
};
buildInputs = [ nava-platform-cli-docs-env ] ++ [ pkgs.gnumake ];
PY_RUN = "";
buildPhase = ''
make docs
'';
installPhase = ''
cp -rf site/ $out
'';
};
in
rec {
packages = {
default = nava-platform-cli;
nava-platform-cli = nava-platform-cli;
docs = cli-docs-site;
docker = pkgs.dockerTools.buildLayeredImage dockerBuildArgs;
dockerStream = pkgs.dockerTools.streamLayeredImage dockerBuildArgs;
};
# nix run .
apps = {
default = apps.nava-platform-cli;
nava-platform-cli = {
type = "app";
program = "${nava-platform-cli}/bin/nava-platform";
};
};
devShells = {
# Shell for general app development.
#
# nix develop
#
# Use unless you are hitting issues, in which case see `devTools` below.
default =
let
# Create an overlay enabling editable mode for all local dependencies.
editableOverlay = workspace.mkEditablePyprojectOverlay {
# Use environment variable
root = "$REPO_ROOT";
# Optional: Only enable editable for these packages
# members = [ "hello-world" ];
};
# Override previous set with our overrideable overlay.
editablePythonSet = pythonSet.overrideScope (
pkgs.lib.composeManyExtensions [
editableOverlay
# Apply fixups for building an editable package of your workspace packages
(final: prev: {
nava-platform-cli = prev.nava-platform-cli.overrideAttrs (old: {
# It's a good idea to filter the sources going into an editable build
# so the editable package doesn't have to be rebuilt on every change.
src = pkgs.lib.fileset.toSource {
root = old.src;
fileset = pkgs.lib.fileset.unions [
(old.src + "/pyproject.toml")
(old.src + "/README.md")
(old.src + "/nava/__init__.py")
];
};
# Hatchling (our build system) has a dependency on the `editables` package when building editables.
#
# In normal Python flows this dependency is dynamically handled, and doesn't need to be explicitly declared.
# This behaviour is documented in PEP-660.
#
# With Nix the dependency needs to be explicitly declared.
nativeBuildInputs =
old.nativeBuildInputs
++ final.resolveBuildSystem {
editables = [ ];
};
});
})
]
);
# Build virtual environment, with local packages being editable.
#
# Enable all optional dependencies for development.
virtualenv = editablePythonSet.mkVirtualEnv "nava-platform-cli-dev-env" workspace.deps.all;
in
pkgs.mkShell {
inputsFrom = [ devShells.devTools ];
packages = [ virtualenv ];
env = {
# Don't create venv using uv
UV_NO_SYNC = "1";
# Force uv to use Python interpreter from venv
UV_PYTHON = "${virtualenv}/bin/python";
# For Make targets, Python things will be available directly, so
# prefer that over running through uv
PY_RUN = "";
};
shellHook = ''
# Get repository root using git. This is expanded at runtime by the editable `.pth` machinery.
export REPO_ROOT=$(git rev-parse --show-toplevel)
'';
};
# Shell for just dev tools, skipping Python dependencies (which may
# have issues building, etc.).
#
# nix develop .#devTools
#
# Can use this shell for changes to pyproject.toml/poetry.lock or
# running misc make targets/dev scripts unrelated to Python.
devTools = pkgs.mkShell {
packages =
generalDevPackages
++ runtimePackages
++ [
uv2nix.packages."${system}".uv-bin
];
env = {
# Force uv to use nixpkgs Python interpreter
UV_PYTHON = python.interpreter;
} // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
# Python libraries often load native shared objects using dlopen(3).
# Setting LD_LIBRARY_PATH makes the dynamic library loader aware of libraries without using RPATH for lookup.
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath pkgs.pythonManylinuxPackages.manylinux1;
};
shellHook = ''
# Undo dependency propagation by nixpkgs.
unset PYTHONPATH
# Disable uv Python interpreter fetching, use nix provided one
export UV_PYTHON_PREFERENCE=only-system;
export UV_PYTHON_DOWNLOADS=never;
'';
};
# Shell for pipx interaction. For testing installing the package via
# pipx or other needs.
pipx = pkgs.mkShell {
packages = [
pkgs.pipx
];
};
};
legacyPackages = pkgs;
}
);
}