Skip to content

Commit 0de8585

Browse files
committed
support nix and nixos
1 parent a17a0d0 commit 0de8585

13 files changed

Lines changed: 214 additions & 138 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ yarn-error.log
2727
!OSAcknowledgements.txt
2828

2929
# Python
30-
venv
30+
venv
31+
result

CMakeLists.txt

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ cmake_policy(SET CMP0042 NEW)
66

77
project(nudelta)
88

9-
if(NOT DEFINED NODE_RUNTIME)
10-
message( FATAL_ERROR "NODE_RUNTIME is not defined. Use `cmake-js` from the root of the repo." )
11-
endif()
12-
139
# Get version from package.json
1410
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/package.json PACKAGE_JSON)
1511
string(JSON NUDELTA_VERSION GET ${PACKAGE_JSON} "version")
@@ -22,11 +18,11 @@ set(BUILD_SHARED_LIBS FALSE)
2218
set(CMAKE_OSX_DEPLOYMENT_TARGET "11.3")
2319

2420
if(APPLE)
25-
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
26-
set(CMAKE_HAVE_THREADS_LIBRARY 1)
27-
set(CMAKE_USE_WIN32_THREADS_INIT 0)
28-
set(CMAKE_USE_PTHREADS_INIT 1)
29-
set(THREADS_PREFER_PTHREAD_FLAG ON)
21+
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
22+
set(CMAKE_HAVE_THREADS_LIBRARY 1)
23+
set(CMAKE_USE_WIN32_THREADS_INIT 0)
24+
set(CMAKE_USE_PTHREADS_INIT 1)
25+
set(THREADS_PREFER_PTHREAD_FLAG ON)
3026
endif()
3127

3228
# Dependencies
@@ -37,57 +33,64 @@ include_directories(include)
3733

3834
# YAML Data
3935
file (GLOB_RECURSE yml_files "res/**/*.yml")
36+
find_program(RUBY_EXECUTABLE ruby)
37+
if(RUBY_EXECUTABLE)
38+
message(STATUS "ruby found at: ${RUBY_EXECUTABLE}")
39+
else()
40+
message(FATAL_ERROR "ruby not found in PATH and is required for an intermediate build step.")
41+
endif()
4042
add_custom_command(
41-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/res.cpp
42-
COMMAND node ${CMAKE_CURRENT_LIST_DIR}/util/res_to_cpp.js ${CMAKE_CURRENT_LIST_DIR}/res > ${CMAKE_CURRENT_BINARY_DIR}/res.cpp
43-
DEPENDS ${yml_files} util/res_to_cpp.js
43+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/res.cpp
44+
COMMAND ${RUBY_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/util/res2cxx.rb ${CMAKE_CURRENT_LIST_DIR}/res > ${CMAKE_CURRENT_BINARY_DIR}/res.cpp
45+
DEPENDS ${yml_files} util/res2cxx.rb
4446
)
4547
add_custom_target(res_file ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/res.cpp)
4648

4749
include_directories(res)
4850

4951
# libnd
52+
include_directories(${CMAKE_JS_INC})
5053
file(GLOB nudelta_lib_src "lib/*.cpp")
5154
add_library(nd ${nudelta_lib_src} ${CMAKE_CURRENT_BINARY_DIR}/res.cpp)
5255
add_dependencies(nd res_file)
5356
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
54-
target_link_libraries(nd hidapi::hidraw)
57+
target_link_libraries(nd PUBLIC hidapi::hidraw)
5558
else()
56-
target_link_libraries(nd hidapi)
59+
target_link_libraries(nd PUBLIC hidapi)
5760
endif()
58-
target_link_libraries(nd yaml-cpp)
59-
target_link_libraries(nd fmt)
60-
target_link_libraries(nd scope_guard)
61+
target_link_libraries(nd PRIVATE yaml-cpp)
62+
target_link_libraries(nd PRIVATE fmt)
63+
target_link_libraries(nd PRIVATE $<COMPILE_ONLY:scope_guard>)
6164

6265
if(!MSVC)
6366
target_compile_options(nd -Wall -Wextra -Wpedantic -Werror)
6467
endif()
6568

69+
if(DEFINED NODE_RUNTIME)
70+
# node-libnd
71+
add_definitions(-DNAPI_VERSION=4)
72+
include_directories(${CMAKE_JS_INC})
73+
include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-addon-api)
74+
include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-api-headers/include)
6675

67-
# node-libnd
68-
add_definitions(-DNAPI_VERSION=4)
69-
include_directories(${CMAKE_JS_INC})
70-
include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-addon-api)
71-
include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-api-headers/include)
72-
add_library(node-libnd SHARED src/node.cpp ${CMAKE_JS_SRC})
73-
target_link_libraries(node-libnd nd)
74-
target_link_libraries(node-libnd ${CMAKE_JS_LIB})
76+
add_library(node-libnd SHARED src/node.cpp ${CMAKE_JS_SRC})
77+
target_link_libraries(node-libnd nd)
78+
target_link_libraries(node-libnd ${CMAKE_JS_LIB})
7579

76-
set_target_properties(node-libnd PROPERTIES PREFIX "" SUFFIX ".node")
80+
set_target_properties(node-libnd PROPERTIES PREFIX "" SUFFIX ".node")
81+
endif()
7782

7883
# nudelta
79-
add_executable(nudelta src/main.cpp)
84+
add_executable(nudelta-cli src/main.cpp)
8085
add_compile_definitions(NUDELTA_VERSION="${CMAKE_PROJECT_VERSION}")
81-
target_link_libraries(nudelta nd)
86+
target_link_libraries(nudelta-cli PUBLIC nd)
8287
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
83-
target_link_libraries(nudelta hidapi::hidraw)
88+
target_link_libraries(nudelta-cli PUBLIC hidapi::hidraw)
8489
else()
85-
target_link_libraries(nudelta hidapi)
90+
target_link_libraries(nudelta-cli PUBLIC hidapi)
8691
endif()
87-
target_link_libraries(nudelta yaml-cpp)
88-
target_link_libraries(nudelta fmt)
89-
target_link_libraries(nudelta ssco)
90-
target_link_libraries(nudelta scope_guard)
92+
target_link_libraries(nudelta-cli PRIVATE ssco)
93+
9194

9295

93-
install(TARGETS nudelta)
96+
install(TARGETS nudelta-cli)

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM quay.io/pypa/manylinux_2_28_x86_64
22

33
# Dependencies
4-
## Python
4+
## CMake (pip is the most reliable option)
55
RUN python3 -m ensurepip
66
RUN python3 -m pip install --upgrade pip wheel
77
RUN python3 -m pip install cmake
@@ -15,6 +15,9 @@ RUN npm i -g yarn
1515
## LibUSB
1616
RUN yum install -y libudev-devel
1717

18+
## Ruby
19+
RUN yum install -y ruby
20+
1821
##
1922
WORKDIR /nudelta
2023
COPY . .

Readme.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ Contributions are super appreciated for all of these.
4949
* macOS 11.3+ or higher (Intel or Apple Silicon)
5050
* Linux 2.6.39 or higher with glibc 2.17 or higher.
5151

52-
### Build Requirements
52+
### Build/Development Requirements
5353

5454
* C++17 Compiler
5555
* Clang recommended for macOS/Linux
5656
* MSVC recommended for Windows
5757
* CMake
5858
* Node 22+ with Yarn
59+
* Ruby 2.0.0+ (glue scripts)
5960

6061
## Build
6162

@@ -65,15 +66,31 @@ cd nudelta
6566
git submodule update --init --recursive
6667
rm -rf build
6768
yarn
68-
yarn build-native # CLI / Library
69+
yarn build_native # CLI / Library
6970
yarn build # GUI
7071
```
7172

7273
The Linux AppImage will be found under ./dist, and the Mac app will be found under ./dist/mac.
7374

75+
### Nix
76+
77+
If you have Nix on either macOS or Linux, you can build and run nudelta
78+
using `nix run github:donn/nudelta`.
79+
80+
On NixOS, you will need to add the requisite udev rule as follows:
81+
82+
```nix
83+
services.udev.extraRules = ''
84+
# Hidraw for Seated Users (This rule requires a reboot)
85+
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", TAG+="uaccess"
86+
'';
87+
```
88+
7489
## Using the CLI
7590

76-
You will need to use **sudo** on Linux. On macOS, you will need to grant Input Monitoring permissions to whichever Terminal host you're using to run Nudelta, likely Terminal.app.
91+
You may need to use **sudo** on Linux. On macOS, you will need to grant Input
92+
Monitoring permissions to whichever Terminal host you're using to run Nudelta,
93+
likely Terminal.app.
7794

7895
No special permissions are required on Windows as far as I can tell.
7996

default.nix

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{
22
src ? ./.,
33
lib,
4-
nodejs,
5-
electron,
64
yarnConfigHook,
75
yarnBuildHook,
86
fetchYarnDeps,
97
fetchFromGitHub,
108
cmake,
11-
clangStdenv,
12-
zip,
139
pkg-config,
14-
udev,
10+
ruby,
11+
makeWrapper,
12+
clangStdenv,
13+
nodejs,
14+
electron,
1515
fmt_11,
1616
yaml-cpp,
1717
hidapi,
18+
ssco,
19+
scope_guard,
1820
}:
1921
clangStdenv.mkDerivation {
2022
name = "nudelta";
@@ -23,12 +25,13 @@ clangStdenv.mkDerivation {
2325

2426
nativeBuildInputs = [
2527
yarnConfigHook
26-
yarnBuildHook
2728
cmake
2829
pkg-config
30+
ruby
31+
makeWrapper
2932
];
30-
31-
buildInputs = [fmt_11 yaml-cpp hidapi] ++ lib.optionals clangStdenv.isLinux [udev];
33+
34+
buildInputs = [fmt_11 yaml-cpp hidapi ssco scope_guard];
3235

3336
dontUseCmakeConfigure = true;
3437

@@ -41,22 +44,25 @@ clangStdenv.mkDerivation {
4144
};
4245

4346
env = {
44-
ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
4547
NIX_CFLAGS_COMPILE = "-I${nodejs.dev}/include/node";
4648
};
4749

48-
preBuild = ''
50+
buildPhase = ''
51+
runHook preBuild
52+
4953
substituteInPlace node_modules/cmake-js/lib/dist.js \
5054
--replace-fail '!this.downloaded' 'false'
5155
cp -R ${electron.dist} electron_dist
5256
chmod -R u+w electron_dist
53-
'';
5457
55-
buildPhase = ''
56-
runHook preBuild
57-
yarn build-native
58-
yarn build-ui
59-
yarn electron-builder \
58+
substituteInPlace node_modules/.bin/rollup \
59+
--replace-fail "#!/usr/bin/env node" "#!${nodejs}/bin/node"
60+
substituteInPlace node_modules/.bin/electron-builder \
61+
--replace-fail "#!/usr/bin/env node" "#!${nodejs}/bin/node"
62+
63+
yarn build_native
64+
yarn run rollup -c
65+
yarn run electron-builder \
6066
--dir \
6167
-c.electronDist=electron_dist \
6268
-c.electronVersion=${electron.version}
@@ -67,7 +73,7 @@ clangStdenv.mkDerivation {
6773
''
6874
runHook preInstall
6975
mkdir -p $out/bin
70-
cp -r build/nudelta $out/bin/nudelta-cli
76+
cp -r build/nudelta-cli $out/bin/nudelta-cli
7177
''
7278
+ lib.optionalString clangStdenv.isDarwin ''
7379
mkdir -p $out/Applications
@@ -76,13 +82,7 @@ clangStdenv.mkDerivation {
7682
''
7783
+ lib.optionalString clangStdenv.isLinux ''
7884
mkdir -p "$out/share/nudelta"
79-
cp -r pack/*-unpacked/{locales,resources{,.pak}} "$out/share/nudelta"
80-
81-
pushd assets/generated/icons/png
82-
for file in *.png; do
83-
install -Dm0644 $file $out/share/icons/hicolor/''${file//.png}/apps/nudelta.png
84-
done
85-
popd
85+
cp -r dist/*-unpacked/{locales,resources{,.pak}} "$out/share/nudelta"
8686
'';
8787

8888
postFixup = lib.optionalString clangStdenv.isLinux ''

flake.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
default = pkgs': pkgs: let
1616
callPackage = lib.callPackageWith pkgs';
1717
in {
18+
scope_guard = callPackage ./nix/scope_guard.nix {};
19+
ssco = callPackage ./nix/ssco.nix {};
1820
nudelta = callPackage ./default.nix {src = self;};
1921
};
2022
};

nix/scope_guard.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
lib,
3+
stdenv,
4+
fetchFromGitHub,
5+
cmake,
6+
}:
7+
stdenv.mkDerivation (finalAttrs: {
8+
pname = "scope_guard";
9+
version = "0.9.1";
10+
11+
src = fetchFromGitHub {
12+
owner = "Neargye";
13+
repo = "scope_guard";
14+
rev = "v${finalAttrs.version}";
15+
sha256 = "sha256-Bs8dr5t9LQ6FyM6btpDBAEvyiKoCF2H99RRFAQfx7/I=";
16+
};
17+
18+
nativeBuildInputs = [cmake];
19+
20+
meta = {
21+
homepage = "https://github.com/neargye/scope_guard";
22+
description = "Scope Guard & Defer C++11 Library";
23+
license = lib.licenses.mit;
24+
# maintainers = [ lib.maintainers.donn ];
25+
platforms = lib.platforms.all;
26+
};
27+
})

nix/ssco.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
lib,
3+
stdenv,
4+
fetchFromGitHub,
5+
cmake,
6+
}:
7+
stdenv.mkDerivation (attrs': {
8+
pname = "ssco";
9+
version = "3.0.0";
10+
11+
src = fetchFromGitHub {
12+
owner = "donn";
13+
repo = "ssco";
14+
rev = "v${attrs'.version}";
15+
sha256 = "sha256-qEKMoBRj6jp3x/MIij+Je5QNFm66WueebNzDdWu37iY=";
16+
};
17+
18+
nativeBuildInputs = [cmake];
19+
20+
meta = {
21+
homepage = "https://github.com/donn/ssco";
22+
description = "Tiny Commandline Processing Library for C++17";
23+
license = lib.licenses.mit;
24+
# maintainers = [ lib.maintainers.donn ];
25+
platforms = lib.platforms.all;
26+
};
27+
})

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nudelta",
33
"author": "Mohamed Gaber <me@donn.website>",
4-
"version": "0.9.0",
4+
"version": "0.10.0",
55
"license": "GPL-3.0-or-later",
66
"homepage": "https://github.com/donn/nudelta#readme",
77
"description": "An open-source alternative to the NuPhy Console",
@@ -11,11 +11,11 @@
1111
"yaml": "^2.1.3"
1212
},
1313
"scripts": {
14-
"build": "yarn build-native && yarn build-ui && electron-builder --publish never",
15-
"build-native": "node util/build-native.js",
16-
"build-ui": "rollup -c",
17-
"watch": "nodemon --ext .js,.mjs,.cjs,.css --ignore ui/app-dist/ --exec sh -c \"yarn build-ui && electron .\"",
18-
"watch-win": "nodemon --ext .js,.mjs,.cjs,.css --ignore ui/app-dist/ --exec cmd /c \"yarn build-ui && electron .\"",
14+
"build": "yarn build_native && yarn build_ui && electron-builder --publish never",
15+
"build_native": "node util/build_native.js",
16+
"build_ui": "rollup -c",
17+
"watch": "nodemon --ext .js,.mjs,.cjs,.css --ignore ui/app-dist/ --exec sh -c \"yarn build_ui && electron .\"",
18+
"watch_win": "nodemon --ext .js,.mjs,.cjs,.css --ignore ui/app-dist/ --exec cmd /c \"yarn build_ui && electron .\"",
1919
"format": "prettier --write . && clang-format -i include/*.hpp lib/*.cpp src/*.cpp",
2020
"lint": "prettier --check . && clang-format --dry-run --Werror include/*.hpp lib/*.cpp src/*.cpp",
2121
"compile_licenses": "node util/compile_licenses.js"

0 commit comments

Comments
 (0)