-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
173 lines (150 loc) · 4.95 KB
/
Copy pathflake.nix
File metadata and controls
173 lines (150 loc) · 4.95 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
{
description = "ProtonFetcher - Reproducible Python zipapp build environment";
inputs = {
nixpkgs.url = "https://flakehub.com/f/DeterminateSystems/nixpkgs-26.05-chilled/0.1";
pyproject-nix = {
url = "github:pyproject-nix/pyproject.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
uv2nix = {
url = "github:pyproject-nix/uv2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
pyproject-build-systems = {
url = "github:pyproject-nix/build-system-pkgs";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
pyproject-nix,
uv2nix,
pyproject-build-systems,
}: let
# Read version from pyproject.toml as source of truth
version = let
content = builtins.readFile ./pyproject.toml;
lines = builtins.split "\n" content;
filtered = builtins.filter (line: builtins.isString line && (builtins.substring 0 9 line) == "version =") lines;
match = builtins.match ".*version = \"([^\"]+)\".*" (builtins.head filtered);
in
if match != null
then builtins.head match
else throw "Version not found in pyproject.toml";
# Use epoch 1 for maximum determinism (Jan 1, 1970)
epoch = 1;
forAllSystems = nixpkgs.lib.genAttrs ["x86_64-linux" "aarch64-linux"];
# Read Python version from .python-version
pyVerRaw = builtins.replaceStrings ["\n"] [""] (builtins.readFile ./.python-version);
pyVerAttr = "python" + builtins.replaceStrings ["."] [""] pyVerRaw;
mkPkgs = system: import nixpkgs {inherit system;};
python = pkgs: pkgs.${pyVerAttr};
# Load workspace from uv.lock and create overlay for reproducible Python packages
workspace = uv2nix.lib.workspace.loadWorkspace {
workspaceRoot = ./.;
};
projectOverlay = workspace.mkPyprojectOverlay {
sourcePreference = "wheel";
};
mkPythonSet = system: let
pkgs' = mkPkgs system;
in
(pkgs'.callPackage pyproject-nix.build.packages {
python = python pkgs';
}).overrideScope (nixpkgs.lib.composeManyExtensions [
pyproject-build-systems.overlays.wheel
projectOverlay
]);
mkZipapp = system: let
pkgs = mkPkgs system;
py = python pkgs;
pythonSet = mkPythonSet system;
venv = pythonSet.mkVirtualEnv "protonfetcher-env" [];
# Step A: Prepare source with version injection
src = pkgs.stdenvNoCC.mkDerivation {
name = "protonge-fetcher-src";
buildInputs = [pkgs.gnused];
phases = ["installPhase"];
installPhase = ''
mkdir -p $out
cp -r ${./src} staging
chmod -R u+w staging
sed -i 's/^__version__ = .*/__version__ = "${version}"/' \
"staging/protonfetcher/__version__.py"
cp -r staging/* $out/
'';
};
in
pkgs.stdenvNoCC.mkDerivation {
name = "protonfetcher.pyz";
nativeBuildInputs = with pkgs; [
coreutils
findutils
gnused
zip
python3
uv
];
PYTHON = "${py}/bin/python3";
buildPhase = ''
mkdir -p staging
cp -r ${src}/* staging
# Create __main__.py entry point
echo "from entry import main; main()" > staging/__main__.py
# Normalize permissions and timestamps for determinism
chmod -R u+w staging
find staging -exec touch -d "@${builtins.toString epoch}" {} +
# Build deterministic zip: sorted file list, no extra attributes (-X)
(cd staging && find . \( -type d -o -type f \) | LC_ALL=C sort | zip -X -q -@ archive.zip)
# Prepend shebang to create executable pyz
echo '#!/usr/bin/env python3' > $out
cat staging/archive.zip >> $out
chmod +x $out
'';
dontUnpack = true;
dontInstall = true;
};
in {
packages = forAllSystems (system: {
default = mkZipapp system;
});
devShells = forAllSystems (system: let
pkgs = mkPkgs system;
pythonSet = mkPythonSet system;
venv = pythonSet.mkVirtualEnv "protonge-fetcher-dev" [];
in
pkgs.mkShell {
name = "protonge-fetcher";
packages = with pkgs;
[
bashInteractive
coreutils
findutils
ripgrep
jq
less
prettier
rsync
util-linux
uv
which
zip
]
++ [
# Drop-in replacement for `uv run` — deterministic venv from uv2nix
venv
];
shellHook = ''
export PYTHONPATH="${venv}":$PYTHONPATH
echo "ProtonFetcher development environment loaded"
echo "Python: $(${python pkgs}/bin/python3 --version)"
echo ""
echo "Build with: make build (local)"
echo "Nix build: nix build (reproducible)"
'';
VIRTUAL_ENV = "${venv}";
PATH = "${venv}/bin:$PATH";
});
};
}