Overlay: integrate properly into configuration.nix

So I have this in terraform.nix

let
  terraformOverlay = final: prev: {
    terraformCustom = final.terraform_0_14.withPlugins
      (p: [ p.aws p.helm p.kubernetes p.local p.null p.random p.template ]);

    terraform_0_14 = prev.terraform_0_14.overrideAttrs (old: rec {
      name = "terraform-${version}";
      version = "0.14.4";
      src = prev.fetchFromGitHub {
        owner = "hashicorp";
        repo = "terraform";
        rev = "v${version}";
        sha256 = "0kjbx1gshp1lvhnjfigfzza0sbl3m6d9qb3in7q5vc6kdkiplb66";
      };
    });

  };

in import <nixpkgs> { overlays = [ terraformOverlay ]; }

default.nix:

self: super:

{
  terraform-custom = self.callPackage ./terraform.nix { };
}

and in configuration.nix I have:

  nixpkgs.overlays = [ (import ./overlays/default.nix) ];

locally I was able to build package with:

nix-build terraform.nix -A terraformCustom

under section:

  environment.systemPackages = with pkgs; [
  ];

I’ve tried to use terraform-custom or terraformCustom from the overlays itself, but apparently I don’t understand how that works.

dir structure:

├── configuration.nix
├── hardware-configuration.nix
├── modules
│   ├── dwm.nix
│   ├── environment.nix
│   ├── gnome.nix
│   ├── hardware.nix
│   ├── i3.nix
│   ├── packages.nix
│   ├── programs.nix
│   ├── services.nix
│   ├── syncthing.nix
│   └── xserver.nix
└── overlays
    ├── default.nix
    ├── result -> /nix/store/jz8sl4h3mv9939sn7ja6s1b0a1jhq6wa-terraform-0.14.4
    └── terraform.nix

currently nixos-rebuild switch works fine, but I don’t see terraform in PATH, probably it does not invoke overlay properly.

what’s the better way to integrate that into configuration.nix?

your overlay should just be:

  # terraformCustom.nix
  final: prev: {
    terraformCustom = final.terraform_0_14.withPlugins
      (p: [ p.aws p.helm p.kubernetes p.local p.null p.random p.template ]);

    terraform_0_14 = prev.terraform_0_14.overrideAttrs (old: rec {
      name = "terraform-${version}";
      version = "0.14.4";
      src = prev.fetchFromGitHub {
        owner = "hashicorp";
        repo = "terraform";
        rev = "v${version}";
        sha256 = "0kjbx1gshp1lvhnjfigfzza0sbl3m6d9qb3in7q5vc6kdkiplb66";
      };
    });
  }

and then

nixpkgs.overlays = [ (import ./overlays/terraformCustom.nix) ];

I have a video here on using overlays: Nixpkgs - Overlays - YouTube

Jon, thanks for taking time for this and for the video. Can’t locate terraform binary in the PATH, should I do some additional steps for it to be available?

That seems to be an empty list. Did you mean to include some version of terraform here?

I meant to showcase where I reference terraform-custom or terraformCustom, but apparently I was not using that right.

So I used:

  environment.systemPackages = with pkgs; [
    terraform-custom or # terraformCustom
  ];

but it feels wrong.