-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
110 lines (109 loc) · 4.31 KB
/
flake.nix
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
inputs.nixpkgs.url = "nixpkgs/nixos-23.05";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
let
packageDef = { buildGoModule }: buildGoModule {
pname = "ve-ctrl-tool";
version = "0.0.1";
src = ./.;
vendorSha256 = "sha256-zwJ13y2B8NluCv8IQhQp6k08wedxb+Y2kKFO0k2SOVc=";
};
in
flake-utils.lib.eachDefaultSystem
(system:
rec {
packages = { ve-ctrl-tool = nixpkgs.legacyPackages.${system}.callPackage packageDef { }; };
defaultPackage = packages.ve-ctrl-tool;
devShell =
with import nixpkgs { inherit system; }; mkShell {
packages = [ go nixpkgs-fmt golangci-lint gofumpt ];
};
}) // {
nixosModule = { pkgs, config, lib, ... }:
let ve-ctrl-tool = pkgs.callPackage packageDef { }; in
{
options.services.ve-ess-shelly = {
enable = lib.mkEnableOption "the multiplus + shelly controller";
shellyEM3 = lib.mkOption {
type = lib.types.str;
default = "10.1.0.210";
description = "Address of the shelly EM 3 Energy Meter";
};
metricsAddress = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1:18001";
description = "address to listen on for serving /metrics requests";
};
serialDevice = lib.mkOption {
type = lib.types.str;
default = "/dev/ttyUSB0";
description = "MK3 device";
};
maxCharge = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};
maxInverter = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};
maxInverterPeak = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};
};
config =
let
cfg = config.services.ve-ess-shelly;
in
lib.mkIf cfg.enable {
environment.systemPackages = [ ve-ctrl-tool ]; # add to system because it's handy for debugging
systemd.services.ve-ess-shelly = {
description = "the multiplus + shelly controller";
wantedBy = [ "default.target" ];
unitConfig = {
StartLimitInterval = 0;
};
serviceConfig = {
ExecStart = ''
${ve-ctrl-tool}/bin/ve-ess-shelly \
-metricsHTTP "${cfg.metricsAddress}" \
${lib.optionalString (cfg.maxCharge != null) "-maxCharge ${toString cfg.maxCharge}"} \
${lib.optionalString (cfg.maxInverter != null) "-maxInverter ${toString cfg.maxInverter}"} \
${lib.optionalString (cfg.maxInverterPeak != null) "-maxInverterPeak ${toString cfg.maxInverterPeak}"} \
"${cfg.shellyEM3}"
'';
LockPersonality = true;
CapabilityBoundingSet = "";
DynamicUser = true;
Group = "dialout";
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
PrivateUsers = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "noaccess";
ProtectSystem = "strict";
RemoveIPC = true;
Restart = "always";
RestartSec = "10s";
RestrictAddressFamilies = "AF_INET AF_INET6";
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallErrorNumber = "EPERM";
SystemCallFilter = [ "@system-service" ];
UMask = "0007";
};
};
};
};
};
}