Use a specific version of Terraform in a nix-shell

We’re using an older version of Terraform than what’s present in nixpkgs-unstable, and I’ve been struggling to get an overlay with the version we want working in my non-flake shell. Using some of the typical methods like overrideAttrs (example discussion) is not working and it seems like it has to do with a conflict in vendored modules. I believe this discussion highlights the same problem I’m running into (and links to a similar issue on GitHub):

It turns out that overrideAttrs and buildGoModule aren’t aware of each other, so the -go-modules derivation there isn’t recomputed when I change the source.

After struggling with the above, I finally stumbled onto a much simpler method using mkTerraform (definition, github issue):

{ pkgs ? import <nixpkgs> {}}:

with pkgs; mkShell {
  name = "my-shell";

  nativeBuildInputs = let
    # https://github.com/NixOS/nixpkgs/blob/8530bee77a1db9ca592bf04acf16ca26df48b738/pkgs/applications/networking/cluster/terraform/default.nix#L171-L174
    terraform_1_3_6 = mkTerraform {
      version = "1.3.6";
      hash = "sha256-aETsvcHoHSwqWCAdn9JPJLcX1Wi1umUghSjkq37OYDU=";
      vendorHash = "sha256-fviukVGBkbxFs2fJpEp/tFMymXex7NRQdcGIIA9W88k=";
    };
  in
  [
    terraform_1_3_6
  ];

}

I have a few questions after all this:

  1. Is there a better way to get a specific version of Terraform in a shell?
  2. If you wanted to include terraform plugins in the build, how would you do that? I’ve seen examples of a terraform.withPlugins( p: [ p.aws ... ]) helper, but I couldn’t seem to get that to work with my custom terraform_1_3_6.
  3. In a flake-based world, how would you pull in specific versions of packages?
    • Would it make sense to create an independent flake that packages the specific version of Terraform that you want?
    • I’ve seen examples of adding multiple inputs that pin nix “channels” containing the package versions you want, but that seems like it could get hairy if you wanted to pin distinct versions of several different packages.
1 Like

You could vendor the terraform file in your overlay.

Generally mixing nixpkgs should be avoided. For simple go programs it usually works but for more complex ones especially that use GUI it usually causes trouble. It also increases evaluation time.

I understand this topic was open back in May 2023, but I just wanted to share this announcement in case anyone still wonders if there is a method to use a specific Terraform version in a nix-shell.

1 Like