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:

Are there other approaches I should consider?

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


That makes sense. Thanks for your suggestion.