Skip to content

Commit

Permalink
modules/mavlink-router: include custom version of gitIni settings format
Browse files Browse the repository at this point in the history
The upstream gitIni quotes strings now, which is not compatible with
mavlink-router.
  • Loading branch information
lopsided98 committed Oct 4, 2023
1 parent 1faa802 commit 6238b3a
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions modules/services/networking/mavlink-router.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
{ config, lib, pkgs, ... }:
{ config, lib, pkgs, ... }: let
cfg = config.services.mavlink-router;

with lib;
# Based on lib.generators.toGitINI, but without quoting of subsection headers
# and string values.
toConf = attrs:
let
mkSectionName = name:
let
sections = lib.strings.splitString "." name;
section = builtins.head sections;
subsections = builtins.tail sections;
subsection = lib.concatStringsSep "." subsections;
in if subsections == [ ]
then name
else "${section} ${subsection}";

let
cfg = config.services.mavlink-router;
settingsFormat = pkgs.formats.gitIni { };
# generation for multiple ini values
mkKeyValue = k: v:
let mkKeyValue = lib.generators.mkKeyValueDefault { } " = " k;
in lib.concatStringsSep "\n" (builtins.map (kv: "\t" + mkKeyValue kv) (lib.toList v));

# converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
flattenAttrs = let
recurse = path: value:
if builtins.isAttrs value && !lib.isDerivation value then
lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value
else if builtins.length path > 1 then {
${lib.concatStringsSep "." (lib.reverseList (builtins.tail path))}.${builtins.head path} = value;
} else {
${builtins.head path} = value;
};
in attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs));

toINI_ = lib.generators.toINI { inherit mkKeyValue mkSectionName; };
in
toINI_ (flattenAttrs attrs);

settingsFormat = {
type = with lib.types; let
iniAtom = (pkgs.formats.ini { }).type/*attrsOf*/.functor.wrapped/*attrsOf*/.functor.wrapped;
in attrsOf (attrsOf (either iniAtom (attrsOf iniAtom)));
generate = name: value: pkgs.writeText name (toConf value);
};
in {
# Interface

options.services.mavlink-router = {
enable = mkEnableOption "MAVLink Router";
enable = lib.mkEnableOption "MAVLink Router";

settings = mkOption {
settings = lib.mkOption {
type = settingsFormat.type;
default = {};
example = {
Expand All @@ -29,7 +66,7 @@ in {

# Implementation

config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {
systemd.packages = [ pkgs.mavlink-router ];

systemd.services.mavlink-router = {
Expand Down

0 comments on commit 6238b3a

Please sign in to comment.