-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathflake.nix
More file actions
255 lines (215 loc) · 8.69 KB
/
Copy pathflake.nix
File metadata and controls
255 lines (215 loc) · 8.69 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
{
description = "ReMemory - a digital safe with multiple keys, held by people you trust";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
playwright.url = "github:pietdevries94/playwright-web-flake";
playwright.inputs.nixpkgs.follows = "nixpkgs";
playwright.inputs.flake-utils.follows = "flake-utils";
};
outputs = { self, nixpkgs, flake-utils, playwright }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
(final: prev: {
inherit (playwright.packages.${system}) playwright-test playwright-driver;
})
];
};
versionFile = builtins.replaceStrings ["\n"] [""] (builtins.readFile ./VERSION);
npmDeps = pkgs.fetchNpmDeps {
src = ./.;
hash = "sha256-E7xqvOiSfz/sAhK4cYGEi3qUm7YJ9Bk2zUtSOp4615Y=";
};
# Build TypeScript + WASM assets with the native toolchain. These are
# architecture-independent and can be reused across cross builds.
# Uses buildGoModule so Go dependencies are available in the sandbox.
wasmAssets = pkgs.buildGoModule {
pname = "rememory-wasm-assets";
version = versionFile;
src = ./.;
vendorHash = "sha256-XR3XoBcyEou/L3QXGBUl8IS1u0lpEBS+GTrD574OWh8=";
proxyVendor = true;
overrideModAttrs = old: {
preBuild = null;
};
nativeBuildInputs = [ pkgs.esbuild pkgs.gnumake pkgs.nodejs pkgs.cacert ];
inherit npmDeps;
# Patch go.mod to match nixpkgs Go version (nixpkgs may lag behind)
prePatch = ''
sed -i "s/^go .*/go ${pkgs.go.version}/" go.mod
'';
# Build only the WASM and TypeScript — skip the native Go binary
preBuild = ''
export HOME=$TMPDIR
npm config set cache "$npmDeps"
npm ci --ignore-scripts --prefer-offline
rm -f node_modules/.bin/esbuild
export PATH="$PWD/node_modules/.bin:$PATH"
make wasm
'';
# Skip the normal Go build — we only want the WASM/JS artifacts
buildPhase = ''
runHook preBuild
'';
installPhase = ''
mkdir -p $out
cp internal/html/assets/*.js internal/html/assets/*.wasm $out/
'';
};
# Shared builder for the rememory binary. Accepts a target Go package set
# (for cross-compilation) and optional pre-built WASM assets to avoid
# rebuilding them with a cross compiler that can't target js/wasm.
mkRememory = { goPkgs ? pkgs, enableManPages ? true, prebuiltAssets ? null }:
goPkgs.buildGoModule {
pname = "rememory";
version = versionFile;
src = ./.;
vendorHash = "sha256-XR3XoBcyEou/L3QXGBUl8IS1u0lpEBS+GTrD574OWh8=";
proxyVendor = true; # Download deps during build instead of vendoring
# The go-modules derivation only fetches Go deps — skip TS/WASM build there
overrideModAttrs = old: {
preBuild = null;
};
nativeBuildInputs = [ pkgs.esbuild pkgs.gnumake pkgs.nodejs pkgs.cacert ];
inherit npmDeps;
# Patch go.mod to match nixpkgs Go version (nixpkgs may lag behind)
prePatch = ''
sed -i "s/^go .*/go ${goPkgs.go.version}/" go.mod
'';
# Install npm deps and build TypeScript + WASM.
# For cross builds, pre-built assets are copied in instead of running
# `make wasm`, because the cross Go linker can't target js/wasm.
preBuild = if prebuiltAssets != null then ''
cp ${prebuiltAssets}/*.js internal/html/assets/
cp ${prebuiltAssets}/*.wasm internal/html/assets/
'' else ''
export HOME=$TMPDIR
npm config set cache "$npmDeps"
npm ci --ignore-scripts --prefer-offline
# Remove broken esbuild from node_modules — npm ci --ignore-scripts
# skips the postinstall that downloads the platform binary, leaving a
# broken wrapper that shadows the working esbuild from nativeBuildInputs.
rm -f node_modules/.bin/esbuild
export PATH="$PWD/node_modules/.bin:$PATH"
make wasm
'';
# Generate and install man pages (only for native builds —
# cross-compiled binaries can't run on the build host)
postInstall = pkgs.lib.optionalString enableManPages ''
mkdir -p $out/share/man/man1
$out/bin/rememory doc $out/share/man/man1
'';
subPackages = [ "cmd/rememory" ];
ldflags = [ "-s" "-w" "-X main.version=${versionFile}" ];
};
rememory = mkRememory { };
mkDockerImage = { rememoryPkg, tag ? "latest", arch ? null }:
pkgs.dockerTools.buildImage ({
name = "rememory";
inherit tag;
copyToRoot = pkgs.buildEnv {
name = "rememory-root";
paths = [
rememoryPkg
pkgs.dockerTools.fakeNss
];
};
runAsRoot = ''
mkdir -p /data
chown 65534:65534 /data
'';
config = {
Cmd = [ "${rememoryPkg}/bin/rememory" "serve" "--host" "0.0.0.0" "--port" "8080" "--data" "/data" ];
ExposedPorts = { "8080/tcp" = { }; };
Volumes = { "/data" = { }; };
User = "65534:65534";
};
} // pkgs.lib.optionalAttrs (arch != null) { architecture = arch; });
# Cross-compiled arm64 packages (only available on x86_64-linux,
# where CI runs — used to produce multi-arch Docker images)
crossPackages = pkgs.lib.optionalAttrs (system == "x86_64-linux") (
let
pkgsCrossArm64 = import nixpkgs {
localSystem = "x86_64-linux";
crossSystem = "aarch64-linux";
};
rememory-arm64 = mkRememory {
goPkgs = pkgsCrossArm64;
enableManPages = false;
prebuiltAssets = wasmAssets;
};
in {
rememory-arm64 = rememory-arm64;
docker-arm64 = mkDockerImage {
rememoryPkg = rememory-arm64;
tag = "latest-arm64";
arch = "arm64";
};
}
);
in
{
packages = {
rememory = rememory;
default = rememory;
docker = mkDockerImage { rememoryPkg = rememory; };
e2e-tests = pkgs.buildNpmPackage {
pname = "rememory-e2e";
version = "1.0.0";
src = ./.;
npmDepsHash = pkgs.lib.fakeHash; # Update after first build
nativeBuildInputs = [
rememory
pkgs.playwright-test
pkgs.playwright-driver
];
env = {
PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers}";
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
};
dontNpmBuild = true;
buildPhase = ''
# Remove npm-installed playwright to avoid conflicts
rm -rf node_modules/@playwright node_modules/.bin/playwright 2>/dev/null || true
mkdir -p node_modules/.bin
ln -s ${pkgs.playwright-test}/bin/playwright node_modules/.bin/playwright
# Create test fixtures
ln -s ${rememory}/bin/rememory rememory
echo "Running Playwright E2E tests..."
${pkgs.playwright-test}/bin/playwright test
'';
installPhase = ''
mkdir -p $out
if [ -d e2e/playwright-report ]; then
cp -r e2e/playwright-report/* $out/
fi
'';
};
} // crossPackages;
apps = {
rememory = flake-utils.lib.mkApp { drv = rememory; };
default = flake-utils.lib.mkApp { drv = rememory; };
};
checks = {
go-tests = rememory;
# e2e-tests = self.packages.${system}.e2e-tests; # Enable after setup
};
devShells.default = pkgs.mkShell {
packages = [
pkgs.go
pkgs.nodejs
pkgs.esbuild
pkgs.playwright-test
pkgs.poppler-utils
];
shellHook = ''
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
export PLAYWRIGHT_BROWSERS_PATH="${pkgs.playwright-driver.browsers}"
'';
};
}
);
}