nix/lib/Util.nix

36 lines
1.2 KiB
Nix
Raw Normal View History

# Collection of common functions.
2024-06-26 11:23:36 +03:00
{ lib }: rec {
2024-04-06 03:03:58 +03:00
# Remove tabs indentation,
trimTabs = text: let
2024-06-26 11:23:36 +03:00
shouldStripTab = lines: builtins.all (line: (line == "") || (lib.strings.hasPrefix " " line)) lines;
stripTab = lines: builtins.map (line: lib.strings.removePrefix " " line) lines;
2024-04-06 03:03:58 +03:00
stripTabs = lines: if (shouldStripTab lines) then (stripTabs (stripTab lines)) else lines;
in
2024-06-26 11:23:36 +03:00
builtins.concatStringsSep "\n" (stripTabs (lib.strings.splitString "\n" text));
2024-04-06 03:03:58 +03:00
# List all files in a dir.
ls = path: map (f: "${path}/${f}") (builtins.attrNames (builtins.readDir path));
2024-04-28 21:24:49 +03:00
# Concat all files by `text` key.
catText = files: args: builtins.foldl' (acc: mod:
acc + trimTabs ((import mod args).text)
) "" files;
2024-06-22 23:34:03 +03:00
# Concat all file paths by `file` key.
catFile = files: args: builtins.foldl' (acc: mod:
acc + trimTabs (builtins.readFile (import mod args).file)
) "" files;
2024-05-12 02:40:37 +03:00
# Concat all files as a set.
catSet = files: args: lib.mkMerge (map (file: import file args) files);
# Systemd service that does not restart with system switch.
mkStaticSystemdService = params: params // {
restartIfChanged = false;
stopIfChanged = false;
unitConfig.X-StopOnRemoval = false;
};
2024-06-25 04:04:39 +03:00
2024-04-06 03:03:58 +03:00
}