wrap uevent-watch in a service

This commit is contained in:
Daniel Barlow 2024-06-02 18:27:59 +01:00
parent f091bbd706
commit 884d8d194e
4 changed files with 76 additions and 5 deletions

View File

@ -4,6 +4,10 @@ let
inherit (lib) mkOption types;
svc = config.system.service;
in {
imports = [
../service-trigger
];
options = {
system.service.wwan = mkOption {
type = liminix.lib.types.serviceDefn;

View File

@ -53,11 +53,16 @@ let
buildInputs = [ modeswitch ];
run = "${uevent-watch}/bin/uevent-watch -s ${modeswitch.name} devtype=usb_device product=12d1/14fe/102";
})
(longrun {
name = "watch-for-modem";
isTrigger = true;
buildInputs = [ atz ];
run = "${uevent-watch}/bin/uevent-watch -n /dev/modem -s ${atz.name} subsystem=tty attrs.idVendor=12d1 attrs.idProduct=1506";
(svc.uevent-rule.build {
service = atz;
terms = {
subsystem = "tty";
attrs = {
idVendor = "12d1";
idProduct = "1506";
};
};
symlink = "/dev/modem";
})
];
};

View File

@ -0,0 +1,37 @@
# this is unlikely to be the final form or location of this code, it's
# an interim module which wraps the uevent-watch command
{ lib, pkgs, config, ... }:
let
inherit (lib) mkOption types;
inherit (pkgs) liminix;
# inherit (pkgs.liminix.services) bundle;
in {
options = {
system.service.uevent-rule = mkOption {
description = "a service which starts other services based on device state (sysfs)";
type = liminix.lib.types.serviceDefn;
};
};
config = {
system.service.uevent-rule = liminix.callService ./rule.nix {
service = mkOption {
description = "the service to run when the rule matches";
type = liminix.lib.types.service;
};
terms = mkOption {
type = types.attrs;
example = {
devtype = "usb_device";
attrs.idVendor = "8086";
};
default = {};
};
symlink = mkOption {
description = "create symlink targeted on devpath";
type = types.nullOr types.str;
default = null;
};
};
};
}

View File

@ -0,0 +1,25 @@
{
liminix
, uevent-watch
, serviceFns
, lib }:
{
service, terms, symlink
}:
let
inherit (liminix.services) longrun;
inherit (lib.attrsets) collect mapAttrsRecursive;
inherit (lib.strings) concatStringsSep;
stringify = attrs :
concatStringsSep " "
(collect lib.isString
(mapAttrsRecursive
(path : value : "${concatStringsSep "." path}=${value}")
attrs));
termsString = stringify terms;
in longrun {
name = "watch-for-${service.name}";
isTrigger = true;
buildInputs = [ service ];
run = "${uevent-watch}/bin/uevent-watch ${if symlink != null then "-n ${symlink}" else ""} -s ${service.name} ${termsString}";
}