Skip to content

Conversation

@DavHau
Copy link
Member

@DavHau DavHau commented Jul 3, 2023

While the all-formats.nix module (introduced via #241) allows to import all formats at once, it lacks an interface to customize formats or add formats.

This is fixed by adding the top-level option formatConfigs (attrsOf deferredModule).

All format modules created under config.formatConfigs are mapped so their outputs are available under config.formats which has also been moved to the top-level (previously config.system.formats).

Done:

  • expose the all-formats module via nixosModules
  • add option formatConfigs (pre-populated with all existing formats)
  • move option system.formats -> formats
  • add test for customizing a format
  • add example to readme
  • add test similar to the new readme example ensuring that formats can be customized and created

Open questions:

  • should the readme section Using in a Flake be removed now, as the new section Using as a nixos-module supports all its features and should be simpler to use (no need to call a function for each format etc.)?

DavHau added 2 commits July 3, 2023 11:54
While the `all-formats.nix` module allows to import all formats at once, it lacks an interface to customize formats or add formats.

This is fixed by adding the top-level option `formatConfigs` (attrsOf deferredModule).

All format modules created under `config.formatConfigs` are mapped so their outputs are available under `config.formats` which has also been moved to the top-level (previously `config.system.formats`).

Done:
- add option `formatConfigs`
- move option `system.formats` -> `formats`
- add test for customizing a format
Comment on lines +198 to +200
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
modules = [self.nixosModules.my-machine];
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a dumb question: if I nixos-rebuild --flake .#my-machine what does this build? Just some kind of store path with the nixos-system in it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and this isn't changed by the all-formats module.

README.md Outdated
Comment on lines 203 to 205
packages.x86_64-linux =
self.nixosConfigurations.my-machine.config.formats;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clear if there is only one machine defined per flake. Is there a convenient way you know of to "namespace" these formats so I could do something like nix build .#machine1.vm and nix build .#machine2.virtualbox?

Copy link
Contributor

@mayl mayl Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, not sure if it's right or not, but looks like the way I've kind of done this before without violating the flakes package schema is to make an unstructured output:

outputs { ... }: {
  ...
  my-machine = nixpkgs.lib.getAttrs ["vm" "virtualbox"] self.nixosConfigurations.my-machine.config.formats;
};

Which then can then be used:

nix build .#my-machine.vm # build the qemu vm
nix build .#my-machine.virtualbox # build the virtualbox vm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clear if there is only one machine defined per flake. Is there a convenient way you know of to "namespace" these formats so I could do something like nix build .#machine1.vm and nix build .#machine2.virtualbox?

Defining packages outputs is not necessary. You can just simply:

nix build .#nixosConfigurations.machine1.config.formats.vm

...or

nix build .#nixosConfigurations.machine2.config.formats.virtualbox

Creating an unstructured output is another way how to achieve this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Via 34bbb3e I now removed the misleading packages output from the example flake and for the nix build commands use the more explicit referencing.

@mayl
Copy link
Contributor

mayl commented Jul 4, 2023

I used comments to ask a couple questions, but I've tried out your branch and it seems to work well for me. This seems really cool, and would be very helpful for how I use nixos-generators.

Work for a future PR, but I'm thinking it would still be useful to have a helper function to wrap up some of this boilerplate. I'm thinking something that took a configuration module and then automatically injected the all-formats import, called lib.nixosSystem and generated the various flake output attrs in one go. Maybe best done with a flake-parts flake module?

Remove the packages flake output
@DavHau
Copy link
Member Author

DavHau commented Jul 5, 2023

I'm thinking something that took a configuration module and then automatically injected the all-formats import, called lib.nixosSystem and generated the various flake output attrs in one go. Maybe best done with a flake-parts flake module?

Making a flake-parts module sounds like a good way to achieve what you describe.

@Lassulus
Copy link
Collaborator

Lassulus commented Jul 7, 2023

alright, finally had some time to test this, looks pretty nice!

@Lassulus Lassulus merged commit 9191c85 into nix-community:master Jul 7, 2023
@MICHAELABICK
Copy link

This seems to work great! I'm not really super experienced with Nix but is there any way to get the image filename path like the nixos-generate script does? It looks like passing the --print-out-paths option to nix build gets part of the way there but only provides the output directory.

@Lassulus
Copy link
Collaborator

This seems to work great! I'm not really super experienced with Nix but is there any way to get the image filename path like the nixos-generate script does? It looks like passing the --print-out-paths option to nix build gets part of the way there but only provides the output directory.

this is now taken care of in #263

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.

4 participants