From 5fa27d3eb205a1d17861bcd82e831e66fd288988 Mon Sep 17 00:00:00 2001 From: kat witch Date: Mon, 6 Sep 2021 16:17:44 +0100 Subject: [PATCH] hosts/shinmyoumaru: DHT22 exporting functional --- config/hosts/shinmyoumaru/nixos.nix | 2 + config/profiles/hardware/raspi.nix | 10 +++++ config/services/dht22-exporter/default.nix | 51 ++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 config/services/dht22-exporter/default.nix diff --git a/config/hosts/shinmyoumaru/nixos.nix b/config/hosts/shinmyoumaru/nixos.nix index 7c11925d..15355d18 100644 --- a/config/hosts/shinmyoumaru/nixos.nix +++ b/config/hosts/shinmyoumaru/nixos.nix @@ -6,6 +6,8 @@ imports = with meta; [ profiles.hardware.raspi profiles.base + services.dnscrypt-proxy + services.dht22-exporter ./image.nix ]; diff --git a/config/profiles/hardware/raspi.nix b/config/profiles/hardware/raspi.nix index 041bcdc2..10f2736a 100644 --- a/config/profiles/hardware/raspi.nix +++ b/config/profiles/hardware/raspi.nix @@ -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; diff --git a/config/services/dht22-exporter/default.nix b/config/services/dht22-exporter/default.nix new file mode 100644 index 00000000..0ca6969a --- /dev/null +++ b/config/services/dht22-exporter/default.nix @@ -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"; + }; + }; +}