diff --git a/nixos/lib/ini.nix b/nixos/lib/ini.nix new file mode 100644 index 0000000000000000000000000000000000000000..4269c0f9db6bf9225fc58773810cdb6e478c735f --- /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 0000000000000000000000000000000000000000..4f77ccab6b98b3289d0c319e9f4bbe8bb3c757ce --- /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 a48de57a1d6c65019960cb14936d2710aefcbcd4..e0e16a9cc17c7c2198545a1610a7b1a7e690add8 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 = ''