Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
365 lines (348 loc) · 13.3 KB
/
Copy pathflake.nix
File metadata and controls
365 lines (348 loc) · 13.3 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
362
363
364
365
{
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;
config = {
allowUnfree = true;
android_sdk.accept_license = true;
};
};
# Drop packages the host can't build (e.g. wayland/ibus on darwin) so the
# devShell evaluates on macOS too; on Linux the lists are unchanged.
availableOnly = builtins.filter (pkgs.lib.meta.availableOn pkgs.stdenv.hostPlatform);
ndkVersion = "26.1.10909125"; # NDK r26b
cmdLineToolsVer = "13.0";
androidComposition = pkgs.androidenv.composeAndroidPackages {
cmdLineToolsVersion = cmdLineToolsVer;
platformToolsVersion = "37.0.1";
buildToolsVersions = [ "35.0.0" ];
platformVersions = [ "36" ];
includeNDK = true;
ndkVersions = [ ndkVersion ];
abiVersions = [
"arm64-v8a"
"x86_64"
];
includeEmulator = false;
includeSources = false;
includeSystemImages = false;
systemImageTypes = [ ];
useGoogleAPIs = false;
useGoogleTVAddOns = false;
};
androidSdk = androidComposition.androidsdk;
in
let
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
# Update after each release with scripts/update-nix-release.sh.
releaseVersion = "1.2.1-alpha.102";
releaseHashes = {
x86_64-linux = "sha256-OBKDToyvOBM/yiHOgeKEEd9sZE7LM/DFgQJPQgQR4pc=";
aarch64-linux = "sha256-UFbQD4WkJg35Pew+xWBtygJCLg49BRKDW+s6DNJeTVA=";
};
in
let
keytaoLinuxIme = pkgs.rustPlatform.buildRustPackage {
pname = "keytao-linux-ime";
inherit version;
src = pkgs.lib.cleanSource ./.;
cargoLock.lockFile = ./Cargo.lock;
cargoBuildFlags = [
"--package"
"keytao-linux-ime"
"--bin"
"keytao-ime"
];
nativeBuildInputs = with pkgs; [
pkg-config
llvmPackages.libclang
];
buildInputs = with pkgs; [
librime
libxkbcommon
libxcb
libx11
dbus
glib
gtk3
openssl
freetype
];
doCheck = false;
RIME_INCLUDE_DIR = "${pkgs.librime}/include";
RIME_LIB_DIR = "${pkgs.librime}/lib";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
# Install the KDE Virtual Keyboard desktop entry so keytao-ime appears in
# KDE Settings → Virtual Keyboard. X-KDE-Wayland-VirtualKeyboard=true tells
# KWin to spawn the process with a private WAYLAND_SOCKET fd.
postInstall = ''
install -Dm644 \
crates/keytao-linux-ime/keytao-wayland-launcher.desktop \
$out/share/applications/keytao-wayland-launcher.desktop
substituteInPlace $out/share/applications/keytao-wayland-launcher.desktop \
--replace-fail 'Exec=keytao-ime' "Exec=$out/bin/keytao-ime"
# ibus-daemon scans $XDG_DATA_DIRS/ibus/component on startup; without
# this file KeyTao only exists for as long as the running daemon
# remembers the RegisterComponent call.
install -Dm644 \
crates/keytao-linux-ime/keytao.xml \
$out/share/ibus/component/keytao.xml
substituteInPlace $out/share/ibus/component/keytao.xml \
--replace-fail '<exec>keytao-ime' "<exec>$out/bin/keytao-ime"
'';
};
keytaoAppRelease = pkgs.stdenv.mkDerivation {
pname = "keytao-app-bin";
version = releaseVersion;
src = pkgs.fetchurl {
url = "https://github.com/xkinput/keytao-app/releases/download/v${releaseVersion}/keytao-app-${releaseVersion}-linux-${
{
x86_64-linux = "x64";
aarch64-linux = "arm64";
}
.${system}
}.deb";
hash = releaseHashes.${system};
};
nativeBuildInputs = with pkgs; [
dpkg
autoPatchelfHook
wrapGAppsHook3
];
buildInputs = with pkgs; [
stdenv.cc.cc.lib
libxkbcommon
libxcb
dbus
glib
gtk3
gdk-pixbuf
pango
atk
cairo
harfbuzz
libepoxy
fontconfig
gflags
snappy
libunwind
zlib
];
# Flutter loads GL/EGL dynamically, so DT_NEEDED alone is insufficient.
runtimeDependencies = [ pkgs.libglvnd ];
unpackPhase = ''
runHook preUnpack
dpkg-deb -x "$src" .
runHook postUnpack
'';
dontConfigure = true;
dontBuild = true;
dontStrip = true;
installPhase = ''
runHook preInstall
mkdir -p "$out"
cp -a usr/bin usr/lib usr/share etc "$out/"
substituteInPlace "$out/share/applications/KeyTao.desktop" \
--replace-fail 'Exec=keytao-app' "Exec=$out/bin/keytao-app"
substituteInPlace "$out/share/applications/keytao-wayland-launcher.desktop" \
"$out/etc/xdg/autostart/keytao-ime.desktop" \
--replace-fail 'Exec=keytao-ime' "Exec=$out/bin/keytao-ime"
substituteInPlace "$out/share/ibus/component/keytao.xml" \
--replace-fail '<exec>keytao-ime' "<exec>$out/bin/keytao-ime"
runHook postInstall
'';
preFixup = ''
addAutoPatchelfSearchPath "$out/lib/KeyTao/lib" "$out/lib/KeyTao/runtime/lib"
# Wrap the bin symlinks; keep the real executables beside their data.
gappsWrapperArgs+=(
--prefix PATH : "${pkgs.lib.makeBinPath [ pkgs.procps ]}"
--prefix LD_LIBRARY_PATH : "$out/lib/KeyTao/lib:$out/lib/KeyTao/runtime/lib"
)
'';
meta = {
description = "KeyTao official Linux release, repackaged from deb";
homepage = "https://github.com/xkinput/keytao-app";
platforms = [
"x86_64-linux"
"aarch64-linux"
];
mainProgram = "keytao-app";
};
};
in
{
packages = pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
default = keytaoAppRelease;
keytao-app-bin = keytaoAppRelease;
keytao-linux-ime = keytaoLinuxIme;
};
apps = pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
default = {
type = "app";
program = "${keytaoAppRelease}/bin/keytao-app";
};
};
devShells.default = pkgs.mkShell {
# Tools (go into PATH / nativeBuildInputs, no effect on RPATH)
packages = availableOnly [
androidSdk
pkgs.jdk17
pkgs.curl
pkgs.file
pkgs.unzip
pkgs.patchelf
pkgs.pkg-config
pkgs.sccache
pkgs.mold
];
# Runtime libraries: placed in buildInputs so the Nix cc-wrapper
# automatically injects -L/-rpath into NIX_LDFLAGS, which cargo uses
# when linking. This gives every compiled binary the correct RPATH
# without needing patchelf.
buildInputs = availableOnly (with pkgs; [
librime
libxcb
libxkbcommon
wayland
dbus
gtk3
glib
gdk-pixbuf
gdk-pixbuf.dev
pango
atk
cairo
harfbuzz
harfbuzz.dev
freetype
freetype.dev
bzip2
bzip2.dev
xz
xz.dev
openssl
xdotool
ibus # Added for IBus IM module
]);
ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
NDK_HOME = "${androidSdk}/libexec/android-sdk/ndk/${ndkVersion}";
RIME_INCLUDE_DIR = "${pkgs.librime}/include";
RIME_LIB_DIR = "${pkgs.librime}/lib";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
BINDGEN_EXTRA_CLANG_ARGS = "-I${pkgs.librime}/include";
BZIP2_LIB_DIR = "${pkgs.bzip2}/lib";
BZIP2_INCLUDE_DIR = "${pkgs.bzip2.dev}/include";
LZMA_API_STATIC = "0";
# Ensure runtime libs are findable even when cargo doesn't embed RPATH.
# As a flake attribute (not shellHook), direnv's `use flake` exports this.
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (availableOnly (
with pkgs;
[
librime
libxcb
libxkbcommon
wayland
dbus
gtk3
glib
gdk-pixbuf
pango
atk
cairo
harfbuzz
freetype
bzip2
xz
openssl
xdotool
ibus
]
));
shellHook = ''
export JAVA_HOME="${pkgs.jdk17}"
export RUSTC_WRAPPER="${pkgs.sccache}/bin/sccache"
export MOLD_PATH="${pkgs.mold}/bin/mold"
export CARGO_INCREMENTAL=0
''
+ pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
# Generate GTK IM Modules cache safely so direnv doesn't destroy it
export GTK_PATH="${pkgs.gtk3}/lib/gtk-3.0/3.0.0:${pkgs.ibus}/lib/gtk-3.0/3.0.0"
_immodules_cache="$HOME/.cache/keytao-immodules.cache"
if ${pkgs.gtk3}/bin/gtk-query-immodules-3.0 > "/tmp/keytao_im.tmp" 2>/dev/null; then
${pkgs.gtk3}/bin/gtk-query-immodules-3.0 ${pkgs.ibus}/lib/gtk-3.0/3.0.0/immodules/im-ibus.so >> "/tmp/keytao_im.tmp" 2>/dev/null
mv "/tmp/keytao_im.tmp" "$_immodules_cache"
fi
export GTK_IM_MODULE_FILE="$_immodules_cache"
''
+ ''
# Add Android platform-tools (adb) and cmdline-tools to PATH
export PATH="${androidSdk}/libexec/android-sdk/platform-tools:$PATH"
export PATH="${androidSdk}/libexec/android-sdk/cmdline-tools/${cmdLineToolsVer}/bin:$PATH"
# Embed RPATH for all runtime libs so binaries work without LD_LIBRARY_PATH.
# The -L flags are injected via NIX_LDFLAGS by mkShell; here we add -rpath
# so the dynamic linker finds the libs at runtime even outside the shell.
# Note: Rust on Linux defaults to lld (-fuse-ld=lld via gcc-ld wrapper),
# so no explicit linker selection is needed here.
export RUSTFLAGS="-C link-arg=-Wl,-rpath,${
pkgs.lib.makeLibraryPath (availableOnly (
with pkgs;
[
librime
libxcb
libxkbcommon
wayland
dbus
gtk3
glib
gdk-pixbuf
pango
atk
cairo
harfbuzz
freetype
bzip2
openssl
ibus
]
))
}"
# Install Android Rust cross-compilation targets (idempotent)
for _t in aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android; do
rustup target add "$_t" 2>/dev/null || true
done
# Nix's mkShell sets DEVELOPER_DIR / SDKROOT to the Nix apple-sdk, which
# lacks libiconv.tbd stubs. Cargo is configured to use /usr/bin/clang as
# linker for host builds, so we point
# SDKROOT at the Xcode CLT SDK so /usr/bin/clang can find system libs.
unset DEVELOPER_DIR
# Try CLT SDK paths in order of preference; fall back to leaving SDKROOT as-is.
for _sdk_candidate in \
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" \
"/Library/Developer/CommandLineTools/SDKs/MacOSX15.4.sdk" \
"/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk"; do
if [ -d "$_sdk_candidate" ]; then
export SDKROOT="$_sdk_candidate"
break
fi
done
'';
};
}
))
// {
nixosModules.default = import ./nix/nixos.nix { inherit self; };
};
}