Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
282 lines (258 loc) · 11 KB
/
Copy pathflake.nix
File metadata and controls
282 lines (258 loc) · 11 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
{
description = "NegPy — a tool for processing film negatives";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
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 = {
self,
nixpkgs,
pyproject-nix,
uv2nix,
pyproject-build-systems,
...
}: let
inherit (nixpkgs) lib;
forAllSystems = lib.genAttrs ["x86_64-linux" "aarch64-linux"];
workspace = uv2nix.lib.workspace.loadWorkspace {workspaceRoot = ./.;};
overlay = workspace.mkPyprojectOverlay {
sourcePreference = "wheel";
};
editableOverlay = workspace.mkEditablePyprojectOverlay {
root = "$REPO_ROOT";
};
# Packages that need system Qt6/Vulkan/LLVM rather than what autoPatchelf can wire
# up from a bare PyPI wheel — swap in nixpkgs' own builds instead.
nixpkgsFallbackOverlay = pkgs: python3Packages: final: prev: let
hacks = pkgs.callPackage pyproject-nix.build.hacks {};
fromNixpkgs = attr: prevPkg:
hacks.nixpkgsPrebuilt {
from = python3Packages.${attr};
prev = prevPkg;
};
in {
numpy = fromNixpkgs "numpy" prev.numpy;
llvmlite = fromNixpkgs "llvmlite" prev.llvmlite;
numba = fromNixpkgs "numba" prev.numba;
# nixpkgs' own "opencv-python-headless" is a metapackage that only propagates
# "opencv4" (where cv2 actually lives); nixpkgsPrebuilt strips propagation, so
# point straight at opencv4 or the venv ends up without a cv2 module.
opencv-python-headless = fromNixpkgs "opencv4" prev.opencv-python-headless;
rawpy = fromNixpkgs "rawpy" prev.rawpy;
# imagecodecs and tifffile are left off this list deliberately: nixpkgs'
# imagecodecs has no JPEG-XL codec (breaks JXL export/import round-trips),
# and nixpkgs' tifffile lags the pinned version enough to break DNG/extrasamples
# handling. Their PyPI manylinux wheels are self-contained, so plain
# autoPatchelf (uv2nix's default wheel path) works better than the fallback.
pillow = fromNixpkgs "pillow" prev.pillow;
# python-sane is an sdist (no PyPI wheels): it compiles against libsane at
# build time, so it needs nixpkgs' sane-backends rather than autoPatchelf.
python-sane = fromNixpkgs "sane" prev.python-sane;
pyqt6-sip = fromNixpkgs "pyqt6-sip" prev.pyqt6-sip;
pyqt6 = hacks.nixpkgsPrebuilt {
from = python3Packages.pyqt6;
# PyQt6-Qt6 (bundled standalone Qt6 libs) has no nixpkgs equivalent and
# isn't needed — nixpkgs' pyqt6 links the system Qt6 instead.
prev = prev.pyqt6.overrideAttrs (old: {
passthru =
old.passthru
// {
dependencies = lib.filterAttrs (name: _: name != "pyqt6-qt6") old.passthru.dependencies;
};
});
};
pyqt6-charts = hacks.nixpkgsPrebuilt {
from = python3Packages.pyqt6-charts;
prev = prev.pyqt6-charts.overrideAttrs (old: {
passthru =
old.passthru
// {
dependencies = lib.filterAttrs (name: _: name != "pyqt6-charts-qt6") old.passthru.dependencies;
};
});
};
# Older nixpkgs packages this as "wgpu-py" (newer renamed it to "wgpu"),
# while upstream's pyproject.toml declares
# name = "wgpu", so pythonMetadataCheckPhase can't find dist-info under
# "wgpu-py" and fails the build. Skip that check.
wgpu = hacks.nixpkgsPrebuilt {
from = (python3Packages.wgpu-py or python3Packages.wgpu).overrideAttrs {dontCheckPythonMetadata = true;};
prev = prev.wgpu;
};
};
# icc/media/crosstalk/gear and VERSION live at the repo root, not inside the
# negpy/ package tree, so setuptools never picks them up as package data.
# get_resource_path() (negpy/kernel/system/paths.py) walks 3 levels up from
# itself and expects to land in the site-packages dir these are copied into.
# negpy/features/**/shaders/*.wgsl and desktop/view/styles/*.qss are inside
# the package but non-.py, so setuptools drops those too — merge the source
# tree back in after install. Without the stylesheet, main.py's
# os.path.exists() guard fails and the app falls back to bare Qt Fusion
# styling instead of NegPy's dark theme.
dataFixupOverlay = python: final: prev: {
negpy = prev.negpy.overrideAttrs (old: {
postInstall =
(old.postInstall or "")
+ ''
site_packages="$out/${python.sitePackages}"
# An editable install has no package tree here and reads these from the repo.
if [ -d "$site_packages/negpy" ]; then
cp -r icc media crosstalk gear "$site_packages/"
cp -r negpy/features "$site_packages/negpy/"
cp -r negpy/desktop/view/styles "$site_packages/negpy/desktop/view/"
cp VERSION "$site_packages/"
fi
'';
});
};
mkPythonSet = pkgs: let
python = pkgs.python313;
in
(pkgs.callPackage pyproject-nix.build.packages {inherit python;}).overrideScope (
lib.composeManyExtensions [
pyproject-build-systems.overlays.wheel
overlay
(nixpkgsFallbackOverlay pkgs pkgs.python313Packages)
(dataFixupOverlay python)
]
);
pythonSets = forAllSystems (system: mkPythonSet nixpkgs.legacyPackages.${system});
# Mirrors release.yml's `uv sync --all-groups` for Linux: every optional
# scan backend ships by default. sane/camera get override flags because,
# like upstream's per-OS group exclusions (no sane on Windows, no camera
# group on win32), they need a system SANE/libgphoto2 install rather than
# being pure-Python. nkscan has no aarch64-linux wheel — like upstream,
# which only builds Linux on x86_64 — so it's included by platform, not a flag.
mkNegpy = pkgs: pythonSet:
lib.makeOverridable (
{
withSane ? true,
withCamera ? true,
}: let
extras = {
negpy =
["plustek" "pieusb"]
++ lib.optionals withSane ["sane"]
++ lib.optionals withCamera ["camera"]
++ lib.optionals (pkgs.stdenv.hostPlatform.system == "x86_64-linux") ["nkscan"];
};
venv = pythonSet.mkVirtualEnv "negpy-env" extras;
# modern_dark.qss requests "Inter"; bundle it via fontconfig rather than
# relying on it being installed system-wide, same as upstream's AppImage.
fontsConf = pkgs.makeFontsConf {fontDirectories = [pkgs.inter];};
in
pkgs.stdenv.mkDerivation {
pname = "negpy";
version = lib.trim (builtins.readFile ./VERSION);
dontUnpack = true;
dontBuild = true;
nativeBuildInputs = [
pkgs.makeWrapper
pkgs.qt6.wrapQtAppsHook
];
buildInputs = [pkgs.qt6.qtbase];
# We wrap by hand below so we control both the Qt env and LD_LIBRARY_PATH
# on the same wrapProgram call; wrapQtAppsHook only supplies qtWrapperArgs.
dontWrapQtApps = true;
installPhase = ''
runHook preInstall
mkdir -p $out/bin
makeWrapper ${venv}/bin/python $out/bin/negpy \
--add-flags "-m negpy.desktop.main" \
"''${qtWrapperArgs[@]}" \
--prefix LD_LIBRARY_PATH : ${
lib.makeLibraryPath (
[pkgs.vulkan-loader pkgs.libGL]
# sane_backend.py dlopens libsane.so.1 itself, ahead of importing
# the _sane extension, so it needs the loader search path — rpath
# on the extension module isn't consulted for that call.
++ lib.optionals withSane [pkgs.sane-backends]
)
} \
--set FONTCONFIG_FILE ${fontsConf}
install -Dm644 ${./NegPy.desktop} $out/share/applications/NegPy.desktop
substituteInPlace $out/share/applications/NegPy.desktop \
--replace-fail "Exec=NegPy" "Exec=$out/bin/negpy"
install -Dm644 ${./media/icons/icon.svg} $out/share/icons/hicolor/scalable/apps/negpy.svg
runHook postInstall
'';
meta = {
description = "Tool for processing film negatives with film-physics simulation";
homepage = "https://github.com/marcinz606/NegPy";
license = lib.licenses.gpl3Only;
mainProgram = "negpy";
platforms = lib.platforms.linux;
};
}
);
in {
packages = forAllSystems (system: {
default = mkNegpy nixpkgs.legacyPackages.${system} pythonSets.${system} {};
});
apps = forAllSystems (system: {
default = {
type = "app";
program = "${self.packages.${system}.default}/bin/negpy";
};
});
devShells = forAllSystems (
system: let
pkgs = nixpkgs.legacyPackages.${system};
pythonSet = pythonSets.${system}.overrideScope editableOverlay;
# Matches CI's test job (uv sync --group dev --group plustek --group pieusb):
# plustek/pieusb are pure Python + bundled libusb, so they build standalone;
# sane and camera need a system SANE/libgphoto2 install and their tests
# importorskip away without them, so they're left out here too.
virtualenv = pythonSet.mkVirtualEnv "negpy-dev-env" {
negpy = [
"dev"
"plustek"
"pieusb"
];
};
in {
default = pkgs.mkShell {
packages = [
virtualenv
pkgs.uv
pkgs.qt6.qtbase
];
env = {
UV_NO_SYNC = "1";
UV_PYTHON = pythonSet.python.interpreter;
UV_PYTHON_DOWNLOADS = "never";
# Makes `uv run` (what the Makefile's targets use) treat this venv as
# the project environment instead of creating its own empty ./.venv.
# UV_NO_SYNC keeps it from then trying to write into that read-only
# Nix store path.
UV_PROJECT_ENVIRONMENT = virtualenv;
LD_LIBRARY_PATH = lib.makeLibraryPath [
pkgs.vulkan-loader
pkgs.libGL
];
# Replaces the host's path: plugins from a different Qt build abort QApplication.
QT_PLUGIN_PATH = "${pkgs.qt6.qtbase}/${pkgs.qt6.qtbase.qtPluginPrefix}";
};
shellHook = ''
unset PYTHONPATH
export REPO_ROOT=$(git rev-parse --show-toplevel)
'';
};
}
);
};
}