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

Allow generating image using existing nixosSystem #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lugeha
Copy link

@lugeha lugeha commented Jun 12, 2023

Accepts a new paramater nixosSystem if not null use extendModules rather than creating a new nixosSystem.

Accepts a new paramater nixosSystem if not null use extendModules
rather than creating a new nixosSystem.
@DavHau
Copy link
Member

DavHau commented Jun 12, 2023

Have you seen #241
It allows you to add all formats to your system by importing a single module.
I believe #241 is even more ergonomic.

@lugeha
Copy link
Author

lugeha commented Jun 13, 2023

I may be missing something, but all the module does is let you include a variety of format that can be built. My personal opinion is that it tightly couples the image generation to the system generation. I'm not sure if or why this would be desirable.

Here is my simple use case,

radius-lab-vm = nixos-generators.nixosGenerate {
          inherit system pkgs;
          nixosSystem = self.nixosConfigurations.radius-lab;
          format = "vm";
          modules = [
            { virtualisation = {
                memorySize = 4096;
                qemu.networkingOptions = [
                    "-net user,hostfwd=udp::1812-:1812"
                ];
              };
            }
          ];
        };

I can modify and tweak the VM generatotion, and the system config remains agnostic. If #241 can do this as simply as this patch can, you've sold me.

@DavHau
Copy link
Member

DavHau commented Jun 13, 2023

Your example reminds me that a missing feature of #241 is to pass your own modules. I'll add that asap. Assuming we have that features, then the whole flake could look like this:

flake.nix

{
  #...
  outputs = {self, nixos-generators, ...} @ inputs: {

    nixosConfigurations.radius-lab = {config, pkgs, ...}: {

      imports = [
        nixos-generators.nixosModules.all-formats
      ];
      
      # all you normal system config here

      # nixos-generators options for all possible formats
      formats.vm = {
        virtualisation = {
          memorySize = 4096;
          qemu.networkingOptions = [
            "-net user,hostfwd=udp::1812-:1812"
          ];
        };
      };
    };
  };

  # this line is just an alias, instead of an extra function call
  radius-lab-vm = self.nixosConfigurations.system.formats.vm;
}

Why is this better?
Instead of expressing your nixos-generators format in an ad-hoc style, this makes your settings become part of the module system.
One could, for example express the formats settings in an extra module and thereby make it portable
Another person could re-use your nixos-generators format from your github by just simply importing the module.
It's also simpler for you to share it between different machines or projects.

@mayl
Copy link
Contributor

mayl commented Jul 3, 2023

So that sounds like it would be awesome, and probably would supersede #185 for my use cases. How close did #241 get to actually being able to do what you suggest? To start with nix flake show github:nix-community/nixos-generators doesn't seem to show that the all-formats module is exposed. Would the module work like you describe above if it was exposed as is?

@DavHau
Copy link
Member

DavHau commented Jul 3, 2023

I just opened #256 which implements the customization options similar to the example above and also allows adding new formats.
It now also exposes nixosModules.all-formats

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.

None yet

3 participants