flake.nix 8.8 KB

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