summary refs log tree commit diff
path: root/lib/generators.nix
diff options
context:
space:
mode:
authorRobert Helgesson <robert@rycee.net>2023-08-19 18:49:03 +0200
committerVwCSXg <VwCSXg@proton.me>2023-08-19 19:22:41 +0200
commita88b90c9dbb0db665ed62bb47296f4f3524cd267 (patch)
tree96c47418923266f0e3051cf4059aec634fda8405 /lib/generators.nix
parent9949be2f80b9eeb671e3260ea80df59194400b59 (diff)
downloadnixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.tar
nixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.tar.gz
nixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.tar.bz2
nixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.tar.lz
nixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.tar.xz
nixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.tar.zst
nixpkgs-a88b90c9dbb0db665ed62bb47296f4f3524cd267.zip
lib.generators.toGitINI: escape string values in configuration
This should handle the special characters that typically occur.
Upstreaming of https://github.com/nix-community/home-manager/commit/642d9ffe24eec1290e94d70e8129ba74922fddf2
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index c37be1942d8..a4671db2eaa 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -189,10 +189,10 @@ rec {
    * }
    *
    *> [url "ssh://git@github.com/"]
-   *>   insteadOf = https://github.com/
+   *>   insteadOf = "https://github.com"
    *>
    *> [user]
-   *>   name = edolstra
+   *>   name = "edolstra"
    */
   toGitINI = attrs:
     with builtins;
@@ -209,9 +209,17 @@ rec {
         else
           ''${section} "${subsection}"'';
 
+      mkValueString = v:
+        let
+          escapedV = ''
+            "${
+              replaceStrings [ "\n" "	" ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ] v
+            }"'';
+        in mkValueStringDefault { } (if isString v then escapedV else v);
+
       # generation for multiple ini values
       mkKeyValue = k: v:
-        let mkKeyValue = mkKeyValueDefault { } " = " k;
+        let mkKeyValue = mkKeyValueDefault { inherit mkValueString; } " = " k;
         in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (lib.toList v));
 
       # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI