|
| 1 | +# SPDX-FileCopyrightText: 2024 Sefa Eyeoglu <[email protected]> |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +{config, self, inputs, lib, ...}: let |
| 5 | + inherit (lib) mkOption types; |
| 6 | + inherit (inputs) deploy-rs; |
| 7 | + |
| 8 | + cfg = config.deploy; |
| 9 | + |
| 10 | + settings = { |
| 11 | + generic = { |
| 12 | + options = { |
| 13 | + sshUser = mkOption { |
| 14 | + type = with types; nullOr str; |
| 15 | + default = null; |
| 16 | + }; |
| 17 | + user = mkOption { |
| 18 | + type = with types; nullOr str; |
| 19 | + default = null; |
| 20 | + }; |
| 21 | + sshOpts = mkOption { |
| 22 | + type = with types; listOf str; |
| 23 | + default = []; |
| 24 | + }; |
| 25 | + fastConnection = mkOption { |
| 26 | + type = with types; nullOr bool; |
| 27 | + default = null; |
| 28 | + }; |
| 29 | + autoRollback = mkOption { |
| 30 | + type = with types; nullOr bool; |
| 31 | + default = null; |
| 32 | + }; |
| 33 | + confirmTimeout = mkOption { |
| 34 | + type = with types; nullOr int; |
| 35 | + default = null; |
| 36 | + }; |
| 37 | + activationTimeout = mkOption { |
| 38 | + type = with types; nullOr int; |
| 39 | + default = null; |
| 40 | + }; |
| 41 | + tempPath = mkOption { |
| 42 | + type = with types; nullOr str; |
| 43 | + default = null; |
| 44 | + }; |
| 45 | + magicRollback = mkOption { |
| 46 | + type = with types; nullOr bool; |
| 47 | + default = null; |
| 48 | + }; |
| 49 | + sudo = mkOption { |
| 50 | + type = with types; nullOr str; |
| 51 | + default = null; |
| 52 | + }; |
| 53 | + remoteBuild = mkOption { |
| 54 | + type = with types; nullOr bool; |
| 55 | + default = null; |
| 56 | + }; |
| 57 | + interactiveSudo = mkOption { |
| 58 | + type = with types; nullOr bool; |
| 59 | + default = null; |
| 60 | + }; |
| 61 | + }; |
| 62 | + }; |
| 63 | + profile = { |
| 64 | + options = { |
| 65 | + path = mkOption { |
| 66 | + type = types.package; |
| 67 | + }; |
| 68 | + profilePath = mkOption { |
| 69 | + type = with types; nullOr str; |
| 70 | + default = null; |
| 71 | + }; |
| 72 | + }; |
| 73 | + }; |
| 74 | + node = { |
| 75 | + options = { |
| 76 | + hostname = mkOption { |
| 77 | + type = types.str; |
| 78 | + }; |
| 79 | + profilesOrder = mkOption { |
| 80 | + type = with types; listOf str; |
| 81 | + default = []; |
| 82 | + }; |
| 83 | + profiles = mkOption { |
| 84 | + type = types.attrsOf profileModule; |
| 85 | + }; |
| 86 | + }; |
| 87 | + }; |
| 88 | + |
| 89 | + nodes = { |
| 90 | + options.nodes = mkOption { |
| 91 | + type = types.attrsOf nodeModule; |
| 92 | + }; |
| 93 | + }; |
| 94 | + }; |
| 95 | + |
| 96 | + profileModule = types.submoduleWith { |
| 97 | + modules = [settings.generic settings.profile]; |
| 98 | + }; |
| 99 | + |
| 100 | + nodeModule = types.submoduleWith { |
| 101 | + modules = [settings.generic settings.node]; |
| 102 | + }; |
| 103 | + |
| 104 | + rootModule = types.submoduleWith { |
| 105 | + modules = [ |
| 106 | + settings.generic |
| 107 | + settings.nodes |
| 108 | + ({ config, ... }: { |
| 109 | + options.input = mkOption { |
| 110 | + type = types.package; |
| 111 | + }; |
| 112 | + options.lib = mkOption { |
| 113 | + type = types.raw; |
| 114 | + }; |
| 115 | + config.input = lib.mkIf |
| 116 | + (inputs ? deploy-rs || inputs ? deploy) |
| 117 | + (lib.mkDefault (inputs.deploy-rs or inputs.deploy)); |
| 118 | + config.lib = lib.mkDefault config.input.lib; |
| 119 | + }) |
| 120 | + ]; |
| 121 | + }; |
| 122 | + |
| 123 | + keepAttrs = attrs: names: |
| 124 | + lib.filterAttrs (name: _: |
| 125 | + lib.elem name names |
| 126 | + ) attrs; |
| 127 | + |
| 128 | + keepOptions = attrs: opts: |
| 129 | + keepAttrs attrs |
| 130 | + (lib.attrNames (settings.generic.options // opts.options)); |
| 131 | + |
| 132 | + filterNullRecursive = lib.filterAttrsRecursive (_: v: v != null); |
| 133 | + |
| 134 | +in { |
| 135 | + options.deploy = mkOption { |
| 136 | + type = rootModule; |
| 137 | + }; |
| 138 | + config = { |
| 139 | + flake.deploy = |
| 140 | + filterNullRecursive |
| 141 | + ((keepOptions cfg settings.nodes) // { |
| 142 | + nodes = lib.mapAttrs (_: node: |
| 143 | + (keepOptions node settings.node) // { |
| 144 | + profiles = lib.mapAttrs (_: profile: |
| 145 | + keepOptions profile settings.profile |
| 146 | + ) node.profiles; |
| 147 | + } |
| 148 | + ) cfg.nodes; |
| 149 | + }); |
| 150 | + perSystem = {system, ...}: { |
| 151 | + checks = lib.mkIf (cfg.lib ? ${system}) (cfg.lib.${system}.deployChecks self.deploy); |
| 152 | + }; |
| 153 | + }; |
| 154 | +} |
0 commit comments