-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflake.nix
More file actions
195 lines (172 loc) · 7.25 KB
/
flake.nix
File metadata and controls
195 lines (172 loc) · 7.25 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
{
description = "An extra set of tools for managing Supabase projects going beyond the possibilities of regular Supabase CLI";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-25.11";
};
nixConfig = {
extra-substituters = [
"https://dsplce-co.cachix.org"
];
extra-trusted-public-keys = [
"dsplce-co.cachix.org-1:OjNARJ8rPKKLSlAz/zq/Ml3C9VnvrqDWU20f/4HzcXU="
];
};
outputs = {
self,
nixpkgs,
}: let
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
forAllSystems = for: nixpkgs.lib.genAttrs systems (system: for system);
in {
packages = forAllSystems (
system: let
pkgs = import nixpkgs {
inherit system;
};
cargoManifest = fromTOML (builtins.readFile ./Cargo.toml);
cargoExcludes = cargoManifest.package.exclude or [];
globToRegex = glob: let
lib = pkgs.lib;
g0 =
if lib.hasSuffix "/" glob
then glob + "**"
else glob;
escaped = lib.replaceStrings
["\\" "." "+" "(" ")" "|" "^" "$" "{" "}" "[" "]"]
["\\\\" "\\." "\\+" "\\(" "\\)" "\\|" "\\^" "\\$" "\\{" "\\}" "\\[" "\\]"]
g0;
r1 = lib.replaceStrings ["**"] [".*"] escaped;
r2 = lib.replaceStrings ["*"] ["[^/]*"] r1;
r3 = lib.replaceStrings ["?"] ["[^/]"] r2;
in
"^" + r3 + "$";
matchesAnyExclude = relPath:
builtins.any (
pat: let
re = globToRegex pat;
in
builtins.match re relPath != null
)
cargoExcludes;
root = toString ./.;
mkPatchedCratesStager = {
cargoToml,
crateHashes ? {},
patchGlob ? "*.patch",
}: let
cargoManifest = fromTOML (builtins.readFile cargoToml);
patchedCrateNames =
(((cargoManifest.package or {}).metadata or {}).patch or {}).crates or [];
dependencyDefinitions = cargoManifest.dependencies or {};
dependencyVersionFor = crateName: let
dependencyRaw =
if dependencyDefinitions ? ${crateName}
then dependencyDefinitions.${crateName}
else null;
in
if builtins.isString dependencyRaw
then dependencyRaw
else dependencyRaw.version;
patchDestinationPathFor = crateName:
(cargoManifest.patch."crates-io".${crateName}).path;
fetchUpstreamSourceFor = crateName:
pkgs.fetchCrate {
pname = crateName;
version = dependencyVersionFor crateName;
hash = crateHashes.${crateName} or pkgs.lib.fakeHash;
};
mkPatchedSourceFor = crateName: let
upstreamSource = fetchUpstreamSourceFor crateName;
patchDirectory = patchDestinationPathFor crateName;
in
pkgs.stdenvNoCC.mkDerivation {
name = "patched-${crateName}";
nativeBuildInputs = [pkgs.git];
unpackPhase = ''
cp -R ${upstreamSource}/* .
chmod -R u+w .
'';
buildPhase = ''
shopt -s nullglob
for patchFile in ${patchDirectory}/${patchGlob}; do
git apply --unsafe-paths "$patchFile"
done
'';
installPhase = ''
mkdir -p $out
cp -R . $out/
'';
};
patchedCrates = builtins.listToAttrs
(map (crateName: {
name = crateName;
value = mkPatchedSourceFor crateName;
})
patchedCrateNames);
stageHook = pkgs.lib.concatStringsSep "\n"
(map (
crateName: let
destinationPath = patchDestinationPathFor crateName;
in ''
rm -rf "${destinationPath}"
mkdir -p "$(dirname "${destinationPath}")"
cp -R "${patchedCrates.${crateName}}" "${destinationPath}"
''
)
patchedCrateNames);
in {
inherit stageHook;
};
patched = mkPatchedCratesStager {
cargoToml = ./Cargo.toml;
crateHashes = {
"promptuity" = "sha256-385Oe4S0Kqo/xAbE7D9DmVIyKdDUQ0E72TfgU20JJns=";
"throbberous" = "sha256-THloggmLgP/UXkrrgfxF8BgHYNa14gsG2updbbUK+V0=";
};
};
in {
default = pkgs.rustPlatform.buildRustPackage {
pname = "sbp";
version = cargoManifest.package.version;
src = pkgs.lib.cleanSourceWith {
src = ./.;
filter = path: type: let
p = toString path;
rel =
if pkgs.lib.hasPrefix (root + "/") p
then pkgs.lib.removePrefix (root + "/") p
else p;
in
!(matchesAnyExclude rel);
};
doCheck = false;
nativeBuildInputs = [pkgs.rustc pkgs.cargo];
cargoLock.lockFile = ./Cargo.lock;
buildPhase = ''
${patched.stageHook}
cargo build --release
'';
installPhase = ''
mkdir -p $out/bin
cp -r target/release/sbp $out/bin
'';
};
}
);
devShells = forAllSystems (
system: let
pkgs = import nixpkgs {
inherit system;
};
in {
default = pkgs.mkShell {
packages = [
pkgs.supabase-cli
pkgs.rustc
pkgs.cargo
];
};
}
);
};
}