Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
231 lines (207 loc) · 7.98 KB
/
Copy pathflake.nix
File metadata and controls
231 lines (207 loc) · 7.98 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
{
description = "akl.dev landing page";
nixConfig = {
extra-substituters = [ "https://devenv.cachix.org" ];
extra-trusted-public-keys = [ "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=" ];
};
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
devenv = {
url = "github:cachix/devenv";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
inputs@{
self,
flake-parts,
devenv,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
imports = [
inputs.treefmt-nix.flakeModule
./nix/treefmt.nix
];
perSystem =
{
pkgs,
lib,
self',
config,
...
}:
let
treefmtWrapper = config.treefmt.build.wrapper;
node = pkgs.nodejs_26;
in
{
packages.default = pkgs.buildNpmPackage {
pname = "akl-dev";
version = "0.1.0";
src = ./.;
# importNpmLock derives the fixed-output derivations directly
# from package-lock.json, so no npmDepsHash is needed.
npmDeps = pkgs.importNpmLock { npmRoot = ./.; };
npmConfigHook = pkgs.importNpmLock.npmConfigHook;
nodejs = node;
env = {
ASTRO_TELEMETRY_DISABLED = "1";
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
# Colophon.astro slices this to 7 chars; empty falls back to "dev".
CF_PAGES_COMMIT_SHA = self.rev or self.dirtyRev or "";
};
installPhase = ''
runHook preInstall
cp -r dist $out
runHook postInstall
'';
};
apps =
let
# Deploy straight to Cloudflare Pages from a workstation, bypassing
# the Pages Git integration: nix build -> dereference -> upload.
deploy = pkgs.writeShellApplication {
name = "deploy";
runtimeInputs = [
pkgs.wrangler
pkgs.git
];
text = ''
export WRANGLER_SEND_METRICS=false
src="''${1:-}"
if [ -z "$src" ]; then
if [ -e result ]; then
src=result
elif [ -d dist ]; then
src=dist
else
echo "error: no build found. Run 'nix build' (preferred — equivalent" >&2
echo " to the Pages build: same lockfile and sources, though Pages" >&2
echo " runs 'npm run build' on its own image) or 'npm run build'." >&2
exit 1
fi
fi
[ -f "$src/index.html" ] || { echo "error: $src/index.html not found" >&2; exit 1; }
: "''${CLOUDFLARE_API_TOKEN:?set CLOUDFLARE_API_TOKEN (a token with the Cloudflare Pages:Edit permission)}"
: "''${CLOUDFLARE_ACCOUNT_ID:?set CLOUDFLARE_ACCOUNT_ID}"
: "''${CLOUDFLARE_PROJECT_NAME:?set CLOUDFLARE_PROJECT_NAME (the Pages project name)}"
# result/ is a symlink into the read-only nix store; wrangler wants
# a real, writable tree. -L dereferences it.
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cp -rL "$src" "$work/site"
chmod -R u+w "$work/site"
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo detached)
commit=$(git rev-parse HEAD 2>/dev/null || echo unknown)
dirty=false
if ! git diff --quiet HEAD 2>/dev/null; then dirty=true; fi
echo "deploying $src -> Pages project $CLOUDFLARE_PROJECT_NAME (branch $branch, dirty=$dirty)"
wrangler pages deploy "$work/site" \
--project-name "$CLOUDFLARE_PROJECT_NAME" \
--branch "$branch" \
--commit-hash "$commit" \
--commit-dirty "$dirty"
'';
};
in
{
deploy = {
type = "app";
program = "${deploy}/bin/deploy";
};
};
devShells.default = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [
(
{ pkgs, ... }:
{
languages.javascript = {
enable = true;
package = node;
npm.enable = true;
npm.install.enable = true;
};
# treefmt is the single formatting authority -- it already
# knows which files ship verbatim and must not be touched.
git-hooks.hooks = {
treefmt = {
enable = true;
package = treefmtWrapper;
};
};
scripts.dev.exec = "npm run dev";
scripts.build.exec = "npm run build";
scripts.preview.exec = "npm run preview";
scripts.verify.exec = "npm run verify";
env = {
# chromium + chromium_headless_shell only. Do NOT use playwright-driver.browsers-chromium: it omits
# the headless shell that the default headless chromium.launch() needs.
PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers.override {
withFirefox = false;
withWebkit = false;
}}";
PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = "true";
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
ASTRO_TELEMETRY_DISABLED = "1";
};
enterShell = ''
echo "akl.dev dev environment"
echo " dev - start the Astro dev server (npm run dev)"
echo " build - build the site to dist/ (npm run build)"
echo " verify - run the acceptance suite (npm run verify)"
echo " nix flake check --impure - build, astro check, treefmt"
'';
}
)
];
};
checks.build = self'.packages.default;
checks.playwright-pin =
let
npmPin = (builtins.fromJSON (builtins.readFile ./package.json)).devDependencies.playwright;
in
assert lib.assertMsg (npmPin == pkgs.playwright-driver.version)
"package.json playwright (${npmPin}) != nixpkgs playwright-driver (${pkgs.playwright-driver.version}); see docs/runbook.md §9";
pkgs.runCommand "playwright-pin" { } "touch $out";
checks.astro-check = pkgs.buildNpmPackage {
pname = "akl-dev-check";
version = "0.1.0";
src = ./.;
npmDeps = pkgs.importNpmLock { npmRoot = ./.; };
npmConfigHook = pkgs.importNpmLock.npmConfigHook;
# Same node as packages.default.
nodejs = node;
env = {
ASTRO_TELEMETRY_DISABLED = "1";
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
};
buildPhase = ''
runHook preBuild
npm run check
runHook postBuild
'';
installPhase = ''
runHook preInstall
touch $out
runHook postInstall
'';
};
};
};
}