From 8edc73cd41fee7faab50be1fd94ef464776baf67 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Sun, 9 Feb 2025 18:50:08 -0500 Subject: [PATCH] refactor(esphome): only delete files that finish with -nix.yaml --- modules/esphome-plus/default.nix | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/modules/esphome-plus/default.nix b/modules/esphome-plus/default.nix index 177278bb..07db3c08 100644 --- a/modules/esphome-plus/default.nix +++ b/modules/esphome-plus/default.nix @@ -6,7 +6,7 @@ }: let inherit (lib) converge getExe mkOption types; inherit (lib.modules) mkForce mkIf; - inherit (lib.lists) elem optionals; + inherit (lib.lists) elem; inherit (lib.strings) concatMapStringsSep optionalString; inherit (lib.attrsets) mapAttrsToList filterAttrsRecursive optionalAttrs; @@ -16,11 +16,11 @@ format = pkgs.formats.yaml {}; # Adapted from https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/home-automation/home-assistant.nix - mkESPConf = name: cfg: let + mkESPConf = n: cfg: let filteredConfig = converge (filterAttrsRecursive (_: v: ! elem v [null])) cfg; - in { - name = "${name}.yaml"; - file = pkgs.runCommandLocal "${name}.yaml" {} '' + in rec { + name = "${n}-nix.yaml"; + file = pkgs.runCommandLocal name {} '' cp ${format.generate name filteredConfig} $out sed -i -e "s/'\!\([a-z_]\+\) \(.*\)'/\!\1 \2/;s/^\!\!/\!/;" $out sed -i 's/ {}//g' $out @@ -39,11 +39,6 @@ in { default = null; type = types.nullOr types.path; }; - - deleteUnmanaged = mkOption { - default = true; - type = types.bool; - }; }; config = mkIf cfg.enable { @@ -99,11 +94,17 @@ in { ExecStartPre = getExe (pkgs.writeShellApplication { name = "esphome-exec-start-pre"; - runtimeInputs = optionals cfg.deleteUnmanaged [ + runtimeInputs = [ pkgs.findutils ]; text = '' + shopt -s nullglob + + for file in ${stateDir}/*-nix.yaml; do + rm "$file" + done + ${optionalString (cfg.secretsFile != null) # bash @@ -111,13 +112,6 @@ in { cp -f "$(realpath "${cfg.secretsFile}")" ${stateDir}/secrets.yaml ''} - ${optionalString - cfg.deleteUnmanaged - # bash - '' - find ${stateDir} -name "*.yaml" ! -name "secrets.yaml" -delete - ''} - ${concatMapStringsSep "\n" (dev: