at 18.09-beta 1.9 kB view raw
1{ lib 2# we pass the kernel version here to keep a nice syntax `whenOlder "4.13"` 3# kernelVersion, e.g., config.boot.kernelPackages.version 4, version 5, mkValuePreprocess ? null 6}: 7 8with lib; 9rec { 10 # Common patterns 11 when = cond: opt: if cond then opt else null; 12 whenAtLeast = ver: when (versionAtLeast version ver); 13 whenOlder = ver: when (versionOlder version ver); 14 whenBetween = verLow: verHigh: when (versionAtLeast version verLow && versionOlder version verHigh); 15 16 # Keeping these around in case we decide to change this horrible implementation :) 17 option = x: if x == null then null else "?${x}"; 18 yes = "y"; 19 no = "n"; 20 module = "m"; 21 22 mkValue = val: 23 let 24 isNumber = c: elem c ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]; 25 in 26 if val == "" then "\"\"" 27 else if val == yes || val == module || val == no then val 28 else if all isNumber (stringToCharacters val) then val 29 else if substring 0 2 val == "0x" then val 30 else val; # FIXME: fix quoting one day 31 32 33 # generate nix intermediate kernel config file of the form 34 # 35 # VIRTIO_MMIO m 36 # VIRTIO_BLK y 37 # VIRTIO_CONSOLE n 38 # NET_9P_VIRTIO? y 39 # 40 # Use mkValuePreprocess to preprocess option values, aka mark 'modules' as 41 # 'yes' or vice-versa 42 # Borrowed from copumpkin https://github.com/NixOS/nixpkgs/pull/12158 43 # returns a string, expr should be an attribute set 44 generateNixKConf = exprs: mkValuePreprocess: 45 let 46 mkConfigLine = key: rawval: 47 let 48 val = if builtins.isFunction mkValuePreprocess then mkValuePreprocess rawval else rawval; 49 in 50 if val == null 51 then "" 52 else if hasPrefix "?" val 53 then "${key}? ${mkValue (removePrefix "?" val)}\n" 54 else "${key} ${mkValue val}\n"; 55 mkConf = cfg: concatStrings (mapAttrsToList mkConfigLine cfg); 56 in mkConf exprs; 57}