summaryrefslogtreecommitdiff
path: root/modules/flake/terranix.nix
blob: 66f79b9278db43102309c36dd71368297290b81f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{
  lib,
  flake-parts-lib,
  ...
}: let
  namespace = "terranix";

  inherit
    (lib)
    literalExpression
    mdDoc
    mkOption
    mkPackageOption
    types
    ;

  inherit
    (flake-parts-lib)
    mkPerSystemOption
    ;
in {
  options = {
    perSystem = mkPerSystemOption ({
      config,
      pkgs,
      system,
      self',
      ...
    }: let
      cfg = config.${namespace};
    in {
      options.${namespace} = {
        builder = mkOption {
          type = types.functionTo (types.lazyAttrsOf types.raw);
          example = literalExpression "inputs.terranix.lib.terranixConfiguration";
          description = mdDoc ''
            Function used to build this terranixConfiguration
          '';
        };

        modules = mkOption {
          type = types.listOf types.unspecified;
          default = [];
          example = literalExpression "[ ./terranix ]";
          description = mdDoc ''
            Modules to use in this terranixConfiguration
          '';
        };

        configuration = mkOption {
          type = types.pathInStore;
          readOnly = true;
          description = mdDoc ''
            Final configuration created by terranix
          '';
        };

        package = mkPackageOption pkgs "opentofu" {
          default = ["opentofu"];
          example = literalExpression "pkgs.opentofu.withPlugins (plugins: [ plugins.tailscale ] )";
        };
      };

      config = {
        terranix.configuration = cfg.builder {
          inherit system;
          inherit (cfg) modules;
        };

        apps.gen-terranix = {
          program = pkgs.writeShellApplication {
            name = "gen-tf";

            text = ''
              config_file="config.tf.json"
              [ -e "$config_file" ] && rm -f "$config_file"
              cp ${cfg.configuration} "$config_file"
            '';
          };
        };

        devShells.terranix = pkgs.mkShellNoCC {
          shellHook = ''
            ${self'.apps.gen-terranix.program}
          '';

          packages = [pkgs.just cfg.package];
        };
      };
    });
  };
}