Skip to content

Commit 621128a

Browse files
committed
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 is the main package derivation. Both this repo & Nixpkgs are derived from the same codebase. Closes: #38
1 parent 33f15b0 commit 621128a

7 files changed

Lines changed: 264 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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
# nrealAirLinuxDriver deps
22+
rustPlatform,
23+
rustc,
24+
cargo,
25+
...
26+
}: let
27+
pythonEnv = python3.withPackages (ps: [ps.pyyaml]);
28+
arch =
29+
if pkgs.stdenv.hostPlatform.system == "aarch64-linux"
30+
then "aarch64"
31+
else if pkgs.stdenv.hostPlatform.system == "x86_64-linux"
32+
then "x86_64"
33+
else throw "Unsupported system ${pkgs.stdenv.hostPlatform.system}";
34+
in
35+
stdenv.mkDerivation rec {
36+
pname = "xrlinuxdriver";
37+
version = "2.0.5";
38+
39+
src = lib.cleanSourceWith {
40+
src = self;
41+
name = "${pname}-src";
42+
};
43+
cargoRoot = "modules/xrealInterfaceLibrary/interface_lib/modules/xreal_one_driver";
44+
45+
cargoDeps = rustPlatform.fetchCargoVendor rec {
46+
name = "xrealInterfaceLibrary-${version}-cargo-deps";
47+
inherit src;
48+
sourceRoot = cargoRoot;
49+
hash = "sha256-ISgdmB5MEZ3P9KWCeURzr1PbIeb4BOCf1Z+Rs8wnJdM=";
50+
};
51+
52+
nativeBuildInputs = [
53+
cmake
54+
pkg-config
55+
pythonEnv
56+
autoPatchelfHook
57+
rustPlatform.cargoSetupHook
58+
rustc
59+
cargo
60+
];
61+
buildInputs = [
62+
curl
63+
hidapi
64+
json_c
65+
libevdev
66+
libffi
67+
libusb1
68+
openssl
69+
stdenv.cc.cc.lib
70+
systemd
71+
wayland
72+
];
73+
74+
cmakeFlags = [
75+
"-DCMAKE_BUILD_TYPE=Release"
76+
"-DCMAKE_SKIP_BUILD_RPATH=OFF"
77+
"-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON"
78+
"-DCMAKE_INSTALL_RPATH=$ORIGIN/../lib"
79+
"-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON"
80+
];
81+
cmakeBuildDir = "build";
82+
cmakeBuildType = "RelWithDebInfo";
83+
84+
preConfigure = ''
85+
autoPatchelf lib/${arch}
86+
addAutoPatchelfSearchPath $out/lib/${arch}
87+
'';
88+
89+
postPatch = ''
90+
# Make git submodule commands do nothing
91+
substituteInPlace CMakeLists.txt \
92+
--replace-fail "execute_process(COMMAND git submodule update --init --recursive" "execute_process(COMMAND echo \"Skipping git submodule update\""
93+
'';
94+
95+
fixupPhase = ''
96+
# Fix RPATH issues
97+
patchelf --set-rpath '$ORIGIN/../lib' $out/bin/xrDriver
98+
'';
99+
100+
installPhase = ''
101+
mkdir -p $out/bin $out/lib/systemd/user $out/lib/udev/rules.d $out/lib/${arch}
102+
install -m755 -t $out/bin xrDriver ../bin/xr_driver_cli ../bin/xr_driver_verify
103+
cp ../udev/* $out/lib/udev/rules.d/
104+
cp -r ../lib/${arch}/* $out/lib/$arch/
105+
cp ../systemd/xr-driver.service $out/lib/systemd/user/xr-driver.service
106+
cp ${hidapi}/lib/libhidapi-hidraw.so.0 $out/lib/
107+
substituteInPlace \
108+
$out/lib/systemd/user/xr-driver.service \
109+
--replace-fail "ExecStart={bin_dir}/xrDriver" "ExecStart=$out/bin/xrDriver" \
110+
--replace-fail "{ld_library_path}" "$out/lib/${arch}"
111+
'';
112+
113+
doInstallCheck = false;
114+
# The default release is a script which will do an impure download
115+
# just ensure that the application can run without network
116+
117+
meta = {
118+
homepage = "https://github.com/wheaney/XRLinuxDriver";
119+
license = lib.licenses.mit;
120+
description = "Linux service for interacting with XR devices.";
121+
mainProgram = "xrDriver";
122+
maintainers = with lib.maintainers; [shymega];
123+
platforms = lib.platforms.linux;
124+
};
125+
}

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)