12{ lib }:3let4 inherit (lib.trivial) isFunction functionArgs;5 inherit (lib.options) mkOption mergeOneOption;6 inherit (lib.modules) mkOverride;7 inherit (lib.types)8 listOf9 submodule10 attrsOf11 mkOptionType12 ;13 inherit (lib.strings) optionalString hasPrefix removePrefix;14in15rec {16 types = {17 overlay = mkOptionType {18 name = "nixpkgs-overlay";19 description = "nixpkgs overlay";20 check = {21 __functor = _self: isFunction;22 isV2MergeCoherent = true;23 };24 merge = mergeOneOption;25 };26 listOfOverlay = listOf types.overlay;2728 mkHostsType = module: attrsOf (submodule module);29 mkDataType = module: submodule module;30 };3132 options = {33 mkHostsOption =34 module:35 mkOption {36 type = types.mkHostsType module;37 };38 mkDataOption =39 module:40 mkOption {41 type = types.mkDataType module;42 };43 };4445 inherit (options) mkHostsOption;4647 modules = {48 495051 mkFleetDefault = mkOverride 999;52 535455 mkFleetGeneratorDefault = mkOverride 1001;56 };5758 inherit (modules) mkFleetDefault mkFleetGeneratorDefault;5960 secrets = {6162 6364656667686970717273 mkPassword =74 {75 size ? 32,76 }:77 (78 {79 coreutils,80 mkSecretGenerator,81 }:82 mkSecretGenerator {83 script = ''84 mkdir $out85 gh generate password -o $out/secret --size ${toString size}86 '';87 parts.secret.encrypted = true;88 }89 );9091 9293949596979899100101102103104 mkEd25519 =105 {106 noEmbedPublic ? false,107 encoding ? null,108 }:109 (110 { mkSecretGenerator }:111 mkSecretGenerator {112 script = ''113 mkdir $out114 gh generate ed25519 -p $out/public -s $out/secret \115 ${optionalString noEmbedPublic "--no-embed-public"} \116 ${optionalString (encoding != null) "--encoding=${encoding}"}117 '';118 parts.secret.encrypted = true;119 parts.public.encrypted = false;120 }121 );122123 124125126127128129130131132133134 mkX25519 =135 {136 encoding ? null,137 }:138 (139 { mkSecretGenerator }:140 mkSecretGenerator {141 script = ''142 mkdir $out143 gh generate x25519 -p $out/public -s $out/secret \144 ${optionalString (encoding != null) "--encoding=${encoding}"}145 '';146147 parts.secret.encrypted = true;148 parts.public.encrypted = false;149 }150 );151152 mkAskPass =153 { prompt ? "Secret value", part ? "secret" }:154 (155 {156 kdePackages,157 mkImpureSecretGenerator,158 }:159 mkImpureSecretGenerator {160 161 script = ''162 ${kdePackages.kdialog}/bin/kdialog --inputbox "${prompt}" | gh private -o $out/${part}163 '';164165 parts.${part}.encrypted = true;166 }167 );168169 170171172173174175176177178179 mkRsa =180 {181 size ? 4096,182 }:183 (184 {185 openssl,186 mkSecretGenerator,187 }:188 mkSecretGenerator {189 script = ''190 mkdir $out191192 ${openssl}/bin/openssl genrsa -out rsa_private.key ${toString size}193 ${openssl}/bin/openssl rsa -in rsa_private.key -pubout -out rsa_public.key194195 cat rsa_private.key | gh private -o $out/secret196 cat rsa_public.key | gh public -o $out/public197 '';198199 parts.secret.encrypted = true;200 parts.public.encrypted = false;201 }202 );203204 205206207208209210211212213214215216217218 mkBytes =219 {220 count ? 32,221 encoding,222 noNuls ? false,223 }:224 (225 { mkSecretGenerator }:226 mkSecretGenerator {227 script = ''228 mkdir $out229 gh generate bytes --count=${toString count} --encoding=${encoding} -o $out/secret \230 ${optionalString noNuls "--no-nuls"}231 '';232 parts.secret.encrypted = true;233 }234 );235 236237238 mkHexBytes =239 {240 count ? 32,241 }:242 mkBytes {243 inherit count;244 encoding = "hex";245 };246 247248249 mkBase64Bytes =250 {251 count ? 32,252 }:253 mkBytes {254 inherit count;255 encoding = "base64";256 };257258 259 260 261 };262263 inherit (secrets)264 mkPassword265 mkEd25519266 mkX25519267 mkRsa268 mkBytes269 mkHexBytes270 mkBase64Bytes271 mkAskPass272 ;273274 strings =275 let276 plaintextPrefix = "<PLAINTEXT>";277 plaintextNewlinePrefix = "<PLAINTEXT-NL>";278 in279 {280 281282283 decodeRawSecret =284 raw:285 if hasPrefix plaintextPrefix raw then286 removePrefix plaintextPrefix raw287 else if hasPrefix plaintextNewlinePrefix raw then288 removePrefix plaintextNewlinePrefix raw289 else290 throw "decodeRawSecret only works with plaintext-encoded secret public parts, got ${raw}";291 };292293 inherit (strings) decodeRawSecret;294}