diff options
author | Alyssa Ross <hi@alyssa.is> | 2019-01-07 02:18:36 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2019-01-07 02:18:47 +0000 |
commit | 36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2 (patch) | |
tree | b3faaf573407b32aa645237a4d16b82778a39a92 /nixpkgs/lib/kernel.nix | |
parent | 4e31070265257dc67d120c27e0f75c2344fdfa9a (diff) | |
parent | abf060725d7614bd3b9f96764262dfbc2f9c2199 (diff) | |
download | nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.tar nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.tar.gz nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.tar.bz2 nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.tar.lz nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.tar.xz nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.tar.zst nixlib-36f56d99fa0a0765c9f1de4a5f17a9b05830c3f2.zip |
Add 'nixpkgs/' from commit 'abf060725d7614bd3b9f96764262dfbc2f9c2199'
git-subtree-dir: nixpkgs git-subtree-mainline: 4e31070265257dc67d120c27e0f75c2344fdfa9a git-subtree-split: abf060725d7614bd3b9f96764262dfbc2f9c2199
Diffstat (limited to 'nixpkgs/lib/kernel.nix')
-rw-r--r-- | nixpkgs/lib/kernel.nix | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/nixpkgs/lib/kernel.nix b/nixpkgs/lib/kernel.nix new file mode 100644 index 000000000000..45b33aea7b87 --- /dev/null +++ b/nixpkgs/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; +} |