Inlining imports

I’m trying to use NixOS to build EC2 instances. I’d like to use a configuration.nix like this:

### https://nixos.org/channels/nixos-22.05

{...}:
{
  imports = [
    <nixpkgs/nixos/modules/virtualisation/amazon-image.nix>
    ../nixos/common/users/default.nix
  ];
  ec2.hvm = true;

  system.stateVersion = "22.05";
}

Obviously this won’t work as ../nixos/common/users/default.nix won’t be found by the EC2 instance.

Is there a tool that will inline the imports giving a single file that I can use here?

I’m not aware of a preprocessor for Nix.
You could use #include and cpp but that might lead to issues with comments.

Another way of “preprocessing” would be to nix-instantiate, and the a subsequent nix-store -r.

This seems like it calls for taking a step back and reconsidering how you want to go about things at a higher level. You’re trying to build nix code in an environment where you have access to only part of the codebase… do you really need to do that? Can you build the generation locally, then copy the resultant closure to the cloud? Or give the cloud machine access to the entire codebase?

A better way to approach this would be to make it into a flake. And then the flake will make sure that the related files are also present when you’re doing the system evaluation.

2 Likes