Skip to content

Commit fec340d

Browse files
shymegajohnrizzo1
andcommitted
feat(nix): Add Nix Flake support
This commit consists of the following changes: - `default.nix`/`shell.nix` This allows for legacy Nix tooling like `nix-build`/`nix-shell` to work. - `flake.nix` This is for the (new) Nix Flake support. - `flake.lock` As above, but for tracking Flake inputs. - `.envrc` This allows for a fully self-contained developer environment to be setup with Nix. - `package.nix` This derives from @johnrizzo1's Nix package for the Nix Flake, which were REALLY helpful for me. Closes: #38 Co-authored-by: John Rizzo <johnrizzo1@gmail.com>
1 parent 33f15b0 commit fec340d

7 files changed

Lines changed: 296 additions & 0 deletions

File tree

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pkg_check_modules(LIBWAYLAND_CLIENT REQUIRED wayland-client)
4949

5050
execute_process(COMMAND git submodule update --init --recursive
5151
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
52+
5253
add_subdirectory(modules/xrealInterfaceLibrary/interface_lib)
5354

5455
# Set the library directory based on architecture

default.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(import (
2+
let
3+
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
4+
in
5+
fetchTarball {
6+
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
7+
sha256 = lock.nodes.flake-compat.locked.narHash;
8+
}
9+
) {src = ./.;}).defaultNix

flake.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
description = "Nix Flake for XRLinuxDriver";
3+
inputs = {
4+
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
5+
flake-utils.url = "github:numtide/flake-utils";
6+
flake-compat = {
7+
url = "github:edolstra/flake-compat";
8+
flake = false;
9+
};
10+
self.submodules = true;
11+
};
12+
outputs = inputs: let
13+
inherit (inputs) self nixpkgs flake-utils;
14+
15+
forEachSystem = let
16+
systems = [
17+
"x86_64-linux"
18+
"aarch64-linux"
19+
];
20+
genPkgs = system: nixpkgs.legacyPackages.${system};
21+
inherit (nixpkgs.lib) genAttrs;
22+
in
23+
f: genAttrs systems (system: f (genPkgs system));
24+
in {
25+
packages = forEachSystem (pkgs: {
26+
xrlinuxdriver = pkgs.callPackage ./package.nix {inherit self;};
27+
default = self.packages.${pkgs.stdenv.hostPlatform.system}.xrlinuxdriver;
28+
});
29+
30+
devShells = forEachSystem (pkgs: {
31+
default = pkgs.mkShell {
32+
inputsFrom = pkgs.lib.singleton self.packages.${pkgs.stdenv.hostPlatform.system}.default;
33+
};
34+
});
35+
36+
overlays.default = _: prev: self.packages.${prev.stdenv.hostPlatform.system};
37+
};
38+
}

package.nix

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
# Core inputs
3+
self,
4+
lib,
5+
pkgs,
6+
stdenv,
7+
autoPatchelfHook,
8+
# xrDriver deps
9+
libusb1,
10+
curl,
11+
openssl,
12+
libevdev,
13+
json_c,
14+
hidapi,
15+
wayland,
16+
cmake,
17+
pkg-config,
18+
python3,
19+
libffi,
20+
systemd,
21+
makeWrapper,
22+
jq,
23+
# nrealAirLinuxDriver deps
24+
rustPlatform,
25+
rustc,
26+
cargo,
27+
...
28+
}:
29+
let
30+
arch =
31+
let
32+
inherit (pkgs.stdenv.hostPlatform) isx86_64 isLinux isAarch64;
33+
in
34+
if isAarch64 && isLinux then
35+
"aarch64"
36+
else if isx86_64 && isLinux then
37+
"x86_64"
38+
else
39+
throw "Unsupported system ${pkgs.stdenv.hostPlatform.system}";
40+
in
41+
stdenv.mkDerivation (finalAttrs: {
42+
pname = "xrlinuxdriver";
43+
version = "2.0.5";
44+
45+
src = lib.cleanSourceWith {
46+
src = self;
47+
name = "${finalAttrs.pname}-src";
48+
};
49+
50+
cargoRoot = "modules/xrealInterfaceLibrary/interface_lib/modules/xreal_one_driver";
51+
52+
cargoDeps = rustPlatform.importCargoLock {
53+
lockFile = "${finalAttrs.src}/${finalAttrs.cargoRoot}/Cargo.lock";
54+
};
55+
56+
nativeBuildInputs =
57+
let
58+
pythonEnv = python3.withPackages (ps: [ ps.pyyaml ]);
59+
in
60+
[
61+
cmake
62+
pkg-config
63+
pythonEnv
64+
autoPatchelfHook
65+
rustPlatform.cargoSetupHook
66+
rustc
67+
cargo
68+
makeWrapper
69+
];
70+
buildInputs = [
71+
curl
72+
hidapi
73+
json_c
74+
libevdev
75+
libffi
76+
libusb1
77+
openssl
78+
systemd
79+
wayland
80+
];
81+
82+
# The vendor .so blobs and hidapi-hidraw need libudev at link time
83+
NIX_LDFLAGS = "-ludev";
84+
85+
autoPatchelfIgnoreMissingDeps = [ "libopencv_*" ];
86+
87+
installPhase = ''
88+
runHook preInstall
89+
90+
# Main binary
91+
install -Dm755 xrDriver $out/bin/xrDriver
92+
93+
# Vendor SDK shared libraries
94+
mkdir -p $out/lib
95+
for so in $src/lib/${arch}/*.so; do
96+
[ -f "$so" ] && install -Dm755 "$so" $out/lib/$(basename "$so")
97+
done
98+
if [ -d "$src/lib/${arch}/viture" ]; then
99+
for so in $src/lib/${arch}/viture/*.so*; do
100+
[ -f "$so" ] && install -Dm755 "$so" $out/lib/$(basename "$so")
101+
done
102+
fi
103+
104+
# Install hidapi shared libs built during CMake
105+
find . -name 'libhidapi*.so*' \( -type f -o -type l \) | while read -r f; do
106+
cp -a "$f" $out/lib/
107+
done
108+
109+
patchelf --set-rpath "$out/lib:${
110+
lib.makeLibraryPath [
111+
systemd
112+
stdenv.cc.cc.lib
113+
curl
114+
openssl
115+
json_c
116+
libusb1
117+
libevdev
118+
wayland
119+
]
120+
}" $out/bin/xrDriver
121+
122+
# CLI tool
123+
install -Dm755 $src/bin/xr_driver_cli $out/bin/xr_driver_cli
124+
wrapProgram $out/bin/xr_driver_cli \
125+
--prefix PATH : ${
126+
lib.makeBinPath [
127+
jq
128+
curl
129+
]
130+
}
131+
132+
# Udev rules
133+
mkdir -p $out/lib/udev/rules.d
134+
cp $src/udev/*.rules $out/lib/udev/rules.d
135+
136+
# Systemd user service
137+
install -Dm644 $src/systemd/xr-driver.service $out/lib/systemd/user/xr-driver.service
138+
substituteInPlace $out/lib/systemd/user/xr-driver.service \
139+
--replace-fail '{ld_library_path}' "$out/lib" \
140+
--replace-fail '{bin_dir}' "$out/bin"
141+
142+
runHook postInstall
143+
'';
144+
145+
doInstallCheck = false;
146+
# The default release is a script which will do an impure download
147+
# just ensure that the application can run without network
148+
149+
meta = {
150+
homepage = "https://github.com/wheaney/XRLinuxDriver";
151+
license = lib.licenses.mit;
152+
description = "Linux service for interacting with XR devices.";
153+
mainProgram = "xrDriver";
154+
maintainers = with lib.maintainers; [ shymega ];
155+
platforms = lib.platforms.linux;
156+
};
157+
})

shell.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2023 Dom Rodriguez <shymega@shymega.org.uk>
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-only
4+
(import (
5+
let
6+
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
7+
in
8+
fetchTarball {
9+
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
10+
sha256 = lock.nodes.flake-compat.locked.narHash;
11+
}
12+
) {src = ./.;}).shellNix

0 commit comments

Comments
 (0)