From 21a4dedd6f980758485bcbedb8806d51b8320d19 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Thu, 21 Sep 2017 13:18:33 +0200 Subject: [PATCH] lib: use generators from Nixpkgs (cherry picked from commit 61042c76065b7ff99d840e1be78c4cf5b011bcab) --- modules/lib/generators.nix | 93 ----------------------------- modules/misc/gtk.nix | 2 +- modules/programs/git.nix | 5 +- modules/programs/gnome-terminal.nix | 9 +-- modules/systemd.nix | 2 +- 5 files changed, 9 insertions(+), 102 deletions(-) delete mode 100644 modules/lib/generators.nix diff --git a/modules/lib/generators.nix b/modules/lib/generators.nix deleted file mode 100644 index 4190703e2..000000000 --- a/modules/lib/generators.nix +++ /dev/null @@ -1,93 +0,0 @@ -/* Functions that generate widespread file - * formats from nix data structures. - * - * They all follow a similar interface: - * generator { config-attrs } data - * - * Tests can be found in ./tests.nix - * Documentation in the manual, #sec-generators - */ -with import ; -let - libStr = import ; - libAttr = import ; - - flipMapAttrs = flip libAttr.mapAttrs; -in - -rec { - - /* Generate a line of key k and value v, separated by - * character sep. If sep appears in k, it is escaped. - * Helper for synaxes with different separators. - * - * mkKeyValueDefault ":" "f:oo" "bar" - * > "f\:oo:bar" - */ - mkKeyValueDefault = sep: k: v: - "${libStr.escape [sep] k}${sep}${toString v}"; - - - /* Generate a key-value-style config file from an attrset. - * - * mkKeyValue is the same as in toINI. - */ - toKeyValue = { - mkKeyValue ? mkKeyValueDefault "=" - }: attrs: - let mkLine = k: v: mkKeyValue k v + "\n"; - in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs); - - - /* Generate an INI-style config file from an - * attrset of sections to an attrset of key-value pairs. - * - * generators.toINI {} { - * foo = { hi = "${pkgs.hello}"; ciao = "bar"; }; - * baz = { "also, integers" = 42; }; - * } - * - *> [baz] - *> also, integers=42 - *> - *> [foo] - *> ciao=bar - *> hi=/nix/store/y93qql1p5ggfnaqjjqhxcw0vqw95rlz0-hello-2.10 - * - * The mk* configuration attributes can generically change - * the way sections and key-value strings are generated. - * - * For more examples see the test cases in ./tests.nix. - */ - toINI = { - # apply transformations (e.g. escapes) to section names - mkSectionName ? (name: libStr.escape [ "[" "]" ] name), - # format a setting line from key and value - mkKeyValue ? mkKeyValueDefault "=" - }: attrsOfAttrs: - let - # map function to string for each key val - mapAttrsToStringsSep = sep: mapFn: attrs: - libStr.concatStringsSep sep - (libAttr.mapAttrsToList mapFn attrs); - mkSection = sectName: sectValues: '' - [${mkSectionName sectName}] - '' + toKeyValue { inherit mkKeyValue; } sectValues; - in - # map input to ini sections - mapAttrsToStringsSep "\n" mkSection attrsOfAttrs; - - - /* Generates JSON from an arbitrary (non-function) value. - * For more information see the documentation of the builtin. - */ - toJSON = {}: builtins.toJSON; - - - /* YAML has been a strict superset of JSON since 1.2, so we - * use toJSON. Before it only had a few differences referring - * to implicit typing rules, so it should work with older - * parsers as well. - */ - toYAML = {}@args: toJSON args; -} diff --git a/modules/misc/gtk.nix b/modules/misc/gtk.nix index ab58ead6b..1d6673391 100644 --- a/modules/misc/gtk.nix +++ b/modules/misc/gtk.nix @@ -8,7 +8,7 @@ let cfg2 = config.gtk.gtk2; cfg3 = config.gtk.gtk3; - toGtk3Ini = (import ../lib/generators.nix).toINI { + toGtk3Ini = generators.toINI { mkKeyValue = key: value: let value' = diff --git a/modules/programs/git.nix b/modules/programs/git.nix index eeab16abd..28d1168d0 100644 --- a/modules/programs/git.nix +++ b/modules/programs/git.nix @@ -6,8 +6,6 @@ let cfg = config.programs.git; - toINI = (import ../lib/generators.nix).toINI {}; - signModule = types.submodule { options = { key = mkOption { @@ -93,7 +91,8 @@ in { home.packages = [ cfg.package ]; - home.file.".gitconfig".text = toINI ini + "\n" + cfg.extraConfig; + home.file.".gitconfig".text = + generators.toINI {} ini + "\n" + cfg.extraConfig; } ); } diff --git a/modules/programs/gnome-terminal.nix b/modules/programs/gnome-terminal.nix index 052962c77..dc8990a96 100644 --- a/modules/programs/gnome-terminal.nix +++ b/modules/programs/gnome-terminal.nix @@ -90,7 +90,7 @@ let } ); - toINI = (import ../lib/generators.nix).toINI { mkKeyValue = mkIniKeyValue; }; + toDconfIni = generators.toINI { mkKeyValue = mkIniKeyValue; }; mkIniKeyValue = key: value: let @@ -181,14 +181,15 @@ in # The dconf service needs to be installed and prepared. home.activation.gnomeTerminal = dagEntryAfter ["installPackages"] ( let - sf = pkgs.writeText "gnome-terminal.ini" (toINI (buildIniSet cfg)); + iniText = toDconfIni (buildIniSet cfg); + iniFile = pkgs.writeText "gnome-terminal.ini" iniText; dconfPath = "/org/gnome/terminal/legacy/"; in '' if [[ -v DRY_RUN ]]; then - echo ${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} "<" ${sf} + echo ${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} "<" ${iniFile} else - ${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} < ${sf} + ${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} < ${iniFile} fi '' ); diff --git a/modules/systemd.nix b/modules/systemd.nix index edc89ab0c..dd4e594c3 100644 --- a/modules/systemd.nix +++ b/modules/systemd.nix @@ -12,7 +12,7 @@ let || cfg.targets != {} || cfg.timers != {}; - toSystemdIni = (import lib/generators.nix).toINI { + toSystemdIni = generators.toINI { mkKeyValue = key: value: let value' =