about summary refs log tree commit diff
path: root/nix/modules/libvirtnix/xml.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nix/modules/libvirtnix/xml.nix')
-rw-r--r--nix/modules/libvirtnix/xml.nix23
1 files changed, 18 insertions, 5 deletions
diff --git a/nix/modules/libvirtnix/xml.nix b/nix/modules/libvirtnix/xml.nix
index 134a878..10eefa3 100644
--- a/nix/modules/libvirtnix/xml.nix
+++ b/nix/modules/libvirtnix/xml.nix
@@ -32,14 +32,27 @@
   closing ? true, # add a closing tag
 }:
 let
-  args_str =
-    " " + lib.strings.concatStrings (lib.strings.intersperse " " (map (x: "${x.key}='${x.val}'") args));
+
+  # filter out all values missing a key
+  # this allows passing empty args such as `{}` which then get discarded
+  filteredArgs = (builtins.filter (x: x ? key) args);
+
+  # convert the list of attr sets into a list of strings
+  mappedArgs = (map (x: "${x.key}='${x.val}'") filteredArgs);
+
+  # [ "A" "B" "C" ] => [ "A" " " "B" " " "C" ]
+  spacedArgs = lib.strings.intersperse "" mappedArgs;
+
+  # [ "A" " " "B" " " "C" ] => "A B C"
+  joinedArgs = lib.strings.concatStrings spacedArgs;
+
+  # prefix the args with a space when inserting
+  args_str = lib.optionalString (filteredArgs != [ ]) (" " + joinedArgs);
+
   child_evaled = lib.strings.concatStrings children;
 
   cond = condition: value: if condition then value else "";
 
   closingTag = if closing == true then "</${name}>" else "";
 in
-"<${name}${
-  lib.optionalString (args != [ ]) args_str
-}${cond (closing == false) "/"}>${value}${child_evaled}${lib.optionalString (closing) closingTag}"
+"<${name}${args_str}${cond (closing == false) "/"}>${value}${child_evaled}${lib.optionalString (closing) closingTag}"