-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflake.nix
More file actions
347 lines (309 loc) · 13.1 KB
/
flake.nix
File metadata and controls
347 lines (309 loc) · 13.1 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
{
description = "Rust project";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
nixpkgs.url = "nixpkgs/nixos-unstable";
nitro-util = {
url = "github:monzo/aws-nitro-util/7d755578b0b0b9850c0d7c4738a6c8daf3ff55c0";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay, nitro-util }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ rust-overlay.overlays.default ];
pkgs = import nixpkgs { inherit system overlays; };
rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
nitro = nitro-util.lib.${system};
# Development environment setup
# Get rust-analyzer matching the channel in rust-toolchain.toml
rustToolchain = builtins.fromTOML (builtins.readFile ./rust-toolchain.toml);
rustChannel = rustToolchain.toolchain.channel;
rustAnalyzer = pkgs.rust-bin.stable."${rustChannel}".rust-analyzer;
commonInputs = [
rust
rustAnalyzer
pkgs.pkg-config
pkgs.openssl
pkgs.zlib
pkgs.gcc
pkgs.clang
pkgs.jq
pkgs.just
pkgs.postgresql
pkgs.diesel-cli
pkgs.python3
(pkgs.python3.withPackages (ps: with ps; [
cryptography
]))
pkgs.go
];
linuxOnlyInputs = [
pkgs.podman
pkgs.conmon
pkgs.slirp4netns
pkgs.fuse-overlayfs
];
darwinOnlyInputs = [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
];
inputs = commonInputs
++ pkgs.lib.optionals pkgs.stdenv.isLinux linuxOnlyInputs
++ pkgs.lib.optionals pkgs.stdenv.isDarwin darwinOnlyInputs;
setupPostgresScript = pkgs.writeShellScript "setup-postgres" ''
export PGDATA="$PWD/.pgdata"
export PGPORT=5432
export PGSOCKETS="$PWD/.pgdata/sockets"
# Skip if Postgres is already running
if ${pkgs.postgresql}/bin/pg_isready -h localhost -p $PGPORT >/dev/null 2>&1; then
exit 0
fi
# Initialize if needed
if [ ! -f "$PGDATA/PG_VERSION" ]; then
${pkgs.postgresql}/bin/initdb -D "$PGDATA"
fi
# Ensure socket directory exists
mkdir -p "$PGSOCKETS"
# Start Postgres
${pkgs.postgresql}/bin/pg_ctl start -D "$PGDATA" -o "-h localhost -p $PGPORT -k $PGSOCKETS" -l "$PGDATA/logfile" -w
# Wait for it to be ready
until ${pkgs.postgresql}/bin/pg_isready -h localhost -p $PGPORT >/dev/null 2>&1; do sleep 0.5; done
# Create user and database if they don't exist
${pkgs.postgresql}/bin/psql -h localhost -p $PGPORT -tc "SELECT 1 FROM pg_roles WHERE rolname='opensecret_user'" postgres 2>/dev/null | grep -q 1 || \
${pkgs.postgresql}/bin/psql -h localhost -p $PGPORT -c "CREATE USER \"opensecret_user\" WITH PASSWORD 'password';" postgres
${pkgs.postgresql}/bin/psql -h localhost -p $PGPORT -tc "SELECT 1 FROM pg_database WHERE datname='opensecret'" postgres 2>/dev/null | grep -q 1 || \
${pkgs.postgresql}/bin/psql -h localhost -p $PGPORT -c "CREATE DATABASE \"opensecret\" OWNER \"opensecret_user\";" postgres
'';
setupEnvScript = pkgs.writeShellScript "setup-env" ''
if [ ! -f .env ]; then
cp .env.sample .env
sed -i 's|DATABASE_URL=postgres://localhost/opensecret|DATABASE_URL=postgres://opensecret_user:password@localhost:5432/opensecret|g' .env
# Get a new ENCLAVE_SECRET_MOCK value using openssl
export enclaveSecret=$(openssl rand -hex 32)
sed -i "s|ENCLAVE_SECRET_MOCK=|ENCLAVE_SECRET_MOCK=$enclaveSecret|g" .env
# Get a new JWT_SECRET value using openssl
export jwtSecret=$(openssl rand -base64 32)
sed -i "s|JWT_SECRET=|JWT_SECRET=$jwtSecret|g" .env
fi
'';
# Function to create rootfs with specific APP_MODE
mkRootfs = appMode: pkgs.buildEnv {
name = "opensecret-rootfs-${appMode}";
paths = [
opensecret
(pkgs.writeScriptBin "entrypoint" ''
#!${pkgs.bash}/bin/bash
# Set up busybox commands and other tools
export PATH="/bin:${pkgs.busybox}/bin:${pkgs.python3}/bin:${pkgs.jq}/bin:${pkgs.socat}/bin:${nitro-bins}/bin:$PATH"
# Create symlinks for busybox commands
mkdir -p /bin
ln -sf ${pkgs.busybox}/bin/busybox /bin/date
ln -sf ${pkgs.busybox}/bin/busybox /bin/ip
ln -sf ${pkgs.python3}/bin/python3 /bin/python3
ln -sf ${pkgs.jq}/bin/jq /bin/jq
ln -sf ${pkgs.socat}/bin/socat /bin/socat
ln -sf ${pkgs.curl}/bin/curl /bin/curl
# Set up CA certificates
mkdir -p /etc/ssl/certs
ln -sf ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt /etc/ssl/certs/ca-bundle.crt
export SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
export AWS_CA_BUNDLE=/etc/ssl/certs/ca-bundle.crt
# Copy required libraries and tools
mkdir -p /lib
export LD_LIBRARY_PATH="/lib:$LD_LIBRARY_PATH"
install -m 755 ${nitro-bins}/lib/libnsm.so /lib/
install -m 755 ${nitro-bins}/bin/kmstool_enclave_cli /bin/
# Copy required C libraries
cp -P ${pkgs.glibc}/lib/ld-linux*.so* /lib/
cp -P ${pkgs.glibc}/lib/libc.so* /lib/
cp -P ${pkgs.glibc}/lib/libdl.so* /lib/
cp -P ${pkgs.glibc}/lib/libpthread.so* /lib/
cp -P ${pkgs.glibc}/lib/librt.so* /lib/
cp -P ${pkgs.glibc}/lib/libm.so* /lib/
# Set up Python environment
export PYTHONPATH="$(find ${pkgs.python3}/lib -name site-packages):$PYTHONPATH"
# Copy opensecret and continuum-proxy to their locations
mkdir -p /app
install -m 755 ${opensecret}/bin/opensecret /app/
install -m 755 ${continuum-proxy}/bin/continuum-proxy /app/
install -m 755 ${tinfoil-proxy}/bin/tinfoil-proxy /app/
${builtins.readFile ./entrypoint.sh}
'')
(pkgs.writeTextFile {
name = "app-mode";
text = builtins.trace "Creating APP_MODE file with value: ${appMode}" appMode;
destination = "/app/APP_MODE";
})
(pkgs.writeTextFile {
name = "traffic_forwarder";
text = builtins.readFile ./nitro-toolkit/traffic_forwarder.py;
destination = "/app/traffic_forwarder.py";
})
(pkgs.writeTextFile {
name = "vsock_helper";
text = builtins.readFile ./nitro-toolkit/vsock_helper.py;
destination = "/app/vsock_helper.py";
})
pkgs.bash
pkgs.busybox
pkgs.openssl
pkgs.postgresql
pkgs.socat
pkgs.python3
pkgs.jq
pkgs.iproute2
pkgs.coreutils
pkgs.cacert
pkgs.curl
nitro-bins
continuum-proxy
tinfoil-proxy
];
pathsToLink = [ "/bin" "/lib" "/app" "/usr/bin" "/usr/sbin" "/sbin" ];
};
# Build custom kernel - use kernel 6.12 which has NSM driver (merged in 6.8)
customKernel = pkgs.linuxPackages_6_12.kernel.override {
structuredExtraConfig = with pkgs.lib.kernel; {
VIRTIO_MMIO = yes;
VIRTIO_MENU = yes;
VIRTIO_MMIO_CMDLINE_DEVICES = yes;
NET = yes;
VSOCKETS = yes;
VIRTIO_VSOCKETS = yes;
NSM = yes; # Enable NSM driver for KMS operations (merged in 6.8+)
};
# Ensure we catch invalid or renamed config flags at build time
ignoreConfigErrors = false;
};
# Function to create EIF with specific APP_MODE
mkEif = appMode: nitro.buildEif {
name = "opensecret-eif-${appMode}";
# The kernel image location varies by architecture
kernel = if arch == "aarch64"
then "${customKernel}/Image" # ARM64 uses Image
else "${customKernel}/bzImage"; # x86_64 uses bzImage
# Use the blob config since extracting from custom kernel is complex
# The important thing is the kernel itself, not the config file
kernelConfig = nitro.blobs.${arch}.kernelConfig;
# NSM driver is built into kernel 6.8+, so we don't need the old module
# Setting to null to skip loading the incompatible old module
nsmKo = null;
copyToRoot = mkRootfs appMode;
entrypoint = "/bin/entrypoint";
};
# Build the main Rust package
opensecret = pkgs.rustPlatform.buildRustPackage {
pname = "opensecret";
version = "0.1.0";
src = pkgs.lib.cleanSourceWith {
src = ./.;
filter = path: type:
let
baseName = baseNameOf path;
parts = pkgs.lib.splitString "/" path;
in
# Explicitly exclude .env files
(baseName != ".env" && baseName != ".env.sample") &&
(
(builtins.elem "src" parts) ||
(type == "regular" && (
baseName == "Cargo.toml" ||
baseName == "Cargo.lock" ||
baseName == "rust-toolchain.toml"
))
);
};
cargoLock = {
lockFile = ./Cargo.lock;
};
nativeBuildInputs = [
pkgs.pkg-config
rustAnalyzer
pkgs.gcc
pkgs.clang
];
buildInputs = [
pkgs.openssl
pkgs.zlib
pkgs.postgresql
pkgs.diesel-cli
];
LIBPQ_LIB_DIR = "${pkgs.postgresql.lib}/lib";
};
# Use pre-built NSM library and KMS tools from nitro-bins directory
nitro-bins = pkgs.stdenv.mkDerivation {
name = "nitro-bins";
version = "1.0";
src = ./nitro-bins;
dontUnpack = true;
installPhase = ''
mkdir -p $out/{lib,bin}
# Use install to copy files and set permissions
install -m 755 $src/libnsm.so $out/lib/
install -m 755 $src/kmstool_enclave_cli $out/bin/
'';
};
# Copy continuum-proxy from local filesystem
continuum-proxy = pkgs.runCommand "continuum-proxy" {} ''
mkdir -p $out/bin
cp ${./continuum-proxy} $out/bin/continuum-proxy
chmod +x $out/bin/continuum-proxy
'';
# Copy tinfoil-proxy from local filesystem
tinfoil-proxy = pkgs.runCommand "tinfoil-proxy" {} ''
mkdir -p $out/bin
cp ${./tinfoil-proxy/dist/tinfoil-proxy} $out/bin/tinfoil-proxy
chmod +x $out/bin/tinfoil-proxy
'';
arch = pkgs.stdenv.hostPlatform.uname.processor;
in
{
packages = {
default = opensecret;
} // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
eif-dev = mkEif "dev";
eif-prod = mkEif "prod";
eif-preview = mkEif "preview";
};
devShell = pkgs.mkShell {
packages = inputs;
shellHook = ''
export LIBCLANG_PATH=${pkgs.libclang.lib}/lib/
export LD_LIBRARY_PATH=${pkgs.openssl}/lib:$LD_LIBRARY_PATH
export CC_wasm32_unknown_unknown=${pkgs.llvmPackages_14.clang-unwrapped}/bin/clang-14
export CFLAGS_wasm32_unknown_unknown="-I ${pkgs.llvmPackages_14.libclang.lib}/lib/clang/14.0.6/include/"
export PKG_CONFIG_PATH=${pkgs.openssl.dev}/lib/pkgconfig
${pkgs.lib.optionalString pkgs.stdenv.isLinux ''
alias docker='podman'
echo "Using 'podman' as an alias for 'docker'"
echo "You can now use 'docker' commands, which will be executed by podman"
# Podman configuration
export CONTAINERS_CONF=$HOME/.config/containers/containers.conf
export CONTAINERS_POLICY=$HOME/.config/containers/policy.json
mkdir -p $HOME/.config/containers
echo '{"default":[{"type":"insecureAcceptAnything"}]}' > $CONTAINERS_POLICY
# Create a basic containers.conf if it doesn't exist
if [ ! -f $CONTAINERS_CONF ]; then
echo "[engine]
cgroup_manager = \"cgroupfs\"
events_logger = \"file\"
runtime = \"crun\"
[storage]
driver = \"vfs\"" > $CONTAINERS_CONF
fi
# Ensure correct permissions
chmod 600 $CONTAINERS_POLICY $CONTAINERS_CONF
''}
${setupPostgresScript}
${setupEnvScript}
'';
};
}
);
}