hosts/shinmyoumaru: DHT22 exporting functional

This commit is contained in:
kat witch 2021-09-06 16:17:44 +01:00
parent 3b09618c50
commit 5fa27d3eb2
No known key found for this signature in database
GPG key ID: 1B477797DCA5EC72
3 changed files with 63 additions and 0 deletions

View file

@ -6,6 +6,8 @@
imports = with meta; [
profiles.hardware.raspi
profiles.base
services.dnscrypt-proxy
services.dht22-exporter
./image.nix
];

View file

@ -30,6 +30,16 @@
};
};
services.udev.extraRules = ''
SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660"
SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="${pkgs.runtimeShell} -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="${pkgs.runtimeShell} -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
T
'';
users.groups.gpio = {};
environment.noXlibs = true;
documentation.info.enable = false;
documentation.man.enable = false;

View file

@ -0,0 +1,51 @@
{ config, lib, pkgs, ... }: with lib; let
cfg = config.services.dht22-exporter;
in {
options.services.dht22-exporter.socat = {
enable = mkEnableOption "socat service";
package = mkOption {
type = types.package;
default = pkgs.socat;
};
addresses = mkOption {
type = with types; coercedTo str singleton (listOf str);
default = singleton "::1";
};
};
config = {
systemd.services = mkIf cfg.socat.enable {
dht22-exporter-socat = let
scfg = cfg.socat;
service = singleton "dht22-exporter.service";
in {
after = service;
bindsTo = service;
serviceConfig = {
DynamicUser = true;
};
script = let
port = toString (if cfg.port == null then 8001 else cfg.port);
addresser = addr: "${scfg.package}/bin/socat TCP6-LISTEN:${port},bind=${addr},fork TCP4:localhost:${port}";
lines = map addresser scfg.addresses;
in ''
${concatStringsSep "\n" lines}
'';
};
};
users.users.dht22-exporter = {
isSystemUser = true;
};
services.dht22-exporter = {
enable = true;
platform = "pi";
address = "127.0.0.1";
socat = {
enable = true;
};
user = "dht22-exporter";
group = "gpio";
};
};
}