Skip to content

Commit 7ad485c

Browse files
authored
Use flake-parts (#40)
* Use flake-parts * Add default overlay * Move nix files to lib; Fix vendor-hash; * Update readme * Use legacyPackages in vendor-hash; Use default pkgs in flake; * Remove allowUnfree = true
1 parent aaa0ea7 commit 7ad485c

8 files changed

+149
-73
lines changed

README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ environments using a [nix-shell] or [devenv](https://devenv.sh).
1515

1616
## How it works
1717

18-
This flake provides a set of Terraform versions in the form of:
18+
This flake provides a set of Terraform versions in the form of:
1919

2020
```nix
2121
nixpkgs-terraform.packages.${system}.${version}
@@ -67,7 +67,9 @@ nixConfig = {
6767
After configuring the inputs from the [Install](#install) section, a common use
6868
case for this flake could be spawning a [nix-shell] with a specific Terraform
6969
version, which could be accomplished by extracting the desired version from
70-
`nixpkgs-terraform.packages` as follows:
70+
`nixpkgs-terraform.packages` or by using an overlay as follows:
71+
72+
#### As a package
7173

7274
```nix
7375
outputs = { self, flake-utils, nixpkgs-terraform, nixpkgs }:
@@ -83,7 +85,25 @@ outputs = { self, flake-utils, nixpkgs-terraform, nixpkgs }:
8385
});
8486
```
8587

86-
Start a new [nix-shell] with Terraform installed by running the following
88+
#### As an overlay
89+
90+
```nix
91+
outputs = { self, flake-utils, nixpkgs-terraform, nixpkgs }:
92+
flake-utils.lib.eachDefaultSystem (system:
93+
let
94+
pkgs = import nixpkgs {
95+
inherit system;
96+
overlays = [ nixpkgs-terraform.overlays.default ];
97+
};
98+
in
99+
{
100+
devShells.default = pkgs.mkShell {
101+
buildInputs = [ pkgs.terraform-versions."1.6.3" ];
102+
};
103+
});
104+
```
105+
106+
Start a new [nix-shell] with Terraform in scope by running the following
87107
command:
88108

89109
```sh
@@ -138,6 +158,7 @@ contributors to follow the commit conventions outlined
138158
to make it easier for maintainers to release new changes.
139159

140160
---
161+
141162
<img src="https://www.stackbuilders.com/media/images/Sb-supports.original.png"
142163
alt="Stack Builders" width="50%"></img>
143164
[Check out our libraries](https://github.com/stackbuilders/) | [Join our

flake.lock

+28-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+38-54
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,45 @@
22
description = "A collection of Terraform versions that are automatically updated";
33

44
inputs = {
5-
flake-utils.inputs.systems.follows = "systems";
6-
flake-utils.url = "github:numtide/flake-utils";
7-
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";
85
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
6+
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";
7+
98
systems.url = "github:nix-systems/default";
9+
flake-parts.url = "github:hercules-ci/flake-parts";
1010
};
1111

12-
outputs = { self, flake-utils, nixpkgs-unstable, nixpkgs, ... }:
13-
flake-utils.lib.eachDefaultSystem
14-
(system:
15-
let
16-
pkgs = nixpkgs.legacyPackages.${system};
17-
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
18-
versions = builtins.fromJSON (builtins.readFile ./versions.json);
19-
in
20-
{
21-
devShells.default = pkgs.mkShell {
22-
buildInputs = [
23-
pkgs-unstable.black
24-
(pkgs-unstable.python3.withPackages
25-
(ps: [
26-
ps.pygithub
27-
ps.semver
28-
])
29-
)
30-
pkgs-unstable.nix-prefetch
31-
pkgs.nodejs
32-
pkgs.rubyPackages.dotenv
33-
];
34-
};
35-
# https://github.com/NixOS/nix/issues/7165
36-
checks = self.packages.${system};
37-
packages = builtins.mapAttrs
38-
(version: { hash, vendorHash }: self.lib.buildTerraform {
39-
inherit system version hash vendorHash;
40-
})
41-
versions;
42-
}) // {
43-
lib.buildTerraform = { system, version, hash, vendorHash }:
44-
let
45-
pkgs = nixpkgs.legacyPackages.${system};
46-
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
47-
in
48-
# https://www.hashicorp.com/blog/hashicorp-adopts-business-source-license
49-
if builtins.compareVersions version "1.6.0" >= 0
50-
then
51-
# https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/applications/networking/cluster/terraform/default.nix
52-
pkgs-unstable.mkTerraform
53-
{
54-
inherit version hash vendorHash;
55-
patches = [ "${nixpkgs-unstable}/pkgs/applications/networking/cluster/terraform/provider-path-0_15.patch" ];
56-
}
57-
else
58-
# https://github.com/NixOS/nixpkgs/blob/nixos-23.05/pkgs/applications/networking/cluster/terraform/default.nix
59-
pkgs.mkTerraform {
60-
inherit version hash vendorHash;
61-
patches = [ "${nixpkgs}/pkgs/applications/networking/cluster/terraform/provider-path-0_15.patch" ];
62-
};
12+
outputs = { self, flake-parts, ... }@inputs: flake-parts.lib.mkFlake { inherit inputs; } {
13+
imports = [
14+
inputs.flake-parts.flakeModules.easyOverlay
15+
];
16+
systems = import inputs.systems;
17+
18+
perSystem = { config, pkgs, pkgs-unstable, system, ... }: {
19+
_module.args = {
20+
pkgs-unstable = inputs.nixpkgs-unstable.legacyPackages.${system};
21+
};
22+
23+
packages = import ./lib/packages.nix { inherit pkgs pkgs-unstable; custom-lib = self.lib; };
24+
25+
overlayAttrs = {
26+
terraform-versions = config.packages;
27+
};
28+
29+
devShells.default = pkgs.mkShell {
30+
buildInputs = [
31+
pkgs-unstable.black
32+
(pkgs-unstable.python3.withPackages (ps: [
33+
ps.pygithub
34+
ps.semver
35+
]))
36+
pkgs-unstable.nix-prefetch
37+
pkgs.nodejs
38+
pkgs.rubyPackages.dotenv
39+
];
40+
};
41+
};
42+
43+
flake = {
6344
templates = {
6445
default = {
6546
description = "Simple nix-shell with Terraform installed via nixpkgs-terraform";
@@ -70,5 +51,8 @@
7051
path = ./templates/devenv;
7152
};
7253
};
54+
55+
lib = import ./lib;
7356
};
57+
};
7458
}

lib/build-terraform.nix

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{ pkgs, pkgs-unstable, version, hash, vendorHash }:
2+
# https://www.hashicorp.com/blog/hashicorp-adopts-business-source-license
3+
if builtins.compareVersions version "1.6.0" >= 0
4+
then
5+
# https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/applications/networking/cluster/terraform/default.nix
6+
(pkgs-unstable.mkTerraform {
7+
inherit version hash vendorHash;
8+
patches = [ ../patches/provider-path-0_15.patch ];
9+
})
10+
else
11+
# https://github.com/NixOS/nixpkgs/blob/nixos-23.05/pkgs/applications/networking/cluster/terraform/default.nix
12+
(pkgs.mkTerraform {
13+
inherit version hash vendorHash;
14+
patches = [ ../patches/provider-path-0_15.patch ];
15+
})

lib/default.nix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
buildTerraform = import ./build-terraform.nix;
3+
}

lib/packages.nix

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{ custom-lib, pkgs, pkgs-unstable }:
2+
let
3+
versions = builtins.fromJSON (builtins.readFile ../versions.json);
4+
in
5+
builtins.mapAttrs
6+
(version: { hash, vendorHash }: custom-lib.buildTerraform {
7+
inherit pkgs pkgs-unstable version hash vendorHash;
8+
})
9+
versions

patches/provider-path-0_15.patch

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
diff -Naur terraform.old/internal/command/init.go terraform.new/internal/command/init.go
2+
--- terraform.old/internal/command/init.go
3+
+++ terraform.new/internal/command/init.go
4+
@@ -3,6 +3,7 @@
5+
import (
6+
"context"
7+
"fmt"
8+
+ "os"
9+
"log"
10+
"strings"
11+
12+
@@ -55,6 +56,11 @@
13+
14+
var diags tfdiags.Diagnostics
15+
16+
+ val, ok := os.LookupEnv("NIX_TERRAFORM_PLUGIN_DIR")
17+
+ if ok {
18+
+ flagPluginPath = append(flagPluginPath, val)
19+
+ }
20+
+
21+
if len(flagPluginPath) > 0 {
22+
c.pluginPath = flagPluginPath
23+
}

vendor-hash.nix

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{ version, hash }: { sha256 }:
22
let
3-
terraform = ((builtins.getFlake (toString ./.)).lib.buildTerraform {
4-
inherit version hash;
5-
system = builtins.currentSystem;
3+
flake = builtins.getFlake (toString ./.);
4+
system = builtins.currentSystem;
5+
6+
pkgs = flake.inputs.nixpkgs.legacyPackages.${system};
7+
pkgs-unstable = flake.inputs.nixpkgs-unstable.legacyPackages.${system};
8+
9+
terraform = flake.lib.buildTerraform {
10+
inherit pkgs pkgs-unstable version hash;
611
vendorHash = sha256;
7-
});
12+
};
813
in
914
terraform.goModules or terraform.go-modules

0 commit comments

Comments
 (0)