lol

nixos/spacenavd: run as user service

authored by

sohalt and committed by
Gabriel Ebner
be01cb8b f8ed8033

+175 -2
+1 -2
nixos/modules/services/hardware/spacenavd.nix
··· 13 13 }; 14 14 15 15 config = mkIf cfg.enable { 16 - systemd.services.spacenavd = { 16 + systemd.user.services.spacenavd = { 17 17 description = "Daemon for the Spacenavigator 6DOF mice by 3Dconnexion"; 18 18 after = [ "syslog.target" ]; 19 19 wantedBy = [ "graphical.target" ]; 20 20 serviceConfig = { 21 21 ExecStart = "${pkgs.spacenavd}/bin/spacenavd -d -l syslog"; 22 - StandardError = "syslog"; 23 22 }; 24 23 }; 25 24 };
+47
pkgs/development/libraries/libspnav/configure-socket-path.patch
··· 1 + diff --git a/spnav.c b/spnav.c 2 + index f9e10f8..27149f7 100644 3 + --- a/spnav.c 4 + +++ b/spnav.c 5 + @@ -36,7 +36,7 @@ OF SUCH DAMAGE. 6 + #include <sys/select.h> 7 + #include "spnav.h" 8 + 9 + -#define SPNAV_SOCK_PATH "/var/run/spnav.sock" 10 + +#define DEFAULT_SPNAV_SOCK_PATH "/run/spnav.sock" 11 + 12 + #ifdef USE_X11 13 + #include <X11/Xlib.h> 14 + @@ -70,6 +70,24 @@ static struct event_node *ev_queue, *ev_queue_tail; 15 + /* AF_UNIX socket used for alternative communication with daemon */ 16 + static int sock = -1; 17 + 18 + +static char *spath = NULL; 19 + + 20 + +static char *socket_path() 21 + +{ 22 + + char *xdg_runtime_dir; 23 + + if((xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"))) { 24 + + if ( spath == NULL ) { 25 + + spath = malloc(strlen(xdg_runtime_dir) + strlen("/spnav.sock") + 1); 26 + + if ( spath != NULL ) { 27 + + sprintf(spath, "%s/spnav.sock", xdg_runtime_dir); 28 + + } 29 + + } 30 + + if(access(spath, F_OK) != -1){ 31 + + return spath; 32 + + } 33 + + } 34 + + return DEFAULT_SPNAV_SOCK_PATH; 35 + +} 36 + 37 + int spnav_open(void) 38 + { 39 + @@ -92,7 +110,7 @@ int spnav_open(void) 40 + 41 + memset(&addr, 0, sizeof addr); 42 + addr.sun_family = AF_UNIX; 43 + - strncpy(addr.sun_path, SPNAV_SOCK_PATH, sizeof(addr.sun_path)); 44 + + strncpy(addr.sun_path, socket_path(), sizeof(addr.sun_path)); 45 + 46 + 47 + if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
+6
pkgs/development/libraries/libspnav/default.nix
··· 14 14 nativeBuildInputs = lib.optional stdenv.isDarwin fixDarwinDylibNames; 15 15 buildInputs = [ libX11 ]; 16 16 17 + patches = [ 18 + # Changes the socket path from /run/spnav.sock to $XDG_RUNTIME_DIR/spnav.sock 19 + # to allow for a user service 20 + ./configure-socket-path.patch 21 + ]; 22 + 17 23 configureFlags = [ "--disable-debug"]; 18 24 makeFlags = [ "CC=${stdenv.cc.targetPrefix}cc" ]; 19 25
+118
pkgs/misc/drivers/spacenavd/configure-socket-path.patch
··· 1 + diff --git a/src/proto_unix.c b/src/proto_unix.c 2 + index 998f234..d38452c 100644 3 + --- a/src/proto_unix.c 4 + +++ b/src/proto_unix.c 5 + @@ -36,11 +36,14 @@ enum { 6 + 7 + static int lsock = -1; 8 + 9 + +static char *spath = NULL; 10 + + 11 + int init_unix(void) 12 + { 13 + int s; 14 + mode_t prev_umask; 15 + struct sockaddr_un addr; 16 + + char *sock_path; 17 + 18 + if(lsock >= 0) return 0; 19 + 20 + @@ -49,16 +52,18 @@ int init_unix(void) 21 + return -1; 22 + } 23 + 24 + - unlink(SOCK_NAME); /* in case it already exists */ 25 + + sock_path = socket_path(); 26 + + 27 + + unlink(sock_path); /* in case it already exists */ 28 + 29 + memset(&addr, 0, sizeof addr); 30 + addr.sun_family = AF_UNIX; 31 + - strcpy(addr.sun_path, SOCK_NAME); 32 + + strcpy(addr.sun_path, sock_path); 33 + 34 + prev_umask = umask(0); 35 + 36 + if(bind(s, (struct sockaddr*)&addr, sizeof addr) == -1) { 37 + - logmsg(LOG_ERR, "failed to bind unix socket: %s: %s\n", SOCK_NAME, strerror(errno)); 38 + + logmsg(LOG_ERR, "failed to bind unix socket: %s: %s\n", sock_path, strerror(errno)); 39 + close(s); 40 + return -1; 41 + } 42 + @@ -68,7 +73,7 @@ int init_unix(void) 43 + if(listen(s, 8) == -1) { 44 + logmsg(LOG_ERR, "listen failed: %s\n", strerror(errno)); 45 + close(s); 46 + - unlink(SOCK_NAME); 47 + + unlink(sock_path); 48 + return -1; 49 + } 50 + 51 + @@ -82,7 +87,7 @@ void close_unix(void) 52 + close(lsock); 53 + lsock = -1; 54 + 55 + - unlink(SOCK_NAME); 56 + + unlink(socket_path()); 57 + } 58 + } 59 + 60 + @@ -173,3 +178,19 @@ int handle_uevents(fd_set *rset) 61 + 62 + return 0; 63 + } 64 + + 65 + +char *socket_path(void) 66 + +{ 67 + + char *xdg_runtime_dir; 68 + + if((xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"))) { 69 + + if ( spath == NULL ) { 70 + + spath = malloc(strlen(xdg_runtime_dir) + strlen("/spnav.sock") + 1); 71 + + if ( spath != NULL ) { 72 + + sprintf(spath, "%s/spnav.sock", xdg_runtime_dir); 73 + + } 74 + + }; 75 + + return spath; 76 + + } else { 77 + + return DEFAULT_SOCK_NAME; 78 + + } 79 + +} 80 + diff --git a/src/proto_unix.h b/src/proto_unix.h 81 + index 045b379..ec4509c 100644 82 + --- a/src/proto_unix.h 83 + +++ b/src/proto_unix.h 84 + @@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. 85 + #include "event.h" 86 + #include "client.h" 87 + 88 + +char *socket_path(void); 89 + int init_unix(void); 90 + void close_unix(void); 91 + int get_unix_socket(void); 92 + diff --git a/src/spnavd.c b/src/spnavd.c 93 + index cbea191..03080da 100644 94 + --- a/src/spnavd.c 95 + +++ b/src/spnavd.c 96 + @@ -344,7 +344,7 @@ static int find_running_daemon(void) 97 + } 98 + memset(&addr, 0, sizeof addr); 99 + addr.sun_family = AF_UNIX; 100 + - strncpy(addr.sun_path, SOCK_NAME, sizeof addr.sun_path); 101 + + strncpy(addr.sun_path, socket_path(), sizeof addr.sun_path); 102 + 103 + if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) { 104 + close(s); 105 + diff --git a/src/spnavd.h b/src/spnavd.h 106 + index fa0a916..2d1c48b 100644 107 + --- a/src/spnavd.h 108 + +++ b/src/spnavd.h 109 + @@ -26,8 +26,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. 110 + #define DEF_CFGFILE "/etc/spnavrc" 111 + #define DEF_LOGFILE "/var/log/spnavd.log" 112 + 113 + -#define SOCK_NAME "/var/run/spnav.sock" 114 + #define PIDFILE "/var/run/spnavd.pid" 115 + +#define DEFAULT_SOCK_NAME "/run/spnav.sock" 116 + #define SYSLOG_ID "spnavd" 117 + 118 + /* Multiple devices support */
+3
pkgs/misc/drivers/spacenavd/default.nix
··· 17 17 url = "https://github.com/FreeSpacenav/spacenavd/commit/d6a25d5c3f49b9676d039775efc8bf854737c43c.patch"; 18 18 sha256 = "02pdgcvaqc20qf9hi3r73nb9ds7yk2ps9nnxaj0x9p50xjnhfg5c"; 19 19 }) 20 + # Changes the socket path from /run/spnav.sock to $XDG_RUNTIME_DIR/spnav.sock 21 + # to allow for a user service 22 + ./configure-socket-path.patch 20 23 ]; 21 24 22 25 buildInputs = [ libX11 ]