-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
flake.nix
196 lines (160 loc) · 5.76 KB
/
flake.nix
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
{
description = "Build env";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};
outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
rustVersion = "1.73.0";
rust = pkgs.rust-bin.stable.${rustVersion}.default.override {
extensions = [
"rust-src" # rust-analyzer
];
};
nixLib = nixpkgs.lib;
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
# Libraries needed both at compile and runtime
sharedDeps = with pkgs; [
dbus
xorg.libX11
fontconfig
udev
glib
gst_all_1.gstreamer
gst_all_1.gst-plugins-base
xorg.libXi
libusb1
expat
];
# Libraries needed at runtime
runtimeDeps = with pkgs; [
xorg.libXcursor
xorg.libxcb
freetype
xorg.libXrandr
libGL
] ++ sharedDeps;
# Manually simulate a vcpkg installation so that it can link the libraries
# properly. Borrowed and adapted from: https://github.com/NixOS/nixpkgs/blob/69a35ff92dc404bf04083be2fad4f3643b2152c9/pkgs/applications/networking/remote/rustdesk/default.nix#L51
vcpkg = pkgs.stdenv.mkDerivation {
pname = "vcpkg";
version = "1.0.0";
unpackPhase =
let
vcpkg_target = "x64-linux";
updates_vcpkg_file = pkgs.writeText "update_vcpkg_legion-kb-rgb"
''
Package : libyuv
Architecture : ${vcpkg_target}
Version : 1.0
Status : is installed
Package : libvpx
Architecture : ${vcpkg_target}
Version : 1.0
Status : is installed
'';
in
''
mkdir -p vcpkg/.vcpkg-root
mkdir -p vcpkg/installed/${vcpkg_target}/lib
mkdir -p vcpkg/installed/vcpkg/updates
ln -s ${updates_vcpkg_file} vcpkg/installed/vcpkg/status
mkdir -p vcpkg/installed/vcpkg/info
touch vcpkg/installed/vcpkg/info/libyuv_1.0_${vcpkg_target}.list
touch vcpkg/installed/vcpkg/info/libvpx_1.0_${vcpkg_target}.list
ln -s ${pkgs.libvpx.out}/lib/* vcpkg/installed/${vcpkg_target}/lib/
ln -s ${pkgs.libyuv.out}/lib/* vcpkg/installed/${vcpkg_target}/lib/
'';
installPhase = ''
cp -r vcpkg $out
'';
};
envVars = rec {
RUST_BACKTRACE = 1;
# MOLD_PATH = "${pkgs.mold.out}/bin/mold";
# RUSTFLAGS = "-Clink-arg=-fuse-ld=${MOLD_PATH} -Clinker=clang";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
VCPKG_ROOT = "${vcpkg.out}";
};
# Allow a few more files to be included in the build workspace
workspaceSrc = ./.;
workspaceSrcString = builtins.toString workspaceSrc;
resFileFilter = path: _type: builtins.match "${workspaceSrcString}/app/res/.*" path != null;
workspaceFilter = path: type:
(resFileFilter path type) || (craneLib.filterCargoSources path type);
src = nixLib.cleanSourceWith
{
src = workspaceSrc;
filter = workspaceFilter;
};
buildInputs = with pkgs;
[
libvpx
libyuv
]
++ sharedDeps
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ ];
nativeBuildInputs = with pkgs;
[
pkg-config
cmake
clang
]
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ ];
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv;
inherit (craneLib.crateNameFromCargoToml { cargoToml = ./app/Cargo.toml; }) pname version;
cargoArtifacts = craneLib.buildDepsOnly ({
inherit pname version src buildInputs nativeBuildInputs stdenv;
# This magic thing here is needed otherwise the build fails
# https://github.com/ipetkov/crane/issues/411#issuecomment-1743441117
# Might revisit later to see if anything else pops up
installCargoArtifactsMode = "use-zstd";
} // envVars);
# The main application derivation
legion-kb-rgb = craneLib.buildPackage
({
inherit pname version src cargoArtifacts buildInputs nativeBuildInputs stdenv;
doCheck = false;
# cargoBuildCommand = "cargo build";
postFixup = ''
patchelf --add-rpath "${nixLib.makeLibraryPath runtimeDeps}" "$out/bin/${pname}"
'';
} // envVars);
in
{
checks = {
# inherit legion-kb-rgb;
};
packages.default = legion-kb-rgb;
apps.default = flake-utils.lib.mkApp {
drv = legion-kb-rgb;
};
devShells.default = legion-kb-rgb;
devShells.rust =
let
deps = buildInputs ++ nativeBuildInputs ++ sharedDeps ++ runtimeDeps;
in
pkgs.mkShell {
LD_LIBRARY_PATH = nixLib.makeLibraryPath deps;
inherit (envVars) VCPKG_ROOT LIBCLANG_PATH;
buildInputs = [ rust ] ++ deps;
};
});
}