mirror of
https://github.com/nix-community/disko.git
synced 2026-12-02 14:03:28 +01:00
disko2: Make generate write output to nix file
This commit is contained in:
1 parent
fa44467f82
commit
0d54c7ad3f
4 files changed
+80
-4
No files matched your search
@@ -1,5 +1,7 @@
|
|||||||
result
|
result
|
||||||
|
|
||||||
|
disko-config.nix
|
||||||
|
|
||||||
.direnv
|
.direnv
|
||||||
|
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
+1
-4
@@ -5,6 +5,7 @@ import json
|
|||||||
from typing import Any, Literal
|
from typing import Any, Literal
|
||||||
|
|
||||||
from disko.mode_dev import run_dev
|
from disko.mode_dev import run_dev
|
||||||
|
from disko.mode_generate import run_generate
|
||||||
from disko_lib.eval_config import eval_config
|
from disko_lib.eval_config import eval_config
|
||||||
from disko_lib.logging import LOGGER, debug, info
|
from disko_lib.logging import LOGGER, debug, info
|
||||||
from disko_lib.messages.msgs import err_missing_mode
|
from disko_lib.messages.msgs import err_missing_mode
|
||||||
@@ -49,10 +50,6 @@ def run_apply(
|
|||||||
return eval_config(disko_file=disko_file, flake=flake)
|
return eval_config(disko_file=disko_file, flake=flake)
|
||||||
|
|
||||||
|
|
||||||
def run_generate() -> DiskoResult[dict[str, Any]]:
|
|
||||||
return generate_config()
|
|
||||||
|
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
args: argparse.Namespace,
|
args: argparse.Namespace,
|
||||||
) -> DiskoResult[None | dict[str, Any]]:
|
) -> DiskoResult[None | dict[str, Any]]:
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import json
|
||||||
|
import re
|
||||||
|
from typing import Any
|
||||||
|
from disko_lib.logging import info
|
||||||
|
from disko_lib.messages.msgs import warn_generate_partial_failure
|
||||||
|
from disko_lib.result import DiskoResult, DiskoError
|
||||||
|
from disko_lib.types.disk import generate_config
|
||||||
|
from disko_lib.run_cmd import run
|
||||||
|
|
||||||
|
DEFAULT_CONFIG_FILE = "disko-config.nix"
|
||||||
|
|
||||||
|
HEADER_COMMENT = """
|
||||||
|
# This file was generated by disko generate
|
||||||
|
# Some disk and partition names were auto-generated from device attributes
|
||||||
|
# to be unique, but might not be very descriptive. Feel free to change them
|
||||||
|
# to something more meaningful.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
PARTIAL_FAILURE_COMMENT = """
|
||||||
|
###############################################################################
|
||||||
|
# WARNING: This file is incomplete! Some devices failed to generate a config. #
|
||||||
|
# Check the logs of the 'disko generate' command for more information. #
|
||||||
|
###############################################################################
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def run_generate() -> DiskoResult[dict[str, Any]]:
|
||||||
|
generated_config_result = generate_config()
|
||||||
|
generated_config = None
|
||||||
|
|
||||||
|
if isinstance(generated_config_result, DiskoError):
|
||||||
|
partial_failure_warning = generated_config_result.find_message(
|
||||||
|
warn_generate_partial_failure
|
||||||
|
)
|
||||||
|
if not partial_failure_warning:
|
||||||
|
return generated_config_result
|
||||||
|
|
||||||
|
generated_config = partial_failure_warning.details["partial_config"]
|
||||||
|
else:
|
||||||
|
generated_config = generated_config_result.value
|
||||||
|
|
||||||
|
config_as_nix = run(
|
||||||
|
[
|
||||||
|
"nix",
|
||||||
|
"eval",
|
||||||
|
"--expr",
|
||||||
|
"{ disko.devices = ("
|
||||||
|
f"builtins.fromJSON(''{json.dumps(generated_config)}'')"
|
||||||
|
"); }",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
if isinstance(config_as_nix, DiskoError):
|
||||||
|
return config_as_nix
|
||||||
|
|
||||||
|
# Contract the main attribute path to a single line to match all the examples
|
||||||
|
nix_code = re.sub(r"^\{ disko = \{ devices", "{ disko.devices", config_as_nix.value)
|
||||||
|
nix_code = re.sub(r"\}; \}$", "}", nix_code)
|
||||||
|
|
||||||
|
with open(DEFAULT_CONFIG_FILE, "w") as f:
|
||||||
|
f.write(HEADER_COMMENT)
|
||||||
|
if isinstance(generated_config_result, DiskoError):
|
||||||
|
f.write(PARTIAL_FAILURE_COMMENT)
|
||||||
|
f.write(nix_code)
|
||||||
|
info(f"Wrote generated config to {DEFAULT_CONFIG_FILE}")
|
||||||
|
|
||||||
|
run(["nixfmt", DEFAULT_CONFIG_FILE])
|
||||||
|
|
||||||
|
return generated_config_result
|
||||||
@@ -28,6 +28,14 @@ class DiskoError:
|
|||||||
) -> "DiskoError":
|
) -> "DiskoError":
|
||||||
return cls([DiskoMessage(factory, **details)], context)
|
return cls([DiskoMessage(factory, **details)], context)
|
||||||
|
|
||||||
|
def find_message(
|
||||||
|
self, message_factory: MessageFactory[P]
|
||||||
|
) -> None | DiskoMessage[P]:
|
||||||
|
for message in self.messages:
|
||||||
|
if message.factory == message_factory:
|
||||||
|
return message
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
DiskoResult = DiskoSuccess[T] | DiskoError
|
DiskoResult = DiskoSuccess[T] | DiskoError
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user