-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflake.nix
129 lines (127 loc) · 3.98 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{
description = "gohome home automation";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
# System specific outputs becomes: packages.<system>.default
packages.default = pkgs.buildGoModule {
pname = "gohome";
version = "0.0.1";
src = ./.;
vendorHash = "sha256-JxToHZUROCvffMN9hZy/W7m2G9w/GaxHU9fY4sQqCuQ=";
};
}
)
// {
# Flake-wide outputs
homeManagerModules = {
default =
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.programs.gohome;
pkg = self.packages.${pkgs.hostPlatform.system}.default;
serviceOptionsType =
with types;
let
primitive = oneOf [
bool
int
str
path
];
in
attrsOf (attrsOf (attrsOf (either primitive (listOf primitive))));
in
{
options = {
programs.gohome = {
enable = mkEnableOption "Gohome";
extraServiceOptions = mkOption {
type = serviceOptionsType;
default = { };
description = "Extra systemd options";
};
mqtt = mkOption {
type = types.str;
default = "tcp://mqtt:1883";
description = "mqtt url";
};
path = mkOption {
type = types.str;
default = "";
description = "Set PATH environment variable";
};
services = mkOption {
type = types.listOf types.str;
default = [ ];
description = "List of gohome services to enable";
};
};
};
config = mkMerge [
(mkIf cfg.enable {
home.packages = [
pkg
pkgs.coreutils
pkgs.systemd
pkgs.tcpdump
];
})
(mkIf (length cfg.services != 0) {
systemd.user.enable = true;
# home manager services doesn't appear to support templated services, so
# produce a service unit per instance.
systemd.user.services =
let
makeService = (
n: {
name = "gohome@${n}";
value = {
Install.WantedBy = [ "default.target" ];
Unit = {
Description = "gohome service ${n}";
StartLimitIntervalSec = "0";
};
Service = {
Environment = [
"GOHOME_MQTT=${cfg.mqtt}"
"GOHOME_API=http://localhost:8723/"
"PATH=${cfg.path}"
];
ExecStart = "${pkg}/bin/gohome run ${n}";
Restart = "always";
RestartSec = "5s";
WatchdogSec = "120s";
Type = "notify";
NotifyAccess = "main";
};
} // cfg.extraServiceOptions;
}
);
in
listToAttrs (map makeService cfg.services);
})
];
};
};
};
}