summary refs log tree commit diff
path: root/lib/kernel.nix
diff options
context:
space:
mode:
authorDan Peebles <pumpkin@me.com>2016-01-05 09:22:43 -0500
committerTuomas Tynkkynen <tuomas@tuxera.com>2018-06-30 16:01:41 +0300
commitff9999ad1b19b941b275b7ec79514d04c7cdad7d (patch)
treee3167d9f1a1483d51c756cb1990bdaab76a58a2d /lib/kernel.nix
parentbae87d504224d853c5548d2aa244b596569fc7f3 (diff)
downloadnixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.tar
nixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.tar.gz
nixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.tar.bz2
nixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.tar.lz
nixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.tar.xz
nixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.tar.zst
nixpkgs-ff9999ad1b19b941b275b7ec79514d04c7cdad7d.zip
linux: translate config to structured config
Instead of using a string to describe kernel config, use a nix
attribute set, then converted to a string.
- allows to override the config, aka convert 'yes' into 'modules' or
vice-versa
- while for now merging different configs is still crude (last spec wins),
at least there should be only one CONFIG_XYZ value compared to the current string
config where the first defined would be used and others ignored.

[initial idea by copumpkin in 2016, a major rebase to 2018 by teto]
Diffstat (limited to 'lib/kernel.nix')
-rw-r--r--lib/kernel.nix57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/kernel.nix b/lib/kernel.nix
new file mode 100644
index 00000000000..45b33aea7b8
--- /dev/null
+++ b/lib/kernel.nix
@@ -0,0 +1,57 @@
+{ lib
+# we pass the kernel version here to keep a nice syntax `whenOlder "4.13"`
+# kernelVersion, e.g., config.boot.kernelPackages.version
+, version
+, mkValuePreprocess ? null
+}:
+
+with lib;
+rec {
+  # Common patterns
+  when        = cond: opt: if cond then opt else null;
+  whenAtLeast = ver: when (versionAtLeast version ver);
+  whenOlder   = ver: when (versionOlder version ver);
+  whenBetween = verLow: verHigh: when (versionAtLeast version verLow && versionOlder version verHigh);
+
+  # Keeping these around in case we decide to change this horrible implementation :)
+  option = x: if x == null then null else "?${x}";
+  yes    = "y";
+  no     = "n";
+  module = "m";
+
+  mkValue = val:
+  let
+    isNumber = c: elem c ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"];
+  in
+    if val == "" then "\"\""
+    else if val == yes || val == module || val == no then val
+    else if all isNumber (stringToCharacters val) then val
+    else if substring 0 2 val == "0x" then val
+    else val; # FIXME: fix quoting one day
+
+
+  # generate nix intermediate kernel config file of the form
+  #
+  #       VIRTIO_MMIO m
+  #       VIRTIO_BLK y
+  #       VIRTIO_CONSOLE n
+  #       NET_9P_VIRTIO? y
+  #
+  # Use mkValuePreprocess to preprocess option values, aka mark 'modules' as
+  # 'yes' or vice-versa
+  # Borrowed from copumpkin https://github.com/NixOS/nixpkgs/pull/12158
+  # returns a string, expr should be an attribute set
+  generateNixKConf = exprs: mkValuePreprocess:
+  let
+    mkConfigLine = key: rawval:
+    let
+      val = if builtins.isFunction mkValuePreprocess then mkValuePreprocess rawval else rawval;
+    in
+      if val == null
+        then ""
+        else if hasPrefix "?" val
+          then "${key}? ${mkValue (removePrefix "?" val)}\n"
+          else "${key} ${mkValue val}\n";
+    mkConf = cfg: concatStrings (mapAttrsToList mkConfigLine cfg);
+  in mkConf exprs;
+}