2024-05-04 23:15:57 +03:00
|
|
|
# Collection of common functions.
|
2024-12-18 09:40:11 +03:00
|
|
|
{ lib }:
|
2024-11-15 01:42:21 +03:00
|
|
|
{
|
2024-12-18 09:40:11 +03:00
|
|
|
# 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));
|
2024-04-06 03:03:58 +03:00
|
|
|
|
2024-12-18 09:40:11 +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
|
|
|
|
2024-12-18 13:50:17 +03:00
|
|
|
# Concat all nix file content by `file` key.
|
2024-12-18 09:40:11 +03:00
|
|
|
catFile =
|
2024-12-18 10:54:24 +03:00
|
|
|
files: args:
|
|
|
|
builtins.foldl' (acc: mod: acc + (builtins.readFile (import mod args).file) + "\n") "" files;
|
2024-06-25 04:04:39 +03:00
|
|
|
|
2024-12-18 13:50:17 +03:00
|
|
|
# Concat all nix files as a set.
|
2024-12-18 09:40:11 +03:00
|
|
|
catSet =
|
|
|
|
files: args: builtins.foldl' (acc: mod: acc // mod) { } (map (file: import file args) files);
|
2024-06-25 04:04:39 +03:00
|
|
|
|
2024-12-18 10:54:24 +03:00
|
|
|
# Concat all file contents.
|
2024-12-18 11:13:33 +03:00
|
|
|
readFiles = files: builtins.foldl' (acc: mod: acc + (builtins.readFile mod) + "\n") "" files;
|
2024-12-18 10:54:24 +03:00
|
|
|
|
2024-12-18 09:40:11 +03:00
|
|
|
# Systemd service that does not restart with system switch.
|
|
|
|
mkStaticSystemdService =
|
|
|
|
params:
|
|
|
|
params
|
|
|
|
// {
|
|
|
|
restartIfChanged = false;
|
|
|
|
stopIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
|
|
|
};
|
2024-04-06 03:03:58 +03:00
|
|
|
}
|