flake.nix 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. {
  2. description = "Transfer.sh";
  3. inputs.flake-utils.url = "github:numtide/flake-utils";
  4. outputs = { self, nixpkgs, flake-utils }:
  5. let
  6. transfer-sh = pkgs: pkgs.buildGoModule {
  7. src = self;
  8. name = "transfer.sh";
  9. vendorSha256 = "sha256-bgQUMiC33yVorcKOWhegT1/YU+fvxsz2pkeRvjf3R7g=";
  10. };
  11. in
  12. flake-utils.lib.eachDefaultSystem (
  13. system:
  14. let
  15. pkgs = nixpkgs.legacyPackages.${system};
  16. in
  17. rec {
  18. packages = flake-utils.lib.flattenTree {
  19. transfer-sh = transfer-sh pkgs;
  20. };
  21. defaultPackage = packages.transfer-sh;
  22. apps.transfer-sh = flake-utils.lib.mkApp { drv = packages.transfer-sh; };
  23. defaultApp = apps.transfer-sh;
  24. }
  25. ) // rec {
  26. nixosModules = {
  27. transfer-sh = { config, lib, pkgs, ... }: with lib; let
  28. RUNTIME_DIR = "/var/lib/transfer.sh";
  29. cfg = config.services.transfer-sh;
  30. general_options = {
  31. enable = mkEnableOption "Transfer.sh service";
  32. listener = mkOption { default = 80; type = types.int; description = "port to use for http (:80)"; };
  33. profile-listener = mkOption { default = 6060; type = types.int; description = "port to use for profiler (:6060)"; };
  34. force-https = mkOption { type = types.nullOr types.bool; description = "redirect to https"; };
  35. tls-listener = mkOption { default = 443; type = types.int; description = "port to use for https (:443)"; };
  36. tls-listener-only = mkOption { type = types.nullOr types.bool; description = "flag to enable tls listener only"; };
  37. tls-cert-file = mkOption { type = types.nullOr types.str; description = "path to tls certificate"; };
  38. tls-private-key = mkOption { type = types.nullOr types.str; description = "path to tls private key "; };
  39. http-auth-user = mkOption { type = types.nullOr types.str; description = "user for basic http auth on upload"; };
  40. http-auth-pass = mkOption { type = types.nullOr types.str; description = "pass for basic http auth on upload"; };
  41. ip-whitelist = mkOption { type = types.nullOr types.str; description = "comma separated list of ips allowed to connect to the service"; };
  42. ip-blacklist = mkOption { type = types.nullOr types.str; description = "comma separated list of ips not allowed to connect to the service"; };
  43. temp-path = mkOption { type = types.nullOr types.str; description = "path to temp folder"; };
  44. web-path = mkOption { type = types.nullOr types.str; description = "path to static web files (for development or custom front end)"; };
  45. proxy-path = mkOption { type = types.nullOr types.str; description = "path prefix when service is run behind a proxy"; };
  46. proxy-port = mkOption { type = types.nullOr types.str; description = "port of the proxy when the service is run behind a proxy"; };
  47. ga-key = mkOption { type = types.nullOr types.str; description = "google analytics key for the front end"; };
  48. uservoice-key = mkOption { type = types.nullOr types.str; description = "user voice key for the front end"; };
  49. lets-encrypt-hosts = mkOption { type = types.nullOr (types.listOf types.str); description = "hosts to use for lets encrypt certificates"; };
  50. log = mkOption { type = types.nullOr types.str; description = "path to log file"; };
  51. cors-domains = mkOption { type = types.nullOr (types.listOf types.str); description = "comma separated list of domains for CORS, setting it enable CORS "; };
  52. clamav-host = mkOption { type = types.nullOr types.str; description = "host for clamav feature"; };
  53. rate-limit = mkOption { type = types.nullOr types.int; description = "request per minute"; };
  54. max-upload-size = mkOption { type = types.nullOr types.int; description = "max upload size in kilobytes "; };
  55. purge-days = mkOption { type = types.nullOr types.int; description = "number of days after the uploads are purged automatically "; };
  56. random-token-length = mkOption { type = types.nullOr types.int; description = "length of the random token for the upload path (double the size for delete path)"; };
  57. };
  58. provider_options = {
  59. aws = {
  60. enable = mkEnableOption "Enable AWS backend";
  61. aws-access-key = mkOption { type = types.str; description = "aws access key"; };
  62. aws-secret-key = mkOption { type = types.str; description = "aws secret key"; };
  63. bucket = mkOption { type = types.str; description = "aws bucket "; };
  64. s3-endpoint = mkOption {
  65. type = types.nullOr types.str;
  66. description = ''
  67. Custom S3 endpoint.
  68. If you specify the s3-region, you don't need to set the endpoint URL since the correct endpoint will used automatically.
  69. '';
  70. };
  71. s3-region = mkOption { type = types.str; description = "region of the s3 bucket eu-west-"; };
  72. s3-no-multipart = mkOption { type = types.nullOr types.bool; description = "disables s3 multipart upload "; };
  73. s3-path-style = mkOption { type = types.nullOr types.str; description = "Forces path style URLs, required for Minio. "; };
  74. };
  75. storj = {
  76. enable = mkEnableOption "Enable storj backend";
  77. storj-access = mkOption { type = types.str; description = "Access for the project"; };
  78. storj-bucket = mkOption { type = types.str; description = "Bucket to use within the project"; };
  79. };
  80. gdrive = {
  81. enable = mkEnableOption "Enable gdrive backend";
  82. gdrive-client-json = mkOption { type = types.str; description = "oauth client json config for gdrive provider"; };
  83. gdrive-chunk-size = mkOption { default = 8; type = types.nullOr types.int; description = "chunk size for gdrive upload in megabytes, must be lower than available memory (8 MB)"; };
  84. basedir = mkOption { type = types.str; description = "path storage for gdrive provider"; default = "${cfg.stateDir}/store"; };
  85. purge-interval = mkOption { type = types.nullOr types.int; description = "interval in hours to run the automatic purge for (not applicable to S3 and Storj)"; };
  86. };
  87. local = {
  88. enable = mkEnableOption "Enable local backend";
  89. basedir = mkOption { type = types.str; description = "path storage for local provider"; default = "${cfg.stateDir}/store"; };
  90. purge-interval = mkOption { type = types.nullOr types.int; description = "interval in hours to run the automatic purge for (not applicable to S3 and Storj)"; };
  91. };
  92. };
  93. in
  94. {
  95. options.services.transfer-sh = fold recursiveUpdate {} [
  96. general_options
  97. {
  98. provider = provider_options;
  99. user = mkOption {
  100. type = types.str;
  101. description = "User to run the service under";
  102. default = "transfer.sh";
  103. };
  104. group = mkOption {
  105. type = types.str;
  106. description = "Group to run the service under";
  107. default = "transfer.sh";
  108. };
  109. stateDir = mkOption {
  110. type = types.path;
  111. description = "Variable state directory";
  112. default = RUNTIME_DIR;
  113. };
  114. }
  115. ];
  116. config = let
  117. mkFlags = cfg: options:
  118. let
  119. mkBoolFlag = option: if cfg.${option} then [ "--${option}" ] else [];
  120. mkFlag = option:
  121. if isBool cfg.${option}
  122. then mkBoolFlag option
  123. else [ "--${option}" "${cfg.${option}}" ];
  124. in
  125. lists.flatten (map (mkFlag) (filter (option: cfg.${option} != null && option != "enable") options));
  126. aws-config = (mkFlags cfg.provider.aws (attrNames provider_options)) ++ [ "--provider" "aws" ];
  127. gdrive-config = mkFlags cfg.provider.gdrive (attrNames provider_options.gdrive) ++ [ "--provider" "gdrive" ];
  128. storj-config = mkFlags cfg.provider.storj (attrNames provider_options.storj) ++ [ "--provider" "storj" ];
  129. local-config = mkFlags cfg.provider.local (attrNames provider_options.local) ++ [ "--provider" "local" ];
  130. general-config = concatStringsSep " " (mkFlags cfg (attrNames general_options));
  131. provider-config = concatStringsSep " " (
  132. if cfg.provider.aws.enable && !cfg.provider.storj.enable && !cfg.provider.gdrive.enable && !cfg.provider.local.enable then aws-config
  133. else if !cfg.provider.aws.enable && cfg.provider.storj.enable && !cfg.provider.gdrive.enable && !cfg.provider.local.enable then storj-config
  134. else if !cfg.provider.aws.enable && !cfg.provider.storj.enable && cfg.provider.gdrive.enable && !cfg.provider.local.enable then gdrive-config
  135. else if !cfg.provider.aws.enable && !cfg.provider.storj.enable && !cfg.provider.gdrive.enable && cfg.provider.local.enable then local-config
  136. else throw "transfer.sh requires exactly one provider (aws, storj, gdrive, local)"
  137. );
  138. in
  139. lib.mkIf cfg.enable
  140. {
  141. systemd.tmpfiles.rules = [
  142. "d ${cfg.stateDir} 0750 ${cfg.user} ${cfg.group} - -"
  143. ] ++ optional cfg.provider.gdrive.enable cfg.provider.gdrive.basedir
  144. ++ optional cfg.provider.local.enable cfg.provider.local.basedir;
  145. systemd.services.transfer-sh = {
  146. wantedBy = [ "multi-user.target" ];
  147. after = [ "network.target" ];
  148. serviceConfig = {
  149. User = cfg.user;
  150. Group = cfg.group;
  151. ExecStart = "${transfer-sh pkgs}/bin/transfer.sh ${general-config} ${provider-config} ";
  152. };
  153. };
  154. networking.firewall.allowedTCPPorts = [ cfg.listener cfg.profile-listener cfg.tls-listener ];
  155. };
  156. };
  157. default = { self, pkgs, ... }: {
  158. imports = [ nixosModules.transfer-sh ];
  159. # Network configuration.
  160. # useDHCP is generally considered to better be turned off in favor
  161. # of <adapter>.useDHCP
  162. networking.useDHCP = false;
  163. networking.firewall.allowedTCPPorts = [];
  164. # Enable the inventaire server.
  165. services.transfer-sh = {
  166. enable = true;
  167. provider.local = {
  168. enable = true;
  169. };
  170. };
  171. nixpkgs.config.allowUnfree = true;
  172. };
  173. };
  174. nixosConfigurations."container" = nixpkgs.lib.nixosSystem {
  175. system = "x86_64-linux";
  176. modules = [
  177. nixosModules.default
  178. ({ ... }: { boot.isContainer = true; })
  179. ];
  180. };
  181. };
  182. }