refactor(esphome): only delete files that finish with -nix.yaml
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2025-02-09 18:50:08 -05:00
parent 6d759fd9a3
commit 8edc73cd41

View file

@ -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: