flake.nix 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. # For releasing Synapse
  98. debian-devscripts # (`dch` for manipulating the Debian changelog)
  99. libnotify # (the release script uses `notify-send` to tell you when CI jobs are done)
  100. ];
  101. # Install Python and manage a virtualenv with Poetry.
  102. languages.python.enable = true;
  103. languages.python.poetry.enable = true;
  104. # Automatically activate the poetry virtualenv upon entering the shell.
  105. languages.python.poetry.activate.enable = true;
  106. # Install all extra Python dependencies; this is needed to run the unit
  107. # tests and utilitise all Synapse features.
  108. languages.python.poetry.install.arguments = ["--extras all"];
  109. # Install the 'matrix-synapse' package from the local checkout.
  110. languages.python.poetry.install.installRootPackage = true;
  111. # This is a work-around for NixOS systems. NixOS is special in
  112. # that you can have multiple versions of packages installed at
  113. # once, including your libc linker!
  114. #
  115. # Some binaries built for Linux expect those to be in a certain
  116. # filepath, but that is not the case on NixOS. In that case, we
  117. # force compiling those binaries locally instead.
  118. env.POETRY_INSTALLER_NO_BINARY = "ruff";
  119. # Install dependencies for the additional programming languages
  120. # involved with Synapse development.
  121. #
  122. # * Rust is used for developing and running Synapse.
  123. # * Golang is needed to run the Complement test suite.
  124. # * Perl is needed to run the SyTest test suite.
  125. languages.go.enable = true;
  126. languages.rust.enable = true;
  127. languages.rust.version = "stable";
  128. languages.perl.enable = true;
  129. # Postgres is needed to run Synapse with postgres support and
  130. # to run certain unit tests that require postgres.
  131. services.postgres.enable = true;
  132. # On the first invocation of `devenv up`, create a database for
  133. # Synapse to store data in.
  134. services.postgres.initdbArgs = ["--locale=C" "--encoding=UTF8"];
  135. services.postgres.initialDatabases = [
  136. { name = "synapse"; }
  137. ];
  138. # Create a postgres user called 'synapse_user' which has ownership
  139. # over the 'synapse' database.
  140. services.postgres.initialScript = ''
  141. CREATE USER synapse_user;
  142. ALTER DATABASE synapse OWNER TO synapse_user;
  143. '';
  144. # Redis is needed in order to run Synapse in worker mode.
  145. services.redis.enable = true;
  146. # Define the perl modules we require to run SyTest.
  147. #
  148. # This list was compiled by cross-referencing https://metacpan.org/
  149. # with the modules defined in './cpanfile' and then finding the
  150. # corresponding nix packages on https://search.nixos.org/packages.
  151. #
  152. # This was done until `./install-deps.pl --dryrun` produced no output.
  153. env.PERL5LIB = "${with pkgs.perl536Packages; makePerlPath [
  154. DBI
  155. ClassMethodModifiers
  156. CryptEd25519
  157. DataDump
  158. DBDPg
  159. DigestHMAC
  160. DigestSHA1
  161. EmailAddressXS
  162. EmailMIME
  163. EmailSimple # required by Email::Mime
  164. EmailMessageID # required by Email::Mime
  165. EmailMIMEContentType # required by Email::Mime
  166. TextUnidecode # required by Email::Mime
  167. ModuleRuntime # required by Email::Mime
  168. EmailMIMEEncodings # required by Email::Mime
  169. FilePath
  170. FileSlurper
  171. Future
  172. GetoptLong
  173. HTTPMessage
  174. IOAsync
  175. IOAsyncSSL
  176. IOSocketSSL
  177. NetSSLeay
  178. JSON
  179. ListUtilsBy
  180. ScalarListUtils
  181. ModulePluggable
  182. NetAsyncHTTP
  183. MetricsAny # required by Net::Async::HTTP
  184. NetAsyncHTTPServer
  185. StructDumb
  186. URI
  187. YAMLLibYAML
  188. ]}";
  189. }
  190. ];
  191. };
  192. });
  193. };
  194. }