feat: add custom env support

This commit is contained in:
Rudra Saraswat 2026-04-02 04:24:49 +01:00
parent d09d0960de
commit 2a08830ac0
3 changed files with 17 additions and 41 deletions

View file

@ -10,15 +10,17 @@ class RootFS:
rootfs_path: A string containing the path to the root filesystem rootfs_path: A string containing the path to the root filesystem
""" """
def __init__(self, rootfs_path: str, distro_config: dict) -> None: def __init__(self, rootfs_path: str, distro_config: dict, env: dict) -> None:
"""Initialises an instance based on a rootfs path and distro configuration. """Initialises an instance based on a rootfs path and distro configuration.
Args: Args:
rootfs: Path to root filesystem. rootfs: Path to root filesystem.
distro_config: Dictionary containing distro configuration. distro_config: Dictionary containing distro configuration.
env: Dictionary containing environment for command execution.
""" """
self.rootfs_path = rootfs_path self.rootfs_path = rootfs_path
self.distro_config = distro_config self.distro_config = distro_config
self.env = env
def exists(self, path): def exists(self, path):
"""Checks if path exists within rootfs. """Checks if path exists within rootfs.
@ -28,18 +30,6 @@ class RootFS:
""" """
return os.path.exists(os.path.join(self.rootfs_path, path)) return os.path.exists(os.path.join(self.rootfs_path, path))
def init(self) -> subprocess.CompletedProcess:
"""Initialise rootfs."""
completedProcess = subprocess.run(
["bash", "-s"],
text=True,
input=self.distro_config["before-stages"],
cwd=self.rootfs_path,
)
return completedProcess
def exec(self, cmd, **kwargs) -> subprocess.CompletedProcess: def exec(self, cmd, **kwargs) -> subprocess.CompletedProcess:
"""Runs command within rootfs. """Runs command within rootfs.
@ -52,6 +42,7 @@ class RootFS:
"systemd-nspawn", "systemd-nspawn",
"--quiet", "--quiet",
"--pipe", "--pipe",
*[f"--setenv={name}={val}" for name, val in self.env.items()],
"-D", "-D",
self.rootfs_path, self.rootfs_path,
] ]
@ -59,31 +50,5 @@ class RootFS:
**kwargs, **kwargs,
) )
# def copy_kernels_to_boot(self) -> None:
# """Copies any found kernels to /boot within rootfs."""
# kernels = [
# kernel
# for kernel in os.listdir(f"{self.rootfs_path}/usr/lib/modules")
# if self.exists(f"/usr/lib/modules/{kernel}/vmlinuz")
# ]
# if len(kernels) == 0:
# return
# for boot_file in os.listdir(f"{self.rootfs_path}/boot"):
# if not os.path.isdir(boot_file):
# self.exec(["rm", "-f", f"/boot/{boot_file}"])
# for kernel in kernels:
# self.exec(
# ["cp", f"/usr/lib/modules/{kernel}/vmlinuz", f"/boot/vmlinuz-{kernel}"],
# stdout=subprocess.DEVNULL,
# stderr=subprocess.DEVNULL,
# )
# def gen_initramfs(self) -> None:
# """Generates initramfs within rootfs."""
# self.exec(["dracut", "--force", "--regenerate-all"])
def __repr__(self) -> str: def __repr__(self) -> str:
return self.rootfs_path return self.rootfs_path

View file

@ -13,16 +13,17 @@ def run_script_rootfs(rootfs: RootFS, input: str, args: list):
rootfs.exec(["bash", "-s", *args], text=True, input=input) rootfs.exec(["bash", "-s", *args], text=True, input=input)
def gen_rootfs(system_config: dict, rootfs_path: str) -> RootFS: def gen_rootfs(system_config: dict, rootfs_path: str, env: dict = {}) -> RootFS:
"""Generates a rootfs for a given system configuration.""" """Generates a rootfs for a given system configuration."""
rootfs = RootFS(rootfs_path, system_config["distro-config"]) rootfs = RootFS(rootfs_path, system_config["distro-config"], env)
subprocess.run( subprocess.run(
["bash", "-s"], ["bash", "-s"],
text=True, text=True,
input=system_config["distro-config"]["before-stages"], input=system_config["distro-config"]["before-stages"],
cwd=rootfs_path, cwd=rootfs_path,
env=os.environ.copy() | env,
) )
modules = {} modules = {}
@ -46,6 +47,7 @@ def gen_rootfs(system_config: dict, rootfs_path: str) -> RootFS:
text=True, text=True,
input=system_config["distro-config"]["after-stages"], input=system_config["distro-config"]["after-stages"],
cwd=str(rootfs_path), cwd=str(rootfs_path),
env=os.environ.copy() | env,
) )
with open(os.path.join(rootfs_path, "usr/system.json"), "w") as system_json_file: with open(os.path.join(rootfs_path, "usr/system.json"), "w") as system_json_file:

View file

@ -22,6 +22,9 @@ def resolve_config(system_config: dict) -> dict:
"override": system_config["override"] "override": system_config["override"]
if isinstance(system_config.get("override"), list) if isinstance(system_config.get("override"), list)
else [], else [],
"env": system_config["env"]
if isinstance(system_config.get("env"), dict)
else {},
"distro-config": system_config["distro-config"], "distro-config": system_config["distro-config"],
"auto-update": system_config["auto-update"] "auto-update": system_config["auto-update"]
if isinstance(system_config.get("auto-update"), dict) if isinstance(system_config.get("auto-update"), dict)
@ -63,6 +66,12 @@ def resolve_config(system_config: dict) -> dict:
else [] else []
) )
base_config["env"] = (
base_config["env"] | system_config["env"]
if isinstance(system_config.get("env"), dict)
else base_config["env"]
)
base_config["auto-update"] = ( base_config["auto-update"] = (
system_config["auto-update"] system_config["auto-update"]
if isinstance(system_config.get("auto-update"), dict) if isinstance(system_config.get("auto-update"), dict)