How to generate a .nix file?

Just wondering about common / best practices –

I’m tinkering on a project where I thought that generating a nix file was a reasonable thing do to, which I later import.

For my case, I was making a list of patches compatible with kernelPatches (so [ { name: patch: }]), and what seemed easiest was to put the following into a runCommand:

  • echo / printf the leading [
  • iterate over some lines of input in bash and echo the attrsets as separate lines
  • echo the trailing ]
  • run alejandra to make it pretty

It seems to work fine, and it doesn’t seem fundamentally different than the process taken for nixos-generate-config: https://github.com/NixOS/nixpkgs/blob/9661e33f2926277abd5e8290bb4d47be65400dc3/nixos/modules/installer/tools/nixos-generate-config.pl#L43

Are there other approaches I should consider?

It’s easier and therefore common practice to generate JSON and read it from Nix.

3 Likes

That makes sense. Thanks for your suggestion.