1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.home-assistant;
7
8 # cfg.config != null can be assumed here
9 configFile = pkgs.writeText "configuration.json"
10 (builtins.toJSON (if cfg.applyDefaultConfig then
11 (lib.recursiveUpdate defaultConfig cfg.config) else cfg.config));
12
13 availableComponents = pkgs.home-assistant.availableComponents;
14
15 # Given component "parentConfig.platform", returns whether config.parentConfig
16 # is a list containing a set with set.platform == "platform".
17 #
18 # For example, the component sensor.luftdaten is used as follows:
19 # config.sensor = [ {
20 # platform = "luftdaten";
21 # ...
22 # } ];
23 useComponentPlatform = component:
24 let
25 path = splitString "." component;
26 parentConfig = attrByPath (init path) null cfg.config;
27 platform = last path;
28 in isList parentConfig && any
29 (item: item.platform or null == platform)
30 parentConfig;
31
32 # Returns whether component is used in config
33 useComponent = component:
34 hasAttrByPath (splitString "." component) cfg.config
35 || useComponentPlatform component;
36
37 # List of components used in config
38 extraComponents = filter useComponent availableComponents;
39
40 package = if (cfg.autoExtraComponents && cfg.config != null)
41 then (cfg.package.override { inherit extraComponents; })
42 else cfg.package;
43
44 # If you are changing this, please update the description in applyDefaultConfig
45 defaultConfig = {
46 homeassistant.time_zone = config.time.timeZone;
47 http.server_port = (toString cfg.port);
48 };
49
50in {
51 meta.maintainers = with maintainers; [ dotlambda ];
52
53 options.services.home-assistant = {
54 enable = mkEnableOption "Home Assistant";
55
56 configDir = mkOption {
57 default = "/var/lib/hass";
58 type = types.path;
59 description = "The config directory, where your <filename>configuration.yaml</filename> is located.";
60 };
61
62 port = mkOption {
63 default = 8123;
64 type = types.int;
65 description = "The port on which to listen.";
66 };
67
68 applyDefaultConfig = mkOption {
69 default = true;
70 type = types.bool;
71 description = ''
72 Setting this option enables a few configuration options for HA based on NixOS configuration (such as time zone) to avoid having to manually specify configuration we already have.
73 </para>
74 <para>
75 Currently one side effect of enabling this is that the <literal>http</literal> component will be enabled.
76 </para>
77 <para>
78 This only takes effect if <literal>config != null</literal> in order to ensure that a manually managed <filename>configuration.yaml</filename> is not overwritten.
79 '';
80 };
81
82 config = mkOption {
83 default = null;
84 type = with types; nullOr attrs;
85 example = literalExample ''
86 {
87 homeassistant = {
88 name = "Home";
89 time_zone = "UTC";
90 };
91 frontend = { };
92 http = { };
93 feedreader.urls = [ "https://nixos.org/blogs.xml" ];
94 }
95 '';
96 description = ''
97 Your <filename>configuration.yaml</filename> as a Nix attribute set.
98 Beware that setting this option will delete your previous <filename>configuration.yaml</filename>.
99 '';
100 };
101
102 package = mkOption {
103 default = pkgs.home-assistant;
104 defaultText = "pkgs.home-assistant";
105 type = types.package;
106 example = literalExample ''
107 pkgs.home-assistant.override {
108 extraPackages = ps: with ps; [ colorlog ];
109 }
110 '';
111 description = ''
112 Home Assistant package to use.
113 Override <literal>extraPackages</literal> or <literal>extraComponents</literal> in order to add additional dependencies.
114 If you specify <option>config</option> and do not set <option>autoExtraComponents</option>
115 to <literal>false</literal>, overriding <literal>extraComponents</literal> will have no effect.
116 '';
117 };
118
119 autoExtraComponents = mkOption {
120 default = true;
121 type = types.bool;
122 description = ''
123 If set to <literal>true</literal>, the components used in <literal>config</literal>
124 are set as the specified package's <literal>extraComponents</literal>.
125 This in turn adds all packaged dependencies to the derivation.
126 You might still see import errors in your log.
127 In this case, you will need to package the necessary dependencies yourself
128 or ask for someone else to package them.
129 If a dependency is packaged but not automatically added to this list,
130 you might need to specify it in <literal>extraPackages</literal>.
131 '';
132 };
133
134 openFirewall = mkOption {
135 default = false;
136 type = types.bool;
137 description = "Whether to open the firewall for the specified port.";
138 };
139 };
140
141 config = mkIf cfg.enable {
142 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
143
144 systemd.services.home-assistant = {
145 description = "Home Assistant";
146 after = [ "network.target" ];
147 preStart = lib.optionalString (cfg.config != null) ''
148 config=${cfg.configDir}/configuration.yaml
149 rm -f $config
150 ${pkgs.remarshal}/bin/json2yaml -i ${configFile} -o $config
151 chmod 444 $config
152 '';
153 serviceConfig = {
154 ExecStart = "${package}/bin/hass --config '${cfg.configDir}'";
155 User = "hass";
156 Group = "hass";
157 Restart = "on-failure";
158 ProtectSystem = "strict";
159 ReadWritePaths = "${cfg.configDir}";
160 PrivateTmp = true;
161 RemoveIPC = true;
162 };
163 path = [
164 "/run/wrappers" # needed for ping
165 ];
166 };
167
168 systemd.targets.home-assistant = rec {
169 description = "Home Assistant";
170 wantedBy = [ "multi-user.target" ];
171 wants = [ "home-assistant.service" ];
172 after = wants;
173 };
174
175 users.users.hass = {
176 home = cfg.configDir;
177 createHome = true;
178 group = "hass";
179 uid = config.ids.uids.hass;
180 };
181
182 users.groups.hass.gid = config.ids.gids.hass;
183 };
184}