nix/lib/Util.nix

39 lines
1.3 KiB
Nix

# Collection of common functions.
{ lib }:
{
# Remove tabs indentation,
trimTabs =
text:
let
shouldStripTab = lines: builtins.all (line: (line == "") || (lib.strings.hasPrefix " " line)) lines;
stripTab = lines: builtins.map (line: lib.strings.removePrefix " " line) lines;
stripTabs = lines: if (shouldStripTab lines) then (stripTabs (stripTab lines)) else lines;
in
builtins.concatStringsSep "\n" (stripTabs (lib.strings.splitString "\n" text));
# List all files in a dir.
ls = path: map (f: "${path}/${f}") (builtins.attrNames (builtins.readDir path));
# Concat all nix file content by `file` key.
catFile =
files: args:
builtins.foldl' (acc: mod: acc + (builtins.readFile (import mod args).file) + "\n") "" files;
# Concat all nix files as a set.
catSet =
files: args: builtins.foldl' (acc: mod: acc // mod) { } (map (file: import file args) files);
# Concat all file contents.
readFiles = files: builtins.foldl' (acc: mod: acc + (builtins.readFile mod) + "\n") "" files;
# Systemd service that does not restart with system switch.
mkStaticSystemdService =
params:
params
// {
restartIfChanged = false;
stopIfChanged = false;
unitConfig.X-StopOnRemoval = false;
};
}