flake.nix 9.0 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. # This is temporarily overridden to a fork that fixes a quirk between
  53. # devenv's service and python language features. This can be removed
  54. # when https://github.com/cachix/devenv/pull/559 is merged upstream.
  55. devenv.url = "github:anoadragon453/devenv/anoa/fix_languages_python";
  56. #devenv.url = "github:cachix/devenv/main";
  57. # Rust toolchains and rust-analyzer nightly.
  58. fenix = {
  59. url = "github:nix-community/fenix";
  60. inputs.nixpkgs.follows = "nixpkgs";
  61. };
  62. };
  63. outputs = { self, nixpkgs, devenv, systems, ... } @ inputs:
  64. let
  65. forEachSystem = nixpkgs.lib.genAttrs (import systems);
  66. in {
  67. devShells = forEachSystem (system:
  68. let
  69. pkgs = nixpkgs.legacyPackages.${system};
  70. in {
  71. # Everything is configured via devenv - a nix module for creating declarative
  72. # developer environments. See https://devenv.sh/reference/options/ for a list
  73. # of all possible options.
  74. default = devenv.lib.mkShell {
  75. inherit inputs pkgs;
  76. modules = [
  77. {
  78. # Make use of the Starship command prompt when this development environment
  79. # is manually activated (via `nix develop --impure`).
  80. # See https://starship.rs/ for details on the prompt itself.
  81. starship.enable = true;
  82. # Configure packages to install.
  83. # Search for package names at https://search.nixos.org/packages?channel=unstable
  84. packages = with pkgs; [
  85. # Native dependencies for running Synapse.
  86. icu
  87. libffi
  88. libjpeg
  89. libpqxx
  90. libwebp
  91. libxml2
  92. libxslt
  93. sqlite
  94. # Native dependencies for unit tests (SyTest also requires OpenSSL).
  95. openssl
  96. # Native dependencies for running Complement.
  97. olm
  98. ];
  99. # Install Python and manage a virtualenv with Poetry.
  100. languages.python.enable = true;
  101. languages.python.poetry.enable = true;
  102. # Automatically activate the poetry virtualenv upon entering the shell.
  103. languages.python.poetry.activate.enable = true;
  104. # Install all extra Python dependencies; this is needed to run the unit
  105. # tests and utilitise all Synapse features.
  106. languages.python.poetry.install.arguments = ["--extras all"];
  107. # Install the 'matrix-synapse' package from the local checkout.
  108. languages.python.poetry.install.installRootPackage = true;
  109. # This is a work-around for NixOS systems. NixOS is special in
  110. # that you can have multiple versions of packages installed at
  111. # once, including your libc linker!
  112. #
  113. # Some binaries built for Linux expect those to be in a certain
  114. # filepath, but that is not the case on NixOS. In that case, we
  115. # force compiling those binaries locally instead.
  116. env.POETRY_INSTALLER_NO_BINARY = "ruff";
  117. # Install dependencies for the additional programming languages
  118. # involved with Synapse development.
  119. #
  120. # * Rust is used for developing and running Synapse.
  121. # * Golang is needed to run the Complement test suite.
  122. # * Perl is needed to run the SyTest test suite.
  123. languages.go.enable = true;
  124. languages.rust.enable = true;
  125. languages.rust.version = "stable";
  126. languages.perl.enable = true;
  127. # Postgres is needed to run Synapse with postgres support and
  128. # to run certain unit tests that require postgres.
  129. services.postgres.enable = true;
  130. # On the first invocation of `devenv up`, create a database for
  131. # Synapse to store data in.
  132. services.postgres.initdbArgs = ["--locale=C" "--encoding=UTF8"];
  133. services.postgres.initialDatabases = [
  134. { name = "synapse"; }
  135. ];
  136. # Create a postgres user called 'synapse_user' which has ownership
  137. # over the 'synapse' database.
  138. services.postgres.initialScript = ''
  139. CREATE USER synapse_user;
  140. ALTER DATABASE synapse OWNER TO synapse_user;
  141. '';
  142. # Redis is needed in order to run Synapse in worker mode.
  143. services.redis.enable = true;
  144. # Define the perl modules we require to run SyTest.
  145. #
  146. # This list was compiled by cross-referencing https://metacpan.org/
  147. # with the modules defined in './cpanfile' and then finding the
  148. # corresponding nix packages on https://search.nixos.org/packages.
  149. #
  150. # This was done until `./install-deps.pl --dryrun` produced no output.
  151. env.PERL5LIB = "${with pkgs.perl536Packages; makePerlPath [
  152. DBI
  153. ClassMethodModifiers
  154. CryptEd25519
  155. DataDump
  156. DBDPg
  157. DigestHMAC
  158. DigestSHA1
  159. EmailAddressXS
  160. EmailMIME
  161. EmailSimple # required by Email::Mime
  162. EmailMessageID # required by Email::Mime
  163. EmailMIMEContentType # required by Email::Mime
  164. TextUnidecode # required by Email::Mime
  165. ModuleRuntime # required by Email::Mime
  166. EmailMIMEEncodings # required by Email::Mime
  167. FilePath
  168. FileSlurper
  169. Future
  170. GetoptLong
  171. HTTPMessage
  172. IOAsync
  173. IOAsyncSSL
  174. IOSocketSSL
  175. NetSSLeay
  176. JSON
  177. ListUtilsBy
  178. ScalarListUtils
  179. ModulePluggable
  180. NetAsyncHTTP
  181. MetricsAny # required by Net::Async::HTTP
  182. NetAsyncHTTPServer
  183. StructDumb
  184. URI
  185. YAMLLibYAML
  186. ]}";
  187. }
  188. ];
  189. };
  190. });
  191. };
  192. }