2024-05-18 02:27:49 +00:00
|
|
|
import re
|
2024-05-27 11:33:15 +00:00
|
|
|
from typing import Tuple, Optional
|
2024-05-18 02:27:49 +00:00
|
|
|
|
2024-05-17 01:23:37 +00:00
|
|
|
FLAKE_CONFIG_PATH = "/etc/nixos/sp-modules/flake.nix"
|
|
|
|
|
|
|
|
|
|
|
|
class FlakeServiceManager:
|
2024-05-18 02:32:56 +00:00
|
|
|
def __enter__(self) -> "FlakeServiceManager":
|
2024-05-17 01:23:37 +00:00
|
|
|
self.services = {}
|
2024-05-18 02:27:49 +00:00
|
|
|
|
|
|
|
with open(FLAKE_CONFIG_PATH, "r") as file:
|
|
|
|
for line in file:
|
|
|
|
service_name, url = self._extract_services(input_string=line)
|
|
|
|
if service_name and url:
|
|
|
|
self.services[service_name] = url
|
|
|
|
|
2024-05-17 01:23:37 +00:00
|
|
|
return self
|
|
|
|
|
2024-05-18 02:32:56 +00:00
|
|
|
def _extract_services(
|
|
|
|
self, input_string: str
|
|
|
|
) -> Tuple[Optional[str], Optional[str]]:
|
2024-05-24 21:13:46 +00:00
|
|
|
pattern = r"inputs\.([\w-]+)\.url\s*=\s*([\S]+);"
|
2024-05-18 02:27:49 +00:00
|
|
|
match = re.search(pattern, input_string)
|
|
|
|
|
|
|
|
if match:
|
|
|
|
variable_name = match.group(1)
|
|
|
|
url = match.group(2)
|
|
|
|
return variable_name, url
|
|
|
|
else:
|
|
|
|
return None, None
|
|
|
|
|
2024-05-18 02:32:56 +00:00
|
|
|
def __exit__(self, exc_type, exc_value, traceback) -> None:
|
2024-05-17 01:23:37 +00:00
|
|
|
with open(FLAKE_CONFIG_PATH, "w") as file:
|
|
|
|
file.write(
|
|
|
|
"""
|
|
|
|
{
|
|
|
|
description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc";\n
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
for key, value in self.services.items():
|
|
|
|
file.write(
|
|
|
|
f"""
|
|
|
|
inputs.{key}.url = {value};
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
file.write(
|
|
|
|
"""
|
|
|
|
outputs = _: { };
|
|
|
|
}
|
|
|
|
"""
|
2024-05-31 13:10:05 +00:00
|
|
|
)
|