terraform-purifier full guide
Detailed setup, dependency management, build commands, and troubleshooting. Start with the overview for the initial commands; use the sections below for the complete setup and behavior. terraform-purifier is classified at maturity tier 4 (see tier assessment for details).
Contents
- Setup and activation
- Version compatibility
- Development loop
- Using locked dependencies
- Working offline
- Language tooling integration
- Supported scope
- Troubleshooting
- Examples
Setup and activation
In your existing Terraform project:
Use the overview’s Quickstart for the initial command sequence. The setup details below apply to that workflow.
Commit .terraform.lock.hcl and terraform-purifier.lock.json. Provider versions
and checksums stay in the native lock; the companion records download URLs
and immutable Git module pins. Prefetch connects the normal provider and
module caches to Nix store dependencies. Terraform runs in your working tree.
This integration uses a wrapper supplied by nix shell. You can install it
through your normal Nix configuration if you want it permanently. Without
a purifier lock it warns and runs ordinary, impure Terraform. Invalid or stale
locks fail with a repair command.
Version compatibility
| Tool or language | Packaged defaults | Tested versions |
|---|---|---|
| Terraform | 1.17.0 | — |
Operating systems and architectures
| OS | CPU architecture | Packaged toolchain | Recorded workflow |
|---|---|---|---|
| Linux | ARM64 | Yes | Not recorded |
| Linux | x86-64 | Yes | Tested |
Development loop
After changing configuration:
nix shell 'github:nix-purifiers/terraform-purifier'
terraform fmt
terraform-purifier lock
terraform-purifier prefetch
terraform validate
terraform test
Locking retains existing provider selections and valid Git module pins,
including when an upstream branch moves. Repeating lock without changes
leaves both lockfiles byte-for-byte identical. Update provider selections
and module revisions through the native upgrade operation:
terraform-purifier update
terraform-purifier prefetch
Terraform has no native targeted provider/module upgrade command, so named
updates are not supported. Use update after changing provider version constraints; lock retains
native provider selections. The wrapper directs native upgrade and
provider-mirroring commands through the purifier CLI.
Configuration fingerprints cover root/local-module .tf and .tf.json
files and their default test directories. Relock after
editing those files. Formatting and version commands remain available
without a prefetch.
Using locked dependencies
Locked, Nix-resolved providers and modules for Terraform.
Working offline
nix shell 'github:nix-purifiers/terraform-purifier'
terraform-purifier prefetch
terraform init -backend=false
terraform validate
terraform test
Prefetch roots the dependencies, native tool and editor worker beside the
selected lock. The wrapper supplies an exclusive local plugin directory to
init, disables module downloading there, and selects a read-only provider
lock. It preserves normal backend options; backend initialization and cloud
operations may need their usual network access. The dependency-only offline
checks use -backend=false and plan tests.
Generated provider/module cache entries point at immutable store paths.
Existing cache entries are retained with .before-purifier suffixes when
first replaced. Backend state is left alone. TF_DATA_DIR is supported.
No project Nix files or generated development shell are needed.
Language tooling integration
Start terraform-ls serve from the same shell. Prefetch supplies the language
server, provider binaries, schemas available from those binaries, and Git
module sources. Native cache metadata lets definition lookup reach the
locked modules. The editor check verifies that lookup in the Nix sandbox.
Supported scope
Supported language repositories
Public provider registries implementing the native service-discovery and
provider-download protocols are supported. Archive checksums are selected
from the native zh: hashes; provider versions and checksums are not copied
into the companion. Provider packages are unpacked through Nix fetchers.
Git and local modules are supported, including Git subdirectories and nested module calls. Registry modules are supported when their installed source is Git-backed. Git modules use fixed commits and NAR hashes; local module files remain in the source. Non-Git downloaded module archives and computed module source/version expressions are not yet supported. Use literal coordinates. The flake targets x86_64 and ARM Linux.
The native HCL lock reader runs during Nix evaluation without import-from-derivation. No native plugin package repository is needed; GitHub Pages hosts this documentation.
Target systems
- x86_64-linux
- aarch64-linux
Troubleshooting
- Configuration or native lock changed: run
terraform-purifier lock, thenprefetch. - Missing provider/module entry:
lockrepairs the companion against the native resolution. A damaged companion may require resolving module sources again if their previous pins cannot be recovered. - Wrong Terraform version: use the wrapper, or override the scope’s
nativeTooland regenerate the companion using that toolchain. - Cache backup already exists: move the additional cache entry aside before prefetching. Existing backups are preserved.
- A different provider platform is needed: add another supported platform when locking. The default records Linux AMD64 and ARM64.
terraform-purifier lock --platform linux_amd64
External native and companion locks are supported without replacing the working tree’s native lock:
terraform-purifier \
--terraform-lock ../locks/providers.hcl \
--lockfile ../locks/terraform.json lock
terraform-purifier \
--terraform-lock ../locks/providers.hcl \
--lockfile ../locks/terraform.json prefetch
Set TERRAFORM_PURIFIER_TERRAFORM_LOCK and TERRAFORM_PURIFIER_LOCKFILE to the same paths
for the native wrapper. A missing external provider lock starts from the
working tree’s copy. The delivered Terraform has a small opt-in extension
for an external lock path; its normal default path and behavior are unchanged.
The wrapper also follows the native -chdir option.
Examples
examples/random combines the Random provider and a
pinned Git module. Its test plans the configuration and checks a module
output:
nix shell 'github:nix-purifiers/terraform-purifier'
cd examples/random
terraform-purifier prefetch
terraform init -backend=false
terraform test
Checks validate the configuration, execute the plan test, resolve module outputs in the real language server, reject unusable locks in independent Nix/Python readers, and build with external locks. Native Git-daemon tests cover nested modules, retained pins and explicit upgrades, then stop the server before validating and testing offline. External-lock tests leave a deliberately different working-tree lock untouched.
Optional Nix build
The overlay adds only the terraform-purifier scope:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ purifier.overlays.default ];
config.allowUnfreePredicate =
pkg: (pkg.pname or "") == "terraform";
};
project = pkgs.terraform-purifier.buildTerraformProject {
src = ./.;
terraformLock = ../locks/providers.hcl;
lockFile = ../locks/terraform.json;
};
in {
packages.default = project.packages.configuration;
checks.default = project.tests;
}
The result exposes packages.configuration, tests, release and
fetches. The builder takes named callPackage dependencies and supports
fetchers, srcFilter, testCommand and scope overrides. Initialization
and validation run offline; the default test command is terraform test.
State, saved plan files, variable input files and generated caches are
excluded from the packaged source by default. release is a deterministic
source archive containing the selected locks. Upload that Nix-built artifact
to a GitHub release with:
terraform-purifier publish \
--repo OWNER/PROJECT --tag v1.0.0
lib.terraform-purifier.readLock exposes the shared in-memory lock shape. CI
checks configuration and release reproducibility with nix build --rebuild.