Skyler Grey | 51e1851 | 2022-08-20 12:24:19 +0100 | [diff] [blame] | 1 | # As our modules have nonstandard properties, we need to have some way of |
| 2 | # properly intepreting them |
| 3 | # This function takes a list of modules, as well as arguments to import them |
| 4 | # with, and returns a list of modules, each with the standard NixOS module |
| 5 | # properties as well as with custom properties as described in /README.md |
Skyler Grey | 995f869 | 2022-09-01 23:31:06 +0100 | [diff] [blame] | 6 | lib: transformArgs: modules: args @ {pkgs, ...}: let |
Skyler Grey | 51e1851 | 2022-08-20 12:24:19 +0100 | [diff] [blame] | 7 | resolver = module: let |
| 8 | importedModule = |
| 9 | if builtins.typeOf module == "path" |
| 10 | then import module |
| 11 | else module; |
| 12 | resolvedModule = |
| 13 | if builtins.typeOf importedModule == "lambda" |
| 14 | then |
Skyler Grey | bcb2cb7 | 2022-08-21 07:24:35 +0100 | [diff] [blame] | 15 | importedModule |
Skyler Grey | 7dfe5c6 | 2022-08-20 21:41:30 +0100 | [diff] [blame] | 16 | (transformArgs args) |
Skyler Grey | bcb2cb7 | 2022-08-21 07:24:35 +0100 | [diff] [blame] | 17 | else importedModule; |
Skyler Grey | f649adf | 2022-08-26 06:47:44 +0100 | [diff] [blame] | 18 | moduleName = |
Skyler Grey | bcb2cb7 | 2022-08-21 07:24:35 +0100 | [diff] [blame] | 19 | if builtins.typeOf module == "lambda" |
| 20 | then "<AnonFunction>" |
Skyler Grey | f649adf | 2022-08-26 06:47:44 +0100 | [diff] [blame] | 21 | else if builtins.typeOf module == "path" |
| 22 | then builtins.toString module |
| 23 | else builtins.toJSON module; |
| 24 | in |
| 25 | lib.warnIfNot |
| 26 | ((lib.pipe resolvedModule [ |
| 27 | builtins.attrNames |
| 28 | (lib.subtractLists ["home" "config" "imports" "options" "traces"]) |
| 29 | ]) |
| 30 | == []) |
| 31 | "Module ${moduleName} had attribute names ${builtins.toJSON (builtins.attrNames resolvedModule)} but only home, config, imports and options are resolved" |
| 32 | [ |
| 33 | { |
| 34 | config = lib.recursiveUpdate (resolvedModule.config or {}) { |
| 35 | home-manager.users."${args.username}".imports = |
| 36 | (resolvedModule.config.home-manager.users."${args.username}".imports or []) |
| 37 | ++ [resolvedModule.home or {}]; |
| 38 | }; |
| 39 | imports = resolvedModule.imports or []; |
| 40 | options = resolvedModule.options or {}; |
| 41 | } |
| 42 | ] |
| 43 | ++ (builtins.map (trace: let |
| 44 | splitTrace = lib.splitString "." trace; |
| 45 | traceHead = builtins.head splitTrace; |
| 46 | traceTail = builtins.tail splitTrace; |
| 47 | resolvedTrace = |
| 48 | ( |
| 49 | if traceHead == "home" |
| 50 | then ["home-manager" "users" args.username] |
| 51 | else lib.errorIfNot (traceHead == "config") [] |
| 52 | ) |
| 53 | ++ traceTail; |
| 54 | in |
| 55 | {config, ...}: (builtins.seq ( |
| 56 | lib.pipe resolvedTrace [ |
| 57 | (lib.foldl ({ |
| 58 | value, |
| 59 | error, |
| 60 | }: key: |
| 61 | if builtins.hasAttr key value |
| 62 | then { |
| 63 | value = value.${key}; |
| 64 | inherit error; |
| 65 | } |
| 66 | else { |
| 67 | value = {}; |
| 68 | error = true; |
| 69 | }) { |
| 70 | value = {}; |
| 71 | error = false; |
| 72 | }) |
| 73 | (data: lib.warnIf data.error "trace@${moduleName}/${trace} is invalid; the key does not exist" data) |
| 74 | ({ |
| 75 | value, |
| 76 | error, |
| 77 | }: { |
| 78 | value = builtins.toJSON value; |
| 79 | inherit error; |
| 80 | }) |
| 81 | ({ |
| 82 | value, |
| 83 | error, |
| 84 | }: { |
| 85 | value = "trace@${moduleName}/${trace}: ${value}"; |
| 86 | inherit error; |
| 87 | }) |
| 88 | ({ |
| 89 | value, |
| 90 | error, |
| 91 | }: |
| 92 | lib.traceIf (!error) value null) |
| 93 | ] |
| 94 | ) {})) (resolvedModule.traces or [])); |
Skyler Grey | bcb2cb7 | 2022-08-21 07:24:35 +0100 | [diff] [blame] | 95 | in { |
| 96 | imports = ( |
| 97 | if builtins.typeOf modules == "list" |
Skyler Grey | f649adf | 2022-08-26 06:47:44 +0100 | [diff] [blame] | 98 | then builtins.concatLists (builtins.map resolver modules) |
| 99 | else resolver modules |
Skyler Grey | bcb2cb7 | 2022-08-21 07:24:35 +0100 | [diff] [blame] | 100 | ); |
| 101 | } |