Skip to content

Commit 519285a

Browse files
committed
nix: Add Nix Flake support
This commit modifies the CMakeLists.txt to allow for Nix to manage submodules itself - by cloning the repo seaprately, which helps with reproducible builds. It also adds: - `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. - `nix/default.nix` This is the main package derivation. Both this repo & Nixpkgs are derived from the same codebase. Squashed commits: - CMakeLists: Add option for allowing/denying submodule fetch This provides finer granularity for packaging, such as Nix, where submodules may not be wanted, and instead for the supporting Git repositories to be fetched separately. Signed-off-by: Dom Rodriguez <shymega@shymega.org.uk>
1 parent 901e007 commit 519285a

File tree

7 files changed

+276
-2
lines changed

7 files changed

+276
-2
lines changed

.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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.16)
22
project(xrDriver VERSION 1.1.0 LANGUAGES C)
33

4+
option(XRLINUXDRIVER_ALLOW_SUBMODULES "Allow CMake to automagically download submodules" ON)
5+
46
configure_file(include/version.h.in version.h)
57
include_directories(${PROJECT_BINARY_DIR})
68

@@ -40,8 +42,11 @@ pkg_check_modules(JSONC REQUIRED json-c)
4042
pkg_check_modules(CURL REQUIRED libcurl)
4143
pkg_check_modules(LIBWAYLAND_CLIENT REQUIRED wayland-client)
4244

43-
execute_process(COMMAND git submodule update --init --recursive
44-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
45+
if (XRLINUXDRIVER_ALLOW_SUBMODULES)
46+
execute_process(COMMAND git submodule update --init --recursive
47+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
48+
endif()
49+
4550
add_subdirectory(modules/xrealInterfaceLibrary/interface_lib)
4651

4752
# 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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};
11+
outputs =
12+
{
13+
self,
14+
nixpkgs,
15+
flake-utils,
16+
...
17+
}:
18+
let
19+
systems = [
20+
"x86_64-linux"
21+
"aarch64-linux"
22+
];
23+
in
24+
flake-utils.lib.eachSystem systems (
25+
system:
26+
let
27+
pkgs = nixpkgs.outputs.legacyPackages.${system};
28+
in
29+
with pkgs;
30+
{
31+
packages = {
32+
xrlinuxdriver = callPackage ./nix { inherit self; };
33+
default = self.packages.${system}.xrlinuxdriver;
34+
};
35+
36+
devShells.default = pkgs.mkShell { buildInputs = [ self.packages.${system}.default ]; };
37+
}
38+
)
39+
// {
40+
overlays.default = final: prev: { inherit (self.packages.${final.system}) xrlinuxdriver; };
41+
};
42+
}

nix/default.nix

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
self,
3+
lib,
4+
pkgs,
5+
stdenv,
6+
fetchFromGitLab,
7+
libusb1,
8+
curl,
9+
openssl,
10+
libevdev,
11+
json_c,
12+
hidapi,
13+
wayland,
14+
cmake,
15+
pkg-config,
16+
python3,
17+
libffi,
18+
autoPatchelfHook,
19+
...
20+
}:
21+
let
22+
pythonEnv = python3.withPackages (ps: [ ps.pyyaml ]);
23+
commonBuildInputs = [
24+
curl
25+
hidapi
26+
json_c
27+
libevdev
28+
libffi
29+
libusb1
30+
openssl
31+
stdenv.cc.cc.lib
32+
wayland
33+
];
34+
arch =
35+
if pkgs.system == "aarch64-linux" then
36+
"aarch64"
37+
else if pkgs.system == "x86_64-linux" then
38+
"x86_64"
39+
else
40+
throw "Unsupported system ${pkgs.system}";
41+
in
42+
stdenv.mkDerivation rec {
43+
pname = "xrlinuxdriver";
44+
version = "0.12.0.1";
45+
46+
srcs = [
47+
(fetchFromGitLab rec {
48+
domain = "gitlab.com";
49+
owner = "TheJackiMonster";
50+
repo = "nrealAirLinuxDriver";
51+
rev = "3225fcc575e19a8407d5019903567cff1c3ed1a8";
52+
hash = "sha256-NRbcANt/CqREQZoYIYtTGVbvkZ7uo2Tm90s6prlsrQE=";
53+
fetchSubmodules = true;
54+
name = "${repo}-src";
55+
})
56+
(lib.cleanSourceWith {
57+
src = self;
58+
name = "${pname}-src";
59+
})
60+
];
61+
sourceRoot = ".";
62+
postUnpack =
63+
let
64+
nrealAirLinuxDriver = (builtins.elemAt srcs 0).name;
65+
xrlinuxdriver = (builtins.elemAt srcs 1).name;
66+
in
67+
''
68+
cp -R ${xrlinuxdriver}/ $sourceRoot/xrlinuxdriver
69+
cp -R ${nrealAirLinuxDriver}/* $sourceRoot/xrlinuxdriver/modules/xrealInterfaceLibrary
70+
chmod -R u+w $sourceRoot/xrlinuxdriver
71+
'';
72+
73+
nativeBuildInputs = [
74+
cmake
75+
pkg-config
76+
pythonEnv
77+
autoPatchelfHook
78+
];
79+
buildInputs = commonBuildInputs;
80+
81+
cmakeFlags = [
82+
"-DCMAKE_SKIP_RPATH=ON"
83+
"-DXRLINUXDRIVER_ALLOW_SUBMODULES=OFF"
84+
];
85+
cmakeBuildDir = "xrlinuxdriver/build";
86+
cmakeBuildType = "RelWithDebInfo";
87+
88+
postPatch =
89+
let
90+
root = "$sourceRoot/xrlinuxdriver";
91+
in
92+
''
93+
substituteInPlace ${root}/systemd/xr-driver.service \
94+
--replace-fail "ExecStart={bin_dir}/${meta.mainProgram}" "ExecStart=$out/bin/${meta.mainProgram}" \
95+
--replace-fail "{ld_library_path}" "$out/usr/lib/${arch}"
96+
'';
97+
98+
installPhase =
99+
let
100+
root = "$sourceRoot/xrlinuxdriver";
101+
in
102+
''
103+
mkdir -p $out/bin $out/usr/lib/systemd/user $out/usr/lib/udev/rules.d $out/usr/lib/${arch}
104+
cp ${meta.mainProgram} ../bin/xr_driver_cli ../bin/xr_driver_verify $out/bin
105+
cp ../udev/* $out/usr/lib/udev/rules.d/
106+
cp ../lib/${arch}/* $out/usr/lib/${arch}/
107+
cp ${hidapi}/lib/libhidapi-hidraw.so.0 $out/usr/lib/
108+
cp ../systemd/xr-driver.service $out/usr/lib/systemd/user/
109+
'';
110+
111+
preBuild = ''
112+
addAutoPatchelfSearchPath $out/usr/lib/${arch}
113+
'';
114+
115+
doInstallCheck = false;
116+
# The default release is a script which will do an impure download
117+
# just ensure that the application can run without network
118+
119+
meta = with lib; {
120+
homepage = "https://github.com/wheaney/XRLinuxDriver";
121+
license = licenses.mit;
122+
description = "Linux service for interacting with XR devices.";
123+
mainProgram = "xrDriver";
124+
maintainers = with maintainers; [ shymega ];
125+
platforms = platforms.linux;
126+
};
127+
}

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)