summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/shack/node-light.nix
blob: 2e69d5aaaa533518761d15181d3933b1ab8aa65a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{ config, lib, pkgs, ... }:

let
  pkg = pkgs.callPackage (
    pkgs.fetchgit {
      url = "https://git.shackspace.de/rz/node-light.git";
      rev = "90a9347b73af3a9960bd992e6293b357226ef6a0";
      sha256 = "1av9w3w8aknlra25jw6gqxzbb01i9kdlfziy29lwz7mnryjayvwk";
    }) { };
    home = "/var/lib/node-light";
    port = "8082";
in {
  # receive response from light.shack / standby.shack
  networking.firewall.allowedUDPPorts = [ 2342 ];
  users.users.node-light = {
    inherit home;
    isSystemUser = true;
    createHome = true;
  };
  services.nginx.virtualHosts."lounge.light.shack" = {
    locations."/" = {
      proxyPass = "http://localhost:${port}/lounge/";
    };
  };
  services.nginx.virtualHosts."power.light.shack" = {
    locations."/" = {
      proxyPass = "http://localhost:${port}/power/";
    };
  };

  services.nginx.virtualHosts."openhab.shack" = {
    extraConfig = ''
      access_log syslog:server=unix:/dev/log combined if=$loggable;
    '';
    serverAliases = [ "lightapi.shack" ];
    locations."/power/".proxyPass = "http://localhost:${port}/power/";
    locations."/lounge/".proxyPass = "http://localhost:${port}/lounge/";
  };
  systemd.services.node-light= {
    description = "node-light";
    wantedBy = [ "multi-user.target" ];
    environment.PORT = port;
    serviceConfig = {
      User = "node-light";
      # do not override the current storage file
      ExecStartPre = pkgs.writeDash "call-light-pre" ''
        cp -vn  ${pkg}/share/storage.json ${home}
        chmod 700 ${home}/storage.json

      '';
      WorkingDirectory = home;
      ExecStart = "${pkg}/bin/node-light";
      PrivateTmp = true;
      Restart = "always";
      RestartSec = "15";
    };
  };
}