flake.nix 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. # For releasing Synapse
  93. debian-devscripts # (`dch` for manipulating the Debian changelog)
  94. libnotify # (the release script uses `notify-send` to tell you when CI jobs are done)
  95. ];
  96. # Install Python and manage a virtualenv with Poetry.
  97. languages.python.enable = true;
  98. languages.python.poetry.enable = true;
  99. # Automatically activate the poetry virtualenv upon entering the shell.
  100. languages.python.poetry.activate.enable = true;
  101. # Install all extra Python dependencies; this is needed to run the unit
  102. # tests and utilitise all Synapse features.
  103. languages.python.poetry.install.arguments = ["--extras all"];
  104. # Install the 'matrix-synapse' package from the local checkout.
  105. languages.python.poetry.install.installRootPackage = true;
  106. # This is a work-around for NixOS systems. NixOS is special in
  107. # that you can have multiple versions of packages installed at
  108. # once, including your libc linker!
  109. #
  110. # Some binaries built for Linux expect those to be in a certain
  111. # filepath, but that is not the case on NixOS. In that case, we
  112. # force compiling those binaries locally instead.
  113. env.POETRY_INSTALLER_NO_BINARY = "ruff";
  114. # Install dependencies for the additional programming languages
  115. # involved with Synapse development.
  116. #
  117. # * Rust is used for developing and running Synapse.
  118. # * Golang is needed to run the Complement test suite.
  119. # * Perl is needed to run the SyTest test suite.
  120. languages.go.enable = true;
  121. languages.rust.enable = true;
  122. languages.rust.version = "stable";
  123. languages.perl.enable = true;
  124. # Postgres is needed to run Synapse with postgres support and
  125. # to run certain unit tests that require postgres.
  126. services.postgres.enable = true;
  127. # On the first invocation of `devenv up`, create a database for
  128. # Synapse to store data in.
  129. services.postgres.initdbArgs = ["--locale=C" "--encoding=UTF8"];
  130. services.postgres.initialDatabases = [
  131. { name = "synapse"; }
  132. ];
  133. # Create a postgres user called 'synapse_user' which has ownership
  134. # over the 'synapse' database.
  135. services.postgres.initialScript = ''
  136. CREATE USER synapse_user;
  137. ALTER DATABASE synapse OWNER TO synapse_user;
  138. '';
  139. # Redis is needed in order to run Synapse in worker mode.
  140. services.redis.enable = true;
  141. # Configure and start Synapse. Before starting Synapse, this shell code:
  142. # * generates a default homeserver.yaml config file if one does not exist, and
  143. # * ensures a directory containing two additional homeserver config files exists;
  144. # one to configure using the development environment's PostgreSQL as the
  145. # database backend and another for enabling Redis support.
  146. process.before = ''
  147. python -m synapse.app.homeserver -c homeserver.yaml --generate-config --server-name=synapse.dev --report-stats=no
  148. mkdir -p homeserver-config-overrides.d
  149. cat > homeserver-config-overrides.d/database.yaml << EOF
  150. ## Do not edit this file. This file is generated by flake.nix
  151. database:
  152. name: psycopg2
  153. args:
  154. user: synapse_user
  155. database: synapse
  156. host: $PGHOST
  157. cp_min: 5
  158. cp_max: 10
  159. EOF
  160. cat > homeserver-config-overrides.d/redis.yaml << EOF
  161. ## Do not edit this file. This file is generated by flake.nix
  162. redis:
  163. enabled: true
  164. EOF
  165. '';
  166. # Start synapse when `devenv up` is run.
  167. processes.synapse.exec = "poetry run python -m synapse.app.homeserver -c homeserver.yaml --config-directory homeserver-config-overrides.d";
  168. # Define the perl modules we require to run SyTest.
  169. #
  170. # This list was compiled by cross-referencing https://metacpan.org/
  171. # with the modules defined in './cpanfile' and then finding the
  172. # corresponding Nix packages on https://search.nixos.org/packages.
  173. #
  174. # This was done until `./install-deps.pl --dryrun` produced no output.
  175. env.PERL5LIB = "${with pkgs.perl536Packages; makePerlPath [
  176. DBI
  177. ClassMethodModifiers
  178. CryptEd25519
  179. DataDump
  180. DBDPg
  181. DigestHMAC
  182. DigestSHA1
  183. EmailAddressXS
  184. EmailMIME
  185. EmailSimple # required by Email::Mime
  186. EmailMessageID # required by Email::Mime
  187. EmailMIMEContentType # required by Email::Mime
  188. TextUnidecode # required by Email::Mime
  189. ModuleRuntime # required by Email::Mime
  190. EmailMIMEEncodings # required by Email::Mime
  191. FilePath
  192. FileSlurper
  193. Future
  194. GetoptLong
  195. HTTPMessage
  196. IOAsync
  197. IOAsyncSSL
  198. IOSocketSSL
  199. NetSSLeay
  200. JSON
  201. ListUtilsBy
  202. ScalarListUtils
  203. ModulePluggable
  204. NetAsyncHTTP
  205. MetricsAny # required by Net::Async::HTTP
  206. NetAsyncHTTPServer
  207. StructDumb
  208. URI
  209. YAMLLibYAML
  210. ]}";
  211. }
  212. ];
  213. };
  214. });
  215. };
  216. }