diff options
author | pennae <pennae.git@eno.space> | 2023-09-25 19:03:24 +0200 |
---|---|---|
committer | pennae <pennae.git@eno.space> | 2023-09-25 19:08:29 +0200 |
commit | 6b1fb96a160e3d6500a182c6c31f74cf85bd819f (patch) | |
tree | 948ca67ad9bd599aa56de83fa3a3c01bf23b3ab0 /openwrt/uci.nix | |
parent | 66c6d2c1dfd4b3ef222bb64d3ccef9be915e0895 (diff) | |
download | dewclaw-6b1fb96a160e3d6500a182c6c31f74cf85bd819f.tar.gz dewclaw-6b1fb96a160e3d6500a182c6c31f74cf85bd819f.tar.xz dewclaw-6b1fb96a160e3d6500a182c6c31f74cf85bd819f.zip |
check correctness of UCI identifiers during build
uci import does not abort properly on config errors, so the deployment
process continues and leaves the device with a partially invalid config.
checking identifiers during build is also a lot faster than having the
targets reboot due to import failures and subsequent rollbacks.
Diffstat (limited to 'openwrt/uci.nix')
-rw-r--r-- | openwrt/uci.nix | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/openwrt/uci.nix b/openwrt/uci.nix index 1557797..ac9c9f6 100644 --- a/openwrt/uci.nix +++ b/openwrt/uci.nix @@ -57,6 +57,17 @@ let lib.listToAttrs lib.attrNames ]; + + uciIdentifierCheck = type: attrs: + let + invalid = lib.filter + (n: builtins.match "[a-zA-Z0-9_]+" n == null) + (lib.attrNames attrs); + in + lib.warnIf + (invalid != []) + ("Invalid UCI ${type} names found: ${toString invalid}") + (invalid == []); in { @@ -101,7 +112,8 @@ in }; }) ]; - options = attrsOf (either scalar (listOf scalar)); + uciAttrsOf = type: elem: addCheck (attrsOf elem) (uciIdentifierCheck type); + options = uciAttrsOf "option" (either scalar (listOf scalar)); in submodule { freeformType = @@ -110,7 +122,7 @@ in attrsOf # config (attrsOf # type (either - (attrsOf options) # name ... + (uciAttrsOf "section" options) # name ... (listOf options) # [{ ... }] )); }; @@ -159,6 +171,9 @@ in build.configFile = pkgs.writeText "config" (formatConfig cfg.settings); deploySteps.uciConfig = + # correctness of config identifiers can't be checked on the type level + # because submodules are weird sometimes, so we have to do it here. + assert uciIdentifierCheck "config" cfg.settings; let cfgName = baseNameOf config.build.configFile; jq = "${pkgs.jq}/bin/jq"; |