Skip to content

Commit

Permalink
nixos-rebuild-ng: refactor using match
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Jan 5, 2025
1 parent fbb00d3 commit d176773
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,39 +487,43 @@ def validate_image_variant(variants: ImageVariants) -> None:
sudo=args.sudo,
)

if action in (Action.SWITCH, Action.BOOT, Action.TEST, Action.DRY_ACTIVATE):
nix.switch_to_configuration(
path_to_config,
action,
target_host=target_host,
sudo=args.sudo,
specialisation=args.specialisation,
install_bootloader=args.install_bootloader,
)
elif action in (Action.BUILD_VM, Action.BUILD_VM_WITH_BOOTLOADER):
# If you get `not-found`, please open an issue
vm_path = next(path_to_config.glob("bin/run-*-vm"), "not-found")
print(
"Done. The virtual machine can be started by running",
end=" ",
file=sys.stderr,
flush=True,
)
print(vm_path, flush=True)
elif action == Action.BUILD_IMAGE:
image_name = variants[args.image_variant]
disk_path = path_to_config / image_name
print(
"Done. The disk image can be found in",
end=" ",
file=sys.stderr,
flush=True,
)
print(disk_path, flush=True)
match action:
case Action.SWITCH | Action.BOOT | Action.TEST | Action.DRY_ACTIVATE:
nix.switch_to_configuration(
path_to_config,
action,
target_host=target_host,
sudo=args.sudo,
specialisation=args.specialisation,
install_bootloader=args.install_bootloader,
)
case Action.BUILD_VM | Action.BUILD_VM_WITH_BOOTLOADER:
# If you get `not-found`, please open an issue
vm_path = next(path_to_config.glob("bin/run-*-vm"), "not-found")
print(
"Done. The virtual machine can be started by running",
end=" ",
file=sys.stderr,
flush=True,
)
print(vm_path, flush=True)
case Action.BUILD_IMAGE:
image_name = variants[args.image_variant]
disk_path = path_to_config / image_name
print(
"Done. The disk image can be found in",
end=" ",
file=sys.stderr,
flush=True,
)
print(disk_path, flush=True)

case Action.EDIT:
nix.edit(flake, flake_build_flags)

case Action.DRY_RUN:
assert False, "DRY_RUN should be a DRY_BUILD alias"
raise AssertionError("DRY_RUN should be a DRY_BUILD alias")

case Action.LIST_GENERATIONS:
generations = nix.list_generations(profile)
if args.json:
Expand All @@ -535,11 +539,13 @@ def validate_image_variant(variants: ImageVariants) -> None:
"current": "Current",
}
print(tabulate(generations, headers=headers))

case Action.REPL:
if flake:
nix.repl_flake("toplevel", flake, flake_build_flags)
else:
nix.repl("system", build_attr, build_flags)

case _:
assert_never(action)

Expand Down

0 comments on commit d176773

Please sign in to comment.