flake.nix 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # A Nix flake that sets up a complete Synapse development environment. Dependencies
  2. # for the SyTest (https://github.com/matrix-org/sytest) and Complement
  3. # (https://github.com/matrix-org/complement) Matrix homeserver test suites are also
  4. # installed automatically.
  5. #
  6. # You must have already installed Nix (https://nixos.org) on your system to use this.
  7. # Nix can be installed on Linux or MacOS; NixOS is not required. Windows is not
  8. # directly supported, but Nix can be installed inside of WSL2 or even Docker
  9. # containers. Please refer to https://nixos.org/download for details.
  10. #
  11. # You must also enable support for flakes in Nix. See the following for how to
  12. # do so permanently: https://nixos.wiki/wiki/Flakes#Enable_flakes
  13. #
  14. # Be warned: you'll need over 3.75 GB of free space to download all the dependencies.
  15. #
  16. # Usage:
  17. #
  18. # With Nix installed, navigate to the directory containing this flake and run
  19. # `nix develop --impure`. The `--impure` is necessary in order to store state
  20. # locally from "services", such as PostgreSQL and Redis.
  21. #
  22. # You should now be dropped into a new shell with all programs and dependencies
  23. # availabile to you!
  24. #
  25. # You can start up pre-configured local Synapse, PostgreSQL and Redis instances by
  26. # running: `devenv up`. To stop them, use Ctrl-C.
  27. #
  28. # All state (the venv, postgres and redis data and config) are stored in
  29. # .devenv/state. Deleting a file from here and then re-entering the shell
  30. # will recreate these files from scratch.
  31. #
  32. # You can exit the development shell by typing `exit`, or using Ctrl-D.
  33. #
  34. # If you would like this development environment to activate automatically
  35. # upon entering this directory in your terminal, first install `direnv`
  36. # (https://direnv.net/). Then run `echo 'use flake . --impure' >> .envrc` at
  37. # the root of the Synapse repo. Finally, run `direnv allow .` to allow the
  38. # contents of '.envrc' to run every time you enter this directory. Voilà!
  39. {
  40. inputs = {
  41. # Use the master/unstable branch of nixpkgs. The latest stable, 22.11,
  42. # does not contain 'perl536Packages.NetAsyncHTTP', needed by Sytest.
  43. nixpkgs.url = "github:NixOS/nixpkgs/master";
  44. # Output a development shell for x86_64/aarch64 Linux/Darwin (MacOS).
  45. systems.url = "github:nix-systems/default";
  46. # A development environment manager built on Nix. See https://devenv.sh.
  47. devenv.url = "github:cachix/devenv/main";
  48. # Rust toolchains and rust-analyzer nightly.
  49. fenix = {
  50. url = "github:nix-community/fenix";
  51. inputs.nixpkgs.follows = "nixpkgs";
  52. };
  53. };
  54. outputs = { self, nixpkgs, devenv, systems, ... } @ inputs:
  55. let
  56. forEachSystem = nixpkgs.lib.genAttrs (import systems);
  57. in {
  58. devShells = forEachSystem (system:
  59. let
  60. pkgs = nixpkgs.legacyPackages.${system};
  61. in {
  62. # Everything is configured via devenv - a Nix module for creating declarative
  63. # developer environments. See https://devenv.sh/reference/options/ for a list
  64. # of all possible options.
  65. default = devenv.lib.mkShell {
  66. inherit inputs pkgs;
  67. modules = [
  68. {
  69. # Make use of the Starship command prompt when this development environment
  70. # is manually activated (via `nix develop --impure`).
  71. # See https://starship.rs/ for details on the prompt itself.
  72. starship.enable = true;
  73. # Configure packages to install.
  74. # Search for package names at https://search.nixos.org/packages?channel=unstable
  75. packages = with pkgs; [
  76. # Native dependencies for running Synapse.
  77. icu
  78. libffi
  79. libjpeg
  80. libpqxx
  81. libwebp
  82. libxml2
  83. libxslt
  84. sqlite
  85. # Native dependencies for unit tests (SyTest also requires OpenSSL).
  86. openssl
  87. xmlsec
  88. # Native dependencies for running Complement.
  89. olm
  90. # For building the Synapse documentation website.
  91. mdbook
  92. ];
  93. # Install Python and manage a virtualenv with Poetry.
  94. languages.python.enable = true;
  95. languages.python.poetry.enable = true;
  96. # Automatically activate the poetry virtualenv upon entering the shell.
  97. languages.python.poetry.activate.enable = true;
  98. # Install all extra Python dependencies; this is needed to run the unit
  99. # tests and utilitise all Synapse features.
  100. languages.python.poetry.install.arguments = ["--extras all"];
  101. # Install the 'matrix-synapse' package from the local checkout.
  102. languages.python.poetry.install.installRootPackage = true;
  103. # This is a work-around for NixOS systems. NixOS is special in
  104. # that you can have multiple versions of packages installed at
  105. # once, including your libc linker!
  106. #
  107. # Some binaries built for Linux expect those to be in a certain
  108. # filepath, but that is not the case on NixOS. In that case, we
  109. # force compiling those binaries locally instead.
  110. env.POETRY_INSTALLER_NO_BINARY = "ruff";
  111. # Install dependencies for the additional programming languages
  112. # involved with Synapse development.
  113. #
  114. # * Rust is used for developing and running Synapse.
  115. # * Golang is needed to run the Complement test suite.
  116. # * Perl is needed to run the SyTest test suite.
  117. languages.go.enable = true;
  118. languages.rust.enable = true;
  119. languages.rust.version = "stable";
  120. languages.perl.enable = true;
  121. # Postgres is needed to run Synapse with postgres support and
  122. # to run certain unit tests that require postgres.
  123. services.postgres.enable = true;
  124. # On the first invocation of `devenv up`, create a database for
  125. # Synapse to store data in.
  126. services.postgres.initdbArgs = ["--locale=C" "--encoding=UTF8"];
  127. services.postgres.initialDatabases = [
  128. { name = "synapse"; }
  129. ];
  130. # Create a postgres user called 'synapse_user' which has ownership
  131. # over the 'synapse' database.
  132. services.postgres.initialScript = ''
  133. CREATE USER synapse_user;
  134. ALTER DATABASE synapse OWNER TO synapse_user;
  135. '';
  136. # Redis is needed in order to run Synapse in worker mode.
  137. services.redis.enable = true;
  138. # Configure and start Synapse. Before starting Synapse, this shell code:
  139. # * generates a default homeserver.yaml config file if one does not exist, and
  140. # * ensures a directory containing two additional homeserver config files exists;
  141. # one to configure using the development environment's PostgreSQL as the
  142. # database backend and another for enabling Redis support.
  143. process.before = ''
  144. python -m synapse.app.homeserver -c homeserver.yaml --generate-config --server-name=synapse.dev --report-stats=no
  145. mkdir -p homeserver-config-overrides.d
  146. cat > homeserver-config-overrides.d/database.yaml << EOF
  147. ## Do not edit this file. This file is generated by flake.nix
  148. database:
  149. name: psycopg2
  150. args:
  151. user: synapse_user
  152. database: synapse
  153. host: $PGHOST
  154. cp_min: 5
  155. cp_max: 10
  156. EOF
  157. cat > homeserver-config-overrides.d/redis.yaml << EOF
  158. ## Do not edit this file. This file is generated by flake.nix
  159. redis:
  160. enabled: true
  161. EOF
  162. '';
  163. # Start synapse when `devenv up` is run.
  164. processes.synapse.exec = "poetry run python -m synapse.app.homeserver -c homeserver.yaml --config-directory homeserver-config-overrides.d";
  165. # Define the perl modules we require to run SyTest.
  166. #
  167. # This list was compiled by cross-referencing https://metacpan.org/
  168. # with the modules defined in './cpanfile' and then finding the
  169. # corresponding Nix packages on https://search.nixos.org/packages.
  170. #
  171. # This was done until `./install-deps.pl --dryrun` produced no output.
  172. env.PERL5LIB = "${with pkgs.perl536Packages; makePerlPath [
  173. DBI
  174. ClassMethodModifiers
  175. CryptEd25519
  176. DataDump
  177. DBDPg
  178. DigestHMAC
  179. DigestSHA1
  180. EmailAddressXS
  181. EmailMIME
  182. EmailSimple # required by Email::Mime
  183. EmailMessageID # required by Email::Mime
  184. EmailMIMEContentType # required by Email::Mime
  185. TextUnidecode # required by Email::Mime
  186. ModuleRuntime # required by Email::Mime
  187. EmailMIMEEncodings # required by Email::Mime
  188. FilePath
  189. FileSlurper
  190. Future
  191. GetoptLong
  192. HTTPMessage
  193. IOAsync
  194. IOAsyncSSL
  195. IOSocketSSL
  196. NetSSLeay
  197. JSON
  198. ListUtilsBy
  199. ScalarListUtils
  200. ModulePluggable
  201. NetAsyncHTTP
  202. MetricsAny # required by Net::Async::HTTP
  203. NetAsyncHTTPServer
  204. StructDumb
  205. URI
  206. YAMLLibYAML
  207. ]}";
  208. }
  209. ];
  210. };
  211. });
  212. };
  213. }