mirror of
https://github.com/gensokyo-zone/infrastructure.git
synced 2026-02-09 12:29:19 -08:00
feat: refactoring + system types
This commit is contained in:
parent
a0f9d0ab48
commit
9794026f6c
36 changed files with 653 additions and 537 deletions
223
tree.nix
223
tree.nix
|
|
@ -1,130 +1,99 @@
|
|||
{ lib }: { config, folder, inputs, ... }@args: let
|
||||
inherit (lib.attrsets) filterAttrs mapAttrs' mapAttrs isAttrs nameValuePair attrValues;
|
||||
inherit (lib.strings) hasPrefix removeSuffix;
|
||||
inherit (lib.lists) imap1 singleton optionals optional sublist;
|
||||
inherit (lib.trivial) pipe;
|
||||
inherit (lib.options) mkOption;
|
||||
inherit (lib.modules) evalModules;
|
||||
pureTreeGrab = { base, path }: let
|
||||
realPath = toString path;
|
||||
dirContents = builtins.readDir path;
|
||||
isDirectory = entry: dirContents."${entry}" == "directory";
|
||||
isHidden = entry: hasPrefix "." entry;
|
||||
isDir = entry: _: (isDirectory entry) && !(isHidden entry);
|
||||
directories = filterAttrs isDir dirContents;
|
||||
isNixFile = entry: _: let
|
||||
result = builtins.match "(.*)\\.nix" entry;
|
||||
in result != null && builtins.length result > 0;
|
||||
nixFiles = filterAttrs isNixFile dirContents;
|
||||
getPath = entry: "${realPath}/${entry}";
|
||||
getPaths = entries: mapAttrs' (n: v:
|
||||
nameValuePair (removeSuffix ".nix" n) (getPath n)
|
||||
) entries;
|
||||
nixFilePaths = getPaths nixFiles;
|
||||
dirPaths = getPaths directories;
|
||||
recursedPaths = mapAttrs (_: fullPath: pureTreeGrab {
|
||||
inherit base;
|
||||
path = fullPath;
|
||||
}) dirPaths;
|
||||
contents = recursedPaths // nixFilePaths;
|
||||
in contents;
|
||||
configTreeStruct = { config, ... }: {
|
||||
options.treeConfig = mkOption {
|
||||
type = lib.types.attrsOf (lib.types.submodule ({ name, options, config, ... }: {
|
||||
options = {
|
||||
evaluateDefault = mkOption {
|
||||
type = lib.types.bool;
|
||||
description = "Replace the contents of this branch or leaf with those provided by the evaluation of default.nix.";
|
||||
default = false;
|
||||
};
|
||||
aliasDefault = mkOption {
|
||||
type = lib.types.bool;
|
||||
description = "Replace the contents of this branch or leaf with the default.nix.";
|
||||
default = false;
|
||||
};
|
||||
excludes = mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
description = "Exclude files or folders from the recurser.";
|
||||
default = [];
|
||||
};
|
||||
functor = {
|
||||
enable = mkOption {
|
||||
type = lib.types.bool;
|
||||
description = "Provide a functor for the path provided";
|
||||
default = false;
|
||||
};
|
||||
external = mkOption {
|
||||
type = lib.types.listOf lib.types.unspecified;
|
||||
description = "Add external imports into the functor.";
|
||||
default = [];
|
||||
};
|
||||
excludes = mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
description = "Exclude files or folders from the functor.";
|
||||
default = [];
|
||||
};
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
config.treeConfig = {
|
||||
"*" = {};
|
||||
"/" = {};
|
||||
};
|
||||
};
|
||||
configTree.treeConfig = config;
|
||||
configTreeModule = (evalModules {
|
||||
modules = [
|
||||
configTreeStruct
|
||||
configTree
|
||||
];
|
||||
}).config.treeConfig;
|
||||
mapAttrsRecursive = f: set: let
|
||||
recurse = path: set: let
|
||||
g = name: value: if isAttrs value
|
||||
then f (path ++ [name]) (recurse (path ++ [name]) value)
|
||||
else f (path ++ [name]) value;
|
||||
in mapAttrs g set;
|
||||
in f [] (recurse [] set);
|
||||
getPathString = path: builtins.concatStringsSep "/" path;
|
||||
getConfig = path: default: configTreeModule.${getPathString path} or default;
|
||||
revtail = path: sublist 0 (builtins.length path - 1) path;
|
||||
getConfigRecursive = path: let
|
||||
parentPath = revtail path;
|
||||
in getConfig (path ++ singleton "*") (getConfigRecursive parentPath);
|
||||
processLeaves = tree: config: mapAttrsRecursive (path: value: let
|
||||
pathString = getPathString path;
|
||||
leafConfig = if path == [] then
|
||||
configTreeModule."/"
|
||||
else getConfig path (getConfigRecursive (revtail path));
|
||||
processConfig = path: value: let
|
||||
processFunctor = prev: prev // {
|
||||
__functor = self: { ... }: {
|
||||
imports = attrValues (removeAttrs prev leafConfig.functor.excludes) ++ leafConfig.functor.external;
|
||||
{ inputs, lib, ... }: let
|
||||
mkTree = import ./mkTree.nix { inherit lib; };
|
||||
localTree = mkTree {
|
||||
inherit inputs;
|
||||
folder = ./.;
|
||||
config = {
|
||||
"/" = {
|
||||
excludes = [
|
||||
"tf"
|
||||
"inputs"
|
||||
"default"
|
||||
"patchedInputs"
|
||||
"mkTree"
|
||||
"outputs"
|
||||
"tree"
|
||||
"flake"
|
||||
"meta"
|
||||
"inputs"
|
||||
"trusted"
|
||||
];
|
||||
};
|
||||
"modules/nixos" = {
|
||||
functor = {
|
||||
external = [
|
||||
(inputs.tf-nix + "/modules/nixos/secrets.nix")
|
||||
(inputs.tf-nix + "/modules/nixos/secrets-users.nix")
|
||||
] ++ (with (import (inputs.arcexprs + "/modules")).nixos; [
|
||||
nix
|
||||
systemd
|
||||
dht22-exporter
|
||||
glauth
|
||||
modprobe
|
||||
kernel
|
||||
crypttab
|
||||
mutable-state
|
||||
common-root
|
||||
pulseaudio
|
||||
wireplumber
|
||||
alsa
|
||||
bindings
|
||||
matrix-appservices
|
||||
matrix-synapse-appservices
|
||||
display
|
||||
filebin
|
||||
mosh
|
||||
base16 base16-shared
|
||||
doc-warnings
|
||||
]);
|
||||
};
|
||||
};
|
||||
processAliasDefault = prev: prev.default;
|
||||
processDefault = prev: import prev.default (args // {
|
||||
inherit lib;
|
||||
tree = {
|
||||
prev = removeAttrs prev (singleton "default");
|
||||
pure = pureTree;
|
||||
impure = impureTree;
|
||||
"modules/home" = {
|
||||
functor = {
|
||||
external = [
|
||||
(import (inputs.arcexprs + "/modules")).home-manager
|
||||
(inputs.tf-nix + "/modules/home/secrets.nix")
|
||||
];
|
||||
};
|
||||
});
|
||||
processExcludes = prev: removeAttrs prev leafConfig.excludes;
|
||||
processes = optionals (isAttrs value) (
|
||||
optional (leafConfig.excludes != []) processExcludes
|
||||
++ optional leafConfig.evaluateDefault processDefault
|
||||
++ optional leafConfig.aliasDefault processAliasDefault
|
||||
++ optional leafConfig.functor.enable processFunctor
|
||||
);
|
||||
in pipe value processes;
|
||||
in processConfig path value) tree;
|
||||
pureTree = pureTreeGrab { base = folder; path = folder; };
|
||||
impureTree = processLeaves pureTree configTreeModule;
|
||||
in {
|
||||
config = configTreeModule;
|
||||
pure = pureTree;
|
||||
impure = impureTree;
|
||||
}
|
||||
};
|
||||
"modules/nixos".functor.enable = true;
|
||||
"modules/darwin".functor.enable = true;
|
||||
"modules/meta".functor.enable = true;
|
||||
"modules/tf".functor.enable = true;
|
||||
"modules/system".functor.enable = true;
|
||||
"modules/home".functor.enable = true;
|
||||
"modules/esphome".functor.enable = true;
|
||||
"modules/type".functor.enable = true;
|
||||
"nixos/systems".functor.enable = false;
|
||||
"darwin/systems".functor.enable = false;
|
||||
"nixos/*".functor = {
|
||||
enable = true;
|
||||
};
|
||||
"darwin/*".functor = {
|
||||
enable = true;
|
||||
};
|
||||
"system".functor.enable = true;
|
||||
"hardware".evaluateDefault = true;
|
||||
"nixos/cross".evaluateDefault = true;
|
||||
"hardware/*".evaluateDefault = true;
|
||||
"services/*".aliasDefault = true;
|
||||
"home".evaluateDefault = true;
|
||||
"home/*".functor.enable = true;
|
||||
};
|
||||
};
|
||||
trustedTree = mkTree {
|
||||
inherit inputs;
|
||||
folder = inputs.trusted;
|
||||
config = {
|
||||
"secrets".evaluateDefault = true;
|
||||
};
|
||||
};
|
||||
tree = localTree // {
|
||||
pure = localTree.pure // {
|
||||
trusted = trustedTree.pure;
|
||||
};
|
||||
impure = localTree.impure // {
|
||||
trusted = trustedTree.impure;
|
||||
};
|
||||
};
|
||||
in tree
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue