Skip to content

Commit

Permalink
Modified and added some custom functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
iivvaannxx committed Sep 1, 2023
1 parent 4672257 commit b5f4267
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 34 deletions.
29 changes: 29 additions & 0 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{ lib }: let

inherit (builtins) filter isAttrs;
inherit (lib) filterAttrs mapAttrs' mapAttrsToList concatLists;

in {

# Maps the given function to the given attribute set. Then filters the result based on the given predicate.
mapAndFilterAttrs = pred: fn: attrs: filterAttrs pred (mapAttrs' fn attrs);

# Returns all the keys of the given attribute set.
attrKeys = attrs: mapAttrsToList (key: _: key) attrs;

# Returns all the keys of the given attribute set, recursively.
attrKeysRecursive = attrs: let

getKeys = prefix: attrs: concatLists (mapAttrsToList (key: value: let

fullKey = if prefix == "" then key else "${prefix}.${key}";

# If the current value is another attrset, recurse into it.
recursedKeys = if isAttrs value
then [fullKey] ++ getKeys fullKey value
else [fullKey];

in recursedKeys) attrs);

in getKeys "" attrs;
}
43 changes: 28 additions & 15 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
includeLib = customLib: import customLib { inherit lib; };

# Include every function lib file.
attrsets = includeLib ./attrsets.nix;
conditionals = includeLib ./conditionals.nix;
filesystem = includeLib ./filesystem.nix;
generators = includeLib ./generators.nix;
global = includeLib ./global.nix;
make = includeLib ./make.nix;
nixos = includeLib ./nixos.nix;
options = includeLib ./options.nix;
parsers = includeLib ./parsers.nix;
strings = includeLib ./strings.nix;
validation = includeLib ./validation.nix;

in {

inherit (attrsets)

mapAndFilterAttrs

attrKeys
attrKeysRecursive
;

inherit (conditionals)

nullUnless
Expand All @@ -27,20 +36,11 @@ in {

inherit (filesystem)

getSubfolders
readJSON
;

inherit (generators)

commaSeparatedList
stringWithSuffix
;
readDirRecursive
readDirRecursive'

inherit (global)

importProfile
importConfig
getModulesRecursive
mapModulesRecursive
;

inherit (make)
Expand All @@ -66,6 +66,19 @@ in {
mkPackageListOption
;

inherit (parsers)

fromJSON'
;

inherit (strings)

commaSeparatedList
stringWithSuffix

removeAllChars
;

inherit (validation)

stringIsEmpty
Expand Down
81 changes: 64 additions & 17 deletions lib/filesystem.nix
Original file line number Diff line number Diff line change
@@ -1,28 +1,75 @@
{ lib }: let

inherit (builtins) readDir;
inherit (lib) filterAttrs mapAttrsToList;
inherit (builtins) readDir filter pathExists baseNameOf;
inherit (lib) mapAttrsToList concatLists filterAttrs hasPrefix hasSuffix;

in {
# Read the content of a directory recursively and return a list of files and directories.
readDirRec = dirPath: filterFn: maxDepth: let

# Retrieves all the folder names from a given directory.
getSubfolders = dirPath: let
readRecursive = fullPath: prefix: currentDepth: let

# Read the directory and filter out anything that is not a directory.
dirContent = readDir dirPath;
subfolders = filterAttrs (_: value: value == "directory") dirContent;
# Read the content of the current directory
dirContent = if currentDepth > 0 || maxDepth == -1 then (readDir fullPath) else { };
filteredDirs = filterAttrs filterFn dirContent;

# Keep the key only, as it contains the folder name.
subfolderList = mapAttrsToList (key: _: key) subfolders;
recurse = (name: type: let

in subfolderList;
# Full path to recursively read other dirs.
nextFullPath = "${fullPath}/${name}";

# Reads and parses a JSON file at the given path.
readJSON = path: let
# Relative path to store in the output.
relativePath = if prefix == "" then name else "${prefix}/${name}";
current = [{ inherit type; path = relativePath; }];

# Read the file and parse it as JSON.
fileContent = builtins.readFile path;
parsedJSON = builtins.fromJSON fileContent;
# If the current directory is a directory, then recursively read it.
output = if type == "directory"
then current ++ readRecursive nextFullPath relativePath (currentDepth - 1)
else current;

in parsedJSON;
in output);

in concatLists (mapAttrsToList recurse filteredDirs);

in readRecursive dirPath "" maxDepth;

in rec {

# Reads the contents of the given directory recursively. Allows to speciify a max depth and whether to include hidden files and directories.
readDirRecursive' = dirPath: { maxDepth ? -1, includeHidden ? false }: let

# Filter function to exclude hidden files and directories.
filterFn = if includeHidden then (_: _: true) else (name: _: (! hasPrefix "." name));
contents = readDirRec dirPath filterFn maxDepth;

in contents;

# Reads the contents of the entire given directory recursively.
readDirRecursive = dirPath: (readDirRecursive' dirPath { });

# Retrieves all the paths of non 'default.nix' files in the given folder.
getModulesRecursive = dirPath: let

# Read the content of the directory recursively.
contents = readDirRecursive dirPath;

# Filter out files that are not modules.
modules = filter ({ type, path }: let

# It's a file, it's not a default.nix and it has a .nix extension.
isModule = type == "regular" && (baseNameOf path) != "default.nix" && hasSuffix ".nix" path;

in isModule) contents;

in map ({ type, path }: path) modules;

# Maps the given function to all the modules in the given directory.
mapModulesRecursive = dirPath: fn: let

# Get all the modules in the given directory.
modules = getModulesRecursive dirPath;

# Map the given function to each module.
mapped = map (path: fn path) modules;

in mapped;
}
4 changes: 2 additions & 2 deletions lib/nixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ in {
mkHost = { hostPath, system, users, inputs, lib, nixpkgs, unstablepkgs, home-manager }: let

inherit (lib) nixosSystem;
inherit (lib.custom) readJSON;
inherit (lib.custom) fromJSON';

# The path where all the source code is located.
rootPath = ../.;
Expand All @@ -73,7 +73,7 @@ in {
nixosModules = (import "${rootPath}/modules/nixos");
nixSystemRegistry = {

nix = { registry = (readJSON "${rootPath}/registry.json"); };
nix = { registry = (fromJSON' "${rootPath}/registry.json"); };
};

nixosUsers = mkSystemUsers users;
Expand Down
18 changes: 18 additions & 0 deletions lib/parsers.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ lib }: let

inherit (builtins) isPath readFile fromJSON;

in {

# Same as 'fromJSON' but allows paths as an input.
fromJSON' = source: let

# Check if it's a file, if so, read it.
isFilepath = isPath path;
jsonContent = if isFilepath then (readFile source) else (source);

# Parse the json contents.
parsedJSON = fromJSON jsonContent;

in parsedJSON;
}
4 changes: 4 additions & 0 deletions lib/generators.nix → lib/strings.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{ lib }: let

inherit (builtins) replaceStrings;
inherit (lib) hasSuffix concatStringsSep;

in {
Expand All @@ -13,4 +14,7 @@ in {
containsSuffix = hasSuffix suffix str;

in if containsSuffix then str else "${str}${suffix}";

# Removes all occurrences of the given characters from the given string.
removeAllChars = chars: str: replaceStrings chars [ "" ] str;
}

0 comments on commit b5f4267

Please sign in to comment.