nyx/modules/flake/lib/helpers.nix

322 lines
6.6 KiB
Nix
Raw Normal View History

2025-01-27 14:03:42 +02:00
{lib}: let
inherit (lib.lists) forEach filter;
inherit (lib.attrsets) filterAttrs mapAttrsToList;
inherit (lib.filesystem) listFilesRecursive;
inherit (lib.strings) hasSuffix;
2025-01-27 14:03:42 +02:00
/*
*
filter files for the .nix suffix
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [k] they key, which is the file name
- [v] the value, which is the type of the file
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
filterNixFiles :: String -> String -> Bool
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
filterNixFiles "default.nix" "regular"
=> true
```
*/
filterNixFiles = k: v: v == "regular" && hasSuffix ".nix" k;
2025-01-27 14:03:42 +02:00
/*
*
Import all file that filterNixFiles allows for
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [path] the path to the directory
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
importNixFiles :: String -> List
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
importNixFiles ./.
=> [ {...} ]
```
*/
2025-01-27 14:03:42 +02:00
importNixFiles = path:
(forEach (
mapAttrsToList (name: _: path + ("/" + name)) (filterAttrs filterNixFiles (builtins.readDir path))
))
2025-01-27 14:03:42 +02:00
import;
2025-01-27 14:03:42 +02:00
/*
*
import all nix files and directories
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [dir] the directory to search for nix files
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
importNixFilesAndDirs :: String -> List
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
importNixFilesAndDirs ./.
=> [ "flake.nix" ]
```
*/
importNixFilesAndDirs = dir: filter (f: f != "default.nix") (listFilesRecursive dir);
2025-01-27 14:03:42 +02:00
/*
*
return an int based on boolean value
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [bool] the boolean value
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
boolToNum :: Bool -> Int
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
boolToNum true
=> 1
```
*/
2025-01-27 14:03:42 +02:00
boolToNum = bool:
if bool
then 1
else 0;
2025-01-27 14:03:42 +02:00
/*
*
convert a list of integers to a list of string
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [list] the list of integers
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
intListToStringList :: List -> List
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
intListToStringList [1 2 3]
=> ["1" "2" "3"]
```
*/
intListToStringList = list: map (toString list);
2025-01-27 14:03:42 +02:00
/*
*
a function that returns the index of an element in a list
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [list] the list to search in
- [elem] the element to search for
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
indexOf :: List -> Any -> Int
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
indexOf [1 2 3] 2
=> 1
```
*/
2025-01-27 14:03:42 +02:00
indexOf = list: elem: let
f = f: i:
if i == (builtins.length list)
then null
else if (builtins.elemAt list i) == elem
then i
else f f (i + 1);
in
f f 0;
2025-01-27 14:03:42 +02:00
/*
*
a function that checks if a list contains a list of given strings
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [list] the list to search in
- [targetStrings] the list of strings to search for
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
containsStrings :: List -> List -> Bool
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
containsStrings ["a" "b" "c"] ["a" "b"]
=> true
```
*/
2025-01-27 14:03:42 +02:00
containsStrings = list: targetStrings: builtins.all (s: builtins.any (x: x == s) list) targetStrings;
2025-01-27 14:03:42 +02:00
/*
*
Create git url aliases for a given domain
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [domain] the domain to create the alias for
- [alias] the alias to use
- [user] the user to use, this defaults to "git"
- [port] the port to use, this is optional
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
giturl :: (String -> String -> String -> Int) -> AttrSet
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
giturl { domain = "github.com"; alias = "gh"; }
=> {
"https://github.com/".insteadOf = "gh:";
"ssh://git@github.com/".pushInsteadOf = "gh:";
}
```
*/
2025-01-27 14:03:42 +02:00
giturl = {
domain,
alias,
user ? "git",
port ? null,
...
}: {
"https://${domain}/".insteadOf = "${alias}:";
"ssh://${user}@${domain}${
if (builtins.isNull port)
then ""
else if (builtins.isInt port)
then ":" + (builtins.toString port)
else ":" + port
}/".pushInsteadOf = "${alias}:";
};
2025-01-27 14:03:42 +02:00
/*
*
Create a public key for a given host
2025-01-27 14:03:42 +02:00
# Arguments
2025-01-27 14:03:42 +02:00
- [host] the host to create the public key for
- [key] this is a attrset with the key type and key
2025-01-27 14:03:42 +02:00
# Type
2025-01-27 14:03:42 +02:00
```
mkPub :: (String -> AttrSet -> AttrSet) -> String -> AttrSet -> AttrSet
```
2025-01-27 14:03:42 +02:00
# Example
2025-01-27 14:03:42 +02:00
```nix
mkPub "github.com" {
type = "rsa";
key = "AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==";
}
=> {
"github.com-rsa" = {
hostNames = [ "github.com" ];
publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==";
};
}
```
*/
mkPub = host: key: {
"${host}-${key.type}" = {
2025-01-27 14:03:42 +02:00
hostNames = [host];
publicKey = "ssh-${key.type} ${key.key}";
};
};
2025-01-27 14:03:42 +02:00
/*
*
Create public keys for a given host
# Arguments
- [host] the host to create the public keys for
- [keys] the list of keys to create
# Type
```
mkPubs :: (String -> List) -> String -> List -> AttrSet
```
# Example
```nix
mkPubs "github.com" [
{
type = "rsa";
key = "AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==";
}
{
type = "ed25519";
key = "AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
}
2025-01-27 14:03:42 +02:00
]
=> {
"github.com-ed25519" = {
hostNames = [ "github.com" ];
publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
};
"github.com-rsa" = {
hostNames = [ "github.com" ];
publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==";
};
}
```
*/
2025-01-27 14:03:42 +02:00
mkPubs = host: keys: lib.foldl' (acc: key: acc // mkPub host key) {} keys;
in {
inherit
mkPub
mkPubs
giturl
filterNixFiles
importNixFiles
importNixFilesAndDirs
boolToNum
containsStrings
indexOf
intListToStringList
;
}