forked from ilysenko/codex-desktop-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
361 lines (315 loc) · 12.5 KB
/
flake.nix
File metadata and controls
361 lines (315 loc) · 12.5 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
{
description = "Codex Desktop for Linux installer";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
sourceRoot = pkgs.lib.cleanSourceWith {
src = ./.;
filter = path: type:
pkgs.lib.cleanSourceFilter path type
&& (let
pathStr = toString path;
in
!(pkgs.lib.hasSuffix "/.codex" pathStr || pkgs.lib.hasInfix "/.codex/" pathStr));
};
codexDmg = pkgs.fetchurl {
url = "https://persistent.oaistatic.com/codex-app-prod/Codex.dmg";
hash = "sha256-MQHAXf1AMUEVQYxK2H7e4CQZ0Jf3FkxnfdvdRVmtikI=";
};
electronLibs = with pkgs; [
glib
gtk3
pango
cairo
gdk-pixbuf
atk
at-spi2-atk
at-spi2-core
nss
nspr
dbus
cups
expat
libdrm
mesa
libgbm
alsa-lib
libX11
libXcomposite
libXdamage
libXext
libXfixes
libXrandr
libxcb
libxkbcommon
libxcursor
libxi
libxtst
libxscrnsaver
libglvnd
systemd
wayland
];
electronLibPath = pkgs.lib.makeLibraryPath electronLibs;
runtimeLibPath = pkgs.lib.makeLibraryPath (with pkgs; [
libxcrypt-legacy
stdenv.cc.cc.lib
zlib
]);
launcherPath = pkgs.lib.makeBinPath (with pkgs; [
bash
coreutils
curl
findutils
gawk
gnugrep
gnused
nodejs
procps
python3
systemd
xdg-utils
]);
patchNixInstalledApp = installDir: ''
# Patch generated scripts for NixOS systems without /bin/bash.
if [ -f "${installDir}/start.sh" ]; then
${pkgs.gnused}/bin/sed -i '1s|^#!/bin/bash$|#!${pkgs.bash}/bin/bash|' "${installDir}/start.sh"
if ! grep -q "NixOS Electron library path" "${installDir}/start.sh"; then
${pkgs.gnused}/bin/sed -i '2i# NixOS Electron library path for dlopen()ed GL/EGL libraries.\nexport LD_LIBRARY_PATH="${electronLibPath}:${runtimeLibPath}:''${LD_LIBRARY_PATH:-}"' "${installDir}/start.sh"
fi
if ! grep -q "codex_nixos_add_runtime_library_dirs" "${installDir}/start.sh"; then
${pkgs.gnused}/bin/sed -i '/^set -euo pipefail$/a\
\
codex_nixos_add_runtime_library_dirs() {\
local cache_home="''${XDG_CACHE_HOME:-''${HOME:-}/.cache}"\
local runtime_root="''${CODEX_PRIMARY_RUNTIME_ROOT:-''${CODEX_RUNTIME_ROOT:-$cache_home/codex-runtimes/codex-primary-runtime}}"\
local dir\
\
for dir in \\\
"$runtime_root/dependencies/python/lib" \\\
"$runtime_root/dependencies/python/lib/python3.12/site-packages/pillow.libs" \\\
"$runtime_root/dependencies/python/lib/python3.12/site-packages/numpy.libs" \\\
"$runtime_root/dependencies/node/node_modules/@img/sharp-libvips-linux-x64/lib" \\\
"$runtime_root/dependencies/node/node_modules/@img/sharp-linux-x64/lib" \\\
"$runtime_root/dependencies/node/node_modules/@napi-rs/canvas-linux-x64-gnu"; do\
if [ -d "$dir" ]; then\
LD_LIBRARY_PATH="$dir:''${LD_LIBRARY_PATH:-}"\
fi\
done\
\
export LD_LIBRARY_PATH\
}\
\
codex_nixos_add_runtime_library_dirs' "${installDir}/start.sh"
fi
fi
# Patch the Electron binary for NixOS.
if [ -f "${installDir}/electron" ]; then
echo "[NIX] Patching Electron binary for NixOS..."
patchelf --set-interpreter "$(cat ${pkgs.stdenv.cc}/nix-support/dynamic-linker)" \
--set-rpath "${installDir}:${electronLibPath}" \
"${installDir}/electron"
if [ -f "${installDir}/chrome_crashpad_handler" ]; then
patchelf --set-interpreter "$(cat ${pkgs.stdenv.cc}/nix-support/dynamic-linker)" \
"${installDir}/chrome_crashpad_handler" || true
fi
if [ -f "${installDir}/chrome-sandbox" ]; then
patchelf --set-interpreter "$(cat ${pkgs.stdenv.cc}/nix-support/dynamic-linker)" \
"${installDir}/chrome-sandbox" || true
fi
find "${installDir}" -maxdepth 1 -name "*.so*" -type f | while read -r so; do
patchelf --set-rpath "${electronLibPath}" "$so" 2>/dev/null || true
done
echo "[NIX] Electron patched successfully"
fi
'';
patchNixGeneratedScripts = installDir: ''
# Patch generated scripts for NixOS systems without /bin/bash.
if [ -f "${installDir}/start.sh" ]; then
${pkgs.gnused}/bin/sed -i '1s|^#!/bin/bash$|#!${pkgs.bash}/bin/bash|' "${installDir}/start.sh"
fi
'';
codexDesktopPayload = pkgs.stdenv.mkDerivation {
pname = "codex-desktop-payload";
version = "unstable-2026-05-02";
src = sourceRoot;
__structuredAttrs = true;
nativeBuildInputs = [
pkgs.bash
pkgs.cargo
pkgs.curl
pkgs.gcc
pkgs.gnumake
pkgs.gnused
pkgs.makeWrapper
pkgs.nodejs
pkgs.p7zip
pkgs.patchelf
pkgs.python3
pkgs.unzip
];
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = "sha256-5bB5LHtOL0x3XAaUrRKRTTxsovHP6VVsgv/dcSfriGs=";
unsafeDiscardReferences.out = true;
dontConfigure = true;
dontBuild = true;
installPhase = ''
runHook preInstall
export HOME="$TMPDIR/home"
export npm_config_cache="$TMPDIR/npm-cache"
export SSL_CERT_FILE="${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
export NIX_SSL_CERT_FILE="$SSL_CERT_FILE"
export npm_config_cafile="$SSL_CERT_FILE"
export CARGO_HOME="$TMPDIR/cargo-home"
mkdir -p "$HOME" "$npm_config_cache" "$CARGO_HOME"
source_dir="$TMPDIR/codex-source"
mkdir -p "$source_dir"
cp -R ./. "$source_dir/"
chmod -R u+w "$source_dir"
cp ${codexDmg} "$source_dir/Codex.dmg"
npm_tools="$TMPDIR/npm-tools"
npm install --prefix "$npm_tools" --ignore-scripts asar @electron/rebuild
patchShebangs "$npm_tools"
export PATH="$npm_tools/node_modules/.bin:$PATH"
substituteInPlace "$source_dir/scripts/lib/asar-patch.sh" \
--replace-fail "npx --yes asar" "asar" \
--replace-fail "npx asar" "asar"
substituteInPlace "$source_dir/scripts/lib/dmg.sh" \
--replace-fail "npx --yes asar" "asar"
substituteInPlace "$source_dir/scripts/lib/native-modules.sh" \
--replace-fail "npx --yes @electron/rebuild" "electron-rebuild"
export CODEX_INSTALL_DIR="$out/opt/codex-desktop"
${pkgs.bash}/bin/bash "$source_dir/install.sh" "$source_dir/Codex.dmg"
rm -rf "$CODEX_INSTALL_DIR/resources/plugins/openai-bundled/plugins/computer-use"
marketplace="$CODEX_INSTALL_DIR/resources/plugins/openai-bundled/.agents/plugins/marketplace.json"
if [ -f "$marketplace" ]; then
node - "$marketplace" <<'NODE'
const fs = require("fs");
const marketplacePath = process.argv[2];
const marketplace = JSON.parse(fs.readFileSync(marketplacePath, "utf8"));
marketplace.plugins = (marketplace.plugins || []).filter((plugin) => plugin.name !== "computer-use");
fs.writeFileSync(marketplacePath, JSON.stringify(marketplace, null, 2) + "\n");
NODE
fi
asar extract "$CODEX_INSTALL_DIR/resources/app.asar" "$CODEX_INSTALL_DIR/resources/app-extracted"
rm -f "$CODEX_INSTALL_DIR/resources/app.asar"
rm -rf "$CODEX_INSTALL_DIR/resources/app.asar.unpacked"
${patchNixGeneratedScripts "$out/opt/codex-desktop"}
runHook postInstall
'';
};
codexDesktop = pkgs.stdenv.mkDerivation {
pname = "codex-desktop";
version = "unstable-2026-05-02";
src = codexDesktopPayload;
nativeBuildInputs = [
pkgs.asar
pkgs.makeWrapper
pkgs.patchelf
];
dontConfigure = true;
dontBuild = true;
installPhase = ''
runHook preInstall
mkdir -p "$out/opt"
cp -aT "$src/opt/codex-desktop" "$out/opt/codex-desktop"
chmod -R u+w "$out/opt/codex-desktop"
resources_dir="$out/opt/codex-desktop/resources"
(cd "$resources_dir/app-extracted" && find . -type f | LC_ALL=C sort | sed 's#^\./##') > "$TMPDIR/app.asar.ordering"
asar pack "$resources_dir/app-extracted" "$resources_dir/app.asar" \
--ordering "$TMPDIR/app.asar.ordering" \
--unpack "{*.node,*.so,*.dylib}"
rm -rf "$resources_dir/app-extracted"
${patchNixInstalledApp "$out/opt/codex-desktop"}
install -Dm0644 "$out/opt/codex-desktop/.codex-linux/codex-desktop.png" \
"$out/share/icons/hicolor/256x256/apps/codex-desktop.png"
install -Dm0644 ${sourceRoot}/packaging/linux/codex-desktop.desktop \
"$out/share/applications/codex-desktop.desktop"
substituteInPlace "$out/share/applications/codex-desktop.desktop" \
--replace-fail "/usr/bin/codex-desktop" "$out/bin/codex-desktop" \
--replace-fail "/usr/share/applications/codex-desktop.desktop" "$out/share/applications/codex-desktop.desktop"
makeWrapper "$out/opt/codex-desktop/start.sh" "$out/bin/codex-desktop" \
--prefix PATH : "${launcherPath}" \
--prefix LD_LIBRARY_PATH : "${electronLibPath}" \
--prefix LD_LIBRARY_PATH : "${runtimeLibPath}" \
--prefix PATH : "/run/current-system/sw/bin" \
--prefix PATH : "/etc/profiles/per-user/$(whoami)/bin"
runHook postInstall
'';
meta = {
description = "Codex Desktop for Linux";
homepage = "https://github.com/ilysenko/codex-desktop-linux";
license = pkgs.lib.licenses.mit;
platforms = pkgs.lib.platforms.linux;
mainProgram = "codex-desktop";
};
};
installer = pkgs.writeShellApplication {
name = "codex-desktop-installer";
runtimeInputs = [
pkgs.bash
pkgs.nodejs
pkgs.python3
pkgs.p7zip
pkgs.curl
pkgs.unzip
pkgs.gnumake
pkgs.gcc
pkgs.patchelf
];
text = ''
set -euo pipefail
root_dir="$(pwd)"
workdir="$(mktemp -d)"
source_dir="$workdir/source"
cleanup() {
rm -rf "$workdir"
}
trap cleanup EXIT
mkdir -p "$source_dir"
cp -R ${sourceRoot}/. "$source_dir"
chmod -R u+w "$source_dir"
cp ${codexDmg} "$source_dir/Codex.dmg"
chmod +x "$source_dir/install.sh"
cd "$source_dir"
export CODEX_INSTALL_DIR="''${CODEX_INSTALL_DIR:-$root_dir/codex-app}"
${pkgs.bash}/bin/bash "$source_dir/install.sh" "$source_dir/Codex.dmg" "$@"
install_dir="''${CODEX_INSTALL_DIR:-$root_dir/codex-app}"
${patchNixInstalledApp "$install_dir"}
'';
};
in
{
packages = {
default = codexDesktop;
codex-desktop = codexDesktop;
installer = installer;
};
apps.default = {
type = "app";
program = "${codexDesktop}/bin/codex-desktop";
};
apps.installer = {
type = "app";
program = "${installer}/bin/codex-desktop-installer";
};
devShells.default = pkgs.mkShell {
packages = [
pkgs.nodejs
pkgs.python3
pkgs.p7zip
pkgs.curl
pkgs.unzip
pkgs.gnumake
pkgs.gcc
];
};
}
);
}