Merge pull request 'Integrate nixos-facter into UI' (#1963) from hsjobeki/clan-core:hsjobeki-main into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/1963
This commit is contained in:
hsjobeki
2024-08-26 09:52:05 +00:00
6 changed files with 113 additions and 9 deletions

54
flake.lock generated
View File

@@ -1,5 +1,28 @@
{
"nodes": {
"blueprint": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"systems": [
"systems"
]
},
"locked": {
"lastModified": 1724053269,
"narHash": "sha256-DinmPyxmUSLjBUYMe3eK0GKykwe33vWbVTmp7++P4Ng=",
"owner": "numtide",
"repo": "blueprint",
"rev": "766302be9063650ca6578e5ba09cc4260b0da29c",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "blueprint",
"type": "github"
}
},
"disko": {
"inputs": {
"nixpkgs": [
@@ -40,6 +63,35 @@
"type": "github"
}
},
"nixos-facter-modules": {
"inputs": {
"blueprint": [
"blueprint"
],
"nixpkgs": [
"nixpkgs"
],
"systems": [
"systems"
],
"treefmt-nix": [
"treefmt-nix"
]
},
"locked": {
"lastModified": 1724320449,
"narHash": "sha256-0hB5P75tIwPS9lNI3nYxEorpcFtuQUJGmGHGIEgWksY=",
"owner": "numtide",
"repo": "nixos-facter-modules",
"rev": "e00d789870e72baabb79d19afcc645428c3c6980",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "nixos-facter-modules",
"type": "github"
}
},
"nixos-images": {
"inputs": {
"nixos-stable": [],
@@ -79,8 +131,10 @@
},
"root": {
"inputs": {
"blueprint": "blueprint",
"disko": "disko",
"flake-parts": "flake-parts",
"nixos-facter-modules": "nixos-facter-modules",
"nixos-images": "nixos-images",
"nixpkgs": "nixpkgs",
"sops-nix": "sops-nix",

View File

@@ -17,6 +17,17 @@
systems.url = "github:nix-systems/default";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
nixos-facter-modules.url = "github:numtide/nixos-facter-modules";
nixos-facter-modules.inputs.nixpkgs.follows = "nixpkgs";
nixos-facter-modules.inputs.systems.follows = "systems";
nixos-facter-modules.inputs.blueprint.follows = "blueprint";
nixos-facter-modules.inputs.treefmt-nix.follows = "treefmt-nix";
# Pinned input for nixos-facter-modules
# Allows downstream flakes to .follow override the input
blueprint.url = "github:numtide/blueprint";
blueprint.inputs.nixpkgs.follows = "nixpkgs";
blueprint.inputs.systems.follows = "systems";
};
outputs =

View File

@@ -64,15 +64,37 @@ let
modules =
let
settings = machineSettings name;
facterJson = "${directory}/machines/${name}/facter.json";
hwConfig = "${directory}/machines/${name}/hardware-configuration.nix";
facterModules = lib.optionals (builtins.pathExists facterJson) [
clan-core.inputs.nixos-facter-modules.nixosModules.facter
{ config.facter.reportPath = facterJson; }
];
in
(machineImports settings)
++ facterModules
++ [
{
# Autoinclude configuration.nix and hardware-configuration.nix
imports = builtins.filter builtins.pathExists [
"${directory}/machines/${name}/configuration.nix"
"${directory}/machines/${name}/hardware-configuration.nix"
hwConfig
];
config.warnings =
lib.optionals
(builtins.all builtins.pathExists [
hwConfig
facterJson
])
[
''
Duplicate hardware facts: '${hwConfig}' and '${facterJson}' exist.
Using both is not recommended.
It is recommended to use the hardware facts from '${facterJson}', please remove '${hwConfig}'.
''
];
}
settings
clan-core.nixosModules.clanCore

View File

@@ -104,6 +104,9 @@ def generate_machine_hardware_info(
password: str | None = None,
keyfile: str | None = None,
force: bool | None = False,
report_type: Literal[
"nixos-generate-config", "nixos-facter"
] = "nixos-generate-config",
) -> HardwareReport:
"""
Generate hardware information for a machine
@@ -114,6 +117,14 @@ def generate_machine_hardware_info(
if hostname is not None:
machine.target_host_address = hostname
nixos_generate_cmd = [
"nixos-generate-config", # Filesystems are managed by disko
"--no-filesystems",
"--show-hardware-config",
]
nixos_facter_cmd = ["nix", "run", "--refresh", "github:numtide/nixos-facter"]
host = machine.target_host
target_host = f"{host.user or 'root'}@{host.host}"
cmd = nix_shell(
@@ -137,10 +148,11 @@ def generate_machine_hardware_info(
else []
),
target_host,
"nixos-generate-config",
# Filesystems are managed by disko
"--no-filesystems",
"--show-hardware-config",
*(
nixos_generate_cmd
if report_type == "nixos-generate-config"
else nixos_facter_cmd
),
],
)
out = run(cmd)
@@ -149,7 +161,9 @@ def generate_machine_hardware_info(
log.error(out)
raise ClanError(f"Failed to inspect {machine_name}. Address: {hostname}")
hw_file = Path(f"{clan_dir}/machines/{machine_name}/hardware-configuration.nix")
hw_file = Path(
f"{clan_dir}/machines/{machine_name}/{hw_nix_file if report_type == 'nixos-generate-config' else facter_file}"
)
hw_file.parent.mkdir(parents=True, exist_ok=True)
# Check if the hardware-configuration.nix file is a template
@@ -167,7 +181,7 @@ def generate_machine_hardware_info(
# Backup the existing file
backup_file = hw_file.with_suffix(".bak")
hw_file.replace(backup_file)
print(f"Backed up existing hardware-configuration.nix to {backup_file}")
print(f"Backed up existing {hw_file} to {backup_file}")
with open(hw_file, "w") as f:
f.write(out.stdout)
@@ -197,7 +211,7 @@ def generate_machine_hardware_info(
location=f"{__name__} {hw_file}",
)
return HardwareReport("nixos-generate-config")
return HardwareReport(report_type)
@dataclass

View File

@@ -8,6 +8,8 @@ let
imports = [
./iwd.nix
self.nixosModules.installer
# Allow to download pre-build binaries from our nix caches
self.clanModules.trusted-nix-caches
];
system.stateVersion = config.system.nixos.version;

View File

@@ -77,7 +77,7 @@ const InstallMachine = (props: InstallMachineProps) => {
machine_name: props.name,
});
if (result.status === "error") throw new Error("Failed to fetch data");
return result.data || null;
return result.data?.file === "nixos-facter" || null;
}
return null;
},
@@ -152,6 +152,7 @@ const InstallMachine = (props: InstallMachineProps) => {
machine_name: props.name,
keyfile: props.sshKey?.name,
hostname: props.targetHost,
report_type: "nixos-facter",
});
toast.dismiss(loading_toast);
hwInfoQuery.refetch();