at master 5.1 kB view raw
1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7 8let 9 cfg = config.services.gokapi; 10 settingsFormat = pkgs.formats.json { }; 11 userSettingsFile = settingsFormat.generate "generated-config.json" cfg.settings; 12in 13{ 14 options.services.gokapi = { 15 enable = lib.mkEnableOption "Lightweight selfhosted Firefox Send alternative without public upload"; 16 17 mutableSettings = lib.mkOption { 18 type = lib.types.bool; 19 default = true; 20 description = '' 21 Allow changes to the program config made by the program to persist between restarts. 22 If disabled all required values must be set using nix, and all changes to config format over application updates must be resolved by user. 23 ''; 24 }; 25 26 package = lib.mkPackageOption pkgs "gokapi" { }; 27 28 environment = lib.mkOption { 29 type = lib.types.submodule { 30 freeformType = lib.types.attrsOf (lib.types.either lib.types.str lib.types.int); 31 options = { 32 GOKAPI_CONFIG_DIR = lib.mkOption { 33 type = lib.types.str; 34 default = "%S/gokapi/config"; 35 description = "Sets the directory for the config file."; 36 }; 37 GOKAPI_CONFIG_FILE = lib.mkOption { 38 type = lib.types.str; 39 default = "config.json"; 40 description = "Sets the filename for the config file."; 41 }; 42 GOKAPI_DATA_DIR = lib.mkOption { 43 type = lib.types.str; 44 default = "%S/gokapi/data"; 45 description = "Sets the directory for the data."; 46 }; 47 GOKAPI_PORT = lib.mkOption { 48 type = lib.types.port; 49 default = 53842; 50 description = "Sets the port of the service."; 51 }; 52 }; 53 }; 54 default = { }; 55 description = '' 56 Environment variables to be set for the gokapi service. Can use systemd specifiers. 57 For full list see <https://gokapi.readthedocs.io/en/latest/advanced.html#environment-variables>. 58 ''; 59 }; 60 settings = lib.mkOption { 61 type = lib.types.submodule { 62 freeformType = settingsFormat.type; 63 options = { }; 64 }; 65 default = { }; 66 description = '' 67 Configuration settings for the generated config json file. 68 See <https://gokapi.readthedocs.io/en/latest/advanced.html#config-json> for more information 69 ''; 70 }; 71 settingsFile = lib.mkOption { 72 type = lib.types.nullOr lib.types.str; 73 default = null; 74 description = '' 75 Path to config file to parse and append to settings. 76 Largely useful for loading secrets from a file not in the nix store. Can use systemd specifiers. 77 See <https://gokapi.readthedocs.io/en/latest/advanced.html#config-json> for more information 78 ''; 79 }; 80 81 }; 82 83 config = lib.mkIf cfg.enable { 84 systemd.services.gokapi = { 85 wantedBy = [ "default.target" ]; 86 wants = [ "network-online.target" ]; 87 after = [ "network-online.target" ]; 88 environment = lib.mapAttrs (_: value: toString value) cfg.environment; 89 unitConfig = { 90 Description = "gokapi service"; 91 }; 92 serviceConfig = { 93 ExecStartPre = 94 let 95 updateScript = lib.getExe ( 96 pkgs.writeShellApplication { 97 name = "merge-config"; 98 runtimeInputs = with pkgs; [ jq ]; 99 text = '' 100 echo "Running merge-config" 101 mutableSettings="$1" 102 statefulSettingsFile="$2" 103 settingsFile="$3" 104 if [[ "$mutableSettings" == true ]]; then 105 if [[ -f "$statefulSettingsFile" ]]; then 106 echo "Updating stateful config file" 107 merged="$(jq -s '.[0] * .[1]' "$statefulSettingsFile" ${userSettingsFile})" 108 echo "$merged" > "$statefulSettingsFile" 109 fi 110 else 111 echo "Overwriting stateful config file" 112 mkdir -p "$(dirname "$statefulSettingsFile")" 113 cat ${userSettingsFile} > "$statefulSettingsFile" 114 fi 115 if [ "$settingsFile" != "null" ]; then 116 echo "Merging settings file into current stateful settings file" 117 merged="$(jq -s '.[0] * .[1]' "$statefulSettingsFile" "$settingsFile")" 118 echo "$merged" > "$statefulSettingsFile" 119 fi 120 ''; 121 } 122 ); 123 in 124 lib.strings.concatStringsSep " " [ 125 updateScript 126 (lib.boolToString cfg.mutableSettings) 127 "${cfg.environment.GOKAPI_CONFIG_DIR}/${cfg.environment.GOKAPI_CONFIG_FILE}" 128 (if (cfg.settingsFile == null) then "null" else cfg.settingsFile) 129 ]; 130 ExecStart = lib.getExe cfg.package; 131 RestartSec = 30; 132 DynamicUser = true; 133 PrivateTmp = true; 134 StateDirectory = "gokapi"; 135 CacheDirectory = "gokapi"; 136 Restart = "on-failure"; 137 }; 138 }; 139 }; 140 meta.maintainers = with lib.maintainers; [ 141 delliott 142 ]; 143}