Merge pull request 'clanModules/localsend: add displayName functionality' (#2228) from kenji/clan-core:kenji-clanModules/localsend/feat/add-displayName into main
This commit is contained in:
@@ -13,11 +13,15 @@ in
|
|||||||
# - cli frontend: https://github.com/localsend/localsend/issues/11
|
# - cli frontend: https://github.com/localsend/localsend/issues/11
|
||||||
# - ipv6 support: https://github.com/localsend/localsend/issues/549
|
# - ipv6 support: https://github.com/localsend/localsend/issues/549
|
||||||
options.clan.localsend = {
|
options.clan.localsend = {
|
||||||
defaultLocation = lib.mkOption {
|
|
||||||
type = lib.types.str;
|
displayName = lib.mkOption {
|
||||||
description = "The default download location";
|
type = lib.types.nullOr lib.types.str;
|
||||||
|
default = null;
|
||||||
|
description = "The name that localsend will use to display your instance.";
|
||||||
};
|
};
|
||||||
|
|
||||||
package = lib.mkPackageOption pkgs "localsend" { };
|
package = lib.mkPackageOption pkgs "localsend" { };
|
||||||
|
|
||||||
ipv4Addr = lib.mkOption {
|
ipv4Addr = lib.mkOption {
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
example = "192.168.56.2/24";
|
example = "192.168.56.2/24";
|
||||||
@@ -35,9 +39,13 @@ in
|
|||||||
config = {
|
config = {
|
||||||
clan.core.state.localsend.folders = [
|
clan.core.state.localsend.folders = [
|
||||||
"/var/localsend"
|
"/var/localsend"
|
||||||
config.clan.localsend.defaultLocation
|
|
||||||
];
|
];
|
||||||
environment.systemPackages = [ config.clan.localsend.package ];
|
environment.systemPackages = [
|
||||||
|
(pkgs.callPackage ./localsend-ensure-config {
|
||||||
|
localsend = config.clan.localsend.package;
|
||||||
|
alias = config.clan.localsend.displayName;
|
||||||
|
})
|
||||||
|
];
|
||||||
|
|
||||||
networking.firewall.interfaces."zt+".allowedTCPPorts = [ 53317 ];
|
networking.firewall.interfaces."zt+".allowedTCPPorts = [ 53317 ];
|
||||||
networking.firewall.interfaces."zt+".allowedUDPPorts = [ 53317 ];
|
networking.firewall.interfaces."zt+".allowedUDPPorts = [ 53317 ];
|
||||||
|
|||||||
22
clanModules/localsend/localsend-ensure-config/default.nix
Normal file
22
clanModules/localsend/localsend-ensure-config/default.nix
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
lib,
|
||||||
|
writers,
|
||||||
|
writeShellScriptBin,
|
||||||
|
localsend,
|
||||||
|
alias ? null,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
localsend-ensure-config = writers.writePython3 "localsend-ensure-config" {
|
||||||
|
flakeIgnore = [
|
||||||
|
# We don't live in the dark ages anymore.
|
||||||
|
# Languages like Python that are whitespace heavy will overrun
|
||||||
|
# 79 characters..
|
||||||
|
"E501"
|
||||||
|
];
|
||||||
|
} (builtins.readFile ./localsend-ensure-config.py);
|
||||||
|
in
|
||||||
|
writeShellScriptBin "localsend" ''
|
||||||
|
set -xeu
|
||||||
|
${localsend-ensure-config} ${lib.optionalString (alias != null) alias}
|
||||||
|
${lib.getExe localsend}
|
||||||
|
''
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def load_json(file_path: Path) -> dict[str, any]:
|
||||||
|
try:
|
||||||
|
with file_path.open("r") as file:
|
||||||
|
return json.load(file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def save_json(file_path: Path, data: dict[str, any]) -> None:
|
||||||
|
with file_path.open("w") as file:
|
||||||
|
json.dump(data, file, indent=4)
|
||||||
|
|
||||||
|
|
||||||
|
def update_json(file_path: Path, updates: dict[str, any]) -> None:
|
||||||
|
data = load_json(file_path)
|
||||||
|
data.update(updates)
|
||||||
|
save_json(file_path, data)
|
||||||
|
|
||||||
|
|
||||||
|
def config_location() -> str:
|
||||||
|
config_file = "shared_preferences.json"
|
||||||
|
config_directory = ".local/share/org.localsend.localsend_app"
|
||||||
|
config_path = Path.home() / Path(config_directory) / Path(config_file)
|
||||||
|
return config_path
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_config_directory() -> None:
|
||||||
|
config_directory = Path(config_location()).parent
|
||||||
|
config_directory.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def load_config() -> dict[str, any]:
|
||||||
|
return load_json(config_location())
|
||||||
|
|
||||||
|
|
||||||
|
def save_config(data: dict[str, any]) -> None:
|
||||||
|
save_json(config_location(), data)
|
||||||
|
|
||||||
|
|
||||||
|
def update_username(username: str, data: dict[str, any]) -> dict[str, any]:
|
||||||
|
data["flutter.ls_alias"] = username
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]) -> None:
|
||||||
|
try:
|
||||||
|
display_name = argv[1]
|
||||||
|
except IndexError:
|
||||||
|
# This is not an error, just don't update the name
|
||||||
|
print("No display name provided.")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
ensure_config_directory()
|
||||||
|
updated_data = update_username(display_name, load_config())
|
||||||
|
save_config(updated_data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main(sys.argv[:2])
|
||||||
Reference in New Issue
Block a user