-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
294 lines (267 loc) · 8.95 KB
/
Copy pathflake.nix
File metadata and controls
294 lines (267 loc) · 8.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
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
{
description = "A Raft-backed database of reactive TypeScript values";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
xmit = {
url = "github:xmit-co/xmit";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
rust-overlay,
xmit,
}:
let
supportedSystems = [
"aarch64-darwin"
"x86_64-darwin"
"aarch64-linux"
"x86_64-linux"
];
# Pin the current stable toolchain independently of the Nixpkgs release
# so local and CI builds use the same compiler.
rustVersion = "1.98.1";
overlays = [ rust-overlay.overlays.default ];
# The QuickJS guest is built with this exact WASI SDK release so that
# vendor/quickjs-ng/build.py can reproduce the checked-in quickjs.wasm
# byte for byte. Digests are the release's published asset digests.
wasiSdkVersion = "34.0";
wasiSdkArchives = {
aarch64-darwin = {
platform = "arm64-macos";
sha256 = "9c59398106b417f8f14913380fdf0097a8cc0ff4af9eb3ce0065a859e88d49e9";
};
x86_64-darwin = {
platform = "x86_64-macos";
sha256 = "87d27fa8adc68dee59bfbf2e22a6d34ef717c34d6bf1d8af2a56fc929d9ce0eb";
};
aarch64-linux = {
platform = "arm64-linux";
sha256 = "f7e243dff54d60bcc576e94d6166b69f410f2500ae4a9ceef34315be10e77971";
};
x86_64-linux = {
platform = "x86_64-linux";
sha256 = "b761e3a0721dbae9c09a0059e5fdb2bf917d1b4a8a7b430fb3b5aafb0984b2c4";
};
};
forAllSystems =
f:
nixpkgs.lib.genAttrs supportedSystems (
system:
f (
import nixpkgs {
inherit system;
inherit overlays;
config.allowDeprecatedx86_64Darwin = true;
}
)
);
mkWasiSdk =
pkgs:
let
archive = wasiSdkArchives.${pkgs.stdenv.hostPlatform.system};
name = "wasi-sdk-${wasiSdkVersion}-${archive.platform}";
in
pkgs.stdenvNoCC.mkDerivation {
pname = "wasi-sdk";
version = wasiSdkVersion;
src = pkgs.fetchurl {
url = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${pkgs.lib.versions.major wasiSdkVersion}/${name}.tar.gz";
inherit (archive) sha256;
};
# Prebuilt Linux binaries need their interpreter and libraries patched.
nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.autoPatchelfHook
];
buildInputs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.stdenv.cc.cc.lib
pkgs.ncurses
pkgs.zlib
];
dontConfigure = true;
dontBuild = true;
# Keep the toolchain exactly as released, apart from ELF patching.
dontStrip = true;
installPhase = ''
mkdir -p $out
cp -R . $out
'';
meta.platforms = supportedSystems;
};
lib = nixpkgs.lib;
version = (lib.importTOML ./Cargo.toml).package.version;
# Only what each build reads, so edits to docs, benchmarks or the other
# half of the repository don't rebuild it.
serverSrc = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./src
# Workspace members: Cargo reads their manifests to resolve the lockfile.
./crates
./examples/goblin-pizza-rs
./vendor/openraft
./vendor/quickjs-ng/quickjs.wasm
./runtime/engine.js
];
};
sdkSrc = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./package.json
./package-lock.json
./tsconfig.json
./tsconfig.build.json
./scripts/build-sdk.mjs
./sdk
./runtime/engine.js
./README.md
./LICENSE-MIT
];
};
commonMeta = pkgs: {
homepage = "https://github.com/xmit-dev/flower";
license = pkgs.lib.licenses.mit;
platforms = supportedSystems;
};
mkFlower =
pkgs:
let
rustToolchain = pkgs.rust-bin.stable.${rustVersion}.default;
in
(pkgs.rustPlatform.buildRustPackage.override {
cargo = rustToolchain;
rustc = rustToolchain;
})
{
pname = "flower";
inherit version;
src = serverSrc;
cargoLock.lockFile = ./Cargo.lock;
cargoBuildFlags = [
"--bin"
"flower"
];
# The Rust suite (cargo test, tests/*.rs) runs from a checkout; some of
# its timing-sensitive cases would make the package flaky to build.
doCheck = false;
meta = commonMeta pkgs // {
description = "A Raft-backed database of reactive TypeScript values";
mainProgram = "flower";
};
};
# @flower-js/sdk as npm would install it: dist/, package.json and its
# runtime dependency (esbuild), under lib/node_modules/@flower-js/sdk.
# bin/flower is the SDK's command (build, deploy, init, call…), not the server.
mkSdk =
pkgs:
pkgs.buildNpmPackage {
pname = "flower-sdk";
inherit version;
src = sdkSrc;
nodejs = pkgs.nodejs_26;
# Straight from package-lock.json's integrity hashes: no npmDepsHash to update.
npmDeps = pkgs.importNpmLock {
package = lib.importJSON ./package.json;
packageLock = lib.importJSON ./package-lock.json;
};
inherit (pkgs.importNpmLock) npmConfigHook;
meta = commonMeta pkgs // {
description = "TypeScript SDK and command for the Flower reactive database";
mainProgram = "flower";
};
};
in
{
packages = forAllSystems (
pkgs:
let
flower = mkFlower pkgs;
in
{
inherit flower;
default = flower;
sdk = mkSdk pkgs;
wasi-sdk = mkWasiSdk pkgs;
}
);
apps = forAllSystems (
pkgs:
let
packages = self.packages.${pkgs.stdenv.hostPlatform.system};
server = {
type = "app";
program = "${packages.flower}/bin/flower";
meta.description = "The Flower server";
};
in
{
default = server;
flower = server;
cli = {
type = "app";
program = "${packages.sdk}/bin/flower";
meta.description = "The SDK's flower command: build, deploy, init, call, query, watch, keys";
};
}
);
# pkgs.flower and pkgs.flower-sdk, built with this flake's pinned Nixpkgs and Rust.
overlays.default = final: _prev: {
flower = self.packages.${final.stdenv.hostPlatform.system}.flower;
flower-sdk = self.packages.${final.stdenv.hostPlatform.system}.sdk;
};
# services.flower.instances.<name>: Flower nodes under systemd. See nix/module.nix.
nixosModules = {
# A key, so importing it twice (say, from Trinity's module and a host's config) declares it once.
flower = {
key = "github:xmit-dev/flower#nixosModules.flower";
imports = [ (import ./nix/module.nix self) ];
};
default = self.nixosModules.flower;
};
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
name = "flower-dev";
packages = [
# Rust guest modules (crates/, examples/goblin-pizza-rs) build for wasm32.
(pkgs.rust-bin.stable.${rustVersion}.default.override {
targets = [ "wasm32-unknown-unknown" ];
})
pkgs.nodejs_26
pkgs.stdenv.cc
xmit.packages.${pkgs.stdenv.hostPlatform.system}.default
];
# Selected by vendor/quickjs-ng/build.py; ordinary Cargo builds don't need it.
FLOWER_WASI_SDK = mkWasiSdk pkgs;
};
});
checks = forAllSystems (
pkgs:
let
packages = self.packages.${pkgs.stdenv.hostPlatform.system};
in
{
inherit (packages) flower sdk;
}
// lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
# A NixOS VM: the module bootstraps a node, the SDK deploys an example to it.
nixos = import ./nix/test.nix {
inherit pkgs;
module = self.nixosModules.flower;
inherit (packages) sdk;
example = ./examples/orders.ts;
exampleArgs = ./examples/orders.create.json;
};
}
);
formatter = forAllSystems (pkgs: pkgs.nixfmt);
};
}