summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/generators.nix45
-rw-r--r--lib/licenses.nix2
-rw-r--r--lib/meta.nix2
-rw-r--r--lib/modules.nix2
-rw-r--r--lib/systems/inspect.nix4
-rw-r--r--lib/tests/misc.nix26
6 files changed, 71 insertions, 10 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 73017f2c6796..d1a8f6bf8dcd 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -4,6 +4,12 @@
  * They all follow a similar interface:
  * generator { config-attrs } data
  *
+ * `config-attrs` are “holes” in the generators
+ * with sensible default implementations that
+ * can be overwritten. The default implementations
+ * are mostly generators themselves, called with
+ * their respective default values; they can be reused.
+ *
  * Tests can be found in ./tests.nix
  * Documentation in the manual, #sec-generators
  */
@@ -20,6 +26,32 @@ in
 
 rec {
 
+  ## -- HELPER FUNCTIONS & DEFAULTS --
+
+  /* Convert a value to a sensible default string representation.
+   * The builtin `toString` function has some strange defaults,
+   * suitable for bash scripts but not much else.
+   */
+  mkValueStringDefault = {}: v: with builtins;
+    let err = t: v: abort
+          ("generators.mkValueStringDefault: " +
+           "${t} not supported: ${toPretty {} v}");
+    in   if isInt      v then toString v
+    # we default to not quoting strings
+    else if isString   v then v
+    # isString returns "1", which is not a good default
+    else if true  ==   v then "true"
+    # here it returns to "", which is even less of a good default
+    else if false ==   v then "false"
+    else if null  ==   v then "null"
+    # if you have lists you probably want to replace this
+    else if isList     v then err "lists" v
+    # same as for lists, might want to replace
+    else if isAttrs    v then err "attrsets" v
+    else if isFunction v then err "functions" v
+    else err "this value is" (toString v);
+
+
   /* 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.
@@ -30,11 +62,14 @@ rec {
    * > "f\:oo:bar"
    */
   mkKeyValueDefault = {
-    mkValueString ? toString
+    mkValueString ? mkValueStringDefault {}
   }: sep: k: v:
     "${libStr.escape [sep] k}${sep}${mkValueString v}";
 
 
+  ## -- FILE FORMAT GENERATORS --
+
+
   /* Generate a key-value-style config file from an attrset.
    *
    * mkKeyValue is the same as in toINI.
@@ -98,6 +133,7 @@ rec {
     */
   toYAML = {}@args: toJSON args;
 
+
   /* Pretty print a value, akin to `builtins.trace`.
     * Should probably be a builtin as well.
     */
@@ -108,8 +144,9 @@ rec {
     allowPrettyValues ? false
   }@args: v: with builtins;
     if      isInt      v then toString v
-    else if isBool     v then (if v == true then "true" else "false")
-    else if isString   v then "\"" + v + "\""
+    else if isString   v then ''"${libStr.escape [''"''] v}"''
+    else if true  ==   v then "true"
+    else if false ==   v then "false"
     else if null ==    v then "null"
     else if isFunction v then
       let fna = lib.functionArgs v;
@@ -132,6 +169,6 @@ rec {
               (name: value:
                 "${toPretty args name} = ${toPretty args value};") v)
         + " }"
-    else abort "toPretty: should never happen (v = ${v})";
+    else abort "generators.toPretty: should never happen (v = ${v})";
 
 }
diff --git a/lib/licenses.nix b/lib/licenses.nix
index ba575c27052d..767fd89b948c 100644
--- a/lib/licenses.nix
+++ b/lib/licenses.nix
@@ -284,7 +284,7 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec {
 
   gpl2Oss = {
     fullName = "GNU General Public License version 2 only (with OSI approved licenses linking exception)";
-    url = http://www.mysql.com/about/legal/licensing/foss-exception;
+    url = https://www.mysql.com/about/legal/licensing/foss-exception;
   };
 
   gpl2Plus = spdx {
diff --git a/lib/meta.nix b/lib/meta.nix
index 199030c103af..8516cc1cd6f1 100644
--- a/lib/meta.nix
+++ b/lib/meta.nix
@@ -86,4 +86,6 @@ rec {
         then { system = elem; }
         else { parsed = elem; };
     in lib.matchAttrs pattern platform;
+
+  enableIfAvailable = p: if p.meta.available or true then [ p ] else [];
 }
diff --git a/lib/modules.nix b/lib/modules.nix
index 654c4c588de6..acd07f732bdb 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -660,7 +660,7 @@ rec {
   doRename = { from, to, visible, warn, use }:
     let
       toOf = attrByPath to
-        (abort "Renaming error: option `${showOption to}' does not exists.");
+        (abort "Renaming error: option `${showOption to}' does not exist.");
     in
       { config, options, ... }:
       { options = setAttrByPath from (mkOption {
diff --git a/lib/systems/inspect.nix b/lib/systems/inspect.nix
index e8ea2bed25f5..d7fabf684b72 100644
--- a/lib/systems/inspect.nix
+++ b/lib/systems/inspect.nix
@@ -39,12 +39,8 @@ rec {
     isAndroid      = [ { abi = abis.android; } { abi = abis.androideabi; } ];
     isMusl         = with abis; map (a: { abi = a; }) [ musl musleabi musleabihf ];
 
-    isKexecable    = map (family: { kernel = kernels.linux; cpu.family = family; })
-                       [ "x86" "arm" "aarch64" "mips" ];
     isEfi          = map (family: { cpu.family = family; })
                        [ "x86" "arm" "aarch64" ];
-    isSeccomputable = map (family: { kernel = kernels.linux; cpu.family = family; })
-                        [ "x86" "arm" "aarch64" "mips" ];
   };
 
   matchAnyAttrs = patterns:
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index e10aea48e48e..5f19dd63f2d8 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -207,6 +207,29 @@ runTests {
     expected = ''f\:oo:bar'';
   };
 
+  testMkValueString = {
+    expr = let
+      vals = {
+        int = 42;
+        string = ''fo"o'';
+        bool = true;
+        bool2 = false;
+        null = null;
+        # float = 42.23; # floats are strange
+      };
+      in mapAttrs
+        (const (generators.mkValueStringDefault {}))
+        vals;
+    expected = {
+      int = "42";
+      string = ''fo"o'';
+      bool = "true";
+      bool2 = "false";
+      null = "null";
+      # float = "42.23" true false [ "bar" ] ]'';
+    };
+  };
+
   testToKeyValue = {
     expr = generators.toKeyValue {} {
       key = "value";
@@ -249,6 +272,8 @@ runTests {
       "section 1" = {
         attribute1 = 5;
         x = "Me-se JarJar Binx";
+        # booleans are converted verbatim by default
+        boolean = false;
       };
       "foo[]" = {
         "he\\h=he" = "this is okay";
@@ -260,6 +285,7 @@ runTests {
 
       [section 1]
       attribute1=5
+      boolean=false
       x=Me-se JarJar Binx
     '';
   };