add nixos module

This commit is contained in:
Robin Appelman 2024-08-10 13:39:16 +02:00
commit a8575345e4
4 changed files with 72 additions and 2 deletions

View file

@ -97,5 +97,17 @@
)
// {
overlays.default = import ./nix/overlay.nix;
nixosModules.default = {
pkgs,
config,
lib,
...
}: {
imports = [./nix/module.nix];
config = lib.mkIf config.services.evtype.enable {
nixpkgs.overlays = [self.overlays.default];
services.evtype.package = lib.mkDefault pkgs.evtype;
};
};
};
}

53
nix/module.nix Normal file
View file

@ -0,0 +1,53 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.evtype;
in {
options.services.evtype = {
enable = mkEnableOption "Enables the evtype service";
package = mkOption {
type = types.package;
description = "package to use";
};
};
config = mkIf cfg.enable {
environment.systemPackages = [cfg.package];
systemd.services.evtype = {
wantedBy = ["multi-user.target"];
description = "EvType daemon";
serviceConfig = {
Restart = "on-failure";
ExecStart = "${cfg.package}/bin/evtype_daemon";
DynamicUser = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
ProtectClock = true;
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = ["AF_UNIX"];
RestrictRealtime = true;
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "any";
RuntimeDirectory = "evtype";
};
};
};
}

View file

@ -4,10 +4,11 @@
lib,
}: let
inherit (lib.sources) sourceByRegex;
inherit (builtins) fromTOML readFile;
in
rustPlatform.buildRustPackage rec {
pname = "evtype";
version = "0.1.0";
version = (fromTOML (readFile ../Cargo.toml)).package.version;
src = sourceByRegex ../. ["Cargo.*" "(src)(/.*)?"];

View file

@ -6,6 +6,7 @@ use std::fs::{create_dir_all, Permissions};
use std::io::Read;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::net::UnixListener;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
use evdev::uinput::VirtualDevice;
@ -37,8 +38,11 @@ fn main() -> Result<(), MainError> {
let mut keyboard = create_device()?;
let path = "/var/run/evtype/evtype.sock";
let dir = Path::new("/var/run/evtype");
create_dir_all("/var/run/evtype")?;
if !dir.exists() {
create_dir_all(dir)?;
}
let listener = UnixListener::bind(path)?;
fs::set_permissions(path, Permissions::from_mode(0o666))?;