diff options
| -rw-r--r-- | dev/shell.nix | 6 | ||||
| -rw-r--r-- | modules/flake/configurations.nix | 8 | ||||
| -rw-r--r-- | modules/flake/terranix.nix | 92 | ||||
| -rw-r--r-- | terranix/default.nix | 49 |
4 files changed, 110 insertions, 45 deletions
diff --git a/dev/shell.nix b/dev/shell.nix index 4ed5501..3b9afcc 100644 --- a/dev/shell.nix +++ b/dev/shell.nix @@ -3,7 +3,7 @@ pkgs, config, inputs', - opentofu', + self', ... }: { devShells = { @@ -14,7 +14,7 @@ actionlint # nix - config.formatter + self'.formatter deadnix nil statix @@ -23,7 +23,7 @@ deploy-rs fzf just - opentofu' # see ../terranix/ + config.terranix.package ] ++ lib.optional stdenv.isLinux inputs'.agenix.packages.agenix; }; diff --git a/modules/flake/configurations.nix b/modules/flake/configurations.nix index 3d2d512..ef1ae4e 100644 --- a/modules/flake/configurations.nix +++ b/modules/flake/configurations.nix @@ -156,8 +156,8 @@ example = literalExpression '' { foo = { - system = "aarch64-${kernelFor type}"; - }; + system = "aarch64-${kernelFor type}"; + }; } ''; description = mdDoc '' @@ -192,8 +192,8 @@ in { example = literalExpression '' { john = { - pkgs = inputs.nixpkgs.legacyPackages.aarch64-linux; - }; + pkgs = inputs.nixpkgs.legacyPackages.aarch64-linux; + }; } ''; description = mdDoc '' diff --git a/modules/flake/terranix.nix b/modules/flake/terranix.nix new file mode 100644 index 0000000..66f79b9 --- /dev/null +++ b/modules/flake/terranix.nix @@ -0,0 +1,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]; + }; + }; + }); + }; +} diff --git a/terranix/default.nix b/terranix/default.nix index 7b33389..12bf013 100644 --- a/terranix/default.nix +++ b/terranix/default.nix @@ -1,14 +1,15 @@ {inputs, ...}: { - perSystem = { - lib, - pkgs, - system, - self', - opentofu', - ... - }: let - terranixConfig = inputs.terranix.lib.terranixConfiguration { - inherit system; + imports = [../modules/flake/terranix.nix]; + + perSystem = {pkgs, ...}: { + terranix = { + builder = inputs.terranix.lib.terranixConfiguration; + + package = pkgs.opentofu.withPlugins (plugins: [ + plugins.cloudflare + plugins.tailscale + ]); + modules = [ ./cloudflare ./tailscale @@ -17,33 +18,5 @@ ./versions.nix ]; }; - in { - _module.args.opentofu' = pkgs.opentofu.withPlugins (plugins: - with plugins; [ - cloudflare - tailscale - ]); - - apps.gen-terranix = { - type = "app"; - - program = pkgs.writeShellApplication { - name = "gen-tf"; - - text = '' - config_file="config.tf.json" - [ -e "$config_file" ] && rm -f "$config_file" - cp ${terranixConfig} "$config_file" - ''; - }; - }; - - devShells.terranix = pkgs.mkShellNoCC { - shellHook = '' - ${self'.apps.gen-terranix.program} - ''; - - packages = [pkgs.just opentofu']; - }; }; } |
