Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build-vm subcommand to nh os build to build a VM start script. #208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cdo256
Copy link

@cdo256 cdo256 commented Jan 9, 2025

This commit adds the boolean argument --vm/-v to build a virtual machine activation script instead of a full system. Note that this only applies to the os build subcommand, since it doesn't entirely make sense to switch to a VM, as I don't believe it's currently possible to switch between guest versions on the host or the guest directly without restarting the guest.

The patch #74 was created in May 2024, which proposed to create a build-vm subcommand, but was never fully implemented. This is fully working, though I've only tested it on flakes on NixOS. There's two outstanding todo's:

  • Get it working on Darwin (I don't have hardware to test it currently, though).
  • Fix the logic with the commands to ideally only show the --vm option for the nh os build subcommand.

Please let me know if you'd be willing to merge a completed patch, and I can sort out those todo's. My feeling is after writing this, that it would make more sense to have a full subcommand like pr 74 suggested. That way it's neater to add VM specific options (such as adding a with --with-bootloader) to act like #config.system.build.vmWithBootloader.

build-image is also possible, but looks to be a little more work since it's handled a bit differently for flakes vs plain Nix. See https://github.com/NixOS/nixpkgs/blob/481c7ead0aafb3d6dfa863ae87e3fdd6badc0b28/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh#L832

Just let me know if you'd merge this patch if I finish it off, and whether you'd prefer a build-vm sub-command or --vm option on the build command. I'm happy to do either, but nh os build-vm would be my vote.

@viperML
Copy link
Owner

viperML commented Jan 10, 2025

Yeah, as you probably saw, the types don't allow to have custom flags for each subcommand. You add make more spaguetti types like so, but I'm not sure I'm a fan:

pub enum OsSubcommand {
    Switch(OsRebuildArgs),
    Build(OsBuildArgs),
    // ...
}

struct OsBuildArgs {
    #[command(flatten)]
    common: OsRebuildArgs,

    #[arg(long)
    vm: bool
}

I'm happy to do either, but nh os build-vm would be my vote.

Indeed

@Eveeifyeve
Copy link

Eveeifyeve commented Jan 10, 2025

  • Get it working on Darwin (I don't have hardware to test it currently, though).

I have the hardware to test if needed, but there is also a github action that you can use to test.

@cdo256
Copy link
Author

cdo256 commented Jan 12, 2025

Yeah, as you probably saw, the types don't allow to have custom flags for each subcommand. You add make more spaguetti types like so, but I'm not sure I'm a fan:

...

I'm happy to do either, but nh os build-vm would be my vote.

Indeed

Thanks confirming, I'll switch the subcommand over to build-vm get a new patch pushed up tonight.

@Eveeifyeve
Copy link

I might add some nix ci testing to test all the functions.

@cdo256 cdo256 changed the title Add argument --vm to nh os build to build a VM start script. Add build-vm subcommand to nh os build to build a VM start script. Jan 13, 2025
@cdo256 cdo256 force-pushed the build-vm branch 2 times, most recently from d2e52bb to f6dc0cb Compare January 13, 2025 17:05
This commit adds the `nh os build-vm` subcommand to build a virtual
machine activation script instead of a full system.
This includes a new option --with-bootloader/-B that applies to just build-vm,
to build a VM with a bootloader.
@cdo256
Copy link
Author

cdo256 commented Jan 13, 2025

I've changed it to be the build-vm subcommand. I've tested on my different system configurations and it works the same as nixos-rebuild build-vm and nixos-rebuild build-vm-with-bootloader.

On researching NixOS images on Darwin, I found Nix documentaiton that states that the process of building a NixOS VM is a bit different in Darwin. I'm not familiar at all with MacOS or Darwin, so I would have to leave that part to someone else. I've backed out my changes so it's NixOS Linux VMs being built on Linux only.

There are four commits off master in my build-vm branch

The first commit "Add build-vm subcommand to nh os." is the main one with build-vm implemented, and --with-bootloader option.

The second commit disables nvd if the system hostname is different from the target hostname. Assuming that you only want to diff the versions if you are building for your local machine, rather than for another machine, or a VM. This might not be the desired behaviour, so anyone with push access can feel free to skip this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants