From 6c9546bf40c821b89ab7adabb94aaa9f27f0baa0 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Wed, 7 Aug 2019 10:54:54 -0400 Subject: [PATCH] Factor out ini generating code and start a test suite --- nixos/lib/ini.nix | 37 ++++++++++++++++++++++++++++++++++++ nixos/lib/tests/test_ini.nix | 20 +++++++++++++++++++ nixos/modules/tahoe.nix | 35 ---------------------------------- 3 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 nixos/lib/ini.nix create mode 100644 nixos/lib/tests/test_ini.nix diff --git a/nixos/lib/ini.nix b/nixos/lib/ini.nix new file mode 100644 index 00000000..4269c0f9 --- /dev/null +++ b/nixos/lib/ini.nix @@ -0,0 +1,37 @@ +{ pkgs ? import <nixpkgs> { } }: +let lib = pkgs.lib; +in rec { + # Map a function over an attrset and concatenate the string results. + # + # concatMapAttrsToList (n: v: "${n} = ${v}\n") { a = "b"; c = "d"; } -> "a = b\nc = d\n" + concatMapAttrsToList = f: a: + lib.strings.concatStrings (lib.attrsets.mapAttrsToList f a); + + # Generate one line of configuration defining one item in one section. + # + # oneConfigItemText "foo" "bar" -> "foo = bar\n" + oneConfigItemText = name: value: + "${name} = ${builtins.toString value}\n"; + + # Generate all lines of configuration defining all items in one section. + # + # allConfigItemsText { foo = "bar"; baz = "quux"; } -> "foo = bar\nbaz = quux" + allConfigItemsText = items: + concatMapAttrsToList oneConfigItemText items; + + # Generate all lines of configuration for one section, header + # and items included. + # + # oneConfigSectionText "foo" { bar = "baz"; } -> "[foo]\nbar = baz\n" + oneConfigSectionText = name: value: '' + [${name}] + ${allConfigItemsText value} + ''; + + # Generate all lines of configuration for all sections, headers + # and items included. + # + # allConfigSectionsText { foo = { bar = "baz"; }; } -> "[foo]\nbar = baz\n" + allConfigSectionsText = sections: + concatMapAttrsToList oneConfigSectionText sections; +} diff --git a/nixos/lib/tests/test_ini.nix b/nixos/lib/tests/test_ini.nix new file mode 100644 index 00000000..4f77ccab --- /dev/null +++ b/nixos/lib/tests/test_ini.nix @@ -0,0 +1,20 @@ +let + pkgs = import <nixpkgs> { }; + ini = import ../ini.nix { inherit pkgs; }; +in +pkgs.lib.runTests +{ test_empty = + { name = "test_empty"; + expected = ""; + expr = ini.allConfigSectionsText { }; + }; + + test_one_section = + { name = "test_one_empty_section"; + expected = '' + [foo] + + ''; + expr = ini.allConfigSectionsText { foo = { }; }; + }; +} diff --git a/nixos/modules/tahoe.nix b/nixos/modules/tahoe.nix index a48de57a..e0e16a9c 100644 --- a/nixos/modules/tahoe.nix +++ b/nixos/modules/tahoe.nix @@ -152,41 +152,6 @@ in (mkIf (cfg.nodes != {}) { environment = { etc = flip mapAttrs' cfg.nodes (node: settings: - let - # Map a function over an attrset and concatenate the string results. - # - # concatMapAttrsToList (n: v: "${n} = ${v}\n") { a = "b"; c = "d"; } -> "a = b\nc = d\n" - concatMapAttrsToList = f: a: - lib.strings.concatStrings (lib.attrsets.mapAttrsToList f a); - - # Generate one line of configuration defining one item in one section. - # - # oneConfigItemText "foo" "bar" -> "foo = bar\n" - oneConfigItemText = name: value: - "${name} = ${builtins.toString value}\n"; - - # Generate all lines of configuration defining all items in one section. - # - # allConfigItemsText { foo = "bar"; baz = "quux"; } -> "foo = bar\nbaz = quux" - allConfigItemsText = items: - concatMapAttrsToList oneConfigItemText items; - - # Generate all lines of configuration for one section, header - # and items included. - # - # oneConfigSectionText "foo" { bar = "baz"; } -> "[foo]\nbar = baz\n" - oneConfigSectionText = name: value: '' - [${name}] - ${allConfigItemsText value} - ''; - - # Generate all lines of configuration for all sections, headers - # and items included. - # - # allConfigSectionsText { foo = { bar = "baz"; }; } -> "[foo]\nbar = baz\n" - allConfigSectionsText = sections: - concatMapAttrsToList oneConfigSectionText sections; - in nameValuePair "tahoe-lafs/${node}.cfg" { mode = "0444"; text = '' -- GitLab