map.nix (3269B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | ## Mapping Functions ## lib: let inherit (builtins) attrNames attrValues isPath pathExists readDir readFile toString typeOf ; inherit (lib) flatten filterAttrs forEach getAttrFromPath hasPrefix hasSuffix id mapAttrs' mapAttrsToList nameValuePair optionalAttrs removeSuffix replaceStrings ; # Import Checks checkName = name: _: !(hasPrefix "_" name); checkAttr = name: let type = typeOf (import name); in type == "set" || type == "lambda"; in rec { array = list: func: forEach list (name: getAttrFromPath [name] func); filter = name: func: attrs: filterAttrs (_: type: type != null) (mapAttrs' func (filterAttrs name attrs)); ## Files Map files = { directory, recursive ? false, apply ? id, extension, check ? (_: true), }: filter checkName ( name: type: let path = "${toString directory}/${name}"; in if (type == "directory" || type == "symlink") && recursive then nameValuePair name (files { inherit path apply; }) else if (type == "directory" || type == "symlink") && ( if (extension == ".nix") then pathExists "${path}/default.nix" else true ) then nameValuePair name (apply path) else if type == "regular" && ( if (extension == ".nix") then name != "default.nix" && name != "flake.nix" else true ) && hasSuffix extension name && (check path) then nameValuePair (removeSuffix extension name) (apply path) else nameValuePair "" null ) (readDir directory); # Module Imports modules = { __functor = _: directory: apply: files { inherit directory apply; extension = ".nix"; check = checkAttr; }; list = path: attrValues (modules path id); name = path: attrNames (modules path id); }; # Configuration Folder Creation folder = { directory, path, extension ? "", apply, replace ? { placeholders = {}; values = {}; }, }: mapAttrs' (name: value: nameValuePair "${path}/${name}${extension}" (apply value)) (files { inherit directory extension; apply = file: with replace; replaceStrings placeholders values (readFile file); }); # Package Patches patches = patch: if isPath patch then flatten ( mapAttrsToList ( name: type: if type == "regular" && (hasSuffix ".diff" name || hasSuffix ".patch" name) then patch + "/${name}" else null ) (readDir patch) ) else patch; # 'sops' Encrypted Secrets secrets = { directory, neededForUsers ? false, }: filter checkName ( name: type: if type == "regular" && hasSuffix ".secret" name then nameValuePair name ({ sopsFile = directory + "/${name}"; format = "binary"; } // optionalAttrs neededForUsers {inherit neededForUsers;}) else nameValuePair "" null ) (readDir directory); } |