@@ -0,0 +1,157 @@
1+{ lib
2+, python3
3+, pkgconfig
4+, wrapGAppsHook
5+, gettext
6+, gtk3
7+, glib
8+, dbus
9+, gobject-introspection
10+, xmodmap
11+, pygobject3
12+, setuptools
13+, evdev
14+, pydantic
15+, pydbus
16+, psutil
17+, fetchFromGitHub
18+, buildPythonApplication
19+, procps
20+, gtksourceview4
21+ # Change the default log level to debug for easier debugging of package issues
22+, withDebugLogLevel ? false
23+ # Xmodmap is an optional dependency
24+ # If you use Xmodmap to set keyboard mappings (or your DE does)
25+ # it is required to correctly map keys
26+, withXmodmap ? true
27+ # Version and rev and hash are package arguments to allow overriding
28+ # while ensuring the values in prePatch and src match
29+ # https://discourse.nixos.org/t/avoid-rec-expresions-in-nixpkgs/8293/7
30+ # The names are prefixed with input_remapper to avoid potential
31+ # collisions with package names
32+, input_remapper_version ? "unstable-2022-02-09"
33+, input_remapper_src_rev ? "55227e0b5a28d21d7333c6c8ea1c691e56fd35c4"
34+, input_remapper_src_hash ? "sha256-kzGlEaYN/JfAgbI0aMLr5mwObYOL43X7QU/ihDEBQFg="
35+}:
36+
37+let
38+ maybeXmodmap = lib.optional withXmodmap xmodmap;
39+in
40+buildPythonApplication {
41+ pname = "input-remapper";
42+ version = input_remapper_version;
43+
44+ src = fetchFromGitHub {
45+ rev = input_remapper_src_rev;
46+ owner = "sezanzeb";
47+ repo = "input-remapper";
48+ hash = input_remapper_src_hash;
49+ };
50+
51+ # Fixes error
52+ # Couldn’t recognize the image file format for file "*.svg"
53+ # at startup, see https://github.com/NixOS/nixpkgs/issues/56943
54+ strictDeps = false;
55+
56+ prePatch = ''
57+ # set revision for --version output
58+ echo "COMMIT_HASH = '${input_remapper_src_rev}'" > inputremapper/commit_hash.py
59+
60+ # fix FHS paths
61+ substituteInPlace inputremapper/configs/data.py \
62+ --replace "/usr/share/input-remapper" "$out/usr/share/input-remapper"
63+ '' + (lib.optionalString (withDebugLogLevel) ''
64+ # if debugging
65+ substituteInPlace inputremapper/logger.py --replace "logger.setLevel(logging.INFO)" "logger.setLevel(logging.DEBUG)"
66+ '');
67+
68+ doCheck = true;
69+ checkInputs = [
70+ psutil
71+ ];
72+ pythonImportsCheck = [
73+ "evdev"
74+ "inputremapper"
75+ ];
76+
77+ # Custom test script, can't use plain pytest / pytestCheckHook
78+ # We only run tests in the unit folder, integration tests require UI
79+ # To allow tests which access the system and session DBUS to run, we start a dbus session
80+ # and bind it to both the system and session buses
81+ installCheckPhase = ''
82+ echo "<busconfig>
83+ <type>session</type>
84+ <listen>unix:tmpdir=$TMPDIR</listen>
85+ <listen>unix:path=/build/system_bus_socket</listen>
86+ <standard_session_servicedirs/>
87+ <policy context=\"default\">
88+ <!-- Allow everything to be sent -->
89+ <allow send_destination=\"*\" eavesdrop=\"true\"/>
90+ <!-- Allow everything to be received -->
91+ <allow eavesdrop=\"true\"/>
92+ <!-- Allow anyone to own anything -->
93+ <allow own=\"*\"/>
94+ </policy>
95+ </busconfig>" > dbus.cfg
96+ PATH=${lib.makeBinPath ([ dbus procps ] ++ maybeXmodmap)}:$PATH \
97+ USER="$(id -u -n)" \
98+ DBUS_SYSTEM_BUS_ADDRESS=unix:path=/build/system_bus_socket \
99+ ${dbus}/bin/dbus-run-session --config-file dbus.cfg \
100+ python tests/test.py --start-dir unit
101+ '';
102+
103+ # Nixpkgs 15.9.4.3. When using wrapGAppsHook with special derivers you can end up with double wrapped binaries.
104+ dontWrapGApps = true;
105+ preFixup = ''
106+ makeWrapperArgs+=(
107+ "''${gappsWrapperArgs[@]}"
108+ --prefix PATH : "${lib.makeBinPath maybeXmodmap}"
109+ )
110+ '';
111+
112+ nativeBuildInputs = [
113+ wrapGAppsHook
114+ gettext # needed to build translations
115+ gtk3
116+ glib
117+ gobject-introspection
118+ pygobject3
119+ ] ++ maybeXmodmap;
120+
121+ propagatedBuildInputs = [
122+ setuptools # needs pkg_resources
123+ pygobject3
124+ evdev
125+ pkgconfig
126+ pydantic
127+ pydbus
128+ gtksourceview4
129+ ];
130+
131+ postInstall = ''
132+ sed -r "s#RUN\+\=\"/bin/input-remapper-control#RUN\+\=\"$out/bin/input-remapper-control#g" -i data/99-input-remapper.rules
133+ sed -r "s#ExecStart\=/usr/bin/input-remapper-service#ExecStart\=$out/bin/input-remapper-service#g" -i data/input-remapper.service
134+
135+ chmod +x data/*.desktop
136+
137+ install -D -t $out/share/applications/ data/*.desktop
138+ install -D -t $out/share/polkit-1/actions/ data/input-remapper.policy
139+ install -D data/99-input-remapper.rules $out/etc/udev/rules.d/99-input-remapper.rules
140+ install -D data/input-remapper.service $out/lib/systemd/system/input-remapper.service
141+ install -D data/input-remapper.policy $out/share/polkit-1/actions/input-remapper.policy
142+ install -D data/inputremapper.Control.conf $out/etc/dbus-1/system.d/inputremapper.Control.conf
143+ install -D -t $out/usr/share/input-remapper/ data/*
144+
145+ # Only install input-remapper prefixed binaries, we don't care about deprecated key-mapper ones
146+ install -m755 -D -t $out/bin/ bin/input-remapper*
147+ '';
148+
149+ meta = with lib; {
150+ description = "An easy to use tool to change the mapping of your input device buttons";
151+ homepage = "https://github.com/sezanzeb/input-remapper";
152+ license = licenses.gpl3Plus;
153+ platforms = platforms.linux;
154+ maintainers = with maintainers; [ LunNova ];
155+ mainProgram = "input-remapper-gtk";
156+ };
157+}