flake.nix 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. Used to fetch the latest
  42. # available versions of packages.
  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/v0.6.3";
  48. # Rust toolchain.
  49. rust-overlay.url = "github:oxalica/rust-overlay";
  50. };
  51. outputs = { self, nixpkgs, devenv, systems, rust-overlay, ... } @ inputs:
  52. let
  53. forEachSystem = nixpkgs.lib.genAttrs (import systems);
  54. in {
  55. devShells = forEachSystem (system:
  56. let
  57. overlays = [ (import rust-overlay) ];
  58. pkgs = import nixpkgs {
  59. inherit system overlays;
  60. };
  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. # The rust toolchain and related tools.
  77. # This will install the "default" profile of rust components.
  78. # https://rust-lang.github.io/rustup/concepts/profiles.html
  79. #
  80. # NOTE: We currently need to set the Rust version unnecessarily high
  81. # in order to work around https://github.com/matrix-org/synapse/issues/15939
  82. (rust-bin.stable."1.70.0".default.override {
  83. # Additionally install the "rust-src" extension to allow diving into the
  84. # Rust source code in an IDE (rust-analyzer will also make use of it).
  85. extensions = [ "rust-src" ];
  86. })
  87. # The rust-analyzer language server implementation.
  88. rust-analyzer
  89. # Native dependencies for running Synapse.
  90. icu
  91. libffi
  92. libjpeg
  93. libpqxx
  94. libwebp
  95. libxml2
  96. libxslt
  97. sqlite
  98. # Native dependencies for unit tests (SyTest also requires OpenSSL).
  99. openssl
  100. xmlsec
  101. # Native dependencies for running Complement.
  102. olm
  103. # For building the Synapse documentation website.
  104. mdbook
  105. # For releasing Synapse
  106. debian-devscripts # (`dch` for manipulating the Debian changelog)
  107. libnotify # (the release script uses `notify-send` to tell you when CI jobs are done)
  108. ];
  109. # Install Python and manage a virtualenv with Poetry.
  110. languages.python.enable = true;
  111. languages.python.poetry.enable = true;
  112. # Automatically activate the poetry virtualenv upon entering the shell.
  113. languages.python.poetry.activate.enable = true;
  114. # Install all extra Python dependencies; this is needed to run the unit
  115. # tests and utilitise all Synapse features.
  116. languages.python.poetry.install.arguments = ["-vvv" "--extras all"];
  117. # Install the 'matrix-synapse' package from the local checkout.
  118. languages.python.poetry.install.installRootPackage = true;
  119. # This is a work-around for NixOS systems. NixOS is special in
  120. # that you can have multiple versions of packages installed at
  121. # once, including your libc linker!
  122. #
  123. # Some binaries built for Linux expect those to be in a certain
  124. # filepath, but that is not the case on NixOS. In that case, we
  125. # force compiling those binaries locally instead.
  126. env.POETRY_INSTALLER_NO_BINARY = "ruff";
  127. # Install dependencies for the additional programming languages
  128. # involved with Synapse development.
  129. #
  130. # * Golang is needed to run the Complement test suite.
  131. # * Perl is needed to run the SyTest test suite.
  132. # * Rust is used for developing and running Synapse.
  133. # It is installed manually with `packages` above.
  134. languages.go.enable = true;
  135. languages.perl.enable = true;
  136. # Postgres is needed to run Synapse with postgres support and
  137. # to run certain unit tests that require postgres.
  138. services.postgres.enable = true;
  139. # On the first invocation of `devenv up`, create a database for
  140. # Synapse to store data in.
  141. services.postgres.initdbArgs = ["--locale=C" "--encoding=UTF8"];
  142. services.postgres.initialDatabases = [
  143. { name = "synapse"; }
  144. ];
  145. # Create a postgres user called 'synapse_user' which has ownership
  146. # over the 'synapse' database.
  147. services.postgres.initialScript = ''
  148. CREATE USER synapse_user;
  149. ALTER DATABASE synapse OWNER TO synapse_user;
  150. '';
  151. # Redis is needed in order to run Synapse in worker mode.
  152. services.redis.enable = true;
  153. # Configure and start Synapse. Before starting Synapse, this shell code:
  154. # * generates a default homeserver.yaml config file if one does not exist, and
  155. # * ensures a directory containing two additional homeserver config files exists;
  156. # one to configure using the development environment's PostgreSQL as the
  157. # database backend and another for enabling Redis support.
  158. process.before = ''
  159. python -m synapse.app.homeserver -c homeserver.yaml --generate-config --server-name=synapse.dev --report-stats=no
  160. mkdir -p homeserver-config-overrides.d
  161. cat > homeserver-config-overrides.d/database.yaml << EOF
  162. ## Do not edit this file. This file is generated by flake.nix
  163. database:
  164. name: psycopg2
  165. args:
  166. user: synapse_user
  167. database: synapse
  168. host: $PGHOST
  169. cp_min: 5
  170. cp_max: 10
  171. EOF
  172. cat > homeserver-config-overrides.d/redis.yaml << EOF
  173. ## Do not edit this file. This file is generated by flake.nix
  174. redis:
  175. enabled: true
  176. EOF
  177. '';
  178. # Start synapse when `devenv up` is run.
  179. processes.synapse.exec = "poetry run python -m synapse.app.homeserver -c homeserver.yaml -c homeserver-config-overrides.d";
  180. # Define the perl modules we require to run SyTest.
  181. #
  182. # This list was compiled by cross-referencing https://metacpan.org/
  183. # with the modules defined in './cpanfile' and then finding the
  184. # corresponding Nix packages on https://search.nixos.org/packages.
  185. #
  186. # This was done until `./install-deps.pl --dryrun` produced no output.
  187. env.PERL5LIB = "${with pkgs.perl536Packages; makePerlPath [
  188. DBI
  189. ClassMethodModifiers
  190. CryptEd25519
  191. DataDump
  192. DBDPg
  193. DigestHMAC
  194. DigestSHA1
  195. EmailAddressXS
  196. EmailMIME
  197. EmailSimple # required by Email::Mime
  198. EmailMessageID # required by Email::Mime
  199. EmailMIMEContentType # required by Email::Mime
  200. TextUnidecode # required by Email::Mime
  201. ModuleRuntime # required by Email::Mime
  202. EmailMIMEEncodings # required by Email::Mime
  203. FilePath
  204. FileSlurper
  205. Future
  206. GetoptLong
  207. HTTPMessage
  208. IOAsync
  209. IOAsyncSSL
  210. IOSocketSSL
  211. NetSSLeay
  212. JSON
  213. ListUtilsBy
  214. ScalarListUtils
  215. ModulePluggable
  216. NetAsyncHTTP
  217. MetricsAny # required by Net::Async::HTTP
  218. NetAsyncHTTPServer
  219. StructDumb
  220. URI
  221. YAMLLibYAML
  222. ]}";
  223. }
  224. ];
  225. };
  226. });
  227. };
  228. }