flake.nix 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. email-contact = mkOption { type = types.nullOr types.str; description = "email contact for the front end"; };
  49. uservoice-key = mkOption { type = types.nullOr types.str; description = "user voice key for the front end"; };
  50. lets-encrypt-hosts = mkOption { type = types.nullOr (types.listOf types.str); description = "hosts to use for lets encrypt certificates"; };
  51. log = mkOption { type = types.nullOr types.str; description = "path to log file"; };
  52. cors-domains = mkOption { type = types.nullOr (types.listOf types.str); description = "comma separated list of domains for CORS, setting it enable CORS "; };
  53. clamav-host = mkOption { type = types.nullOr types.str; description = "host for clamav feature"; };
  54. rate-limit = mkOption { type = types.nullOr types.int; description = "request per minute"; };
  55. max-upload-size = mkOption { type = types.nullOr types.int; description = "max upload size in kilobytes "; };
  56. purge-days = mkOption { type = types.nullOr types.int; description = "number of days after the uploads are purged automatically "; };
  57. 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)"; };
  58. };
  59. provider_options = {
  60. aws = {
  61. enable = mkEnableOption "Enable AWS backend";
  62. aws-access-key = mkOption { type = types.str; description = "aws access key"; };
  63. aws-secret-key = mkOption { type = types.str; description = "aws secret key"; };
  64. bucket = mkOption { type = types.str; description = "aws bucket "; };
  65. s3-endpoint = mkOption {
  66. type = types.nullOr types.str;
  67. description = ''
  68. Custom S3 endpoint.
  69. If you specify the s3-region, you don't need to set the endpoint URL since the correct endpoint will used automatically.
  70. '';
  71. };
  72. s3-region = mkOption { type = types.str; description = "region of the s3 bucket eu-west-"; };
  73. s3-no-multipart = mkOption { type = types.nullOr types.bool; description = "disables s3 multipart upload "; };
  74. s3-path-style = mkOption { type = types.nullOr types.str; description = "Forces path style URLs, required for Minio. "; };
  75. };
  76. storj = {
  77. enable = mkEnableOption "Enable storj backend";
  78. storj-access = mkOption { type = types.str; description = "Access for the project"; };
  79. storj-bucket = mkOption { type = types.str; description = "Bucket to use within the project"; };
  80. };
  81. gdrive = {
  82. enable = mkEnableOption "Enable gdrive backend";
  83. gdrive-client-json = mkOption { type = types.str; description = "oauth client json config for gdrive provider"; };
  84. 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)"; };
  85. basedir = mkOption { type = types.str; description = "path storage for gdrive provider"; default = "${cfg.stateDir}/store"; };
  86. purge-interval = mkOption { type = types.nullOr types.int; description = "interval in hours to run the automatic purge for (not applicable to S3 and Storj)"; };
  87. };
  88. local = {
  89. enable = mkEnableOption "Enable local backend";
  90. basedir = mkOption { type = types.str; description = "path storage for local provider"; default = "${cfg.stateDir}/store"; };
  91. purge-interval = mkOption { type = types.nullOr types.int; description = "interval in hours to run the automatic purge for (not applicable to S3 and Storj)"; };
  92. };
  93. };
  94. in
  95. {
  96. options.services.transfer-sh = fold recursiveUpdate {} [
  97. general_options
  98. {
  99. provider = provider_options;
  100. user = mkOption {
  101. type = types.str;
  102. description = "User to run the service under";
  103. default = "transfer.sh";
  104. };
  105. group = mkOption {
  106. type = types.str;
  107. description = "Group to run the service under";
  108. default = "transfer.sh";
  109. };
  110. stateDir = mkOption {
  111. type = types.path;
  112. description = "Variable state directory";
  113. default = RUNTIME_DIR;
  114. };
  115. }
  116. ];
  117. config = let
  118. mkFlags = cfg: options:
  119. let
  120. mkBoolFlag = option: if cfg.${option} then [ "--${option}" ] else [];
  121. mkFlag = option:
  122. if isBool cfg.${option}
  123. then mkBoolFlag option
  124. else [ "--${option}" "${cfg.${option}}" ];
  125. in
  126. lists.flatten (map (mkFlag) (filter (option: cfg.${option} != null && option != "enable") options));
  127. aws-config = (mkFlags cfg.provider.aws (attrNames provider_options)) ++ [ "--provider" "aws" ];
  128. gdrive-config = mkFlags cfg.provider.gdrive (attrNames provider_options.gdrive) ++ [ "--provider" "gdrive" ];
  129. storj-config = mkFlags cfg.provider.storj (attrNames provider_options.storj) ++ [ "--provider" "storj" ];
  130. local-config = mkFlags cfg.provider.local (attrNames provider_options.local) ++ [ "--provider" "local" ];
  131. general-config = concatStringsSep " " (mkFlags cfg (attrNames general_options));
  132. provider-config = concatStringsSep " " (
  133. if cfg.provider.aws.enable && !cfg.provider.storj.enable && !cfg.provider.gdrive.enable && !cfg.provider.local.enable then aws-config
  134. else if !cfg.provider.aws.enable && cfg.provider.storj.enable && !cfg.provider.gdrive.enable && !cfg.provider.local.enable then storj-config
  135. else if !cfg.provider.aws.enable && !cfg.provider.storj.enable && cfg.provider.gdrive.enable && !cfg.provider.local.enable then gdrive-config
  136. else if !cfg.provider.aws.enable && !cfg.provider.storj.enable && !cfg.provider.gdrive.enable && cfg.provider.local.enable then local-config
  137. else throw "transfer.sh requires exactly one provider (aws, storj, gdrive, local)"
  138. );
  139. in
  140. lib.mkIf cfg.enable
  141. {
  142. systemd.tmpfiles.rules = [
  143. "d ${cfg.stateDir} 0750 ${cfg.user} ${cfg.group} - -"
  144. ] ++ optional cfg.provider.gdrive.enable cfg.provider.gdrive.basedir
  145. ++ optional cfg.provider.local.enable cfg.provider.local.basedir;
  146. systemd.services.transfer-sh = {
  147. wantedBy = [ "multi-user.target" ];
  148. after = [ "network.target" ];
  149. serviceConfig = {
  150. User = cfg.user;
  151. Group = cfg.group;
  152. ExecStart = "${transfer-sh pkgs}/bin/transfer.sh ${general-config} ${provider-config} ";
  153. };
  154. };
  155. networking.firewall.allowedTCPPorts = [ cfg.listener cfg.profile-listener cfg.tls-listener ];
  156. };
  157. };
  158. default = { self, pkgs, ... }: {
  159. imports = [ nixosModules.transfer-sh ];
  160. # Network configuration.
  161. # useDHCP is generally considered to better be turned off in favor
  162. # of <adapter>.useDHCP
  163. networking.useDHCP = false;
  164. networking.firewall.allowedTCPPorts = [];
  165. # Enable the inventaire server.
  166. services.transfer-sh = {
  167. enable = true;
  168. provider.local = {
  169. enable = true;
  170. };
  171. };
  172. nixpkgs.config.allowUnfree = true;
  173. };
  174. };
  175. nixosConfigurations."container" = nixpkgs.lib.nixosSystem {
  176. system = "x86_64-linux";
  177. modules = [
  178. nixosModules.default
  179. ({ ... }: { boot.isContainer = true; })
  180. ];
  181. };
  182. };
  183. }