checkpoint
This commit is contained in:
parent
59e8ca812c
commit
6b3389c4b1
13 changed files with 608 additions and 188 deletions
|
|
@ -1,27 +0,0 @@
|
|||
{
|
||||
config,
|
||||
inputs,
|
||||
lib,
|
||||
...
|
||||
}: let
|
||||
inherit (lib) mkOption types;
|
||||
in {
|
||||
imports = [
|
||||
./options
|
||||
./strings
|
||||
];
|
||||
|
||||
config = {
|
||||
_module.args = {
|
||||
inherit
|
||||
baseNixosModules
|
||||
channelConfig
|
||||
mkPkgs
|
||||
sharedContext
|
||||
systemOverlays
|
||||
;
|
||||
};
|
||||
|
||||
flake.lib = config.localLib;
|
||||
};
|
||||
}
|
||||
37
lib/options.nix
Normal file
37
lib/options.nix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
{lib, ...}: let
|
||||
inherit (lib) mkOption types;
|
||||
in {
|
||||
mkUrlOptions = defaults: {
|
||||
host =
|
||||
mkOption {
|
||||
type = types.str;
|
||||
example = "host.tld";
|
||||
description = ''
|
||||
Hostname
|
||||
'';
|
||||
}
|
||||
// (defaults.host or {});
|
||||
|
||||
port =
|
||||
mkOption {
|
||||
type = types.port;
|
||||
default = 1234;
|
||||
example = "1234";
|
||||
description = ''
|
||||
Port
|
||||
'';
|
||||
}
|
||||
// (defaults.port or {});
|
||||
|
||||
protocol =
|
||||
mkOption {
|
||||
type = types.str;
|
||||
default = "https";
|
||||
example = "https";
|
||||
description = ''
|
||||
Which protocol to use when creating a url string
|
||||
'';
|
||||
}
|
||||
// (defaults.protocol or {});
|
||||
};
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
{lib, ...}: let
|
||||
inherit (lib) mkOption types;
|
||||
in {
|
||||
localLib.options = {
|
||||
mkUrlOptions =
|
||||
defaults:
|
||||
{
|
||||
host = mkOption {
|
||||
type = types.str;
|
||||
example = "host.tld";
|
||||
description = ''
|
||||
Hostname
|
||||
'';
|
||||
} // (defaults.host or {});
|
||||
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 1234;
|
||||
example = "1234";
|
||||
description = ''
|
||||
Port
|
||||
'';
|
||||
} // (defaults.port or {});
|
||||
|
||||
protocol = mkOption {
|
||||
type = types.str;
|
||||
default = "https";
|
||||
example = "https";
|
||||
description = ''
|
||||
Which protocol to use when creating a url string
|
||||
'';
|
||||
} // (defaults.protocol or {});
|
||||
};
|
||||
};
|
||||
}
|
||||
53
lib/strings.nix
Normal file
53
lib/strings.nix
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
{lib, ...}: let
|
||||
inherit (builtins) isString typeOf match toString head;
|
||||
inherit (lib) throwIfNot concatStringsSep splitStringBy toLower map concatMapAttrsStringSep;
|
||||
in {
|
||||
#========================================================================================
|
||||
# Converts a string to snake case
|
||||
#
|
||||
# simply replaces any uppercase letter to its lowercase variant preceeded by an underscore
|
||||
#========================================================================================
|
||||
toSnakeCase = str:
|
||||
throwIfNot (isString str) "toSnakeCase only accepts string values, but got ${typeOf str}" (
|
||||
str
|
||||
|> splitStringBy (prev: curr: builtins.match "[a-z]" prev != null && builtins.match "[A-Z]" curr != null) true
|
||||
|> map (p: toLower p)
|
||||
|> concatStringsSep "_"
|
||||
);
|
||||
|
||||
#========================================================================================
|
||||
# Converts a set of url parts to a string
|
||||
#========================================================================================
|
||||
toUrl = {
|
||||
protocol ? null,
|
||||
host,
|
||||
port ? null,
|
||||
path ? null,
|
||||
query ? null,
|
||||
hash ? null,
|
||||
}: let
|
||||
trim_slashes = str: str |> match "^\/*(.+?)\/*$" |> head;
|
||||
encode_to_str = set: concatMapAttrsStringSep "&" (n: v: "${n}=${v}") set;
|
||||
|
||||
_protocol =
|
||||
if protocol != null
|
||||
then "${protocol}://"
|
||||
else "";
|
||||
_port =
|
||||
if port != null
|
||||
then ":${toString port}"
|
||||
else "";
|
||||
_path =
|
||||
if path != null
|
||||
then "/${path |> trim_slashes}"
|
||||
else "";
|
||||
_query =
|
||||
if query != null
|
||||
then "?${query |> encode_to_str}"
|
||||
else "";
|
||||
_hash =
|
||||
if hash != null
|
||||
then "#${hash |> encode_to_str}"
|
||||
else "";
|
||||
in "${_protocol}${host}${_port}${_path}${_query}${_hash}";
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
{lib, ...}: let
|
||||
inherit (builtins) isString typeOf match toString head;
|
||||
inherit (lib) throwIfNot concatStringsSep splitStringBy toLower map concatMapAttrsStringSep;
|
||||
in {
|
||||
strings = {
|
||||
#========================================================================================
|
||||
# Converts a string to snake case
|
||||
#
|
||||
# simply replaces any uppercase letter to its lowercase variant preceeded by an underscore
|
||||
#========================================================================================
|
||||
toSnakeCase = str:
|
||||
throwIfNot (isString str) "toSnakeCase only accepts string values, but got ${typeOf str}" (
|
||||
str
|
||||
|> splitStringBy (prev: curr: builtins.match "[a-z]" prev != null && builtins.match "[A-Z]" curr != null) true
|
||||
|> map (p: toLower p)
|
||||
|> concatStringsSep "_"
|
||||
);
|
||||
|
||||
#========================================================================================
|
||||
# Converts a set of url parts to a string
|
||||
#========================================================================================
|
||||
toUrl = {
|
||||
protocol ? null,
|
||||
host,
|
||||
port ? null,
|
||||
path ? null,
|
||||
query ? null,
|
||||
hash ? null,
|
||||
}: let
|
||||
trim_slashes = str: str |> match "^\/*(.+?)\/*$" |> head;
|
||||
encode_to_str = set: concatMapAttrsStringSep "&" (n: v: "${n}=${v}") set;
|
||||
|
||||
_protocol =
|
||||
if protocol != null
|
||||
then "${protocol}://"
|
||||
else "";
|
||||
_port =
|
||||
if port != null
|
||||
then ":${toString port}"
|
||||
else "";
|
||||
_path =
|
||||
if path != null
|
||||
then "/${path |> trim_slashes}"
|
||||
else "";
|
||||
_query =
|
||||
if query != null
|
||||
then "?${query |> encode_to_str}"
|
||||
else "";
|
||||
_hash =
|
||||
if hash != null
|
||||
then "#${hash |> encode_to_str}"
|
||||
else "";
|
||||
in "${_protocol}${host}${_port}${_path}${_query}${_hash}";
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue