From 989158ef4d429ca8d5a4b020013e3ca4b7c60198 Mon Sep 17 00:00:00 2001 From: 778a69cd <778a69cd@potsda.mn> Date: Tue, 5 Mar 2024 17:10:04 +0100 Subject: [PATCH] development environment for flake --- flake.nix | 116 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 9 deletions(-) diff --git a/flake.nix b/flake.nix index 2e91648be..68d202e4a 100644 --- a/flake.nix +++ b/flake.nix @@ -92,20 +92,118 @@ }); devShells = forAllSystems (system: - let pkgs = nixpkgsFor.${system}; - in { + let + pkgs = nixpkgsFor.${system}; + settingsFormat = pkgs.formats.elixirConf { }; + in + { default = pkgs.mkShell { - buildInputs = with pkgs; [ - elixir - mix2nix - cmake - imagemagick - nodejs - ]; + MIX_ENV = "dev"; + PGUSER = "mobilizon"; + PGDATABASE = "mobilizon"; + MOBILIZON_CONFIG_PATH = settingsFormat.generate "runtime.exs" { + ":mobilizon" = { + "Mobilizon.Web.Endpoint" = { + server = true; + url.host = "mobilizon.dev"; + http = { + ip = settingsFormat.lib.mkTuple [ 0 0 0 0 0 0 0 1 ]; + port = 4000; + }; + secret_key_base = + "2q/l1WDx3RQQy7gZ1k001//6nc66moWUEJQyGuMK/z3zPLYW6FYtIgCkUzGP0+X/"; + }; + "Mobilizon.Web.Auth.Guardian" = { + secret_key = "N8x7/tf0kInLFS2poO22g6OGPiMjSrDEhmk29nFqV35q7hQ0DtBt/cRYCsqBNp2L"; + }; + ":instance" = { + name = "Mobilizon"; + description = "Change this to a proper description of your instance"; + hostname = "mobilizon.dev"; + registrations_open = true; + email_from = "noreply@mobilizon.dev"; + email_reply_to = "noreply@mobilizon.dev"; + }; + "Mobilizon.Storage.Repo" = { + adapter = settingsFormat.lib.mkAtom "Ecto.Adapters.Postgres"; + pool_size = 10; + username = "mobilizon"; + database = "mobilizon"; + socket_dir = "/var/run/postgresql"; + }; + }; + }; + buildInputs = + with pkgs; [ + elixir + mix2nix + cmake + imagemagick + nodejs + inotify-tools + (pkgs.writeShellScriptBin "build" '' + ${elixir}/bin/mix deps.get + ${elixir}/bin/mix deps.compile + ${elixir}/bin/mix phx.digest + ${nodejs}/bin/npm install + ${nodejs}/bin/npm run build + '') + (pkgs.writeShellScriptBin "setup" '' + ${elixir}/bin/mix ecto.create + sudo -u postgres ${postgresql}/bin/psql -d mobilizon -c "create extension if not exists postgis; create extension if not exists unaccent; create extension if not exists pg_trgm;" + ${elixir}/bin/mix ecto.migrate + '') + (pkgs.writeShellScriptBin "start" '' + ${elixir}/bin/mix phx.server + '') + (pkgs.writeShellScriptBin "clean" '' + rm -rf deps/ _build/ node_modules/ + sudo -u postgres ${postgresql}/bin/psql -c "DROP DATABASE mobilizon;" + sudo systemctl restart postgresql.service + '') + ]; }; }); + nixosModules.devSetup = { config, lib, pkgs, ... }: + let + cfg = config.mobilizonDevEnvironment; + in + { + + options.mobilizonDevEnvironment = { + enable = lib.mkEnableOption + (lib.mdDoc "development environment for Mobilizon"); + user = lib.mkOption { + type = lib.types.str; + description = "Unix user that runs the backend application to connect to the database"; + }; + }; + + config = lib.mkIf cfg.enable { + services.postgresql = { + enable = true; + ensureDatabases = [ "mobilizon" ]; + ensureUsers = [ + { + name = "mobilizon"; + ensureDBOwnership = true; + } + ]; + extraPlugins = with config.services.postgresql.package.pkgs; [ postgis ]; + identMap = '' + map-mobilizon ${cfg.user} mobilizon + ''; + authentication = '' + local all mobilizon ident map=map-mobilizon + ''; + }; + + }; + + }; + overlays.default = final: prev: { inherit (self.packages."${prev.system}") mobilizon; };