From 3e3b5895ca2088126219559768e412902b137ee0 Mon Sep 17 00:00:00 2001 From: Peter Hoeg Date: Sat, 21 Apr 2018 22:30:59 +0800 Subject: [PATCH 1/4] remarshal: use python3 instead of python2 Officially both python2 and 3 are supported. --- pkgs/development/tools/remarshal/default.nix | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pkgs/development/tools/remarshal/default.nix b/pkgs/development/tools/remarshal/default.nix index baba4fd75ea..23c9768f7e0 100644 --- a/pkgs/development/tools/remarshal/default.nix +++ b/pkgs/development/tools/remarshal/default.nix @@ -1,7 +1,7 @@ -{ stdenv, pythonPackages, fetchFromGitHub }: +{ stdenv, python3Packages, fetchFromGitHub }: -pythonPackages.buildPythonApplication rec { - name = "remarshal-${version}"; +python3Packages.buildPythonApplication rec { + pname = "remarshal"; version = "0.7.0"; src = fetchFromGitHub { @@ -11,10 +11,8 @@ pythonPackages.buildPythonApplication rec { sha256 = "1wsgvzfp40lvly7nyyhv9prip4vi32rfc8kdji587jpw28zc1dfb"; }; - propagatedBuildInputs = with pythonPackages; [ - dateutil - pytoml - pyyaml + propagatedBuildInputs = with python3Packages; [ + dateutil pytoml pyyaml ]; meta = with stdenv.lib; { From 4cd88807d8434efdc3fb666767098e2b3ee0b812 Mon Sep 17 00:00:00 2001 From: Peter Hoeg Date: Sat, 21 Apr 2018 22:32:09 +0800 Subject: [PATCH 2/4] home-assistant: make port configurable so we can use it elsewhere Additionally, some settings based on NixOS configuation is set via defaultConfig which is then merged with the user provided configration. For now that just means http port and time zone but others can easily be added. --- .../modules/services/misc/home-assistant.nix | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/misc/home-assistant.nix b/nixos/modules/services/misc/home-assistant.nix index ac37c11106e..07b14bb6764 100644 --- a/nixos/modules/services/misc/home-assistant.nix +++ b/nixos/modules/services/misc/home-assistant.nix @@ -5,7 +5,9 @@ with lib; let cfg = config.services.home-assistant; - configFile = pkgs.writeText "configuration.yaml" (builtins.toJSON cfg.config); + configFile = pkgs.writeText "configuration.json" (builtins.toJSON (if cfg.applyDefaultConfig + then (lib.recursiveUpdate defaultConfig (if (isNull cfg.config) then {} else cfg.config)) + else cfg.config)); availableComponents = pkgs.home-assistant.availableComponents; @@ -38,6 +40,12 @@ let then (cfg.package.override { inherit extraComponents; }) else cfg.package; + # If you are changing this, please update the description in applyDefaultConfig + defaultConfig = { + homeassistant.time_zone = config.time.timeZone; + http.server_port = (toString cfg.port); + }; + in { meta.maintainers = with maintainers; [ dotlambda ]; @@ -50,6 +58,26 @@ in { description = "The config directory, where your configuration.yaml is located."; }; + port = mkOption { + default = 8123; + type = types.int; + description = "The port on which to listen."; + }; + + applyDefaultConfig = mkOption { + default = true; + type = types.bool; + description = '' + 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. + + + Currently one side effect of enabling this is that the http component will be enabled. + + + This only takes effect if config != null in order to ensure that a manually managed configuration.yaml is not overwritten. + ''; + }; + config = mkOption { default = null; type = with types; nullOr attrs; @@ -110,15 +138,14 @@ in { ln -s ${configFile} ${cfg.configDir}/configuration.yaml ''; serviceConfig = { - ExecStart = '' - ${package}/bin/hass --config "${cfg.configDir}" - ''; + ExecStart = "${package}/bin/hass --config '${cfg.configDir}'"; User = "hass"; Group = "hass"; Restart = "on-failure"; ProtectSystem = "strict"; ReadWritePaths = "${cfg.configDir}"; PrivateTmp = true; + RemoveIPC = true; }; path = [ "/run/wrappers" # needed for ping From b886faa6b60eb67bc9c09484673998924083c12b Mon Sep 17 00:00:00 2001 From: Peter Hoeg Date: Sat, 21 Apr 2018 22:33:32 +0800 Subject: [PATCH 3/4] home-assistant: use remarshal to convert configuration to YAML HA doesn't mind the configuration being JSON instead of YAML but since YAML is the official language, use that as it allows users to easily exchange config data with other parties in the community. --- nixos/modules/services/misc/home-assistant.nix | 6 ++++-- nixos/tests/home-assistant.nix | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/nixos/modules/services/misc/home-assistant.nix b/nixos/modules/services/misc/home-assistant.nix index 07b14bb6764..b9a97296779 100644 --- a/nixos/modules/services/misc/home-assistant.nix +++ b/nixos/modules/services/misc/home-assistant.nix @@ -134,8 +134,10 @@ in { description = "Home Assistant"; after = [ "network.target" ]; preStart = lib.optionalString (cfg.config != null) '' - rm -f ${cfg.configDir}/configuration.yaml - ln -s ${configFile} ${cfg.configDir}/configuration.yaml + config=${cfg.configDir}/configuration.yaml + rm -f $config + ${pkgs.remarshal}/bin/json2yaml -i ${configFile} -o $config + chmod 444 $config ''; serviceConfig = { ExecStart = "${package}/bin/hass --config '${cfg.configDir}'"; diff --git a/nixos/tests/home-assistant.nix b/nixos/tests/home-assistant.nix index 2e45dc78471..4ebccb7ab86 100644 --- a/nixos/tests/home-assistant.nix +++ b/nixos/tests/home-assistant.nix @@ -51,9 +51,9 @@ in { startAll; $hass->waitForUnit("home-assistant.service"); - # Since config is specified using a Nix attribute set, - # configuration.yaml is a link to the Nix store - $hass->succeed("test -L ${configDir}/configuration.yaml"); + # The config is specified using a Nix attribute set, + # but then converted from JSON to YAML + $hass->succeed("test -f ${configDir}/configuration.yaml"); # Check that Home Assistant's web interface and API can be reached $hass->waitForOpenPort(8123); From e4a6e320bb8f2adb79a3c9b83478da753de4eedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=BCtz?= Date: Tue, 24 Apr 2018 00:12:19 +0200 Subject: [PATCH 4/4] home-assistant: simplify definition of configFile --- nixos/modules/services/misc/home-assistant.nix | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/misc/home-assistant.nix b/nixos/modules/services/misc/home-assistant.nix index b9a97296779..1dc7b44ee37 100644 --- a/nixos/modules/services/misc/home-assistant.nix +++ b/nixos/modules/services/misc/home-assistant.nix @@ -5,9 +5,10 @@ with lib; let cfg = config.services.home-assistant; - configFile = pkgs.writeText "configuration.json" (builtins.toJSON (if cfg.applyDefaultConfig - then (lib.recursiveUpdate defaultConfig (if (isNull cfg.config) then {} else cfg.config)) - else cfg.config)); + # cfg.config != null can be assumed here + configFile = pkgs.writeText "configuration.json" + (builtins.toJSON (if cfg.applyDefaultConfig then + (lib.recursiveUpdate defaultConfig cfg.config) else cfg.config)); availableComponents = pkgs.home-assistant.availableComponents;