Skip to content

Commit 9fcae88

Browse files
committed
feat: add nix flake
1 parent 1d7c35c commit 9fcae88

2 files changed

Lines changed: 207 additions & 0 deletions

File tree

flake.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
{
2+
description = "An extra set of tools for managing Supabase projects going beyond the possibilities of regular Supabase CLI";
3+
4+
inputs = {
5+
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
6+
};
7+
8+
outputs = {
9+
self,
10+
nixpkgs,
11+
}: let
12+
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
13+
forAllSystems = for: nixpkgs.lib.genAttrs systems (system: for system);
14+
in {
15+
packages = forAllSystems (
16+
system: let
17+
pkgs = import nixpkgs {
18+
inherit system;
19+
};
20+
21+
cargoManifest = fromTOML (builtins.readFile ./Cargo.toml);
22+
cargoExcludes = cargoManifest.package.exclude or [];
23+
24+
globToRegex = glob: let
25+
lib = pkgs.lib;
26+
27+
g0 =
28+
if lib.hasSuffix "/" glob
29+
then glob + "**"
30+
else glob;
31+
32+
escaped = lib.replaceStrings
33+
["\\" "." "+" "(" ")" "|" "^" "$" "{" "}" "[" "]"]
34+
["\\\\" "\\." "\\+" "\\(" "\\)" "\\|" "\\^" "\\$" "\\{" "\\}" "\\[" "\\]"]
35+
g0;
36+
37+
r1 = lib.replaceStrings ["**"] [".*"] escaped;
38+
r2 = lib.replaceStrings ["*"] ["[^/]*"] r1;
39+
r3 = lib.replaceStrings ["?"] ["[^/]"] r2;
40+
in
41+
"^" + r3 + "$";
42+
43+
matchesAnyExclude = relPath:
44+
builtins.any (
45+
pat: let
46+
re = globToRegex pat;
47+
in
48+
builtins.match re relPath != null
49+
)
50+
cargoExcludes;
51+
52+
root = toString ./.;
53+
54+
mkPatchedCratesStager = {
55+
cargoToml,
56+
crateHashes ? {},
57+
patchGlob ? "*.patch",
58+
}: let
59+
cargoManifest = fromTOML (builtins.readFile cargoToml);
60+
patchedCrateNames =
61+
(((cargoManifest.package or {}).metadata or {}).patch or {}).crates or [];
62+
dependencyDefinitions = cargoManifest.dependencies or {};
63+
64+
dependencyVersionFor = crateName: let
65+
dependencyRaw =
66+
if dependencyDefinitions ? ${crateName}
67+
then dependencyDefinitions.${crateName}
68+
else null;
69+
in
70+
if builtins.isString dependencyRaw
71+
then dependencyRaw
72+
else dependencyRaw.version;
73+
74+
patchDestinationPathFor = crateName:
75+
(cargoManifest.patch."crates-io".${crateName}).path;
76+
77+
fetchUpstreamSourceFor = crateName:
78+
pkgs.fetchCrate {
79+
pname = crateName;
80+
version = dependencyVersionFor crateName;
81+
hash = crateHashes.${crateName} or pkgs.lib.fakeHash;
82+
};
83+
84+
mkPatchedSourceFor = crateName: let
85+
upstreamSource = fetchUpstreamSourceFor crateName;
86+
patchDirectory = patchDestinationPathFor crateName;
87+
in
88+
pkgs.stdenvNoCC.mkDerivation {
89+
name = "patched-${crateName}";
90+
nativeBuildInputs = [pkgs.git];
91+
unpackPhase = ''
92+
cp -R ${upstreamSource}/* .
93+
chmod -R u+w .
94+
'';
95+
buildPhase = ''
96+
shopt -s nullglob
97+
for patchFile in ${patchDirectory}/${patchGlob}; do
98+
git apply --unsafe-paths "$patchFile"
99+
done
100+
'';
101+
installPhase = ''
102+
mkdir -p $out
103+
cp -R . $out/
104+
'';
105+
};
106+
107+
patchedCrates = builtins.listToAttrs
108+
(map (crateName: {
109+
name = crateName;
110+
value = mkPatchedSourceFor crateName;
111+
})
112+
patchedCrateNames);
113+
114+
stageHook = pkgs.lib.concatStringsSep "\n"
115+
(map (
116+
crateName: let
117+
destinationPath = patchDestinationPathFor crateName;
118+
in ''
119+
rm -rf "${destinationPath}"
120+
mkdir -p "$(dirname "${destinationPath}")"
121+
cp -R "${patchedCrates.${crateName}}" "${destinationPath}"
122+
''
123+
)
124+
patchedCrateNames);
125+
in {
126+
inherit stageHook;
127+
};
128+
129+
patched = mkPatchedCratesStager {
130+
cargoToml = ./Cargo.toml;
131+
crateHashes = {
132+
"promptuity" = "sha256-385Oe4S0Kqo/xAbE7D9DmVIyKdDUQ0E72TfgU20JJns=";
133+
"throbberous" = "sha256-THloggmLgP/UXkrrgfxF8BgHYNa14gsG2updbbUK+V0=";
134+
};
135+
};
136+
in {
137+
default = pkgs.rustPlatform.buildRustPackage {
138+
pname = "sbp";
139+
version = cargoManifest.package.version;
140+
src = pkgs.lib.cleanSourceWith {
141+
src = ./.;
142+
filter = path: type: let
143+
p = toString path;
144+
rel =
145+
if pkgs.lib.hasPrefix (root + "/") p
146+
then pkgs.lib.removePrefix (root + "/") p
147+
else p;
148+
in
149+
!(matchesAnyExclude rel);
150+
};
151+
152+
nativeBuildInputs = [pkgs.rustc pkgs.cargo];
153+
cargoLock.lockFile = ./Cargo.lock;
154+
155+
buildPhase = ''
156+
${patched.stageHook}
157+
cargo build --release
158+
'';
159+
160+
installPhase = ''
161+
mkdir -p $out/bin
162+
cp -r target/release/sbp $out/bin
163+
'';
164+
};
165+
}
166+
);
167+
168+
devShells = forAllSystems (
169+
system: let
170+
pkgs = import nixpkgs {
171+
inherit system;
172+
};
173+
in {
174+
default = pkgs.mkShell {
175+
packages = [];
176+
};
177+
}
178+
);
179+
};
180+
}

0 commit comments

Comments
 (0)