dotfiles

My PC Dotfiles & Website

modules/lib/_map.nix (3269B)


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