tenseleyflow/hyprkvm / 1042579

Browse files

feat: add home-manager module and improve packaging

- Add home-manager module for declarative service management
- Users can now enable with: services.hyprkvm.enable = true
- Module supports settings for auto-generating config file
- Update contrib/systemd service for system-installed binary
- Remove dev template (users can manually adapt if needed)
Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
10425796ac77153455ddf6c64b089d2d41e3b22f
Parents
c5eafea
Tree
2c5e603

2 changed files

StatusFile+-
M contrib/systemd/hyprkvm.service 2 4
M flake.nix 202 113
contrib/systemd/hyprkvm.servicemodified
@@ -5,10 +5,8 @@ PartOf=graphical-session.target
55
 
66
 [Service]
77
 Type=simple
8
-# Set WorkingDirectory to your hyprKVM repo path
9
-WorkingDirectory=%h/path/to/hyprKVM
10
-# Uses nix develop to provide required libraries
11
-ExecStart=/bin/sh -c 'exec nix develop --command ./target/release/hyprkvm daemon'
8
+# For system-installed binaries (AUR, RPM, etc.)
9
+ExecStart=/usr/bin/hyprkvm daemon
1210
 Restart=on-failure
1311
 RestartSec=1
1412
 
flake.nixmodified
@@ -11,7 +11,93 @@
1111
   };
1212
 
1313
   outputs = { self, nixpkgs, flake-utils, rust-overlay }:
14
-    flake-utils.lib.eachDefaultSystem (system:
14
+    let
15
+      # System-independent outputs
16
+      systemIndependent = {
17
+        # Home-manager module for easy service management
18
+        homeManagerModules.default = { config, lib, pkgs, ... }:
19
+          let
20
+            cfg = config.services.hyprkvm;
21
+          in {
22
+            options.services.hyprkvm = {
23
+              enable = lib.mkEnableOption "HyprKVM daemon";
24
+
25
+              package = lib.mkOption {
26
+                type = lib.types.package;
27
+                default = self.packages.${pkgs.system}.default;
28
+                defaultText = lib.literalExpression "pkgs.hyprkvm";
29
+                description = "The HyprKVM package to use.";
30
+              };
31
+
32
+              settings = lib.mkOption {
33
+                type = lib.types.attrs;
34
+                default = {};
35
+                description = ''
36
+                  Configuration for HyprKVM. Will be written to
37
+                  ~/.config/hyprkvm/hyprkvm.toml
38
+                '';
39
+                example = lib.literalExpression ''
40
+                  {
41
+                    machines = {
42
+                      self_name = "my-machine";
43
+                      neighbors = [
44
+                        { name = "other-machine"; direction = "right"; address = "192.168.1.100:24850"; }
45
+                      ];
46
+                    };
47
+                    network = {
48
+                      listen_port = 24850;
49
+                    };
50
+                  }
51
+                '';
52
+              };
53
+
54
+              extraFlags = lib.mkOption {
55
+                type = lib.types.listOf lib.types.str;
56
+                default = [];
57
+                description = "Extra command-line flags to pass to the daemon.";
58
+              };
59
+            };
60
+
61
+            config = lib.mkIf cfg.enable {
62
+              # Install the package
63
+              home.packages = [ cfg.package ];
64
+
65
+              # Generate config file if settings provided
66
+              xdg.configFile."hyprkvm/hyprkvm.toml" = lib.mkIf (cfg.settings != {}) {
67
+                source = (pkgs.formats.toml {}).generate "hyprkvm.toml" cfg.settings;
68
+              };
69
+
70
+              # Systemd user service
71
+              systemd.user.services.hyprkvm = {
72
+                Unit = {
73
+                  Description = "HyprKVM Daemon - Hyprland-native software KVM switch";
74
+                  After = [ "graphical-session.target" ];
75
+                  PartOf = [ "graphical-session.target" ];
76
+                };
77
+
78
+                Service = {
79
+                  Type = "simple";
80
+                  ExecStart = "${cfg.package}/bin/hyprkvm daemon ${lib.concatStringsSep " " cfg.extraFlags}";
81
+                  Restart = "on-failure";
82
+                  RestartSec = 1;
83
+                  # Exit code 75 triggers restart for direction changes
84
+                  RestartForceExitStatus = [ 75 ];
85
+                  Environment = [ "WAYLAND_DISPLAY=wayland-1" ];
86
+                };
87
+
88
+                Install = {
89
+                  WantedBy = [ "graphical-session.target" ];
90
+                };
91
+              };
92
+            };
93
+          };
94
+
95
+        # Alias for backwards compatibility
96
+        homeManagerModules.hyprkvm = self.homeManagerModules.default;
97
+      };
98
+
99
+      # System-specific outputs (packages, devShells)
100
+      systemSpecific = flake-utils.lib.eachDefaultSystem (system:
15101
         let
16102
           pkgs = import nixpkgs {
17103
             inherit system;
@@ -141,4 +227,7 @@
141227
           };
142228
         }
143229
       );
230
+    in
231
+    # Merge system-independent and system-specific outputs
232
+    systemIndependent // systemSpecific;
144233
 }